Ejemplo n.º 1
0
                protected override void OnSetObject()
                {
                    FieldInfo[] inputFields = GetEditableObject().GetEditorInputFields();
                    _inputNodes = new NodeEditorField[inputFields.Length];

                    for (int i = 0; i < _inputNodes.Length; i++)
                    {
                        _inputNodes[i] = new NodeEditorField();
                        _inputNodes[i]._nodeEditorGUI = this;
                        _inputNodes[i]._name          = StringUtils.FromPropertyCamelCase(inputFields[i].Name);
                        _inputNodes[i]._fieldInfo     = inputFields[i];
                        _inputNodes[i]._type          = SystemUtils.GetGenericImplementationType(typeof(NodeInputFieldBase <>), inputFields[i].FieldType);
                    }

                    _outputNode = null;
                    Type outputType = SystemUtils.GetGenericImplementationType(typeof(IValueSource <>), GetEditableObject().GetType());

                    if (outputType != null)
                    {
                        _outputNode = new NodeEditorField();
                        _outputNode._nodeEditorGUI = this;
                        _outputNode._name          = "Output";
                        _outputNode._type          = outputType;
                    }

                    _rect = new Rect();
                }
                private int GetNodeInputFieldLinkNodeId(NodeEditorField nodeField)
                {
                    object          inputValueInstance = nodeField.GetValue();
                    INodeInputField inputField         = inputValueInstance as INodeInputField;

                    return(inputField.GetSourceNodeId());
                }
Ejemplo n.º 3
0
                private int GetNodeInputFieldLinkNodeId(NodeEditorField nodeField)
                {
                    object          inputValueInstance = nodeField._fieldInfo.GetValue(nodeField._nodeEditorGUI.GetEditableObject());
                    INodeInputField inputField         = inputValueInstance as INodeInputField;

                    return(inputField.GetSourceNodeId());
                }
                private void SetNodeInputFieldLinkNodeID(NodeEditorField nodeField, int nodeId)
                {
                    object          inputValueInstance = nodeField.GetValue();
                    INodeInputField inputField         = inputValueInstance as INodeInputField;

                    inputField.SetSourceNode(nodeId);
                    nodeField.SetValue(inputValueInstance);
                    nodeField._nodeEditorGUI.MarkAsDirty(true);
                }
Ejemplo n.º 5
0
                private void RenderLinks(float scale, NodeEditorField highlightedField)
                {
                    foreach (NodeEditorGUI node in _editableObjects)
                    {
                        if (node.HasOutput())
                        {
                            RenderLinkIcon(node.GetOutputField()._position, kOutputLinkIconColor, scale, false);
                        }

                        foreach (NodeEditorField nodeInputField in node.GetInputFields())
                        {
                            Color color;

                            if (_dragMode == eDragType.Custom &&
                                (nodeInputField._type != _draggingNodeFieldFrom._type && !_draggingNodeFieldFrom._type.IsAssignableFrom(nodeInputField._type)))
                            {
                                color = kUnusableLinkIconColor;
                            }
                            else
                            {
                                color = kInputLinkIconColor;
                            }

                            RenderStaticValueBox(nodeInputField, nodeInputField._position, kUnusableLinkIconColor, scale);
                            RenderLinkIcon(nodeInputField._position, color, scale, nodeInputField == highlightedField);
                        }
                    }

                    foreach (NodeEditorGUI node in _editableObjects)
                    {
                        foreach (NodeEditorField nodeInputField in node.GetInputFields())
                        {
                            if (_dragMode != eDragType.Custom || _draggingNodeFieldTo != nodeInputField)
                            {
                                int linkedOutputNodeId = GetNodeInputFieldLinkNodeId(nodeInputField);

                                if (linkedOutputNodeId != -1)
                                {
                                    NodeEditorGUI outputNode = GetEditableObject(linkedOutputNodeId);

                                    if (outputNode != null && outputNode.HasOutput())
                                    {
                                        RenderLink(outputNode.GetOutputField()._position, nodeInputField._position, _dragMode == eDragType.Custom ? Color.Lerp(kLinkLineColor, Color.black, 0.3f) : kLinkLineColor, scale);
                                    }
                                }
                            }
                        }
                    }

                    if (_dragMode == eDragType.Custom)
                    {
                        //Instead of rendering current dragging link, render it to mouse position
                        RenderLink(_draggingNodeFieldFrom._position, Event.current.mousePosition, kLinkLineColor, scale, true);
                    }
                }
