コード例 #1
0
ファイル: Node.cs プロジェクト: BrettRToomey/forge
        public bool EventsNeedRepaint(float scale, GraphEditor graphEditor)
        {
            bool needsRepaint = false, showType = false;
            int inputHover = -1, outputHover = -1;

            if (scale != _cachedScale) RecalculateBounds(scale);

            Event ev = Event.current;

            bool mouseInNode = NodeRect.Contains(ev.mousePosition);
            bool mouseInInputs = InputsRect.Contains(ev.mousePosition);
            bool mouseInOutputs = OutputsRect.Contains(ev.mousePosition);

            // MouseOver node
            if (mouseInNode || mouseInInputs || mouseInOutputs) {
                showType = true;
            }

            // MouseOver inputs
            int inputIndex = Mathf.FloorToInt((ev.mousePosition.y - InputsRect.y) / IOHeight);
            if (mouseInInputs) {
                inputHover = inputIndex;
            }

            // MouseOver outputs
            int outputIndex = Mathf.FloorToInt((ev.mousePosition.y - OutputsRect.y) / IOHeight);
            if (mouseInOutputs) {
                outputHover = outputIndex;
            }

            // Left mouse button actions
            if (ev.button == 0) {

                // MouseDown outlets
                if (ev.type == EventType.MouseDown) {

                    // Input
                    if (mouseInInputs && inputIndex < Operator.Inputs.Length) {
                        GraphEditor.CurrentEvent = new GraphEvent(GEType.Unresolved, GEContext.Input, this, Operator.Inputs[inputIndex]);
                    }

                    // Output
                    if (mouseInOutputs && outputIndex < Operator.Outputs.Length) {
                        GraphEditor.CurrentEvent = new GraphEvent(GEType.Unresolved, GEContext.Output, this, Operator.Outputs[outputIndex]);
                    }
                }

                // MouseUp outlets
                if (ev.type == EventType.MouseUp) {

                    // Releasing Output on Input
                    if (mouseInInputs && GraphEditor.CurrentEvent.Type == GEType.Drag && GraphEditor.CurrentEvent.Context == GEContext.Output) {

                        if (IOOutlet.CanConnect(GraphEditor.CurrentEvent.Outlet, Operator.Inputs[inputIndex])) {
                            GraphEditor.Template.Connect(
                                GraphEditor.CurrentEvent.Node.Operator, GraphEditor.CurrentEvent.Outlet,
                                Operator, Operator.Inputs[inputIndex]
                            );
                        }
                        GraphEditor.CurrentEvent.Empty();
                        needsRepaint = true;
                    }

                    // Releasing Input on Output
                    if (mouseInOutputs && GraphEditor.CurrentEvent.Type == GEType.Drag && GraphEditor.CurrentEvent.Context == GEContext.Input) {
                        if (IOOutlet.CanConnect(Operator.Outputs[inputIndex], GraphEditor.CurrentEvent.Outlet)) {
                            GraphEditor.Template.Connect(
                                Operator, Operator.Outputs[inputIndex],
                                GraphEditor.CurrentEvent.Node.Operator, GraphEditor.CurrentEvent.Outlet
                            );
                        }
                        GraphEditor.CurrentEvent.Empty();
                        needsRepaint = true;
                    }
                }

                // Mouse cursor over node
                if (NodeRect.Contains(ev.mousePosition)) {

                    // Mouse Down
                    if (ev.type == EventType.MouseDown) {
                        GraphEditor.CurrentEvent = new GraphEvent(GEType.Unresolved, GEContext.Node, this, IOOutlet.None);
                    }

                    // Mouse Up
                    if (ev.type == EventType.MouseUp && GraphEditor.CurrentEvent.Type == GEType.Unresolved) {
                        if (!GraphEditor.Selection.Contains(this)) {
                            if (Event.current.modifiers == EventModifiers.Shift) {
                                GraphEditor.Selection.Add(this);
                            } else {
                                GraphEditor.Selection.Clear();
                                GraphEditor.Selection.Add(this);
                            }
                        }
                        needsRepaint = true;
                    }
                    else if (ev.type == EventType.MouseUp && GraphEditor.CurrentEvent.Node == this && GraphEditor.CurrentEvent.Type == GEType.Drag) {
                        GraphEditor.Template.Serialize();
                    }

                }

                // MouseDrag
                if (ev.type == EventType.MouseDrag) {

                    // Drag node
                    if (GraphEditor.CurrentEvent.IsNodeDrag(this)) {
                        Operator.EditorPosition.x += ev.delta.x / scale;
                        Operator.EditorPosition.y += ev.delta.y / scale;
                        RecalculateBounds(scale);
                        GraphEditor.CurrentEvent.Type = GEType.Drag;
                        needsRepaint = true;
                    }

                    // Drag input
                    else if (GraphEditor.CurrentEvent.CanDragOutlet(this, GEContext.Input)) {
                        GraphEditor.CurrentEvent = new GraphEvent(GEType.Drag, GEContext.Input, this, GraphEditor.CurrentEvent.Outlet);
                        needsRepaint = true;
                    }

                    // Drag output
                    else if (GraphEditor.CurrentEvent.CanDragOutlet(this, GEContext.Output)) {
                        GraphEditor.CurrentEvent = new GraphEvent(GEType.Drag, GEContext.Output, this, GraphEditor.CurrentEvent.Outlet);
                        needsRepaint = true;
                    }

                }
            }

            if (showType != ShowType || inputHover != InputHover || outputHover != OutputHover) {
                ShowType = showType;
                InputHover = inputHover;
                OutputHover = outputHover;
                needsRepaint = true;
            }

            return needsRepaint;
        }
コード例 #2
0
ファイル: GraphEditor.cs プロジェクト: asmboom/forge
		public static void ShowEditor() {
			GraphEditor editor = (GraphEditor) EditorWindow.GetWindow(typeof(GraphEditor));
			editor.titleContent = new GUIContent("Forge Graph");
			editor.Show();
		}