//in here manage the onlyOneSelectionAllowed private void OnSelectEvents(MenuElementInfo arg0) { if (OnlyOneSelectable && arg0.isSelected) { foreach (MenuElementInfo mei in MenuElements) { //if someone isSelected and not the freshly selected item if (mei.isSelected && mei != arg0) { //catchNextDeselectionEvent = true; mei.interactiveToggle.SetSelection(false); //mei.OnDeselectEvents(); // we need to fire the deselect event ourself } } } else if (OnlyOneSelectable && !arg0.isSelected)//it is not allowed to deselect { arg0.isSelected = true; arg0.interactiveToggle.SetSelection(true); arg0.interactiveToggle.HasSelection = true; arg0.interactiveToggle.Selection = true; return;//do not invoke } //in the end, invoke subscribers onSelectMenuController.Invoke(arg0); }
/// <summary> /// removes the element and updates the collection. /// removes the listener as well, if nessesary /// </summary> /// <param name="ID"></param> /// <param name="onSelectToRemove"></param> public void removeElement(string ID, UnityAction <MenuElementInfo> onSelectToRemove = null) { MenuElementInfo mei = MenuElements.Find(x => x.ID == ID); Destroy(mei?.collectionNode.transform.gameObject); if (mei != null) { mei.onSelect.RemoveListener(OnSelectEvents); mei.collectionNode.transform.parent = null; mei.collectionNode.transform = null; } if (onSelectToRemove != null) { onSelectMenuController.RemoveListener(onSelectToRemove); } updateCollection(); MenuElements.Remove(mei); }
public int addMenuElement( GameObject miniatureGO, string ID,//we save a key string, so we know later what the menu element is about GameObject alternativeMiniatureGO = null, UnityAction <MenuElementInfo> onSelect = null, int placement = default(int), UnityAction <MenuElementInfo> onHold = null, string textOnToggleSelect = "On", string textOnToggleUnselect = "Off") { if (miniatureGO == null) { return(0); } MenuElementInfo mei = new MenuElementInfo(); if (mei.onSelect == null) { mei.onSelect = new MyOnSelectEvent(); } mei.onSelect.AddListener(OnSelectEvents);//connection to this controller GameObject MenuElement = null; try { //fill all of the menu element info, that we already know if (onSelect != null) { //adds the listener if (onSelectMenuController == null) { onSelectMenuController = new MyOnSelectEvent(); } onSelectMenuController.RemoveListener(onSelect); //we need to ensure only one listener of the methiode is in there onSelectMenuController.AddListener(onSelect); } //fill all of the menu element info, that we already know if (onHold != null) { //adds the hold listener if (mei.OnHold == null) { mei.OnHold = new MyOnSelectEvent(); } mei.OnHold.AddListener(onHold); } mei.miniatureGO = miniatureGO; mei.alternativeMiniatureGO = alternativeMiniatureGO; // doesnt matter if null mei.ID = ID; //now lets instantiate the prefab for the menu element MenuElement = Instantiate(MenuElement_Prefab, this.transform); //the menu element is now part of the collection MenuElement.transform.SetSiblingIndex(placement); //TODO: out of range exceptions //generate a name and a number var nameAndNumber = generateMenuElementName(); MenuElement.name = nameAndNumber.Key; mei.number = nameAndNumber.Value; mei.backgroundButton = MenuElement.GetComponent <CompoundButton>(); mei.interactiveToggle = MenuElement.GetComponentInChildren <InteractiveToggle>();//registers automatiacally the event mei.miniaturGameObjectScript = MenuElement.GetComponentInChildren <MiniaturGameObjectScript>(); mei.miniaturGameObjectSwitcher = MenuElement.GetComponentInChildren <miniaturGameObjectSwitcher>(); //Last step is to refresh the collection. updateCollection(); //now we can access the node mei.collectionNode = objectCollection.NodeList.Find(x => x.Name == nameAndNumber.Key); //so it shows a miniature object immediately mei.miniaturGameObjectScript.toShowInstance = Instantiate(miniatureGO); //set references mei.miniaturGameObjectSwitcher.normal_toCopy = miniatureGO; mei.miniaturGameObjectSwitcher.alternative_toCopy = alternativeMiniatureGO; //set text of the toggle mei.interactiveToggle.GetComponent <LabelTheme>().Selected = textOnToggleSelect; mei.interactiveToggle.GetComponent <LabelTheme>().Default = textOnToggleUnselect; //We now have a complete MenuElementInfo MenuElements.Add(mei); //id of the element return(mei.number); } catch (System.Exception e) { //deleta already instantiated stuff Debug.LogError(e.Message); Destroy(MenuElement); throw e; } }