Ejemplo n.º 6
0
                protected override void OnStopDragging(Event inputEvent)
                {
                    if (_dragMode == eDragType.Custom)
                    {
                        //if over a input node field of correct type, set link on node
                        NodeEditorField mouseOverNodeField = GetHighlightedNode(inputEvent.mousePosition);

                        bool linkChanged = (_draggingNodeFieldTo == null && mouseOverNodeField != null) ||
                                           (_draggingNodeFieldTo != null && _draggingNodeFieldTo != mouseOverNodeField);

                        if (linkChanged)
                        {
                            List <NodeEditorGUI> effectedNodes = new List <NodeEditorGUI>();

                            if (_draggingNodeFieldTo != null)
                            {
                                _draggingNodeFieldTo._nodeEditorGUI.SaveUndoState();
                                effectedNodes.Add(_draggingNodeFieldTo._nodeEditorGUI);
                                SetNodeInputFieldLinkNodeID(_draggingNodeFieldTo, -1);
                            }

                            if (mouseOverNodeField != null)
                            {
                                mouseOverNodeField._nodeEditorGUI.SaveUndoState();
                                effectedNodes.Add(mouseOverNodeField._nodeEditorGUI);
                                SetNodeInputFieldLinkNodeID(mouseOverNodeField, _draggingNodeFieldFrom._nodeEditorGUI.GetEditableObject()._nodeId);
                            }

                            Undo.RecordObjects(effectedNodes.ToArray(), "Edit Node Link(s)");

                            foreach (NodeEditorGUI editorGUI in effectedNodes)
                            {
                                editorGUI.SaveUndoState();
                            }

                            Undo.FlushUndoRecordObjects();

                            foreach (NodeEditorGUI editorGUI in effectedNodes)
                            {
                                editorGUI.ClearUndoState();
                            }
                        }

                        inputEvent.Use();
                        _dragMode = eDragType.NotDragging;
                        _draggingNodeFieldFrom = null;
                        _draggingNodeFieldTo   = null;
                    }
                    else
                    {
                        base.OnStopDragging(inputEvent);
                    }
                }
