/// <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();
        }
예제 #2
0
        public UncategorizedFilterableEntity(UncategorizedFilterableRow parent, Tag elementTag, string name)
        {
            Parent     = parent;
            ElementTag = elementTag;
            Name       = name;

            var tint   = Color.white;
            var sprite = GetStorageObjectSprite(elementTag, out tint);

            var background = new PPanel("Background")
            {
                Direction = PanelDirection.Vertical,
                Alignment = TextAnchor.MiddleCenter
            }.AddChild(new PEntityToggle("Select")
            {
                OnChecked     = OnCheck,
                InitialState  = PCheckBox.STATE_CHECKED,
                Sprite        = sprite,
                SpriteTint    = tint,
                Margin        = ELEMENT_MARGIN,
                TextAlignment = TextAnchor.UpperCenter,
                CheckSize     = CHECK_SIZE,
                SpriteSize    = ICON_SIZE,
            });

            // Background
            background.OnRealize += (obj) =>
            {
                var kImage          = obj.AddComponent <KImage>();
                var ButtonBlueStyle = ScriptableObject.CreateInstance <ColorStyleSetting>();
                ButtonBlueStyle.activeColor         = new Color(0.5033521f, 0.5444419f, 0.6985294f);
                ButtonBlueStyle.inactiveColor       = new Color(0.2431373f, 0.2627451f, 0.3411765f);
                ButtonBlueStyle.disabledColor       = new Color(0.4156863f, 0.4117647f, 0.4f);
                ButtonBlueStyle.disabledActiveColor = new Color(0.625f, 0.6158088f, 0.5882353f);
                ButtonBlueStyle.hoverColor          = new Color(0.3461289f, 0.3739619f, 0.4852941f);
                ButtonBlueStyle.disabledhoverColor  = new Color(0.5f, 0.4898898f, 0.4595588f);
                kImage.colorStyleSetting            = ButtonBlueStyle;
                kImage.color = ButtonBlueStyle.inactiveColor;

                var kButton = obj.AddComponent <KButton>();
                kButton.additionalKImages = new KImage[0];
                kButton.soundPlayer       = ButtonSounds;
                kButton.bgImage           = kImage;
                kButton.colorStyleSetting = ButtonBlueStyle;
            };
            CheckBox = new PPanel("Border")
            {
                Margin    = BORDER_MARGIN,
                Direction = PanelDirection.Vertical,
                Alignment = TextAnchor.MiddleCenter,
                Spacing   = 1,
                BackColor = new Color(0, 0, 0, 255)
            }.AddChild(background).Build();

            var tooltip = CheckBox.AddComponent <ToolTip>();

            tooltip.SetSimpleTooltip(name);
            tooltip.tooltipPivot          = new Vector2(0.5f, 1f);
            tooltip.tooltipPositionOffset = new Vector2(0.0f, -60f);
            tooltip.parentPositionAnchor  = new Vector2(0.5f, 0.5f);
        }