Example #1
0
        public override void BuildUsedTemplates(UnityEngine.UIElements.VisualElement container, SerializedObject so)
        {
            var header = new Label("Used in Data Configs:");

            header.AddToClassList("header");
            container.Add(header);

            var templatesContainer = new VisualElement();

            container.Add(templatesContainer);
            var source = so.FindProperty("usedIn");

            var usedComponents = new System.Collections.Generic.HashSet <ME.ECS.DataConfigs.DataConfig>();

            BuildTemplates(usedComponents, templatesContainer, source);
        }
        // IVisualElementHierarchy container
        public void Add(VisualElement child)
        {
            if (child == null)
            {
                return;
            }

            if (contentContainer == this)
            {
                hierarchy.Add(child);
            }
            else
            {
                contentContainer?.Add(child);
            }

            child.m_LogicalParent = this;
        }
Example #3
0
        private VisualElement CloneSetupRecursively(VisualElementAsset root,
                                                    Dictionary <int, List <VisualElementAsset> > idToChildren, CreationContext context)
        {
            VisualElement ve = Create(root, context);

            // context.target is the created templateContainer
            if (root.id == context.visualTreeAsset.contentContainerId)
            {
                if (context.target is TemplateContainer)
                {
                    ((TemplateContainer)context.target).SetContentContainer(ve);
                }
                else
                {
                    Debug.LogError(
                        "Trying to clone a VisualTreeAsset with a custom content container into a element which is not a template container");
                }
            }

            // if the current element had a slot-name attribute, put it in the resulting slot mapping
            string slotName;

            if (context.slotInsertionPoints != null && TryGetSlotInsertionPoint(root.id, out slotName))
            {
                context.slotInsertionPoints.Add(slotName, ve);
            }

            if (root.classes != null)
            {
                for (int i = 0; i < root.classes.Length; i++)
                {
                    ve.AddToClassList(root.classes[i]);
                }
            }

            if (root.ruleIndex != -1)
            {
                if (inlineSheet == null)
                {
                    Debug.LogWarning("VisualElementAsset has a RuleIndex but no inlineStyleSheet");
                }
                else
                {
                    StyleRule r          = inlineSheet.rules[root.ruleIndex];
                    var       stylesData = new VisualElementStylesData(false);
                    ve.SetInlineStyles(stylesData);
                    stylesData.ApplyRule(inlineSheet, Int32.MaxValue, r,
                                         StyleSheetCache.GetPropertyIDs(inlineSheet, root.ruleIndex));
                }
            }

            var templateAsset = root as TemplateAsset;
            List <VisualElementAsset> children;

            if (idToChildren.TryGetValue(root.id, out children))
            {
                children.Sort(CompareForOrder);

                foreach (VisualElementAsset childVea in children)
                {
                    // this will fill the slotInsertionPoints mapping
                    VisualElement childVe = CloneSetupRecursively(childVea, idToChildren, context);
                    if (childVe == null)
                    {
                        continue;
                    }

                    // if the parent is not a template asset, just add the child to whatever hierarchy we currently have
                    // if ve is a scrollView (with contentViewport as contentContainer), this will go to the right place
                    if (templateAsset == null)
                    {
                        ve.Add(childVe);
                        continue;
                    }

                    int index = templateAsset.slotUsages == null
                        ? -1
                        : templateAsset.slotUsages.FindIndex(u => u.assetId == childVea.id);
                    if (index != -1)
                    {
                        VisualElement parentSlot;
                        string        key = templateAsset.slotUsages[index].slotName;
                        Assert.IsFalse(String.IsNullOrEmpty(key),
                                       "a lost name should not be null or empty, this probably points to an importer or serialization bug");
                        if (context.slotInsertionPoints == null ||
                            !context.slotInsertionPoints.TryGetValue(key, out parentSlot))
                        {
                            Debug.LogErrorFormat("Slot '{0}' was not found. Existing slots: {1}", key,
                                                 context.slotInsertionPoints == null
                                ? String.Empty
                                : String.Join(", ",
                                              System.Linq.Enumerable.ToArray(context.slotInsertionPoints.Keys)));
                            ve.Add(childVe);
                        }
                        else
                        {
                            parentSlot.Add(childVe);
                        }
                    }
                    else
                    {
                        ve.Add(childVe);
                    }
                }
            }

            if (templateAsset != null && context.slotInsertionPoints != null)
            {
                context.slotInsertionPoints.Clear();
            }

            return(ve);
        }
