public void PoolPrefabItem(PrefabItem item)
 {
     item.gameObject.SetActive(false);
     item.posIndex = -1;
     item.toggle.SetIsOnWithoutNotify(false);
     item.favourite.SetIsOnWithoutNotify(false);
     PrefabItemPool.Enqueue(item);
 }
        private void FavouritePrefab(bool favourite, PrefabItem caller)
        {
            string name = caller.GetName();

            PrefabStates[name].isFavourite = favourite;

            EasySpawnerPlugin.SaveFavourites();
            RebuildPrefabDropdown();
        }
        private void SelectPrefab(PrefabItem caller)
        {
            foreach (PrefabItem prefabItem in PrefabItems)
            {
                // Disable all other prefabItems, without calling this method recursively
                prefabItem.toggle.SetIsOnWithoutNotify(prefabItem == caller);
            }

            SelectedPrefabName = caller != null?caller.GetName() : null;
        }
        public void UpdateItemPrefabPool(Vector2 slider)
        {
            PrefabScrollView.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, (SearchItems.Count - 1) * 20f);
            Rect    scrollRect = PrefabScrollView.GetComponent <RectTransform>().rect;
            Vector2 scrollPos  = PrefabScrollView.content.anchoredPosition;

            // search for items that are out of visible scroll rect
            foreach (PrefabItem item in PrefabItems)
            {
                if (PrefabItemPool.Contains(item))
                {
                    continue;
                }

                float posY = item.rectTransform.anchoredPosition.y;

                if (posY > -scrollPos.y + 20 || posY < -scrollPos.y - scrollRect.height - 20)
                {
                    PoolPrefabItem(item);
                }
            }

            int startIndex = Mathf.Max(0, Mathf.CeilToInt((scrollPos.y - 20) / 20));
            int maxItems   = Mathf.CeilToInt((scrollRect.height + 40) / 20);

            for (int i = startIndex; i < Mathf.Min(startIndex + maxItems, SearchItems.Count); i++)
            {
                if (PrefabItems.Any(x => x.posIndex == i))
                {
                    continue;
                }

                if (PrefabItemPool.Count > 0)
                {
                    PrefabItem item = PrefabItemPool.Dequeue();
                    item.rectTransform.anchoredPosition = new Vector2(0, -i * 20 - 10f);
                    item.posIndex = i;
                    item.SetName(SearchItems[i]);
                    item.toggle.SetIsOnWithoutNotify(SelectedPrefabName == item.GetName());
                    item.SetFavouriteOn(PrefabStates[item.GetName()].isFavourite, true);
                    item.gameObject.SetActive(true);
                }
            }
        }
        public void CreateMenu(GameObject menuGameObject)
        {
            PlayerDropdown   = menuGameObject.transform.Find("PlayerDropdown").GetComponent <Dropdown>();
            PrefabScrollView = menuGameObject.transform.Find("PrefabScrollView").GetComponent <ScrollRect>();

            SearchField = menuGameObject.transform.Find("SearchInputField").GetComponent <InputField>();
            SearchField.onValueChanged.AddListener(delegate { RebuildPrefabDropdown(); });

            AmountField = menuGameObject.transform.Find("AmountInputField").GetComponent <InputField>();
            LevelField  = menuGameObject.transform.Find("LevelInputField").GetComponent <InputField>();

            PutInInventoryToggle  = menuGameObject.transform.Find("PutInInventoryToggle").GetComponent <Toggle>();
            IgnoreStackSizeToggle = menuGameObject.transform.Find("IgnoreStackSizeToggle").GetComponent <Toggle>();
            FavouritesOnlyToggle  = menuGameObject.transform.Find("FavouritesOnlyToggle").GetComponent <Toggle>();
            FavouritesOnlyToggle.onValueChanged.AddListener(delegate { RebuildPrefabDropdown(); });

            SpawnButton = menuGameObject.transform.Find("SpawnButton").GetComponent <Button>();
            SpawnButton.onClick.AddListener(SpawnButtonPress);

            Transform hotkeyTexts = menuGameObject.transform.Find("HotkeyText");

            SpawnText = hotkeyTexts.Find("SpawnText").GetComponent <Text>();
            UndoText  = hotkeyTexts.Find("UndoText").GetComponent <Text>();
            CloseText = hotkeyTexts.Find("CloseText").GetComponent <Text>();

            //Set hotkey texts
            EasySpawnerConfig config = EasySpawnerPlugin.config;

            if (config.SpawnHotkeyModifierSet)
            {
                SpawnText.text = "Spawn: " + config.FirstSpawnHotkeyModifier.Value + " + " + config.FirstSpawnHotkey.Value;
            }
            else
            {
                SpawnText.text = "Spawn: " + config.FirstSpawnHotkey.Value;
            }

            if (config.UndoHotkeyModifierSet)
            {
                UndoText.text = "Undo: " + config.UndoHotkeyModifier.Value + " + " + config.UndoHotkey.Value;
            }
            else
            {
                UndoText.text = "Undo: " + config.UndoHotkey.Value;
            }

            if (config.OpenHotkeyModifierSet)
            {
                CloseText.text = "Open: " + config.FirstOpenHotkeyModifier.Value + " + " + config.FirstOpenHotkey.Value;
            }
            else
            {
                CloseText.text = "Open: " + config.FirstOpenHotkey.Value;
            }

            //Initial player dropdown
            PlayerDropdown.ClearOptions();
            RebuildPlayerDropdown();

            //Create prefab dropdown pool
            PrefabItems = new PrefabItem[20];
            PrefabItem template = PrefabScrollView.content.GetChild(0).gameObject.AddComponent <PrefabItem>();

            template.gameObject.SetActive(false);

            for (int i = 0; i < 20; i++)
            {
                GameObject option = UnityEngine.Object.Instantiate(template.gameObject, PrefabScrollView.content);
                PrefabItem item   = option.GetComponent <PrefabItem>();
                item.Init(SelectPrefab, FavouritePrefab);
                PoolPrefabItem(item);
                PrefabItems[i] = item;
            }

            PrefabScrollView.onValueChanged.AddListener(UpdateItemPrefabPool);
            RebuildPrefabDropdown();
        }
 public void PoolPrefabItem(PrefabItem item)
 {
     item.Pool();
     PrefabItemPool.Enqueue(item);
 }