Example #1
0
        protected NameAndParameterListView(SerializedProperty property, Action applyAction, string expectedControlLayout, TypeTable listOptions, Func <Type, Type> getValueType, string itemName)
        {
            m_ItemName     = itemName;
            m_GetValueType = getValueType;
            m_DeleteButton = EditorGUIUtility.TrIconContent("Toolbar Minus", $"Delete {itemName}");
            m_UpButton     = EditorGUIUtility.TrIconContent(GUIHelpers.LoadIcon("ChevronUp"), $"Move {itemName} up");
            m_DownButton   = EditorGUIUtility.TrIconContent(GUIHelpers.LoadIcon("ChevronDown"), $"Move {itemName} down");

            m_Property    = property;
            m_Apply       = applyAction;
            m_ListOptions = listOptions;

            m_ExpectedControlLayout = expectedControlLayout;
            if (!string.IsNullOrEmpty(m_ExpectedControlLayout))
            {
                m_ExpectedValueType = EditorInputControlLayoutCache.GetValueType(m_ExpectedControlLayout);
            }

            m_ParametersForEachListItem         = NameAndParameters.ParseMultiple(m_Property.stringValue).ToArray();
            m_EditableParametersForEachListItem = new ParameterListView[m_ParametersForEachListItem.Length];

            for (var i = 0; i < m_ParametersForEachListItem.Length; i++)
            {
                m_EditableParametersForEachListItem[i] = new ParameterListView {
                    onChange = OnParametersChanged
                };
                var typeName = m_ParametersForEachListItem[i].name;
                var rowType  = m_ListOptions.LookupTypeRegistration(typeName);
                m_EditableParametersForEachListItem[i].Initialize(rowType, m_ParametersForEachListItem[i].parameters);

                var name = ObjectNames.NicifyVariableName(typeName);

                ////REVIEW: finding this kind of stuff should probably have better support globally on the asset; e.g. some
                ////        notification that pops up and allows fixing all occurrences in one click
                // Find out if we still support this option and indicate it in the list, if we don't.
                if (rowType == null)
                {
                    name += " (Obsolete)";
                }
                else if (m_ExpectedValueType != null)
                {
                    var valueType = getValueType(rowType);
                    if (!m_ExpectedValueType.IsAssignableFrom(valueType))
                    {
                        name += " (Ignored)";
                    }
                }
                m_EditableParametersForEachListItem[i].name = name;
            }
        }
Example #2
0
        protected PropertiesReorderableList(SerializedProperty property, Action applyAction, string expectedControlLayout)
        {
            m_Property    = property;
            m_Apply       = applyAction;
            m_ListItems   = new List <string>();
            m_ListOptions = GetOptions();
            m_EditableParametersForSelectedItem = new ParameterListView {
                onChange = OnParametersChanged
            };
            m_ParametersForEachListItem = InputControlLayout.ParseNameAndParameterList(m_Property.stringValue)
                                          ?? new InputControlLayout.NameAndParameters[0];
            m_ExpectedControlLayout = expectedControlLayout;

            foreach (var nameAndParams in m_ParametersForEachListItem)
            {
                var name = ObjectNames.NicifyVariableName(nameAndParams.name);

                ////REVIEW: finding this kind of stuff should probably have better support globally on the asset; e.g. some
                ////        notification that pops up and allows fixing all occurrences in one click
                // Find out if we still support this option and indicate it in the list, if we don't.
                if (m_ListOptions.LookupTypeRegistration(new InternedString(nameAndParams.name)) == null)
                {
                    name += " (Obsolete)";
                }

                m_ListItems.Add(name);
            }

            m_ListView = new ReorderableList(m_ListItems, typeof(string))
            {
                headerHeight          = 3,
                onAddDropdownCallback = (rect, list) =>
                {
                    Type expectedValueType = null;
                    if (!string.IsNullOrEmpty(m_ExpectedControlLayout))
                    {
                        expectedValueType = EditorInputControlLayoutCache.GetValueType(m_ExpectedControlLayout);
                    }

                    // Add only original names to the menu and not aliases.
                    var menu = new GenericMenu();
                    foreach (var name in m_ListOptions.internedNames.Where(x => !m_ListOptions.aliases.Contains(x)).OrderBy(x => x.ToString()))
                    {
                        // Skip if not compatible with value type.
                        if (expectedValueType != null)
                        {
                            var type      = m_ListOptions.LookupTypeRegistration(name);
                            var valueType = GetValueType(type);
                            if (valueType != null && !expectedValueType.IsAssignableFrom(valueType))
                            {
                                continue;
                            }
                        }

                        var niceName = ObjectNames.NicifyVariableName(name);
                        menu.AddItem(new GUIContent(niceName), false, OnAddElement, name.ToString());
                    }
                    menu.ShowAsContext();
                },
                onRemoveCallback = list =>
                {
                    var index = list.index;
                    list.list.RemoveAt(index);
                    ArrayHelpers.EraseAt(ref m_ParametersForEachListItem, index);
                    m_EditableParametersForSelectedItem.Clear();
                    m_Apply();
                    list.index = -1;
                },
                onReorderCallbackWithDetails = (list, oldIndex, newIndex) =>
                {
                    MemoryHelpers.Swap(ref m_ParametersForEachListItem[oldIndex],
                                       ref m_ParametersForEachListItem[newIndex]);
                    OnSelection(list);
                    m_Apply();
                },
                onSelectCallback = OnSelection
            };
        }
 public void Editor_CanGetValueTypeOfLayout()
 {
     Assert.That(EditorInputControlLayoutCache.GetValueType("Axis"), Is.SameAs(typeof(float)));
     Assert.That(EditorInputControlLayoutCache.GetValueType("Button"), Is.SameAs(typeof(float)));
     Assert.That(EditorInputControlLayoutCache.GetValueType("Stick"), Is.SameAs(typeof(Vector2)));
 }