Example #1
0
        public BuilderLibraryTreeView(IList <ITreeViewItem> items)
        {
            m_TreeViewItemTemplate = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(Path.Combine(BuilderConstants.LibraryUIPath, "BuilderLibraryTreeViewItem.uxml"));

            style.flexGrow = 1;
            m_TreeView     = new TreeView {
                name = k_TreeViewName
            };
            m_TreeView.AddToClassList(k_TreeViewClassName);
            Add(m_TreeView);

            m_TreeView.viewDataKey = "samples-tree";
            m_TreeView.itemHeight  = 20;
            m_TreeView.rootItems   = items;
            m_TreeView.makeItem    = MakeItem;
            m_TreeView.bindItem    = BindItem;
#if UNITY_2020_1_OR_NEWER
            m_TreeView.onItemsChosen += OnItemsChosen;
#else
            m_TreeView.onItemChosen += (s) => OnItemChosen(s);
#endif
            m_TreeView.Refresh();

            foreach (var item in m_TreeView.rootItems)
            {
                m_TreeView.ExpandItem(item.id);
            }
        }
        public BuilderLibraryTreeView(IList <TreeViewItem> items)
        {
            m_TreeViewItemTemplate = BuilderPackageUtilities.LoadAssetAtPath <VisualTreeAsset>(BuilderConstants.LibraryUIPath + "/BuilderLibraryTreeViewItem.uxml");

            style.flexGrow = 1;
            m_TreeView     = new TreeView {
                name = k_TreeViewName
            };
            m_TreeView.AddToClassList(k_TreeViewClassName);
            Add(m_TreeView);

            m_TreeView.viewDataKey     = "samples-tree";
            m_TreeView.fixedItemHeight = 20;
            m_TreeView.SetRootItems(items);
            m_TreeView.makeItem       = MakeItem;
            m_TreeView.bindItem       = BindItem;
            m_TreeView.onItemsChosen += OnItemsChosen;
            m_TreeView.Rebuild();

            m_TreeView.ExpandRootItems();
        }
        public void OnEnable()
        {
            var root = rootVisualElement;

            var styleSheet = EditorGUIUtility.Load(s_StyleSheetPath) as StyleSheet;

            root.styleSheets.Add(styleSheet);

            var themedStyleSheet = EditorGUIUtility.isProSkin
                ? EditorGUIUtility.Load(s_DarkStyleSheetPath) as StyleSheet
                : EditorGUIUtility.Load(s_LightStyleSheetPath) as StyleSheet;

            root.styleSheets.Add(themedStyleSheet);

            SampleTreeItem.ResetNextId();
            var items = new List <ITreeViewItem>()
            {
                new SampleTreeItem("Styles", StylesExplorer.Create),
                new SampleTreeItem("Button", ButtonSnippet.Create),
                new SampleTreeItem("Scroller", ScrollerSnippet.Create),
                new SampleTreeItem("Toggle", ToggleSnippet.Create),
                new SampleTreeItem("Label", LabelSnippet.Create),
                new SampleTreeItem("Text Field", TextFieldSnippet.Create),
                new SampleTreeItem("HelpBox", HelpBoxSnippet.Create),
                new SampleTreeItem("Object Field", ObjectFieldSnippet.Create),
                new SampleTreeItem("List View", ListViewSnippet.Create),
                new SampleTreeItem("Numeric Fields", MakeNumericFieldsPanel, new List <TreeViewItem <string> >()
                {
                    new SampleTreeItem("Integer", IntegerFieldSnippet.Create),
                    new SampleTreeItem("Float", FloatFieldSnippet.Create),
                    new SampleTreeItem("Long", LongFieldSnippet.Create),
                    new SampleTreeItem("MinMaxSlider", MinMaxSliderSnippet.Create),
                    new SampleTreeItem("Slider", SliderSnippet.Create),
                    new SampleTreeItem("Vector2", Vector2FieldSnippet.Create),
                    new SampleTreeItem("Vector3", Vector3FieldSnippet.Create),
                    new SampleTreeItem("Vector4", Vector4FieldSnippet.Create),
                    new SampleTreeItem("Rect", RectFieldSnippet.Create),
                    new SampleTreeItem("Bounds", BoundsFieldSnippet.Create),
                    new SampleTreeItem("SliderInt", SliderIntSnippet.Create),
                    new SampleTreeItem("Vector2Int", Vector2IntFieldSnippet.Create),
                    new SampleTreeItem("Vector3Int", Vector3IntFieldSnippet.Create),
                    new SampleTreeItem("RectInt", RectIntFieldSnippet.Create),
                    new SampleTreeItem("BoundsInt", BoundsIntFieldSnippet.Create)
                }),
                new SampleTreeItem("Value Fields", MakeValueFieldsPanel, new List <TreeViewItem <string> >()
                {
                    new SampleTreeItem("Color", ColorFieldSnippet.Create),
                    new SampleTreeItem("Curve", CurveFieldSnippet.Create),
                    new SampleTreeItem("Gradient", GradientFieldSnippet.Create)
                }),
                new SampleTreeItem("Choice Fields", MakeChoiceFieldsPanel, new List <TreeViewItem <string> >()
                {
                    new SampleTreeItem("Enum", EnumFieldSnippet.Create),
                    new SampleTreeItem("EnumFlags", EnumFlagsFieldSnippet.Create),
                    new SampleTreeItem("Popup", PopupFieldSnippet.Create),
                    new SampleTreeItem("Tag", TagFieldSnippet.Create),
                    new SampleTreeItem("Mask", MaskFieldSnippet.Create),
                    new SampleTreeItem("Layer", LayerFieldSnippet.Create),
                    new SampleTreeItem("LayerMask", LayerMaskFieldSnippet.Create)
                }),
            };

            Func <VisualElement> makeItem = () =>
            {
                var box = new VisualElement();
                box.AddToClassList(k_TreeItemClassName);

                var label = new Label();
                label.AddToClassList(k_TreeItemLabelClassName);

                box.Add(label);
                return(box);
            };

            Action <VisualElement, ITreeViewItem> bindItem = (element, item) =>
            {
                (element.ElementAt(0) as Label).text = (item as SampleTreeItem).data;
                element.userData = item;
            };

            Action <IEnumerable <ITreeViewItem> > onSelectionChanged = selectedItems =>
            {
                var item = (SampleTreeItem)selectedItems.FirstOrDefault();
                if (item == null)
                {
                    return;
                }

                m_ContentPanel.Clear();
                m_ContentPanel.Add(item.makeItem(item));
            };

            var treeView = new TreeView()
            {
                name = k_TreeViewName
            };

            treeView.AddToClassList(k_TreeViewClassName);
            m_ContentPanel = new VisualElement()
            {
                name = k_ContentPanelName
            };
            m_ContentPanel.AddToClassList(k_ContentPanelClassName);

            var splitter = new DebuggerSplitter();

            splitter.AddToClassList(k_SplitterClassName);
            splitter.leftPane.style.width = k_SplitterLeftPaneStartingWidth;
            root.Add(splitter);

            splitter.leftPane.Add(treeView);
            splitter.rightPane.Add(m_ContentPanel);

            treeView.viewDataKey        = "samples-tree";
            treeView.itemHeight         = 20;
            treeView.rootItems          = items;
            treeView.makeItem           = makeItem;
            treeView.bindItem           = bindItem;
            treeView.onSelectionChange += onSelectionChanged;
            treeView.Refresh();

            // Force TreeView to call onSelectionChanged when it restores its own selection from view data.
            treeView.schedule.Execute(() =>
            {
                onSelectionChanged(treeView.selectedItems);
            }).StartingIn(k_TreeViewSelectionRestoreDelay);

            // Force TreeView to select something if nothing is selected.
            treeView.schedule.Execute(() =>
            {
                if (treeView.selectedItems.Count() > 0)
                {
                    return;
                }

                treeView.SetSelection(0);

                // Auto-expand all items on load.
                foreach (var item in treeView.rootItems)
                {
                    treeView.ExpandItem(item.id);
                }
            }).StartingIn(k_TreeViewInitialSelectionDelay);
        }