Example #4
0
        internal void AddToListAndToVisualTree(UIDocument uiDocument, VisualElement visualTree, int firstInsertIndex = 0)
        {
            int index = 0;

            foreach (var sibling in m_AttachedUIDocuments)
            {
                if (uiDocument.sortingOrder > sibling.sortingOrder)
                {
                    index++;
                    continue;
                }

                if (uiDocument.sortingOrder < sibling.sortingOrder)
                {
                    break;
                }

                // They're the same value, compare their count (UIDocuments created first show up first).
                if (uiDocument.m_UIDocumentCreationIndex > sibling.m_UIDocumentCreationIndex)
                {
                    index++;
                    continue;
                }

                break;
            }

            if (index < m_AttachedUIDocuments.Count)
            {
                m_AttachedUIDocuments.Insert(index, uiDocument);

                if (visualTree == null || uiDocument.rootVisualElement == null)
                {
                    return;
                }

                // Not every UIDocument is in the tree already (because their root is null, for example), so we need
                // to figure out the insertion point.
                if (index > 0)
                {
                    VisualElement previousInTree = null;
                    int           i = 1;
                    while (previousInTree == null && index - i >= 0)
                    {
                        var previousUIDocument = m_AttachedUIDocuments[index - i++];
                        previousInTree = previousUIDocument.rootVisualElement;
                    }

                    if (previousInTree != null)
                    {
                        index = visualTree.IndexOf(previousInTree) + 1;
                    }
                }

                if (index > visualTree.childCount)
                {
                    index = visualTree.childCount;
                }
            }
            else
            {
                // Add in the end.
                m_AttachedUIDocuments.Add(uiDocument);
            }

            if (visualTree == null || uiDocument.rootVisualElement == null)
            {
                return;
            }

            int insertionIndex = firstInsertIndex + index;

            if (insertionIndex < visualTree.childCount)
            {
                visualTree.Insert(insertionIndex, uiDocument.rootVisualElement);
            }
            else
            {
                visualTree.Add(uiDocument.rootVisualElement);
            }
        }
        public static void BuildInspectorPropertiesElement(string elementPath, IEditorContainer editor, System.Collections.Generic.HashSet <System.Type> usedComponents, SerializedProperty obj, UnityEngine.UIElements.VisualElement container, bool noFields, System.Action <int, PropertyField> onBuild = null)
        {
            obj = obj.Copy();
            container.Clear();
            var source = obj.Copy();
            SerializedProperty iterator = obj;

            if (iterator.NextVisible(true) == false)
            {
                return;
            }
            if (iterator.NextVisible(true) == false)
            {
                return;
            }
            var depth        = iterator.depth;
            var i            = 0;
            var iteratorNext = iterator.Copy();

            do
            {
                if (string.IsNullOrEmpty(elementPath) == false)
                {
                    iterator = iteratorNext.FindPropertyRelative(elementPath);
                }
                else
                {
                    iterator = iteratorNext;
                }
                if (iterator.propertyType != SerializedPropertyType.ManagedReference)
                {
                    continue;
                }

                var element = new VisualElement();
                element.AddToClassList("element");

                var itCopy = iterator.Copy();
                GetTypeFromManagedReferenceFullTypeName(iterator.managedReferenceFullTypename, out var type);
                element.AddToClassList(i % 2 == 0 ? "even" : "odd");
                element.RegisterCallback <UnityEngine.UIElements.ContextClickEvent, int>((evt, idx) => {
                    var menu = new GenericMenu();
                    if (usedComponents != null)
                    {
                        menu.AddItem(new GUIContent("Delete"), false, () => {
                            RemoveComponent((DataConfigEditor)editor, usedComponents, source, type, noFields);
                            editor.Save();
                            BuildInspectorProperties(editor, usedComponents, source, container, noFields);
                        });

                        menu.AddItem(new GUIContent("Copy JSON"), false, () => {
                            var instance = itCopy.GetValue();
                            var json     = JsonUtility.ToJson(instance, true);
                            EditorGUIUtility.systemCopyBuffer = json;
                        });
                    }

                    editor.OnComponentMenu(menu, idx);
                    menu.ShowAsContext();
                }, i);

                if (type != null && usedComponents?.Contains(type) == false)
                {
                    usedComponents?.Add(type);
                }
                if (type == null)
                {
                    var label = new UnityEngine.UIElements.Label("MISSING: " + iterator.managedReferenceFullTypename);
                    element.name = "missing";
                    label.AddToClassList("inner-element");
                    label.AddToClassList("missing-label");
                    element.Add(label);
                }
                else if (iterator.hasVisibleChildren == false || noFields == true)
                {
                    var horizontal = new UnityEngine.UIElements.VisualElement();
                    horizontal.AddToClassList("inner-element");
                    horizontal.AddToClassList("no-fields-container");
                    element.name = type.Name;

                    var toggle = new UnityEngine.UIElements.Toggle();
                    toggle.AddToClassList("no-fields-toggle");
                    toggle.SetEnabled(false);
                    toggle.SetValueWithoutNotify(true);
                    horizontal.Add(toggle);

                    var label = new UnityEngine.UIElements.Label(GUILayoutExt.GetStringCamelCaseSpace(type.Name));
                    label.AddToClassList("no-fields-label");
                    horizontal.Add(label);

                    element.Add(horizontal);
                }
                else
                {
                    var label = GUILayoutExt.GetStringCamelCaseSpace(type.Name);
                    if (iterator.hasVisibleChildren == true)
                    {
                        var childs = iterator.Copy();
                        //var height = EditorUtilities.GetPropertyHeight(childs, true, new GUIContent(label));
                        var cnt = EditorUtilities.GetPropertyChildCount(childs);
                        if (cnt == 1 /*&& height <= 22f*/)
                        {
                            iterator.NextVisible(true);
                        }
                    }

                    var propertyField = new PropertyField(iterator.Copy(), label);
                    propertyField.BindProperty(iterator);
                    onBuild?.Invoke(i, propertyField);
                    propertyField.AddToClassList("property-field");
                    propertyField.AddToClassList("inner-element");
                    element.name = type.Name;
                    element.Add(propertyField);
                }

                if (type != null)
                {
                    var helps = type.GetCustomAttributes(typeof(ComponentHelpAttribute), false);
                    if (helps.Length > 0)
                    {
                        var label = new UnityEngine.UIElements.Label(((ComponentHelpAttribute)helps[0]).comment);
                        label.AddToClassList("comment");
                        element.Add(label);
                    }

                    if (typeof(IComponentStatic).IsAssignableFrom(type) == true)
                    {
                        var label = new UnityEngine.UIElements.Label("Static");
                        label.AddToClassList("static-component");
                        element.AddToClassList("has-static-component");
                        element.Add(label);
                    }

                    if (typeof(IComponentShared).IsAssignableFrom(type) == true)
                    {
                        var label = new UnityEngine.UIElements.Label("Shared");
                        label.AddToClassList("shared-component");
                        element.AddToClassList("has-shared-component");
                        element.Add(label);
                    }
                }

                container.Add(element);
                ++i;
            } while (iteratorNext.NextVisible(false) == true && depth <= iteratorNext.depth);
        }
