Exemplo n.º 1
0
        public override void OnClick(Node source, PortCommandData data, Vector2 mousePosition)
        {
            if (source.owner)
            {
                Undo.SetCurrentGroupName("Promote to node");
                Undo.RegisterFullObjectHierarchyUndo(source.owner, "Promote to node");
            }
            MemberData m = data.member;

            if (data.portType != null && (!m.isAssigned || m.type == null))
            {
                if (ReflectionUtils.CanCreateInstance(data.portType))
                {
                    m.CopyFrom(new MemberData(ReflectionUtils.CreateInstance(data.portType)));
                }
                else
                {
                    m.CopyFrom(MemberData.CreateFromValue(null, data.portType));
                }
            }
            NodeEditorUtility.AddNewNode <MultipurposeNode>(graph.editorData, null, null, new Vector2(source.editorRect.x - 100, source.editorRect.y), (node) => {
                node.target.target = new MemberData(m);
                MemberDataUtility.UpdateMultipurposeMember(node.target);
                m.CopyFrom(new MemberData(node, MemberData.TargetType.ValueNode));
            });
            graph.Refresh();
        }
Exemplo n.º 2
0
        public override void OnClick(Node source, PortCommandData data, Vector2 mousePosition)
        {
            if (source.owner)
            {
                Undo.SetCurrentGroupName("To value node");
                Undo.RegisterFullObjectHierarchyUndo(source.owner, "To value node");
            }
            var member = data.member;

            NodeEditorUtility.AddNewNode <MultipurposeNode>(graph.editorData, null, null, new Vector2(source.editorRect.x - 100, source.editorRect.y), (node) => {
                node.target.target = MemberData.CreateFromValue(ReflectionUtils.CreateInstance(filter.Types[0]), filter.Types[0]);
                MemberDataUtility.UpdateMultipurposeMember(node.target);
                member.CopyFrom(MemberData.CreateConnection(node, false));
            });
            graph.Refresh();
        }