Example #4
0
        void RefreshTreeView()
        {
            Clear();
            LibraryTreeItem.ResetNextId();
            var items = new List <ITreeViewItem>();

            var unityItem = new LibraryTreeItem("Unity", () => null);

            unityItem.isHeader = true;
            IList <ITreeViewItem> unityItemList = new List <ITreeViewItem>()
            {
                new LibraryTreeItem("VisualElement",
                                    () =>
                {
                    var ve              = new VisualElement();
                    var veMinSizeChild  = new VisualElement();
                    veMinSizeChild.name = BuilderConstants.SpecialVisualElementInitialMinSizeName;
                    veMinSizeChild.AddToClassList(BuilderConstants.SpecialVisualElementInitialMinSizeClassName);
                    ve.Add(veMinSizeChild);
                    return(ve);
                },
                                    (inVta, inParent) =>
                {
                    var vea = new VisualElementAsset(typeof(VisualElement).ToString());
                    VisualTreeAssetUtilities.InitializeElement(vea);
                    inVta.AddElement(inParent, vea);
                    return(vea);
                }),
                new LibraryTreeItem("Button", () => new Button()
                {
                    text = "Button"
                }),
                new LibraryTreeItem("Scroller", () => new Scroller(0, 100, (v) => { }, SliderDirection.Horizontal)
                {
                    value = 42
                }),
                new LibraryTreeItem("Toggle", () => new Toggle("Toggle")),
                new LibraryTreeItem("Label", () => new Label("Label")),
                new LibraryTreeItem("Text Field", () => new TextField("Text Field")
                {
                    value = "filler text"
                }),
                new LibraryTreeItem("Object Field", () => new ObjectField("Object Field")
                {
                    value = new Texture2D(10, 10)
                    {
                        name = "new_texture"
                    }
                }),
                new LibraryTreeItem("Foldout", () => new Foldout()
                {
                    text = "Foldout"
                }),
                new LibraryTreeItem("Numeric Fields", () => null, null, new List <TreeViewItem <string> >()
                {
                    new LibraryTreeItem("Integer", () => new IntegerField("Int Field")
                    {
                        value = 42
                    }),
                    new LibraryTreeItem("Float", () => new FloatField("Float Field")
                    {
                        value = 42.2f
                    }),
                    new LibraryTreeItem("Long", () => new LongField("Long Field")
                    {
                        value = 42
                    }),
                    new LibraryTreeItem("Min-Max Slider", () => new MinMaxSlider("Min/Max Slider", 0, 20, -10, 40)
                    {
                        value = new Vector2(10, 12)
                    }),
                    new LibraryTreeItem("Slider", () => new Slider("Slider", 0, 100)
                    {
                        value = 42
                    }),
                    new LibraryTreeItem("Progress Bar", () => new ProgressBar()
                    {
                        title = "my-progress", value = 22
                    }),
                    new LibraryTreeItem("Vector2", () => new Vector2Field("Vec2 Field")),
                    new LibraryTreeItem("Vector3", () => new Vector3Field("Vec3 Field")),
                    new LibraryTreeItem("Vector4", () => new Vector4Field("Vec4 Field")),
                    new LibraryTreeItem("Rect", () => new RectField("Rect")),
                    new LibraryTreeItem("Bounds", () => new BoundsField("Bounds")),
                    new LibraryTreeItem("Slider (Int)", () => new SliderInt("SliderInt", 0, 100)
                    {
                        value = 42
                    }),
                    new LibraryTreeItem("Vector2 (Int)", () => new Vector2IntField("Vector2Int")),
                    new LibraryTreeItem("Vector3 (Int)", () => new Vector3IntField("Vector3Int")),
                    new LibraryTreeItem("Rect (Int)", () => new RectIntField("RectInt")),
                    new LibraryTreeItem("Bounds (Int)", () => new BoundsIntField("BoundsInt"))
                }),
                new LibraryTreeItem("Value Fields", () => null, null, new List <TreeViewItem <string> >()
                {
                    new LibraryTreeItem("Color", () => new ColorField("Color")
                    {
                        value = Color.cyan
                    }),
                    new LibraryTreeItem("Curve", () => new CurveField("Curve")
                    {
                        value = new AnimationCurve(new Keyframe[] { new Keyframe(0, 0), new Keyframe(5, 8), new Keyframe(10, 4) })
                    }),
                    new LibraryTreeItem("Gradient", () => new GradientField("Gradient")
                    {
                        value = new Gradient()
                        {
                            colorKeys = new GradientColorKey[]
                            {
                                new GradientColorKey(Color.red, 0),
                                new GradientColorKey(Color.blue, 10),
                                new GradientColorKey(Color.green, 20)
                            }
                        }
                    })
                }),
                new LibraryTreeItem("Choice Fields", () => null, null, new List <TreeViewItem <string> >()
                {
                    new LibraryTreeItem("Enum", () => new EnumField("Enum", TextAlignment.Center)),

                    // No UXML support for PopupField.
                    //new LibraryTreeItem("Popup", () => new PopupField<string>("Normal Field", choices, 0)),

                    new LibraryTreeItem("Tag", () => new TagField("Tag", "Player")),
                    new LibraryTreeItem("Mask", () => new MaskField("Mask")),
                    new LibraryTreeItem("Layer", () => new LayerField("Layer")),
                    new LibraryTreeItem("LayerMask", () => new LayerMaskField("LayerMask"))
                }),
                new LibraryTreeItem("Containers", () => null, null, new List <TreeViewItem <string> >()
                {
                    new LibraryTreeItem("ScrollView", () => new ScrollView()),
                    new LibraryTreeItem("ListView", () => new ListView()),
                    new LibraryTreeItem("IMGUIContainer", () => new IMGUIContainer()),
                }),
                new LibraryTreeItem("Toolbar", () => null, null, new List <TreeViewItem <string> >()
                {
                    new LibraryTreeItem("Toolbar", () => new Toolbar()),
                    new LibraryTreeItem("Toolbar Menu", () => new ToolbarMenu()),
                    new LibraryTreeItem("Toolbar Button", () => new ToolbarButton {
                        text = "Button"
                    }),
                    new LibraryTreeItem("Toolbar Spacer", () => new ToolbarSpacer()),
                    new LibraryTreeItem("Toolbar Toggle", () => new ToolbarToggle {
                        label = "Toggle"
                    }),
#if UNITY_2019_3_OR_NEWER
                    new LibraryTreeItem("Toolbar Breadcrumbs", () => new ToolbarBreadcrumbs()),
#endif
                    new LibraryTreeItem("Toolbar Search Field", () => new ToolbarSearchField()),
                    new LibraryTreeItem("Toolbar Popup Search Field", () => new ToolbarPopupSearchField()),
                }),
                new LibraryTreeItem("Inspectors", () => null, null, new List <TreeViewItem <string> >()
                {
                    new LibraryTreeItem("PropertyField", () => new PropertyField())
                }),
            };

            unityItem.AddChildren(unityItemList);
            items.Add(unityItem);

            // From Project
            var fromProjectCategory = new LibraryTreeItem("Project", () => null);

            fromProjectCategory.isHeader = true;
            items.Add(fromProjectCategory);
            ImportUxmlFromProject(fromProjectCategory);
            ImportFactoriesFromSource(fromProjectCategory);

            var treeView = new TreeView()
            {
                name = k_TreeViewName
            };

            treeView.AddToClassList(k_TreeViewClassName);
            Add(treeView);

            treeView.viewDataKey = "samples-tree";
            treeView.itemHeight  = 20;
            treeView.rootItems   = items;
            treeView.makeItem    = () => MakeItem(); // This is apparently more optimal than "= MakeItem;".
            treeView.bindItem    = (e, i) => BindItem(e, i);
#if UNITY_2020_1_OR_NEWER
            treeView.onItemsChosen += (s) => OnItemsChosen(s);
#else
            treeView.onItemChosen += (s) => OnItemChosen(s);
#endif
            treeView.Refresh();

            // Make sure the Hierarchy View gets focus when the pane gets focused.
            primaryFocusable = treeView.Q <ListView>();

            // Auto-expand all items on load.
            foreach (var item in treeView.rootItems)
            {
                treeView.ExpandItem(item.id);
            }
        }