/// <summary> /// Populates the menu with the available destroy modes. /// </summary> /// <param name="parameters">The modes to show in the menu.</param> internal void PopulateMenu(IList <DestroyFilter> parameters) { int i = 0; var prefab = ToolMenu.Instance.toolParameterMenu.widgetPrefab; ClearMenu(); foreach (var parameter in parameters) { // Create prefab based on existing Klei menu var widgetPrefab = Util.KInstantiateUI(prefab, choiceList, true); PUIElements.SetText(widgetPrefab, parameter.Title); var toggle = widgetPrefab.GetComponentInChildren <MultiToggle>(); var checkbox = toggle?.gameObject; if (checkbox != null) { // Set initial state, note that ChangeState is only called by SetCheckState // if it appears to be different, but since this executes before the // parent is active it must be set to something different var option = new DestroyMenuOption(parameter, checkbox); PCheckBox.SetCheckState(checkbox, 2); if (i == 0 || SandboxToolsPatches.AdvancedFilterEnabled) { option.State = ToolParameterMenu.ToggleState.On; } options.Add(parameter.ID, option); toggle.onClick += () => OnClick(checkbox); } else { PUtil.LogWarning("Could not find destroy menu checkbox!"); } i++; } }
// штуки для создания сидескреенов // чекбокс и слидер с обвязкой и подгрузкой строк, добавляемые на панельку // установка и считывание значений реализованы через акции-каллбаки public static PPanel AddCheckBox(this PPanel parent, string prefix, string name, Action <bool> onChecked, out Action <bool> setChecked, out Action <bool> setActive) { prefix = (prefix + name).ToUpperInvariant(); GameObject cb_go = null; var cb = new PCheckBox(name) { CheckColor = PUITuning.Colors.ComponentLightStyle, CheckSize = new Vector2(26f, 26f), Text = Strings.Get(prefix + ".NAME"), TextAlignment = TextAnchor.MiddleLeft, TextStyle = PUITuning.Fonts.TextDarkStyle, ToolTip = Strings.Get(prefix + ".TOOLTIP"), OnChecked = (go, state) => { // переворачиваем предыдующее значение bool @checked = state == PCheckBox.STATE_UNCHECKED; PCheckBox.SetCheckState(go, @checked ? PCheckBox.STATE_CHECKED : PCheckBox.STATE_UNCHECKED); onChecked?.Invoke(@checked); // внесапно, численное значение состояния чекбокса совпало с индексом таблицы звуков KFMOD.PlayUISound(WidgetSoundPlayer.getSoundPath(ToggleSoundPlayer.default_values[state])); }, }.AddOnRealize(realized => cb_go = realized); setChecked = @checked => { if (cb_go != null) { PCheckBox.SetCheckState(cb_go, @checked ? PCheckBox.STATE_CHECKED : PCheckBox.STATE_UNCHECKED); } }; setActive = on => cb_go?.SetActive(on); return(parent.AddChild(cb)); }
/// <summary> /// Updates the visible checkboxes to correspond with the layer settings. /// </summary> private void OnChange() { bool all = true; foreach (var option in options.Values) { var checkbox = option.Checkbox; switch (option.State) { case ToolParameterMenu.ToggleState.On: PCheckBox.SetCheckState(checkbox, 1); break; case ToolParameterMenu.ToggleState.Off: PCheckBox.SetCheckState(checkbox, 0); all = false; break; case ToolParameterMenu.ToggleState.Disabled: default: PCheckBox.SetCheckState(checkbox, 2); all = false; break; } } AllSelected = all; }
/// <summary> /// Deselects all items in this category. /// </summary> public void ClearAll() { PCheckBox.SetCheckState(CheckBox, PCheckBox.STATE_UNCHECKED); foreach (var child in children) { PCheckBox.SetCheckState(child.Value.CheckBox, PCheckBox.STATE_UNCHECKED); } }
/// <summary> /// Selects all items. /// </summary> public void CheckAll() { PCheckBox.SetCheckState(allItems, PCheckBox.STATE_CHECKED); foreach (var child in children) { child.Value.CheckAll(); } }
/// <summary> /// Deselects all items. /// </summary> public void ClearAll() { PCheckBox.SetCheckState(allItems, PCheckBox.STATE_UNCHECKED); foreach (var child in children) { foreach (var entity in child.Children) { PCheckBox.SetCheckState(entity.CheckBox, PCheckBox.STATE_UNCHECKED); } } }
/// <summary> /// Deselects all items. /// </summary> public void ClearAll() { PCheckBox.SetCheckState(allItems, PCheckBox.STATE_UNCHECKED); foreach (var row in rows) { foreach (var entity in row.entities) { PCheckBox.SetCheckState(entity.CheckBox, PCheckBox.STATE_UNCHECKED); Target.GetComponent <UncategorizedFilterable>().RemoveTagFromFilter(entity.ElementTag); } } }
/// <summary> /// Sets the selected state of this type. /// </summary> /// <param name="selected">true to select this type, or false otherwise.</param> public void SetSelected(bool selected) { if (selected) { // Clicked when unchecked, check and possibly check all PCheckBox.SetCheckState(CheckBox, PCheckBox.STATE_CHECKED); } else { // Clicked when checked, clear and possibly uncheck PCheckBox.SetCheckState(CheckBox, PCheckBox.STATE_UNCHECKED); } parent.UpdateFromChildren(); }
private void OnCheck(GameObject source, int state) { if (state == PCheckBox.STATE_UNCHECKED) { // Clicked when unchecked, check and possibly check all PCheckBox.SetCheckState(CheckBox, PCheckBox.STATE_CHECKED); } else { // Clicked when checked, clear and possibly uncheck PCheckBox.SetCheckState(CheckBox, PCheckBox.STATE_UNCHECKED); } parent.UpdateFromChildren(); }
private void OnCheck(GameObject source, int state) { switch (state) { case PCheckBox.STATE_UNCHECKED: // Clicked when unchecked, check and possibly check all PCheckBox.SetCheckState(CheckBox, PCheckBox.STATE_CHECKED); break; default: // Clicked when checked, clear and possibly uncheck PCheckBox.SetCheckState(CheckBox, PCheckBox.STATE_UNCHECKED); break; } parent.UpdateFromChildren(); }
/// <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"); } }
private void OnCheck(GameObject source, int state) { UncategorizedFilterable uncategorizedFilterable = Parent.Parent.Target.GetComponent <UncategorizedFilterable>(); switch (state) { case PCheckBox.STATE_UNCHECKED: // Clicked when unchecked, check and possibly check all PCheckBox.SetCheckState(CheckBox, PCheckBox.STATE_CHECKED); uncategorizedFilterable.AddTagToFilter(ElementTag); break; default: // Clicked when checked, clear and possibly uncheck PCheckBox.SetCheckState(CheckBox, PCheckBox.STATE_UNCHECKED); uncategorizedFilterable.RemoveTagFromFilter(ElementTag); break; } Parent.UpdateFromChildren(); }
/// <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); cb.SetParent(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 all check box state from the children. /// </summary> /// <param name="allItems">The "all" or "none" check box.</param> /// <param name="children">The child check boxes.</param> internal static void UpdateAllItems <T>(GameObject allItems, IEnumerable <T> children) where T : IHasCheckBox { if (allItems != null) { bool all = true, none = true; foreach (var child in children) { if (PCheckBox.GetCheckState(child.CheckBox) == PCheckBox.STATE_CHECKED) { none = false; } else { // Partially checked or unchecked all = false; } } PCheckBox.SetCheckState(allItems, none ? PCheckBox.STATE_UNCHECKED : (all ? PCheckBox.STATE_CHECKED : PCheckBox.STATE_PARTIAL)); } }
/// <summary> /// Updates the checkbox state based on the current mod selections. /// </summary> internal void UpdateCheckedState() { bool all = true, some = false; var mods = Global.Instance.modManager.mods; // Check to see if all, some, or none are enabled foreach (var mod in mods) { if (mod.enabled) { some = true; } else { all = false; } } // Apply to checkbox if (checkbox != null) { PCheckBox.SetCheckState(checkbox, all ? PCheckBox.STATE_CHECKED : (some ? PCheckBox.STATE_PARTIAL : PCheckBox.STATE_UNCHECKED)); } }
/// <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(); }
private void Update() { PCheckBox.SetCheckState(checkbox, check ? PCheckBox.STATE_CHECKED : PCheckBox. STATE_UNCHECKED); }
private static void ToggleCheckbox(GameObject checkbox, int state) { PCheckBox.SetCheckState(checkbox, state == PCheckBox.STATE_CHECKED ? PCheckBox. STATE_UNCHECKED : PCheckBox.STATE_CHECKED); }