/// <summary> /// Updates all elements in the specified category. /// </summary> /// <param name="instance">The inventory of discovered elements.</param> /// <param name="category">The category to search.</param> private void UpdateCategory(WorldInventory instance, Tag category) { if (instance.TryGetDiscoveredResourcesFromTag(category, out HashSet <Tag> found) && found.Count > 0) { // Attempt to add to type select control if (!children.TryGetValue(category, out TypeSelectCategory current)) { current = new TypeSelectCategory(this, category); children.Add(category, current); int index = children.IndexOfKey(category) << 1; GameObject header = current.Header, panel = current.ChildPanel; // Header goes in even indexes, panel goes in odds PUIElements.SetParent(header, childPanel); PUIElements.SetAnchors(header, PUIAnchoring.Stretch, PUIAnchoring.Stretch); header.transform.SetSiblingIndex(index); PUIElements.SetParent(panel, childPanel); PUIElements.SetAnchors(panel, PUIAnchoring.Stretch, PUIAnchoring.Stretch); panel.transform.SetSiblingIndex(index + 1); } foreach (var element in found) { current.TryAddType(element); } } }
protected override void OnDeactivateTool(InterfaceTool newTool) { var menu = ToolMenu.Instance; // Unparent but do not dispose if (TypeSelect != null) PUIElements.SetParent(TypeSelect.RootPanel, null); menu.PriorityScreen.Show(false); base.OnDeactivateTool(newTool); }
protected override void OnActivateTool() { var menu = ToolMenu.Instance; base.OnActivateTool(); // Update only on tool activation to improve performance if (TypeSelect != null) { var root = TypeSelect.RootPanel; TypeSelect.Update(); PUIElements.SetParent(root, menu.gameObject); root.transform.SetAsFirstSibling(); } menu.PriorityScreen.Show(true); }
/// <summary> /// Updates the list of available elements. /// </summary> public void Update(GridFilterableSideScreen screen) { Console.WriteLine("updating with the storage"); Storage storage = screen.storage; GameObject target = screen.target; if (storage.storageFilters != null && storage.storageFilters.Count >= 1) { // check for which ones aren't added already and add them foreach (Tag tag in storage.storageFilters) { Console.WriteLine($"Should be checking presence of tag {tag.ToString()}"); if (!HasElement(tag)) { Console.WriteLine($"Attempted to add {tag.ToString()} to the panel"); if (children.Count <= 0) { GridFilterableRow firstRow = new GridFilterableRow(this); children.Add(firstRow); PUIElements.SetParent(firstRow.ChildPanel, childPanel); PUIElements.SetAnchors(firstRow.ChildPanel, PUIAnchoring.Stretch, PUIAnchoring.Stretch); } GridFilterableRow lastRow = children[children.Count - 1]; if (lastRow.RowSize >= PER_ROW) { lastRow = new GridFilterableRow(this); PUIElements.SetParent(lastRow.ChildPanel, childPanel); PUIElements.SetAnchors(lastRow.ChildPanel, PUIAnchoring.Stretch, PUIAnchoring.Stretch); children.Add(lastRow); } GridFilterableSelectableEntity entity = new GridFilterableSelectableEntity(lastRow, tag); lastRow.Children.Add(entity); PUIElements.SetParent(entity.CheckBox, lastRow.ChildPanel); if (PCheckBox.GetCheckState(entity.CheckBox) == PCheckBox.STATE_CHECKED) { // Set to checked PCheckBox.SetCheckState(entity.CheckBox, PCheckBox.STATE_CHECKED); } } } } else { Debug.LogError((object)"If you're filtering, your storage filter should have the filters set on it"); } }
/// <summary> /// Attempts to add a type to this category. /// </summary> /// <param name="element">The type to add.</param> /// <returns>true if it was added, or false if it already exists.</returns> public bool TryAddType(Tag element) { bool add = !children.ContainsKey(element); if (add) { var child = new TypeSelectElement(this, element); var cb = child.CheckBox; // Add the element to the list, then get its index and move it in the panel // to maintain sorted order children.Add(element, child); PUIElements.SetParent(cb, ChildPanel); if (PCheckBox.GetCheckState(cb) == PCheckBox.STATE_CHECKED) { // Set to checked PCheckBox.SetCheckState(cb, PCheckBox.STATE_CHECKED); } cb.transform.SetSiblingIndex(children.IndexOfKey(element)); } return(add); }
/// <summary> /// Updates the list of available elements. /// </summary> public void Update(GameObject target) { Target = target; Storage storage = Target.GetComponent <Storage>(); if (storage.storageFilters == null || storage.storageFilters.Count < 1) { PUtil.LogError("If you're filtering, your storage filter should have the filters set on it"); return; } HashSet <Tag> containedTags = ContainedTags(); HashSet <Tag> goalTags = new HashSet <Tag>(storage.storageFilters.Where(tag => WorldInventory.Instance.IsDiscovered(tag))); // if this is not supposed to display the exact same UI elements rebuild the entire UI // not the *most* performant way to handle things but trying to perform multiple insertions/deletions into this // row system is probably *less* performant if (!containedTags.SetEquals(goalTags)) { // clear the UI foreach (var row in rows) { // null the parent of the entity and disable it foreach (var entity in row.entities) { PUIElements.SetParent(entity.CheckBox, null); entity.CheckBox.SetActive(false); entity.Parent = null; } // clear all the entities from this row row.entities.Clear(); // do not null the parent of the row since it will be reused in same spot but do disable it row.ChildPanel.SetActive(false); } // build the UI with tags in alphabetic order List <Tag> goalList = goalTags.ToList(); goalList.Sort(TagAlphabetComparer.INSTANCE); int rowIndex = 0; foreach (Tag tag in goalList) { // wrap around when rows are full if (rowIndex < rows.Count && rows[rowIndex].RowSize >= PER_ROW) { rowIndex++; } // build new rows as needed if (rows.Count <= rowIndex) { UncategorizedFilterableRow newRow = new UncategorizedFilterableRow(this); rows.Add(newRow); PUIElements.SetParent(newRow.ChildPanel, childPanel); PUIElements.SetAnchors(newRow.ChildPanel, PUIAnchoring.Stretch, PUIAnchoring.Stretch); } var row = rows[rowIndex]; row.ChildPanel.SetActive(true); // build new entity for tag when it is first encountered if (!entities.ContainsKey(tag)) { UncategorizedFilterableEntity newEntity = new UncategorizedFilterableEntity(null, tag, tag.ProperName()); if (PCheckBox.GetCheckState(newEntity.CheckBox) == PCheckBox.STATE_CHECKED) { // Set to checked PCheckBox.SetCheckState(newEntity.CheckBox, PCheckBox.STATE_CHECKED); } entities[tag] = newEntity; } var entity = entities[tag]; row.entities.Add(entity); PUIElements.SetParent(entity.CheckBox, row.ChildPanel); entity.CheckBox.SetActive(true); entity.Parent = row; } } // with the right elements in the UI it is now necessary to set the properties for each entity correctly based on // if they are checked already and if they are present in the world UncategorizedFilterable filterable = Target.GetComponent <UncategorizedFilterable>(); foreach (var row in rows) { foreach (var entity in row.entities) { // set checkbox state if (filterable.AcceptedTags.Contains(entity.ElementTag)) { PCheckBox.SetCheckState(entity.CheckBox, PCheckBox.STATE_CHECKED); } else { PCheckBox.SetCheckState(entity.CheckBox, PCheckBox.STATE_UNCHECKED); } // set active state var button = entity.CheckBox.GetComponentInChildren <KButton>(); button.isInteractable = WorldInventory.Instance.GetTotalAmount(entity.ElementTag) > 0.0; } } UpdateFromChildren(); }