Example #6
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="minValue">The minimum value in the range to be represented.</param>
        /// <param name="maxValue">The maximum value in the range to be represented.</param>
        /// <param name="minLimit">The minimum value of the slider limit.</param>
        /// <param name="maxLimit">The maximum value of the slider limit.</param>
        public MinMaxSlider(string label, float minValue = 0, float maxValue = kDefaultHighValue, float minLimit = float.MinValue, float maxLimit = float.MaxValue)
            : base(label, null)
        {
            m_MinLimit = float.MinValue;
            m_MaxLimit = float.MaxValue;

            lowLimit  = minLimit;
            highLimit = maxLimit;

            // Can't set to value here, because it could be overriden in a derived type.
            var clampedValue = ClampValues(new Vector2(minValue, maxValue));

            this.minValue = clampedValue.x;
            this.maxValue = clampedValue.y;
            AddToClassList(ussClassName);
            labelElement.AddToClassList(labelUssClassName);
            visualInput.AddToClassList(inputUssClassName);

            pickingMode = PickingMode.Ignore;

            m_DragState = DragState.NoThumb;

            visualInput.pickingMode = PickingMode.Position;
            var trackElement = new VisualElement()
            {
                name = "unity-tracker"
            };

            trackElement.AddToClassList(trackerUssClassName);
            visualInput.Add(trackElement);

            dragElement = new VisualElement()
            {
                name = "unity-dragger"
            };
            dragElement.AddToClassList(draggerUssClassName);
            dragElement.RegisterCallback <GeometryChangedEvent>(UpdateDragElementPosition);
            visualInput.Add(dragElement);

            // For a better handling of the cursor style, children elements are created so that the style is automatic with the uss.
            dragMinThumb = new VisualElement()
            {
                name = "unity-thumb-min"
            };
            dragMaxThumb = new VisualElement()
            {
                name = "unity-thumb-max"
            };
            dragMinThumb.AddToClassList(minThumbUssClassName);
            dragMaxThumb.AddToClassList(maxThumbUssClassName);
            dragElement.Add(dragMinThumb);
            dragElement.Add(dragMaxThumb);

            clampedDragger = new ClampedDragger <float>(null, SetSliderValueFromClick, SetSliderValueFromDrag);
            visualInput.AddManipulator(clampedDragger);

            m_MinLimit = minLimit;
            m_MaxLimit = maxLimit;
            rawValue   = ClampValues(new Vector2(minValue, maxValue));
            UpdateDragElementPosition();
        }