Exemplo n.º 3
0
 private static bool CallbackRefactorFunction(object obj, uNodeFunction func, string name, Type[] paramTypes)
 {
     if (obj is MemberData)
     {
         MemberData member = obj as MemberData;
         if (member.instance as UnityEngine.Object == func.owner && member.targetType == MemberData.TargetType.uNodeFunction)
         {
             int pLength = member.ParameterTypes[0] == null ? 0 : member.ParameterTypes[0].Length;
             if (member.startName.Equals(name) && pLength == paramTypes.Length)
             {
                 bool isValid = true;
                 if (pLength != 0)
                 {
                     for (int x = 0; x < paramTypes.Length; x++)
                     {
                         if (paramTypes[x] != member.ParameterTypes[0][x])
                         {
                             isValid = false;
                             break;
                         }
                     }
                 }
                 if (isValid)
                 {
                     if (!member.isDeepTarget)
                     {
                         MemberData.CreateFromValue(func).CopyTo(member);
                     }
                     else
                     {
                         var mVal  = MemberData.CreateFromValue(func);
                         var items = member.Items;
                         items[0] = mVal.Items[0];
                         member.SetItems(items);
                         member.startName = func.Name;
                     }
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Exemplo n.º 4
0
 protected void CreateOverrideMembers(uNodeRoot graph)
 {
     foreach (var member in graphOverrideMembers)
     {
         if (member is MethodInfo method)
         {
             var func = CreateComponent <uNodeFunction>(method.Name, GetRootTransfrom(graph), (val) => {
                 val.Name       = method.Name;
                 val.returnType = MemberData.CreateFromType(method.ReturnType);
                 val.parameters = method.GetParameters().Select(p => new ParameterData(p.Name, p.ParameterType)
                 {
                     value = p.DefaultValue
                 }).ToArray();
                 val.genericParameters = method.GetGenericArguments().Select(p => new GenericParameterData(p.Name)).ToArray();
             });
             CreateComponent <Nodes.NodeAction>("Entry", func, node => {
                 func.startNode = node;
             });
         }
         else if (member is PropertyInfo property)
         {
             var prop = CreateComponent <uNodeProperty>(property.Name, GetRootTransfrom(graph), (val) => {
                 val.Name = property.Name;
                 val.type = MemberData.CreateFromType(property.PropertyType);
             });
             var getMethod = property.GetGetMethod(true);
             var setMethod = property.GetSetMethod(true);
             if (getMethod != null)
             {
                 var func = CreateComponent <uNodeFunction>("Getter", GetRootTransfrom(graph), (val) => {
                     val.Name       = getMethod.Name;
                     val.returnType = MemberData.CreateFromType(getMethod.ReturnType);
                     val.parameters = getMethod.GetParameters().Select(p => new ParameterData(p.Name, p.ParameterType)
                     {
                         value = p.DefaultValue
                     }).ToArray();
                     val.genericParameters = getMethod.GetGenericArguments().Select(p => new GenericParameterData(p.Name)).ToArray();
                 });
                 CreateComponent <Nodes.NodeAction>("Entry", func, node => {
                     func.startNode = node;
                     CreateComponent <NodeReturn>("Return", func, returnNode => {
                         node.onFinished        = MemberData.CreateConnection(returnNode, true);
                         returnNode.returnValue = MemberData.CreateFromValue(null, property.PropertyType);
                     });
                 });
                 prop.getRoot = func;
             }
             if (setMethod != null)
             {
                 var func = CreateComponent <uNodeFunction>("Setter", GetRootTransfrom(graph), (val) => {
                     val.Name       = setMethod.Name;
                     val.returnType = MemberData.CreateFromType(setMethod.ReturnType);
                     val.parameters = setMethod.GetParameters().Select(p => new ParameterData(p.Name, p.ParameterType)
                     {
                         value = p.DefaultValue
                     }).ToArray();
                     val.genericParameters = setMethod.GetGenericArguments().Select(p => new GenericParameterData(p.Name)).ToArray();
                 });
                 CreateComponent <Nodes.NodeAction>("Entry", func, node => {
                     func.startNode = node;
                 });
                 prop.setRoot = func;
             }
         }
     }
 }
Exemplo n.º 5
0
        protected override void InitializeView()
        {
            HLNode node = targetNode as HLNode;

            node.Refresh();
            var instance = node.instance;

            if (instance == null)
            {
                return;
            }
            if (instance is IStateNode || instance is IStateCoroutineNode)
            {
                AddOutputFlowPort(
                    new PortData()
                {
                    portID         = nameof(node.onSuccess),
                    onValueChanged = (o) => {
                        RegisterUndo();
                        node.onSuccess = o as MemberData;
                    },
                    getPortName    = () => "Success",
                    getPortType    = () => typeof(FlowInput),
                    getPortValue   = () => node.onSuccess,
                    getPortTooltip = () => "Flow to execute on success",
                }
                    );
                AddOutputFlowPort(
                    new PortData()
                {
                    portID         = nameof(node.onFailure),
                    onValueChanged = (o) => {
                        RegisterUndo();
                        node.onFailure = o as MemberData;
                    },
                    getPortName    = () => "Failure",
                    getPortType    = () => typeof(FlowInput),
                    getPortValue   = () => node.onFailure,
                    getPortTooltip = () => "Flow to execute on failure",
                }
                    );
            }
            if (node.IsFlowNode())              //Flow input
            {
                nodeFlowPort = AddInputFlowPort(
                    new PortData()
                {
                    portID        = UGraphView.SelfPortID,
                    getPortName   = () => "",
                    getConnection = () => {
                        return(new MemberData(node, MemberData.TargetType.FlowNode));
                    },
                }
                    );
                AddOutputFlowPort(
                    new PortData()
                {
                    portID         = "onFinished",
                    onValueChanged = (o) => {
                        RegisterUndo();
                        node.onFinished = o as MemberData;
                    },
                    getPortName  = () => instance is IStateNode || instance is IStateCoroutineNode ? "Finished" : "",
                    getPortType  = () => typeof(FlowInput),
                    getPortValue = () => node.onFinished,
                }
                    );
            }
            if (node.CanGetValue() || node.CanSetValue())              //Value output
            {
                nodeValuePort = AddOutputValuePort(
                    new PortData()
                {
                    portID        = UGraphView.SelfPortID,
                    getPortName   = () => "Out",
                    getPortType   = () => node.ReturnType(),
                    getPortValue  = () => node,
                    getConnection = () => {
                        return(new MemberData(node, MemberData.TargetType.ValueNode));
                    },
                }
                    );
            }
            Type type   = instance.GetType();
            var  fields = EditorReflectionUtility.GetFields(type);

            foreach (var field in fields)
            {
                if (field.IsDefined(typeof(NonSerializedAttribute)) || field.IsDefined(typeof(HideAttribute)))
                {
                    continue;
                }
                var option = field.GetCustomAttribute(typeof(NodePortAttribute), true) as NodePortAttribute;
                if (option != null && option.hideInNode)
                {
                    continue;
                }
                var val = node.initializers.FirstOrDefault(d => d.name == field.Name);
                if (val == null)
                {
                    val = new FieldValueData()
                    {
                        name  = field.Name,
                        value = MemberData.CreateFromValue(field.GetValue(instance), field.FieldType),
                    };
                    node.initializers.Add(val);
                }
                AddInputValuePort(
                    new PortData()
                {
                    portID         = field.Name,
                    onValueChanged = (o) => {
                        RegisterUndo();
                        val.value = o as MemberData;
                    },
                    getPortName    = () => option != null ? option.name : field.Name,
                    getPortType    = () => field.FieldType,
                    getPortValue   = () => val.value,
                    getPortTooltip = () => {
                        var tooltip = field.GetCustomAttribute(typeof(TooltipAttribute), true) as TooltipAttribute;
                        return(tooltip != null ? tooltip.tooltip : string.Empty);
                    },
                });
            }
        }
Exemplo n.º 6
0
 private void BuildContextualMenu(ContextualMenuPopulateEvent evt)
 {
     if (config.type != null)
     {
         if (config.filter == null || !config.filter.SetMember && config.filter.IsValidTarget(MemberData.TargetType.Values))
         {
             evt.menu.AppendAction("To Default", (e) => {
                 if (config.type == typeof(Type))
                 {
                     member = new MemberData(typeof(object));
                 }
                 else if (config.type is RuntimeType)
                 {
                     member = MemberData.CreateFromValue(null, config.type);
                 }
                 else
                 {
                     member = new MemberData(ReflectionUtils.CanCreateInstance(config.type) ? ReflectionUtils.CreateInstance(config.type) : null)
                     {
                         startType      = config.type,
                         targetTypeName = config.type.FullName,
                     };
                 }
                 config.OnValueChanged(member);
                 config.owner.OnValueChanged();
                 config.owner.MarkRepaint();
                 UpdateControl();
                 uNodeEditor.GUIChanged();
             }, DropdownMenuAction.AlwaysEnabled);
             if (config.type == typeof(object))
             {
                 var types = EditorReflectionUtility.GetCommonType();
                 foreach (var t in types)
                 {
                     evt.menu.AppendAction("To Value/" + t.PrettyName(), (e) => {
                         member = new MemberData(ReflectionUtils.CanCreateInstance(t) ? ReflectionUtils.CreateInstance(t) : null)
                         {
                             startType      = t,
                             targetTypeName = t.FullName,
                         };
                         config.OnValueChanged(member);
                         config.owner.OnValueChanged();
                         config.owner.MarkRepaint();
                         UpdateControl();
                         uNodeEditor.GUIChanged();
                     }, DropdownMenuAction.AlwaysEnabled);
                 }
             }
             else if (config.type == typeof(Type))
             {
                 var mPos = NodeGraph.openedGraph.GetMousePosition();
                 evt.menu.AppendAction("Change Type", (e) => {
                     TypeSelectorWindow.ShowWindow(mPos, new FilterAttribute()
                     {
                         OnlyGetType = true
                     },
                                                   delegate(MemberData[] types) {
                         uNodeEditorUtility.RegisterUndo(config.owner.targetNode);
                         member.CopyFrom(types[0]);
                         config.OnValueChanged(member);
                         config.owner.OnValueChanged();
                         config.owner.MarkRepaint();
                         UpdateControl();
                         uNodeEditor.GUIChanged();
                     }, new TypeItem[1] {
                         member
                     }).targetObject = config.owner.targetNode;
                 }, DropdownMenuAction.AlwaysEnabled);
             }
             if (!config.type.IsValueType)
             {
                 evt.menu.AppendAction("To Null", (e) => {
                     uNodeEditorUtility.RegisterUndo(config.owner.targetNode);
                     member.CopyFrom(MemberData.Null);
                     config.OnValueChanged(member);
                     config.owner.OnValueChanged();
                     config.owner.MarkRepaint();
                     UpdateControl();
                     uNodeEditor.GUIChanged();
                 }, DropdownMenuAction.AlwaysEnabled);
             }
         }
     }
 }
Exemplo n.º 7
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.º 8
0
 void InitializeHLBlock(Block block)
 {
     if (block is Events.HLAction action)
     {
         Type type = action.type.startType;
         if (type != null)
         {
             var fields = EditorReflectionUtility.GetFields(type);
             foreach (var field in fields)
             {
                 if (field.IsDefined(typeof(NonSerializedAttribute)) || field.IsDefined(typeof(HideAttribute)))
                 {
                     continue;
                 }
                 var option = field.GetCustomAttribute(typeof(NodePortAttribute), true) as NodePortAttribute;
                 if (option != null && option.hideInNode)
                 {
                     continue;
                 }
                 var val = action.initializers.FirstOrDefault(d => d.name == field.Name);
                 if (val == null)
                 {
                     val = new FieldValueData()
                     {
                         name  = field.Name,
                         value = MemberData.CreateFromValue(field.GetValueOptimized(ReflectionUtils.CreateInstance(type)), field.FieldType),
                     };
                     action.initializers.Add(val);
                 }
                 var port = AddPort(new PortData()
                 {
                     portID         = field.Name,
                     getPortName    = () => option != null ? option.name : field.Name,
                     getPortType    = () => field.FieldType,
                     getPortValue   = () => val.value,
                     onValueChanged = (obj) => {
                         RegisterUndo();
                         val.value = obj as MemberData;
                         Repaint();
                     },
                     getPortTooltip = () => {
                         var tooltip = field.GetCustomAttribute(typeof(TooltipAttribute), true) as TooltipAttribute;
                         return(tooltip != null ? tooltip.tooltip : string.Empty);
                     },
                 });
                 port.Add(new ControlView(port.portData.InstantiateControl(true), true));
             }
         }
     }
     else if (block is Events.HLCondition condition)
     {
         Type type = condition.type.startType;
         if (type != null)
         {
             var fields = EditorReflectionUtility.GetFields(type);
             foreach (var field in fields)
             {
                 if (field.IsDefined(typeof(NonSerializedAttribute)) || field.IsDefined(typeof(HideAttribute)))
                 {
                     continue;
                 }
                 var option = field.GetCustomAttribute(typeof(NodePortAttribute), true) as NodePortAttribute;
                 if (option != null && option.hideInNode)
                 {
                     continue;
                 }
                 var val = condition.initializers.FirstOrDefault(d => d.name == field.Name);
                 if (val == null)
                 {
                     val = new FieldValueData()
                     {
                         name  = field.Name,
                         value = MemberData.CreateFromValue(field.GetValueOptimized(ReflectionUtils.CreateInstance(type)), field.FieldType),
                     };
                     condition.initializers.Add(val);
                 }
                 var port = AddPort(new PortData()
                 {
                     portID         = field.Name,
                     getPortName    = () => option != null ? option.name : field.Name,
                     getPortType    = () => field.FieldType,
                     getPortValue   = () => val.value,
                     onValueChanged = (obj) => {
                         RegisterUndo();
                         val.value = obj as MemberData;
                         Repaint();
                     },
                     getPortTooltip = () => {
                         var tooltip = field.GetCustomAttribute(typeof(TooltipAttribute), true) as TooltipAttribute;
                         return(tooltip != null ? tooltip.tooltip : string.Empty);
                     },
                 });
                 port.Add(new ControlView(port.portData.InstantiateControl(true), true));
             }
         }
     }
 }