Ejemplo n.º 7
0
                protected override void RenderObjectsOnGrid()
                {
                    NodeEditorField highlightedField = GetHighlightedNode(Event.current.mousePosition);

                    List <NodeEditorGUI> toRender = new List <NodeEditorGUI>();

                    foreach (NodeEditorGUI editorGUI in _editableObjects)
                    {
                        toRender.Add(editorGUI);
                    }

                    foreach (NodeEditorGUI node in toRender)
                    {
                        bool selected = _selectedObjects.Contains(node);
                        node.CalcBounds(_nodeTitleTextStyle, _nodeTextStyle, _currentZoom);
                        Rect renderedRect = GetScreenRect(node.GetBounds());
                        node.Render(renderedRect, selected, _nodeTitleTextStyle, _nodeTextStyle, _currentZoom, highlightedField, _draggingNodeFieldFrom);
                    }

                    RenderLinks(_currentZoom, highlightedField);
                }
                private void RenderStaticValueBox(NodeEditorField nodeField, Vector2 position, Color color, float scale)
                {
                    //Get _value field from the input field and then
                    object inputValueInstance = nodeField.GetValue();

                    INodeInputField inputField = inputValueInstance as INodeInputField;

                    if (inputField.IsStaticValue())
                    {
                        object nodeValue = SerializedObjectMemberInfo.GetSerializedFieldInstance(inputValueInstance, "value");

                        if (nodeValue != null)
                        {
                            string     labelText    = nodeValue + "  ";
                            GUIContent labelContent = new GUIContent(labelText);
                            Vector2    size         = _nodeBoldTextStyle.CalcSize(labelContent);
                            size.y  = kLinkIconWidth * scale;
                            size.x += kLinkIconWidth;

                            Rect staticFieldPos = new Rect(position.x - size.x, position.y - size.y * 0.5f, size.x, size.y);

                            Handles.BeginGUI();
                            Handles.color = color;
                            Handles.DrawSolidDisc(new Vector3(staticFieldPos.x + kLinkIconWidth * 0.25f, staticFieldPos.y + kLinkIconWidth * 0.5f * scale, 0.0f), -Vector3.forward, kLinkIconWidth * 0.5f * scale);
                            Handles.EndGUI();

                            Color origBackgroundColor = GUI.backgroundColor;
                            GUI.backgroundColor = color;

                            GUI.BeginGroup(staticFieldPos, EditorUtils.ColoredRoundedBoxStyle);
                            {
                                GUI.Label(new Rect(0, 0, staticFieldPos.width, staticFieldPos.height), labelContent, _nodeBoldTextStyle);
                            }
                            GUI.EndGroup();
                        }
                    }
                }
                protected override void OnLeftMouseDown(Event inputEvent)
                {
                    //Check for clicking on a link
                    NodeEditorField clickedOnNodeFromField = null;
                    NodeEditorField clickedOnNodeToField   = null;

                    for (int i = 0; i < _editableObjects.Count && clickedOnNodeFromField == null; i++)
                    {
                        NodeEditorGUI node = (NodeEditorGUI)_editableObjects[i];

                        if (node.GetOutputField() != null)
                        {
                            Vector2 toField = inputEvent.mousePosition - node.GetOutputField()._position;

                            if (toField.magnitude < kLinkIconWidth * 0.5f)
                            {
                                clickedOnNodeFromField = node.GetOutputField();
                                break;
                            }
                        }

                        foreach (NodeEditorField nodeInputField in node.GetInputFields())
                        {
                            //If has an link going into it we can drag it away
                            int linkNodeId = GetNodeInputFieldLinkNodeId(nodeInputField);

                            if (linkNodeId != -1)
                            {
                                NodeEditorGUI linkedNode = GetEditableObject(linkNodeId);

                                if (linkedNode != null)
                                {
                                    Vector2 toField = inputEvent.mousePosition - nodeInputField._position;

                                    if (toField.magnitude < kLinkIconWidth)
                                    {
                                        clickedOnNodeFromField = linkedNode.GetOutputField();
                                        clickedOnNodeToField   = nodeInputField;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (clickedOnNodeFromField != null)
                    {
                        _draggingNodeFieldFrom = clickedOnNodeFromField;
                        _draggingNodeFieldTo   = clickedOnNodeToField;
                        _dragMode     = eDragType.Custom;
                        _dragPos      = inputEvent.mousePosition;
                        _dragAreaRect = new Rect(-1.0f, -1.0f, 0.0f, 0.0f);
                    }
                    //Normal object clicking
                    else
                    {
                        _dragMode = eDragType.LeftClick;

                        base.OnLeftMouseDown(inputEvent);
                    }
                }
Ejemplo n.º 10
0
                public void Render(Rect renderedRect, bool selected, GUIStyle titleStyle, GUIStyle textStyle, float scale, NodeEditorField highlightedField, NodeEditorField draggingFromField)
                {
                    Color origBackgroundColor = GUI.backgroundColor;

                    GUI.backgroundColor = Color.clear;

                    GUI.BeginGroup(renderedRect, EditorUtils.ColoredRoundedBoxStyle);
                    {
                        Rect mainBox = new Rect(0.0f, 0.0f, renderedRect.width - kShadowSize, renderedRect.height - kShadowSize);

                        //Draw shadow
                        EditorUtils.DrawColoredRoundedBox(new Rect(mainBox.x + kShadowSize, mainBox.y + kShadowSize, mainBox.width, mainBox.height), kShadowColor);

                        //Draw white background
                        EditorUtils.DrawColoredRoundedBox(mainBox, selected ? kBorderColorHighlighted : kBorderColor);

                        //Draw label
                        Rect labelRect = new Rect(mainBox.x + 1.0f, mainBox.y + 1.0f, mainBox.width - 2.0f, kLineHeight * 2.0f * scale);
                        GUI.backgroundColor = GetEditableObject().GetEditorColor();
                        GUI.BeginGroup(labelRect, EditorUtils.ColoredRoundedBoxStyle);
                        {
                            float h, s, v;
                            Color.RGBToHSV(GetEditableObject().GetEditorColor(), out h, out s, out v);
                            titleStyle.normal.textColor = v > 0.66f ? Color.black : Color.white;

                            GUI.backgroundColor  = Color.clear;
                            titleStyle.fontStyle = FontStyle.Bold;
                            GetEditableObject()._editorDescription = GUI.TextField(new Rect(kTextPadding * scale, -2.0f * scale, labelRect.width - kTextPadding * 2.0f * scale, kLableHeight * scale), GetEditableObject()._editorDescription, titleStyle);

                            string nodeTypeText = "<b>(" + StringUtils.FromPropertyCamelCase(GetEditableObject().GetType().Name) + ")</b>";
                            textStyle.alignment = TextAnchor.MiddleLeft;
                            GUI.Label(new Rect(kTextPadding * scale, (kLineHeight - 4.0f) * scale, labelRect.width - kTextPadding * 2.0f * scale, kLableHeight * scale), nodeTypeText, textStyle);
                        }
                        GUI.EndGroup();

                        //Draw inputs / outputs
                        Rect fieldBox = new Rect(labelRect.x, labelRect.y + labelRect.height + 1.0f, labelRect.width, kLineHeight * scale);

                        //Draw output
                        if (HasOutput())
                        {
                            fieldBox.width = _outputFieldWidth;
                            fieldBox.x     = labelRect.x + _inputFieldWidth + 1.0f;

                            GUI.backgroundColor = kFieldColor;
                            GUI.BeginGroup(fieldBox, EditorUtils.ColoredRoundedBoxStyle);
                            {
                                GUI.backgroundColor = Color.clear;
                                string text = "Output";
                                textStyle.alignment = TextAnchor.MiddleRight;
                                GUI.Label(new Rect(0, 0, _outputFieldWidth - (kFieldIconPadding * scale), fieldBox.height), text, textStyle);
                            }
                            GUI.EndGroup();

                            _outputNode._position = new Vector3(renderedRect.x + fieldBox.x + fieldBox.width, renderedRect.y + fieldBox.y + (kLineHeight * scale * 0.5f));
                        }

                        //Draw inputs
                        {
                            fieldBox.width = _inputFieldWidth;
                            fieldBox.x     = labelRect.x;
                            foreach (NodeEditorField inputField in _inputNodes)
                            {
                                if (inputField == highlightedField)
                                {
                                    GUI.backgroundColor = kFieldColorHighlighted;
                                }
                                else if (draggingFromField != null && !draggingFromField._type.IsAssignableFrom(inputField._type))
                                {
                                    GUI.backgroundColor = kFieldColorDisabled;
                                }
                                else
                                {
                                    GUI.backgroundColor = kFieldColor;
                                }

                                GUI.BeginGroup(fieldBox, EditorUtils.ColoredRoundedBoxStyle);
                                {
                                    GUI.backgroundColor = Color.clear;
                                    textStyle.alignment = TextAnchor.MiddleLeft;
                                    GUI.Label(new Rect(kFieldIconPadding * scale, 0, _inputFieldWidth - (kFieldIconPadding * scale), fieldBox.height), inputField._name, textStyle);
                                }
                                GUI.EndGroup();

                                inputField._position = new Vector2(renderedRect.x + fieldBox.x, renderedRect.y + fieldBox.y + (kLineHeight * scale * 0.5f));

                                fieldBox.y += (kLineHeight * scale) + 1.0f;
                            }
                        }
                    }
                    GUI.EndGroup();

                    GUI.backgroundColor = origBackgroundColor;
                }