예제 #1
0
        /// <summary>
        /// Opens the default label window
        /// </summary>
        /// <param name="position">The position where the window is going to be drawn</param>
        /// <param name="labels">Label array used to populate the list</param>
        /// <param name="selectedLabels">Selected labels</param>
        /// <param name="populateDefaultLabels">Should the default labels be included</param>
        /// <param name="allowMultipleSelection">Should the system allow more than 1 label selected</param>
        /// <param name="closeOnSelection">Should the window close on selection</param>
        /// <param name="allowCustom">Allow custom labels</param>
        /// <param name="maxCount">Max label count</param>
        /// <param name="sortAlphabetically">Should the labels be sorted</param>
        /// <param name="enableAutoCompletition">Should the window show auto completetion</param>
        public void OpenLabelWindow(Rect position, string[] labels, string[] selectedLabels, bool populateDefaultLabels = true, bool allowMultipleSelection = true, bool closeOnSelection = false, bool allowCustom = true, int maxCount = 15, bool sortAlphabetically = true, bool enableAutoCompletition = true)
        {
            if (labels == null)
            {
                labels = new string[] { }
            }
            ;
            if (selectedLabels == null)
            {
                selectedLabels = new string[] { }
            }
            ;

            this.allowMultipleSelection = allowMultipleSelection;
            this.allowCustom            = allowCustom;

            //Cache the data required
            CacheData(labels, selectedLabels, populateDefaultLabels, closeOnSelection, maxCount, sortAlphabetically, enableAutoCompletition);

            //Create a reference of the window
            var popupListReference = Activator.CreateInstance(PopupList, new object[] { m_AssetLabels });

            //Get the correct show method
            var showMethod = typeof(PopupWindow).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).Where(x =>
                                                                                                                x.Name.Equals("Show") && x.GetParameters().Length == 4).Single();

            //Invoke the method with the correct arguments
            showMethod.Invoke(null, new object[] { position, popupListReference, null, 6 });
        }

        void CacheData(string[] labels, string[] selectedLabels, bool populateDefaultLabels, bool closeOnSelection, int maxCount, bool sortAlphabetically, bool enableAutoCompletition)
        {
            //Create instance to the delegate
            Delegate action = Delegate.CreateDelegate(OnSelectCallback.FieldType, this, OnInternalSelectCallback);

            //Create instance of data to send to the popup window
            m_AssetLabels = Activator.CreateInstance(InputDataType);

            //Assign all the respective values, including the delegate callback
            CloseOnSelection.SetValue(m_AssetLabels, closeOnSelection);
            AllowCustom.SetValue(m_AssetLabels, true);
            OnSelectCallback.SetValue(m_AssetLabels, action);
            MaxCount.SetValue(m_AssetLabels, maxCount);
            SortAlphabetically.SetValue(m_AssetLabels, sortAlphabetically);
            EnableAutoCompletion.SetValue(m_AssetLabels, enableAutoCompletition);

            //Get all the labels available
            if (populateDefaultLabels)
            {
                allLabels = (Dictionary <string, float>) typeof(AssetDatabase).InvokeMember("GetAllLabels", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, null);
            }

            //Include any custom one sent on the array
            foreach (var tag in labels)
            {
                if (string.IsNullOrEmpty(tag))
                {
                    continue;
                }

                if (!allLabels.ContainsKey(tag))
                {
                    allLabels.Add(tag, 0);
                }
            }

            //Asing all the selected values
            foreach (var pair in allLabels)
            {
                var element = NewOrMatchingElement.Invoke(m_AssetLabels, new object[] { pair.Key });

                if ((float)(FilterScore.GetValue(element, null)) < pair.Value)
                {
                    FilterScore.SetValue(element, pair.Value, null);
                }
                Selected.SetValue(element, selectedLabels.Any(label => string.Equals(label, pair.Key, StringComparison.OrdinalIgnoreCase)), null);
            }
        }

        /// <summary>
        /// Function called by Unity when an element is selected
        /// </summary>
        /// <param name="data">Element data</param>
        void _OnInternalSelectCallback <T>(T data)
        {
            string selectedLabel = Text.GetValue(data, null).ToString();

            if (!allowCustom && !allLabels.Keys.Any(x => x.ToLower().Equals(selectedLabel.ToLower())))
            {
                return;
            }

            if (!allowMultipleSelection)
            {
                foreach (var pair in allLabels)
                {
                    if (pair.Key.ToLower().Equals(selectedLabel.ToLower()))
                    {
                        continue;
                    }

                    var element = NewOrMatchingElement.Invoke(m_AssetLabels, new object[] { pair.Key });

                    Selected.SetValue(element, false, null);
                }
            }

            bool currentValue = (bool)(Selected.GetValue(data, null));

            Selected.SetValue(data, !currentValue, null);

            if (OnSelect != null)
            {
                OnSelect.Invoke(selectedLabel, !currentValue);
            }
        }
    }
}