}//LoadSprite() /// <summary> /// Adds a UI button. /// </summary> /// <param name="obj">Parent game object.</param> /// <param name="fileName">Resource file name with path, relative to Rersources folder.</param> public UIObject Add(UIType type, GameObject obj, string fileName, Sprite spr = null) { Log.Info("Add: <color=yellow>"+obj.name+" - id:"+obj.GetComponent<GUIText>()+"</color> type:"+type); //// Log.GameTimes("_______________-_-_-_-_-_ <color=yellow>"+fileName+" _____ obj: "+obj+"</color>, type: "+type); if(images.ContainsKey(obj.GetInstanceID())) { //give warning, then return existing one (ignore new registration) Log.Debug("This button already registered, ignoring new registration. <color=cyan>" + obj.name+"</color> "); return images[obj.GetInstanceID()]; } Sprite[] sprList = new Sprite[1]; if(fileName.Length == 0) { if(spr == null) { Log.Debug("This button <color=yellow>"+obj.name+"</color> has no image."); } else { Log.Debug("Sprite >>>>>>>>>>>>>>>>> "+spr.name); sprList[0] = spr; } } else { sprList = LoadSprite(fileName); } UIObject image = null; switch(type) { case UIType.Image: Log.Debug("Add UIImage"); image = new UIImage(obj, sprList); break; case UIType.Button: Log.Debug("Add UIButton"); image = new UIButton(obj, sprList); break; case UIType.ToggleButton: Log.Debug("Add UIToggleButton"); image = new UIToggleButton(obj, sprList); break; case UIType.Slider: Log.Debug("Add UISlider"); image = new UISlider(obj, sprList); break; case UIType.Character: Log.Debug("Add UICharacter"); image = new UICharacter(obj, sprList); break; case UIType.ChartPie: Log.Debug("Add UIChartPie"); image = new UIChartPie(obj); break; case UIType.ChartLine: Log.Debug("Add UIChartLine"); image = new UIChartLine(obj); break; case UIType.Checkbox: Log.Debug("Add Checkbox"); image = new UICheckbox(obj, sprList); break; default: Log.Exception("Invalid UIType to add:" + type); break; } //TODO remove this images.Add(obj.GetInstanceID(), image); // //images.Add(""+lastId++, image); //Log.Info("Button added:"+obj.name+" image:"+image.Name); return image; }//Add()
}//LoadResources() /// <summary> /// Load object resources from dictionary /// TODO make more generic helpers and way of parse similar stuff. /// ie. transform parameter should be one parser for all /// </summary> public Dictionary<string, UIObject> LoadResources( Dictionary<string,object> dict, GameObject gameObject = null) { //parse list of objects if(dict.ContainsKey("objects")) { // Log.Debug("Number of objects:"+((List<object>)dict["objects"]).Count); Dictionary<string, UIObject> screenObjects = new Dictionary<string, UIObject>(); foreach(Dictionary<string,object> obj in ((List<object>)dict["objects"])) { //Log.Debug("objects:"+obj["id"].ToString()+" type:"+obj["type"].ToString()); if(obj.ContainsKey("type") && obj.ContainsKey("id")) { //string objId = obj["id"].ToString(); //get id and prefix it by parent name string objId = obj["id"].ToString(); if(gameObject!=null) objId = gameObject.name+"."+objId; string objType = obj["type"].ToString(); // Log.Debug("Create ui object:"+objId+":"+objType); //create objects based on types if(objType == "image") { // Log.Debug("Create Image:"+objId); //TODO add creation of an image gameObject here or there? UIImage image = new UIImage(obj); if(gameObject) image.GameObject.transform.parent = gameObject.transform; image.Name = objId; //images.Add(objId, image); images.Add(image.GameObject.GetInstanceID(), image); screenObjects.Add(obj["id"].ToString(),image); } else if(objType == "button") { Log.Debug("Create Button:<color=yellow>"+objId+"</color>"); UIButton button = new UIButton(obj); if(gameObject) { button.GameObject.transform.parent = gameObject.transform; button.Name = objId; } //images.Add(objId, button); images.Add(button.GameObject.GetInstanceID(), button); screenObjects.Add(obj["id"].ToString(),button); } else if(objType == "togglebutton" ) { Log.Debug("Create Toggle Button:<color=yellow>"+objId+"</color>"); UIToggleButton button = new UIToggleButton(obj); if(gameObject) { button.GameObject.transform.parent = gameObject.transform; button.Name = objId; } //TODO FIX_TOGGLE this hack! move button init to Settings.cs to Enter() // for SoundButton: // bool mutedMusic = AUDIO.AudioManager.Instance.MuteMusic; // bool mutedSfx = AUDIO.AudioManager.Instance.MuteSfx; // if(((mutedMusic) && (objId == "Settings.btn_Sound")) || // sets Sound button // ((mutedSfx) && (objId == "Settings.btn_SoundEffects"))) // sets SoundEffects button // { // //button.SoundState = UISoundButton.UISoundButtonState.SOUNDOFF; // //button.SaveState = "Off"; // button.Active = false; // } //images.Add(objId, button); images.Add(button.GameObject.GetInstanceID(), button); screenObjects.Add(obj["id"].ToString(),button); } else if(objType == "text") { Log.Debug("Create TEXT:<color=yellow>"+objId+"</color>"); GameObject txt = new GameObject(); txt.SetActive(false); txt.name = objId; UIText uiText = txt.AddComponent<UIText>() as UIText; uiText.Parse(obj); if(gameObject) txt.transform.parent = gameObject.transform; //images.Add(objId, text); txt.SetActive(true); screenObjects.Add(obj["id"].ToString(),uiText); } else if(objType == "popup") { //set prefab default to id and reset if defined string prefab = (obj.ContainsKey("prefab")) ? obj["prefab"].ToString() : obj["id"].ToString(); GameObject popup = GetPopup(obj["id"].ToString(), prefab); popup.SetActive(true); if(obj.ContainsKey("position")) { string pos = obj["position"].ToString(); float px = System.Convert.ToSingle(pos.Split(',')[0]); float py = System.Convert.ToSingle(pos.Split(',')[1]); Log.Debug("HUD "+objId+" Position:"+px+","+py); popup.transform.position = new Vector3(px, py, 0); //this.gameObject.name = dict["position"].ToString(); } if(obj.ContainsKey("rotation")) { string rot = obj["rotation"].ToString(); float rx = System.Convert.ToSingle(rot.Split(',')[0]); float ry = System.Convert.ToSingle(rot.Split(',')[1]); //Log.Debug("Rotation:"+rot+":"+rx+","+ry); popup.transform.localRotation = Quaternion.Euler(new Vector3(rx, ry, 0)); } if(obj.ContainsKey("scale")) { string sc = obj["scale"].ToString(); float sx = System.Convert.ToSingle(sc.Split(',')[0]); float sy = System.Convert.ToSingle(sc.Split(',')[1]); popup.transform.localScale = new Vector3(sx, sy, 0); Log.Debug("Scale:"+sx+","+sy); } if(dict.ContainsKey("layer")) { string layer = dict["layer"].ToString(); popup.layer = LayerMask.NameToLayer(layer); ChangeLayers(popup, layer); } } else if(objType == "hud") { //set prefab default to id and reset if defined string prefab = (obj.ContainsKey("prefab")) ? obj["prefab"].ToString() : obj["id"].ToString(); //Log.Debug("Create HUD as:"+prefab); GameObject hud = GetHud(obj["id"].ToString(), prefab); hud.SetActive(true); //Log.Warning("dict"+dict["layer"].ToString()); if(obj.ContainsKey("position")) { string pos = obj["position"].ToString(); float px = System.Convert.ToSingle(pos.Split(',')[0]); float py = System.Convert.ToSingle(pos.Split(',')[1]); Log.Debug("HUD "+objId+" Position:"+px+","+py); hud.transform.position = new Vector3(px, py, 0); //this.gameObject.name = dict["position"].ToString(); } if(obj.ContainsKey("rotation")) { string rot = obj["rotation"].ToString(); float rx = System.Convert.ToSingle(rot.Split(',')[0]); float ry = System.Convert.ToSingle(rot.Split(',')[1]); //Log.Debug("Rotation:"+rot+":"+rx+","+ry); hud.transform.localRotation = Quaternion.Euler(new Vector3(rx, ry, 0)); } if(obj.ContainsKey("scale")) { string[] sc = obj["scale"].ToString().Split(','); float sx = System.Convert.ToSingle(sc[0]); float sy = System.Convert.ToSingle(sc[1]); float sz = 0; if (sc.Length > 2) { sz = System.Convert.ToSingle(sc[2]); } hud.transform.localScale = new Vector3(sx, sy, sz); Log.Debug(string.Format("Scale: {0}, {1}, {2}", sx, sy, sz)); } // if(obj.ContainsKey("order")) // { // int order = System.Convert.ToInt32(obj["order"].ToString()); // SortingOrder = order; // } if(obj.ContainsKey("layer")) { string layer = obj["layer"].ToString(); hud.layer = LayerMask.NameToLayer(layer); ChangeLayers(hud, layer); } } else if(objType == "model") { //set prefab default to id and reset if defined string prefab = (obj.ContainsKey("prefab")) ? obj["prefab"].ToString() : obj["id"].ToString(); //Log.Debug("Create HUD as:"+prefab); GameObject hud = GetModel(obj["id"].ToString(), prefab); hud.SetActive(true); //Log.Warning("dict"+dict["layer"].ToString()); if(obj.ContainsKey("position")) { string pos = obj["position"].ToString(); float px = System.Convert.ToSingle(pos.Split(',')[0]); float py = System.Convert.ToSingle(pos.Split(',')[1]); float pz = System.Convert.ToSingle(pos.Split(',')[2]); Log.Debug("Model "+objId+" Position:"+px+","+py+","+pz); hud.transform.position = new Vector3(px, py, pz); //this.gameObject.name = dict["position"].ToString(); } if(obj.ContainsKey("rotation")) { string rot = obj["rotation"].ToString(); float rx = System.Convert.ToSingle(rot.Split(',')[0]); float ry = System.Convert.ToSingle(rot.Split(',')[1]); //Log.Debug("Rotation:"+rot+":"+rx+","+ry); hud.transform.localRotation = Quaternion.Euler(new Vector3(rx, ry, 0)); } if(obj.ContainsKey("scale")) { string sc = obj["scale"].ToString(); float sx = System.Convert.ToSingle(sc.Split(',')[0]); float sy = System.Convert.ToSingle(sc.Split(',')[1]); hud.transform.localScale = new Vector3(sx, sy, 0); Log.Debug("Scale:"+sx+","+sy); } // if(obj.ContainsKey("order")) // { // int order = System.Convert.ToInt32(obj["order"].ToString()); // SortingOrder = order; // } if(obj.ContainsKey("layer")) { string layer = obj["layer"].ToString(); hud.layer = LayerMask.NameToLayer(layer); ChangeLayers(hud, layer); } } else if(objType == "character") { Log.Debug("Create Character:<color=yellow>"+objId+"</color>"); UICharacter uiChar = new UICharacter(obj); if(gameObject) { uiChar.GameObject.transform.parent = gameObject.transform; uiChar.Name = objId; } //TODO we might want this later //NPCView npcview = uiChar.GameObject.AddComponent<NPCView>(); //npcview.id = objId; //npcview.image = uiChar; //read bubble info if any //TODO move this somewhere else? Should a bubble exist outside of uichar? if(obj.ContainsKey("bubble")) { foreach(Dictionary<string,object> bobj in ((List<object>)obj["bubble"])) { //TODO parse text parameter. (as text section?) This part is parsed //by UIText already, so we should only pass it over. //size (of text area), font, fontSize, anchor, alignment, wrapsize if(bobj.ContainsKey("position")) { string pos = bobj["position"].ToString(); float px = System.Convert.ToSingle(pos.Split(',')[0]); float py = System.Convert.ToSingle(pos.Split(',')[1]); uiChar.bubblePosition = new Vector3(px, py, 0); Log.Debug("HUD "+objId+" bubble.Position:"+px+","+py); } if(bobj.ContainsKey("scale")) { string sc = bobj["scale"].ToString(); float sx = System.Convert.ToSingle(sc.Split(',')[0]); float sy = System.Convert.ToSingle(sc.Split(',')[1]); uiChar.bubbleScale = new Vector3(sx, sy, 1); //Log.Debug("---------- bubble.scale:"+sx+","+sy); } if(bobj.ContainsKey("tail")) { uiChar.bubbleTail = bobj["tail"].ToString(); } } } images.Add(uiChar.GameObject.GetInstanceID(), uiChar); screenObjects.Add(obj["id"].ToString(),uiChar); } else if(objType == "skit") { UISkit uiSkit = new UISkit(obj); if(gameObject) { uiSkit.GameObject.transform.parent = gameObject.transform; uiSkit.Name = objId; } if(obj.ContainsKey("bubble")) { foreach(Dictionary<string,object> bobj in ((List<object>)obj["bubble"])) { //TODO parse text parameter. (as text section?) This part is parsed //by UIText already, so we should only pass it over. //size (of text area), font, fontSize, anchor, alignment, wrapsize if(bobj.ContainsKey("position")) { string pos = bobj["position"].ToString(); float px = System.Convert.ToSingle(pos.Split(',')[0]); float py = System.Convert.ToSingle(pos.Split(',')[1]); uiSkit.bubblePosition = new Vector3(px, py, 0); } if(bobj.ContainsKey("scale")) { string sc = bobj["scale"].ToString(); float sx = System.Convert.ToSingle(sc.Split(',')[0]); float sy = System.Convert.ToSingle(sc.Split(',')[1]); uiSkit.bubbleScale = new Vector3(sx, sy, 1); } if(bobj.ContainsKey("tail")) { uiSkit.bubbleTail = bobj["tail"].ToString(); } } } images.Add(uiSkit.GameObject.GetInstanceID(), uiSkit); screenObjects.Add(obj["id"].ToString(), uiSkit); } else if(objType == "video") { //Log.Info(" ---------------------------- -- "); //create canvas GameObject canvas = GameObject.CreatePrimitive(PrimitiveType.Quad); GameObject.Destroy(canvas.GetComponent("MeshCollider")); canvas.name = objId; if(gameObject) canvas.transform.parent = gameObject.transform; UIVideo video = new UIVideo(canvas, obj); images.Add(video.GameObject.GetInstanceID(), video); screenObjects.Add(obj["id"].ToString(),video); } } else { Log.Error("Object missing id and/or type definition"); } } return screenObjects; } return null; }//LoadResources()
//------------------------------------------------------------------------------ /// <summary> /// Handles default visibility. So the intersection set of /// visibility of all situations handled with this /// this screen /// </summary> private void HandleVisibilityOnEnter() { GameObject obj = UIManager.Instance.GetPopup("DecisionPopup"); if(obj) obj.SetActive(true); if(engineer.Emotion != EmotionType.NEUTRAL) engineer.Emotion = EmotionType.NEUTRAL; player = (UICharacter)GetObject("Player"); if(player.Emotion != EmotionType.NEUTRAL) player.Emotion = EmotionType.NEUTRAL; }
//------------------------------------------------------------------------------ /// <summary> /// Enter into this screen /// </summary> public override void Enter() { Log.Info("1 ENTER "+Name); engineer = (UICharacter)GetObject("Engineer"); dialogView = DialogManager.Instance.DialogView; dialogView.uiCharacter = engineer; HandleVisibilityOnEnter(); HandleDialogDependantLogic(); Log.Debug("NPC ="+engineer); //connect event handlers GameManager.Instance.RegisterEventHandler("SCREEN", ProcessScreenEvent, gameObject); GameManager.Instance.RegisterEventHandler("DIALOG", ProcessDialogEvent, gameObject); GameManager.Instance.RegisterEventHandler("POPUP_DECISION", ProcessPopupEvent, gameObject); }
/// <summary> /// Process dialog events /// </summary> public void ProcessDialogEvent(string eventId, string param) { if(gameObject.activeSelf == false || eventId == null || eventId.Length == 0) return; // Debug.Log ("eventId : " + eventId); // Debug.Log ("storyId : " + storyId); if(storyId == null || !eventId.StartsWith(storyId)) return; // Debug.Log ("Save last "); lastEventID = eventId; lastParam = param; if(param == "show") { UIDialog d = ui.GetDialog(eventId); //protect from too ealry or late process, //when dialog might not exist anymore if(d != null) { //// Log.Debug(gameObject.transform.parent.name+" SHOW Dialog:"+d.Id+","+d.Type); //for debug only lastId = d.Id; //get text, check type and show the right bubble/mc // // Log.GameTimes("Character "+d.CharacterImage.ToString()); SpeechBubble bubble = GetBubble(d.CharacterImage); //show debug info for bubble if(GameManager.Instance.debugMode) bubble.DebugText = d.Id; else bubble.DebugText = ""; switch(d.Type) { case DialogType.BUBBLE: if(d.CharacterImage == "Player") { //hide multi if(popup != null) popup.SetActive(false); uiCharacter = (UICharacter)ui.ActiveScreen.GetObject(d.CharacterImage); //// Log.Debug("d.CharImg:"+d.CharacterImage+" uiCharacter="+uiCharacter); if(uiCharacter != null) { d.Emotion = uiCharacter.Emotion; //set to settings from character bubble.gameObject.transform.position = uiCharacter.GameObject.transform.position + uiCharacter.bubblePosition; bubble.gameObject.transform.localScale = uiCharacter.bubbleScale; bubble.SetTail(uiCharacter.bubbleTail); //// Log.Debug("emotion: "+d.Emotion); } else { //set position bubble.gameObject.transform.position = defPlayerBubblePosition; bubble.gameObject.transform.localScale = defPlayerBubbleScale; } //d.Emotion = emotion; playerDialog = d; bubble.SetActive(true); bubble.Text = playerDialog.Text; bubble.Id = playerDialog.Id; bubble.Name = ""; } else { //set emotion level and bubble position for character UIObject obj = ui.ActiveScreen.GetObject(d.CharacterImage); if (obj.GetType() == UIType.Character) { UICharacter uiCharacter = (UICharacter)obj; //// Log.Debug("d.CharImg:"+d.CharacterImage+" uiCharacter="+uiCharacter); if(uiCharacter != null) { //uiCharacter.Visible = true; //d.Emotion = uiCharacter.Emotion; UICharacter player = (UICharacter)ui.ActiveScreen.GetObject("Player"); if(uiCharacter.Name.StartsWith("Engineer.") || uiCharacter.Name.StartsWith("CityHall.")) { d.Emotion = uiCharacter.Emotion; } else if(player.Emotion != EmotionType.NEUTRAL) { d.Emotion = player.Emotion; uiCharacter.Emotion = player.Emotion; } else { d.Emotion = EmotionType.NEUTRAL; uiCharacter.Emotion = EmotionType.NEUTRAL; } if(uiCharacter.Name == "ArtistCommunity.ArtistGirl") { UICharacter uiCharacterArtist = (UICharacter)ui.ActiveScreen.GetObject("ArtistBoy"); uiCharacterArtist.Emotion = uiCharacter.Emotion; } else if(uiCharacter.Name == "ArtistCommunity.ArtistBoy") { UICharacter uiCharacterArtist = (UICharacter)ui.ActiveScreen.GetObject("ArtistGirl"); uiCharacterArtist.Emotion = uiCharacter.Emotion; } //set to settings from character bubble.gameObject.transform.position = uiCharacter.GameObject.transform.position + uiCharacter.bubblePosition; bubble.gameObject.transform.localScale = uiCharacter.bubbleScale; bubble.SetTail(uiCharacter.bubbleTail); //// Log.Debug("--------------- TAIL"+uiCharacter.bubbleTail); } } else if (obj.GetType() == UIType.Skit) { skit = (UISkit)obj; if(skit != null) { skit.Show(); bubble.gameObject.transform.position = skit.GameObject.transform.position + skit.bubblePosition; bubble.gameObject.transform.localScale = skit.bubbleScale; bubble.SetTail(skit.bubbleTail); } } else { //set to defaults bubble.gameObject.transform.position = defNpcBubblePosition; bubble.gameObject.transform.localScale = defNpcBubbleScale; } //show and set emotion level npcDialog = d; bubble.SetActive(true); bubble.Text = d.Text; bubble.Name = d.CharacterName; bubble.Id = d.Id; } break; case DialogType.MULTICHOICE: if(d.CharacterImage != "Player") { // Log.Error("Only Player can have multichoice dialog: "+d.Id); return; } //hide player bubble.SetActive(false); playerDialog = d; popup.SetActive(true); UIManager.Instance.ShowModal(popup); ArrayList index = new ArrayList(new int[] {0, 1, 2}); int[] order = new int[3]; if (!GameManager.Instance.debugMode) { for (int i = 0; i < 3; i++) { int rnd = Random.Range(0, index.Count); order[i] = (int)index[rnd]; index.RemoveAt(rnd); } } else { order[0] = 0; order[1] = 1; order[2] = 2; } neutralButton.transform.position = multichoicePositions[order[0]]; negativeButton.transform.position = multichoicePositions[order[1]]; positiveButton.transform.position = multichoicePositions[order[2]]; neutralText.Text = playerDialog.GetText(EmotionType.NEUTRAL); positiveText.Text = playerDialog.GetText(EmotionType.POSITIVE); negativeText.Text = playerDialog.GetText(EmotionType.NEGATIVE); break; } } } else if(param == "hide") { // Log.Info(gameObject.transform.parent.name+" DialogView HIDE "+type+" event:"+eventId); if (skit != null) { skit.Hide(); skit = null; } if(npcDialog != null && eventId == npcDialog.Id) { SpeechBubble bubble = GetBubble(npcDialog.CharacterImage); bubble.SetActive(false); npcDialog = null; } else if(playerDialog != null && eventId == playerDialog.Id) { if(playerDialog.Type == DialogType.BUBBLE) { SpeechBubble bubble = GetBubble("Player"); bubble.SetActive(false); } else popup.SetActive(false); playerDialog = null; } } }