Exemplo n.º 1
0
        private void OnEnable()
        {
            Load();
            ExplorerManager.explorerData = explorerData;
            explorer = new ExplorerManager();
            explorer.onSavePerformed += () => {
                if (explorer.graphTree != null)
                {
                    explorer.graphTree.Query <ObjectTreeView>().ForEach((tree) => {
                        var index = explorerData.objects.FindIndex(data => data.guid == tree.data.guid);
                        if (index >= 0)
                        {
                            explorerData.objects[index] = tree.data;
                        }
                    });
                }
                if (explorer.macroTree != null)
                {
                    explorer.graphTree.Query <GraphTreeView>().ForEach((tree) => {
                        var index = explorerData.graphs.FindIndex(data => data.guid == tree.data.guid);
                        if (index >= 0)
                        {
                            explorerData.graphs[index] = tree.data;
                        }
                    });
                }
            };
            explorer.Refresh();

            ScrollView scroll = new ScrollView(ScrollViewMode.VerticalAndHorizontal)
            {
                name = "scroll-view",
            };

            scroll.styleSheets.Add(Resources.Load <StyleSheet>("ExplorerStyles/ExplorerView"));
            scroll.Add(explorer);

            var toolbar = new Toolbar();
            {
                //toolbar.Add(new ToolbarButton(() => {

                //}) { text = "New Project" });

                toolbar.Add(new ToolbarButton(() => {
                    explorer.Refresh();
                })
                {
                    text = "Refresh"
                });
                var searchField = new ToolbarPopupSearchField();
                searchField.style.flexGrow = 1;
#if UNITY_2019_3_OR_NEWER
                searchField.style.width = new StyleLength(StyleKeyword.Auto);
#else
                searchField.Children().First().style.flexGrow = 1;
#endif
                searchField.Q <TextField>().style.width = new StyleLength(StyleKeyword.Auto);
                searchField.RegisterCallback <KeyDownEvent>(evt => {
                    if (evt.keyCode == KeyCode.Return)
                    {
                        explorer.Search(searchField.value.ToLower());
                    }
                }, TrickleDown.TrickleDown);
                searchField.RegisterValueChangedCallback((evt) => {
                    if (string.IsNullOrEmpty(evt.newValue))
                    {
                        explorer.Search(evt.newValue);
                    }
                });
                searchField.menu.AppendAction("Contains", (menu) => {
                    explorerData.searchKind = SearchKind.Contains;
                }, (act) => explorerData.searchKind == SearchKind.Contains ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                searchField.menu.AppendAction("Equals", (menu) => {
                    explorerData.searchKind = SearchKind.Equals;
                }, (act) => explorerData.searchKind == SearchKind.Equals ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                searchField.menu.AppendAction("Ends with", (menu) => {
                    explorerData.searchKind = SearchKind.Endswith;
                }, (act) => explorerData.searchKind == SearchKind.Endswith ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                searchField.menu.AppendAction("Start with", (menu) => {
                    explorerData.searchKind = SearchKind.Startwith;
                }, (act) => explorerData.searchKind == SearchKind.Startwith ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                toolbar.Add(searchField);
                //toolbar.Add(new ToolbarSpacer() {
                //	flex = true,
                //});
                var toolbarMenu = new ToolbarMenu()
                {
                    text = "{}",
                };
                toolbarMenu.style.paddingLeft  = 0;
                toolbarMenu.style.paddingRight = 2;
                toolbarMenu.menu.AppendAction("Show or Hide", (menu) => { }, (act) => DropdownMenuAction.AlwaysDisabled(act));
                toolbarMenu.menu.AppendSeparator("");
                toolbarMenu.menu.AppendAction("Summary", (menu) => {
                    explorerData.showSummary = !explorerData.showSummary;
                    explorer.Refresh();
                }, (act) => explorerData.showSummary ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                toolbarMenu.menu.AppendAction("Type Icon", (menu) => {
                    explorerData.showTypeIcon = !explorerData.showTypeIcon;
                    explorer.Refresh();
                }, (act) => explorerData.showTypeIcon ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                toolbarMenu.menu.AppendSeparator("");
                toolbarMenu.menu.AppendAction("Variables", (menu) => {
                    explorerData.showVariable = !explorerData.showVariable;
                    explorer.Refresh();
                }, (act) => explorerData.showVariable ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                toolbarMenu.menu.AppendAction("Properties", (menu) => {
                    explorerData.showProperty = !explorerData.showProperty;
                    explorer.Refresh();
                }, (act) => explorerData.showProperty ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                toolbarMenu.menu.AppendAction("Functions", (menu) => {
                    explorerData.showFunction = !explorerData.showFunction;
                    explorer.Refresh();
                }, (act) => explorerData.showFunction ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                toolbarMenu.menu.AppendAction("Enums", (menu) => {
                    explorerData.showEnum = !explorerData.showEnum;
                    explorer.Refresh();
                }, (act) => explorerData.showEnum ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                toolbarMenu.menu.AppendAction("Interfaces", (menu) => {
                    explorerData.showInterface = !explorerData.showInterface;
                    explorer.Refresh();
                }, (act) => explorerData.showInterface ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                toolbarMenu.menu.AppendSeparator("");
                toolbarMenu.menu.AppendAction("Graphs", (menu) => {
                    explorerData.showGraph = !explorerData.showGraph;
                    explorer.Refresh();
                }, (act) => explorerData.showGraph ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                toolbarMenu.menu.AppendAction("Macros", (menu) => {
                    explorerData.showMacro = !explorerData.showMacro;
                    explorer.Refresh();
                }, (act) => explorerData.showMacro ? DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
                toolbar.Add(toolbarMenu);
                UIElementUtility.ForceDarkToolbarStyleSheet(toolbar);
            }
            rootVisualElement.Add(toolbar);
            rootVisualElement.Add(scroll);
            scroll.style.marginTop = 19;
            scroll.StretchToParentSize();
        }
Exemplo n.º 2
0
        public void UpdateControl()
        {
            if (control != null)
            {
                control.RemoveFromHierarchy();
                control      = null;
                showInstance = false;
            }
            Type targetType = member.type;

            if (member.isTargeted)
            {
                tooltip = member.Tooltip;
            }
            if (member.targetType == MemberData.TargetType.Values && targetType != null)
            {
                Type portType = targetType;
                // var mVal = member.Get();
                // if(mVal != null && !mVal.GetType().IsCastableTo(portType)) {
                //  portType = mVal.GetType();
                // }
                var           controlAtts = UIElementUtility.FindControlsField();
                ControlConfig config      = new ControlConfig()
                {
                    owner          = this.config.owner,
                    value          = member.Get(),
                    filter         = this.config.filter,
                    type           = portType,
                    onValueChanged = (val) => {
                        this.config.OnValueChanged(MemberData.CreateFromValue(val, member.type));
                        MarkDirtyRepaint();
                    },
                };
                if (config.value == null && config.type.IsValueType)
                {
                    //Ensure to initialize value for value type since value type cannot be null
                    config.value = ReflectionUtils.CreateInstance(config.type);
                    member.CopyFrom(MemberData.CreateFromValue(config.value));
                }
                control = UIElementUtility.CreateControl(portType, config, ClassListContains("Layout"));
                if (control is ObjectControl && config.value as uNodeRoot == config.owner.targetNode.owner)
                {
                    control = new DefaultControl(config, ClassListContains("Layout"));
                }
                if (control is StringControl && ClassListContains("multiline"))
                {
                    (control as StringControl).field.multiline = true;
                }
                if (popupElement != null && popupElement.visible)
                {
                    popupElement.visible = false;
                    popupElement.RemoveFromHierarchy();
                }
            }
            else if (!member.IsTargetingUNode && !member.isStatic && !hideInstance && member.startType != null)
            {
                if (config.filter == null)
                {
                    config.filter = new FilterAttribute();
                }
                FilterAttribute filter = config.filter;
                if (filter.UnityReference && !filter.OnlyGetType && !member.targetType.HasFlags(
                        MemberData.TargetType.Values |
                        MemberData.TargetType.Type |
                        MemberData.TargetType.Null |
                        MemberData.TargetType.uNodeGenericParameter) && member.instance as UnityEngine.Object != config.owner.targetNode.owner)
                {
                    Type portType = member.startType;
                    if (member.instance is MemberData)
                    {
                        portType = typeof(MemberData);
                    }
                    var           controlAtts = UIElementUtility.FindControlsField();
                    ControlConfig config      = new ControlConfig()
                    {
                        owner          = this.config.owner,
                        value          = member.instance,
                        filter         = new FilterAttribute(member.startType),
                        type           = member.startType,
                        onValueChanged = (val) => {
                            bool flag3 = false;
                            if (member.instance != null && !member.instance.GetType().IsCastableTo(portType))
                            {
                                flag3 = true;
                            }
                            member.instance = val;
                            this.config.OnValueChanged(member);
                            MarkDirtyRepaint();
                            if (flag3)
                            {
                                UpdateControl();
                            }
                        },
                    };
                    control = UIElementUtility.CreateControl(portType, config, ClassListContains("Layout"));
                    if (control is ObjectControl)
                    {
                        var objectControl = control as ObjectControl;
                        if (config.value != null && !config.value.GetType().IsCastableTo(portType))
                        {
                            objectControl.style.backgroundColor = Color.red;
                        }
                        objectControl.AddManipulator(new ContextualMenuManipulator(evt => {
                            object instance = member.instance;
                            if (instance != null)
                            {
                                if (instance as GameObject)
                                {
                                    var go            = instance as GameObject;
                                    Component[] comps = go.GetComponents <Component>();
                                    evt.menu.AppendAction("0-" + typeof(GameObject).Name, (act) => {
                                        objectControl.config.OnValueChanged(go);
                                        UpdateControl();
                                    }, DropdownMenuAction.AlwaysEnabled);
                                    for (int x = 0; x < comps.Length; x++)
                                    {
                                        Component com = comps[x];
                                        evt.menu.AppendAction((x + 1) + "-" + com.GetType().Name, (act) => {
                                            objectControl.config.OnValueChanged(com);
                                            UpdateControl();
                                        }, DropdownMenuAction.AlwaysEnabled);
                                    }
                                }
                                else if (instance as Component)
                                {
                                    var component     = instance as Component;
                                    Component[] comps = component.GetComponents <Component>();
                                    evt.menu.AppendAction("0-" + typeof(GameObject).Name, (act) => {
                                        objectControl.config.OnValueChanged(new MemberData(component.gameObject));
                                        UpdateControl();
                                    }, DropdownMenuAction.AlwaysEnabled);
                                    for (int x = 0; x < comps.Length; x++)
                                    {
                                        Component com = comps[x];
                                        evt.menu.AppendAction((x + 1) + "-" + com.GetType().Name, (act) => {
                                            objectControl.config.OnValueChanged(com);
                                            UpdateControl();
                                        }, DropdownMenuAction.AlwaysEnabled);
                                    }
                                }
                            }
                            evt.menu.AppendSeparator("");
                            if (config.owner.targetNode != null)
                            {
                                uNodeRoot UNR = config.owner.targetNode.owner;
                                if (UNR != null)
                                {
                                    GameObject go     = UNR.gameObject;
                                    Component[] comps = go.GetComponents <Component>();
                                    evt.menu.AppendAction("this uNode/0-" + typeof(GameObject).Name, (act) => {
                                        objectControl.config.OnValueChanged(go);
                                        UpdateControl();
                                    }, DropdownMenuAction.AlwaysEnabled);
                                    for (int x = 0; x < comps.Length; x++)
                                    {
                                        Component com = comps[x];
                                        evt.menu.AppendAction("this uNode/" + x + "-" + com.GetType().Name, (act) => {
                                            objectControl.config.OnValueChanged(com);
                                            UpdateControl();
                                        }, DropdownMenuAction.AlwaysEnabled);
                                    }
                                }
                            }
                            evt.menu.AppendAction("Reset", (act) => {
                                objectControl.config.OnValueChanged(null);
                                UpdateControl();
                            }, DropdownMenuAction.AlwaysEnabled);
                        }));
                    }
                    showInstance = true;
                }
            }
            if (popupElement != null)
            {
                if (!popupElement.visible && (control == null || showInstance || config.filter == null || config.filter.ValidTargetType != MemberData.TargetType.Values))
                {
                    UpdatePopupLabel();
                    popupElement.visible = true;
                    Add(popupElement);
                }
                popupElement.MarkDirtyRepaint();
            }
            if (control != null)
            {
                control.RemoveFromHierarchy();
                Insert(0, control);
            }
        }
Exemplo n.º 3
0
        void InitializeView()
        {
            if (data.block == null)
            {
                return;
            }
            if (data.block is Events.HLAction || data.block is Events.HLCondition)
            {
                InitializeHLBlock(data.block);
                return;
            }
            var fields = EditorReflectionUtility.GetFields(data.block.GetType());

            if (fields != null && fields.Length > 0)
            {
                for (int idx = 0; idx < fields.Length; idx++)
                {
                    FieldInfo field = fields[idx];
                    if (uNodeGUIUtility.IsHide(field, data.block))
                    {
                        continue;
                    }
                    Type type = field.FieldType;
                    if (type == typeof(MemberData))
                    {
                        MemberData member         = field.GetValueOptimized(data.block) as MemberData;
                        object[]   fieldAttribute = field.GetCustomAttributes(true);
                        if (!ReflectionUtils.TryCorrectingAttribute(data.block, ref fieldAttribute))
                        {
                            continue;
                        }
                        var filter = ReflectionUtils.GetAttribute <FilterAttribute>(fieldAttribute);
                        if (member == null)
                        {
                            if (filter != null && !filter.SetMember && !filter.OnlyGetType && ReflectionUtils.CanCreateInstance(filter.GetActualType()))
                            {
                                member = new MemberData(ReflectionUtils.CreateInstance(filter.GetActualType()));
                            }
                            else if (filter != null && !filter.SetMember && !filter.OnlyGetType)
                            {
                                member = MemberData.none;
                            }
                            field.SetValueOptimized(data.block, member);
                            Repaint();
                        }
                        bool hasDependencies = EditorReflectionUtility.HasFieldDependencies(new string[] { field.Name }, fields);
                        var  port            = AddPort(new PortData()
                        {
                            getPortName    = () => ObjectNames.NicifyVariableName(field.Name),
                            getPortType    = () => filter?.GetActualType() ?? typeof(object),
                            getPortValue   = () => field.GetValueOptimized(data.block),
                            filter         = filter,
                            onValueChanged = (obj) => {
                                RegisterUndo();
                                member = obj as MemberData;
                                field.SetValueOptimized(data.block, member);
                                Repaint();
                                if (hasDependencies)
                                {
                                    owner.nodeView.MarkRepaint();
                                }
                            },
                        });
                        port.Add(new ControlView(port.portData.InstantiateControl(true), true));
                    }
                    else if (type == typeof(MultipurposeMember))
                    {
                        InitMultipurposeMember(field);
                    }
                    else
                    {
                        object   fieldVal       = field.GetValueOptimized(data.block);
                        object[] fieldAttribute = field.GetCustomAttributes(true);
                        if (!ReflectionUtils.TryCorrectingAttribute(data.block, ref fieldAttribute))
                        {
                            continue;
                        }
                        bool          hasDependencies = EditorReflectionUtility.HasFieldDependencies(new string[] { field.Name }, fields);
                        var           filter          = ReflectionUtils.GetAttribute <FilterAttribute>(fieldAttribute);
                        ControlConfig config          = new ControlConfig()
                        {
                            owner          = owner.nodeView,
                            value          = fieldVal,
                            type           = type,
                            filter         = filter,
                            onValueChanged = (obj) => {
                                RegisterUndo();
                                field.SetValueOptimized(data.block, obj);
                                Repaint();
                                if (hasDependencies)
                                {
                                    owner.nodeView.MarkRepaint();
                                }
                            },
                        };
                        var valueControl = UIElementUtility.CreateControl(type, config, true);
                        AddControl(field.Name, new ControlView(valueControl, true));
                        if (fieldVal is IList <MemberData> )
                        {
                            IList <MemberData> members = fieldVal as IList <MemberData>;
                            if (members != null)
                            {
                                for (int i = 0; i < members.Count; i++)
                                {
                                    int index  = i;
                                    var member = members[i];
                                    if (member == null)
                                    {
                                        if (filter != null && !filter.SetMember && !filter.OnlyGetType &&
                                            ReflectionUtils.CanCreateInstance(filter.GetActualType()))
                                        {
                                            member = new MemberData(ReflectionUtils.CreateInstance(filter.GetActualType()));
                                        }
                                        else
                                        {
                                            member = MemberData.none;
                                        }
                                        members[index] = member;
                                        field.SetValueOptimized(data.block, members);
                                        Repaint();
                                    }
                                    var port = AddPort(new PortData()
                                    {
                                        getPortName    = () => "Element " + index.ToString(),
                                        getPortType    = () => filter?.GetActualType() ?? typeof(object),
                                        getPortValue   = () => (field.GetValueOptimized(data.block) as IList <MemberData>)[index],
                                        filter         = filter,
                                        onValueChanged = (obj) => {
                                            RegisterUndo();
                                            member         = obj as MemberData;
                                            members[index] = member;
                                            field.SetValueOptimized(data.block, members);
                                            Repaint();
                                        },
                                    });
                                    port.Add(new ControlView(port.portData.InstantiateControl(true), true));
                                }
                            }
                        }
                        else if (fieldVal is IList)
                        {
                            IList list = fieldVal as IList;
                            if (list != null)
                            {
                                for (int i = 0; i < list.Count; i++)
                                {
                                    int           index   = i;
                                    var           element = list[i];
                                    ControlConfig cfg     = new ControlConfig()
                                    {
                                        owner          = owner.nodeView,
                                        value          = fieldVal,
                                        type           = type,
                                        filter         = filter,
                                        onValueChanged = (obj) => {
                                            RegisterUndo();
                                            field.SetValueOptimized(data.block, obj);
                                            Repaint();
                                        },
                                    };
                                    var elementControl = UIElementUtility.CreateControl(type, cfg, true);
                                    AddControl("Element " + index, new ControlView(elementControl, true));
                                }
                            }
                        }
                    }
                }
            }
        }