Esempio n. 1
0
        public void EDITOR_DrawNode()
        {
            if (!is_ready)
            {
                return;
            }
            gui_color         = GUI.color;
            gui_back_color    = GUI.backgroundColor;
            gui_content_color = GUI.contentColor;

            if (is_active || is_selected || GraphEditor.hover_node == this || (GraphEditor.drag_port && GraphEditor.drag_port.node == this))
            {
            }
            else
            {
                GUI.color = new Color(gui_color.r, gui_color.g, gui_color.b, 0.5f);
            }

            // for better performance
            if (is_occluded)
            {
                foreach (Port port in portValues)
                {
                    if (!port.display_port)
                    {
                        continue;
                    }
                    IPlugIn plug_in = port as IPlugIn;
                    if (plug_in == null || !plug_in.IsPlugged())
                    {
                        continue;
                    }
                    port.node = this;

                    Vector2 start = GetPortPoint(port).center;
                    Vector2 end   = GetPortPoint((Port)plug_in.GetPluggedPort()).center;
                    if (port is ActionPort)
                    {
                        end   = GetPortPoint(port).center;
                        start = GetPortPoint((Port)plug_in.GetPluggedPort()).center;
                    }

                    Node.DrawConnection(start, end, GetPortColor(port), false);
                    if (Application.isPlaying)
                    {
                        if (port.flow_state == FlowState.Active)
                        {
                            port.unit_delta_size = 1.0f;
                            port.flow_state      = FlowState.Idle;
                        }
                        else
                        {
                            port.unit_delta_size = Mathf.MoveTowards(port.unit_delta_size, 0.0f, EditorTime.deltaTime / 2.0f);
                        }

                        GUI.backgroundColor = GetPortColor(port);
                        float distance = FPMath.SnapValue(Vector3.Distance(start, end) / 100.0f, 1);
                        for (int id = 0; id < Mathf.RoundToInt(distance); id++)
                        {
                            float   t         = 1.0f - (((EditorTime.time / distance) + (1.0f / distance) * id) % 1.0f);
                            Vector2 unit_size = V2x16y16 * port.unit_delta_size;
                            GUI.Box(new Rect(LerpUnit(start, end, t) - V2x0y2 - unit_size / 2.0f, unit_size), "", styles.unit);
                        }
                        GUI.backgroundColor = gui_back_color;
                    }
                    else
                    {
                        if (GUI.Button(new Rect(MiddleOfConnection(end, start) - V2x8y9, V2x16y16), "x", styles.unplug_button))
                        {
                            GraphEditor.UnplugPort(port);
                        }
                    }
                }
            }
            else
            {
                if (is_selected)
                {
                    GUI.Box(nodeRect, string.Empty, styles.highlight_node);
                }

                is_active = GraphEditor.makeAllNodesActive || this is EventNode || this is InputNode || this is OutputNode;
                //Draw Node with custom color
                GUI.backgroundColor = node_color;
                if (slim)
                {
                    DrawSlimNode();
                }
                else
                {
                    DrawNode();
                }
                GUI.backgroundColor = gui_back_color;

                // Color gui_color = GUI.color;
                foreach (Port input in inputValues)
                {
                    input.node = this;
                    if (!input.display_port)
                    {
                        continue;
                    }
                    IPlug       plug        = input as IPlug;
                    IPlugIn     plug_in     = input as IPlugIn;
                    bool        on          = plug != null && plug.IsPlugged();
                    IInputValue input_value = input as IInputValue;
                    Rect        port_rect   = GetPortPoint(input);

                    if (plug_in != null)
                    {
                        if (plug_in.IsPlugged())
                        {
                            Vector2 start = port_rect.center;
                            Vector2 end   = GetPortPoint((Port)plug_in.GetPluggedPort()).center;

                            Node.DrawConnection(start, end, GetPortColor(input), false);

                            if (Application.isPlaying)
                            {
                                if (input.flow_state == FlowState.Active)
                                {
                                    input.unit_delta_size = 1.0f;
                                    input.flow_state      = FlowState.Idle;
                                }
                                else
                                {
                                    input.unit_delta_size = Mathf.MoveTowards(input.unit_delta_size, 0.0f, EditorTime.deltaTime / 2.0f);
                                }

                                GUI.backgroundColor = GetPortColor(input);
                                float distance = FPMath.SnapValue(Vector3.Distance(start, end) / 100.0f, 1);
                                for (int id = 0; id < Mathf.RoundToInt(distance); id++)
                                {
                                    float   t         = 1.0f - (((EditorTime.time / distance) + (1.0f / distance) * id) % 1.0f);
                                    Vector2 unit_size = V2x16y16 * input.unit_delta_size;
                                    GUI.Box(new Rect(LerpUnit(start, end, t) - V2x0y2 - unit_size / 2.0f, unit_size), "", styles.unit);
                                }
                                GUI.backgroundColor = gui_back_color;
                            }
                            else
                            {
                                if (GUI.Button(new Rect(MiddleOfConnection(start, end) - V2x8y9, V2x16y16), "x", styles.unplug_button))
                                {
                                    GraphEditor.UnplugPort(input);
                                }
                            }
                        }
                        else if (input_value != null)
                        {
                            if (GraphEditor.showPortValues)
                            {
                                object value = input_value.GetDefaultValue();
                                Rect   value_label_rect;
                                string value_content     = "NO INFO";
                                float  value_label_width = 0.0f;
                                if (value == null)
                                {
                                    if (typeof(UnityEngine.Component).IsAssignableFrom(input_value.valueType) || typeof(Graph).IsAssignableFrom(input_value.valueType) || typeof(UnityEngine.GameObject).IsAssignableFrom(input_value.valueType))
                                    {
                                        if (EditorGUIUtility.isProSkin)
                                        {
                                            value_content = string.Format("<b><color=#0667FF>SELF: {0}</color></b>", input_value.valueType.GetTypeName());
                                        }
                                        else
                                        {
                                            value_content = string.Format("<b><color=#458fff>SELF: {0}</color></b>", input_value.valueType.GetTypeName());
                                        }
                                    }
                                    else
                                    {
                                        value_content = input_value.valueType.GetTypeName(true);
                                    }
                                }
                                else
                                {
                                    if (typeof(string).IsAssignableFrom(input_value.valueType))
                                    {
                                        value_content = string.Format("<color=#FFA06396>\"{0}\"</color>", value);
                                    }
                                    else if (typeof(UnityEngine.Component).IsAssignableFrom(input_value.valueType) || typeof(UnityEngine.GameObject).IsAssignableFrom(input_value.valueType) || typeof(Graph).IsAssignableFrom(input_value.valueType))
                                    {
                                        value_content = string.Format("<b><color=#0667FF>{0}</color></b>", value);
                                    }
                                    else if (typeof(Type).IsAssignableFrom(input_value.valueType))
                                    {
                                        value_content = ReflectionUtils.GetTypeName((Type)value, true);
                                    }
                                    else
                                    {
                                        if (input_value.valueType.IsGenericType)
                                        {
                                            value_content = input_value.valueType.GetTypeName(true);
                                        }
                                        else
                                        {
                                            value_content = value.ToString();
                                        }
                                    }
                                }
                                value_label_width = GUIUtils.GetTextWidth(value_content, styles.input_label);
                                value_label_rect  = new Rect(port_rect.x - (value_label_width + 15.0f), port_rect.y, value_label_width, 18.0f);
                                GUI.Label(value_label_rect, value_content, styles.input_label);
                            }
                        }
                    }

                    port_rect = GraphEditor.ZoomedRect(GetPortPoint(input));
                    if (port_rect.Contains(GraphEditor.mouse_position))
                    {
                        GraphEditor.hover_port = input;
                    }
                    else
                    {
                        GUI.backgroundColor = GetPortColor(input);
                    }
                    port_rect = GetPortPoint(input);

                    if (input is ActionPort)
                    {
                        if (!is_active && ((IPlug)input).IsPlugged())
                        {
                            List <IPlugIn> list = ((IPlugOut)input).GetPluggedPorts();
                            if (list != null && list.Any(p => ((Port)p).node && ((Port)p).node.is_active))
                            {
                                is_active = true;
                            }
                        }
                        GUI.Box(port_rect, slim ? string.Empty : input.name, on ? styles.on_input_action : styles.input_action);
                    }
                    else
                    {
                        GUI.Box(port_rect, slim ? string.Empty : input.name, on ? styles.on_input_port : styles.input_port);
                    }

                    GUI.backgroundColor = gui_back_color;
                }

                foreach (Port output in outputValues)
                {
                    output.node = this;
                    if (!output.display_port)
                    {
                        continue;
                    }
                    IPlug   plug      = output as IPlug;
                    IPlugIn plug_in   = output as IPlugIn;
                    bool    on        = plug != null && plug.IsPlugged();
                    Rect    port_rect = GetPortPoint(output);

                    if (plug_in != null)
                    {
                        if (plug_in.IsPlugged())
                        {
                            Vector2 start = port_rect.center;
                            Vector2 end   = GetPortPoint((Port)plug_in.GetPluggedPort()).center;

                            Node.DrawConnection(end, start, GetPortColor(output), false);

                            if (Application.isPlaying)
                            {
                                if (output.flow_state == FlowState.Active)
                                {
                                    output.unit_delta_size = 1.0f;
                                    output.flow_state      = FlowState.Idle;
                                }
                                else
                                {
                                    output.unit_delta_size = Mathf.MoveTowards(output.unit_delta_size, 0.0f, EditorTime.deltaTime / 2.0f);
                                }

                                float distance = FPMath.SnapValue(Vector3.Distance(start, end) / 100.0f, 1);
                                GUI.backgroundColor = GetPortColor(output);
                                for (int id = 0; id < Mathf.RoundToInt(distance); id++)
                                {
                                    float   t         = 1.0f - (((EditorTime.time / distance) + (1.0f / distance) * id) % 1.0f);
                                    Vector2 unit_size = V2x16y16 * output.unit_delta_size;
                                    GUI.Box(new Rect(LerpUnit(end, start, t) - V2x0y2 - unit_size / 2.0f, unit_size), "", styles.unit);
                                }
                                GUI.backgroundColor = gui_back_color;
                            }
                            else
                            {
                                if (GUI.Button(new Rect(MiddleOfConnection(start, end) - V2x8y9, V2x16y16), "x", styles.unplug_button))
                                {
                                    GraphEditor.UnplugPort(output);
                                }
                            }
                        }
                    }
                    port_rect = GraphEditor.ZoomedRect(GetPortPoint(output));
                    if (port_rect.Contains(GraphEditor.mouse_position))
                    {
                        GraphEditor.hover_port = output;
                    }
                    else
                    {
                        GUI.backgroundColor = GetPortColor(output);
                    }
                    port_rect = GetPortPoint(output);

                    if (output is ActionPort)
                    {
                        GUI.Box(port_rect, slim ? string.Empty : output.name, on ? styles.on_output_action : styles.output_action);
                    }
                    else
                    {
                        if (!is_active && ((IPlug)output).IsPlugged() && ((IPlugOut)output).GetPluggedPorts().Any(p => ((Port)p).node.is_active))
                        {
                            is_active = true;
                        }
                        GUI.Box(port_rect, slim ? string.Empty : output.name, on ? styles.on_output_port : styles.output_port);
                    }
                    GUI.backgroundColor = gui_back_color;
                }
                GUI.backgroundColor = gui_back_color;
            }
            GUI.color           = gui_color;
            GUI.backgroundColor = gui_back_color;
            GUI.contentColor    = gui_content_color;
        }