示例#1
0
        void OnGUI()
        {
            if (_gridRenderer == null) _gridRenderer = new GridRenderer();
            Event currentEvent = Event.current;

            Rect scrollViewRect = new Rect(0, 0, position.width - SidebarWidth, position.height);
            Rect sidebarRect = new Rect(position.width - SidebarWidth, 0, SidebarWidth, position.height);

            OperatorInspector.DrawInspector(sidebarRect);

            ScrollPoint = GUI.BeginScrollView(scrollViewRect, ScrollPoint, Canvas);

            bool needsRepaint = false;

            if (currentEvent.type == EventType.ScrollWheel) {
                Zoom += -currentEvent.delta.y / 50;
                if (Zoom < 0.25f) Zoom = 0.25f;
                if (Zoom > 1f) Zoom = 1f;
                needsRepaint = true;
                currentEvent.Use();
            }

            Canvas = new Rect(0f, 0f, position.width*4*Zoom, position.height*4*Zoom);

            _gridRenderer.Draw(ScrollPoint, Zoom, Canvas);

            if (Template != null) {

                // Draw the connections
                Template.DrawConnections();

                // Draw the nodes
                foreach (var kvp in Template.Operators) {
                    Node node = GetNode(kvp.Key); // kvp.Key = GUID
                    needsRepaint = needsRepaint || node.EventsNeedRepaint(Zoom, this);
                    node.Draw(Zoom);
                }

                // Handle left mouse button events
                if (currentEvent.button == 0) {

                    // MouseDown
                    if (currentEvent.type == EventType.MouseDown && CurrentEvent.Type == GEType.None) {
                        CurrentEvent = new GraphEvent(GEType.Unresolved, GEContext.Grid, null, IOOutlet.None);
                    }

                    // MouseDrag
                    if (currentEvent.type == EventType.MouseDrag && CurrentEvent.IsType(GEType.Unresolved, GEType.Drag) && CurrentEvent.Context == GEContext.Grid) {
                        if (currentEvent.delta.magnitude > 0) {
                            CurrentEvent = new GraphEvent(GEType.Drag, GEContext.Grid, null, IOOutlet.None);
                            ScrollPoint.x += - currentEvent.delta.x;
                            ScrollPoint.y += - currentEvent.delta.y;
                            needsRepaint = true;
                        }
                    }

                    // MouseUp
                    if (currentEvent.type == EventType.MouseUp) {
                        if (CurrentEvent.Type == GEType.Unresolved && CurrentEvent.Context == GEContext.Grid) {
                            Selection.Clear();
                            needsRepaint = true;
                        }
                        needsRepaint = needsRepaint || CurrentEvent.IsConnecting();
                        CurrentEvent.Empty();
                    }

                } // Left mouse down/drag/up

                if (currentEvent.button == 1 && currentEvent.type == EventType.MouseUp) {
                    var menu = new GenericMenu();

                    var opTypes = Operator.GetAvailableOperators();
                    foreach (var opType in opTypes) {
                        var payload = new MenuActionPayload() {OperatorType=opType, Position=currentEvent.mousePosition};
                        menu.AddItem(new GUIContent(opType.Name), false, MenuAction, payload);
                    }

                    menu.ShowAsContext();
                }

            }

            if (needsRepaint) {
                Repaint();
            }

            GUI.EndScrollView();
        }
示例#2
0
		void OnGUI () {
			if (_gridRenderer == null) _gridRenderer = new GridRenderer();
			Event currentEvent = Event.current;

			Rect scrollViewRect = new Rect(0, 0, position.width - SidebarWidth, position.height);
			Rect sidebarRect = new Rect(position.width - SidebarWidth, 0, SidebarWidth, position.height);

			OperatorInspector.DrawInspector(sidebarRect);

			ScrollPoint = GUI.BeginScrollView(scrollViewRect, ScrollPoint, Canvas);

			bool needsRepaint = false;

			// Mouse wheel event to control zoom
			if (currentEvent.type == EventType.ScrollWheel) {

				float previousZoom = Zoom;
				Vector2 pivotPoint = currentEvent.mousePosition / Zoom;
				float wheelDelta = Event.current.delta.y > 0 ? -0.1f : 0.1f;

				// Apply the delta to the zoom level and clamp to min and max
				Zoom = (Zoom + wheelDelta).Clamp(MinZoom, MaxZoom);
				Zoom = (float) System.Math.Round(Zoom, 2);
				
				if (previousZoom != Zoom) {
					// Keep the viewport anchored to the position of the mouse cursor
					float zoomDelta = Zoom - previousZoom;
					ScrollPoint += pivotPoint * zoomDelta;

					// Repaint if there was a zoom change (after clamping)
					needsRepaint = true;
				}

				currentEvent.Use();
			}

			Canvas = new Rect(0f, 0f, CanvasWidth * Zoom, CanvasHeight * Zoom);

			_gridRenderer.Draw(ScrollPoint, Zoom, Canvas);

			if (Template != null) {

				// Draw the connections
				Template.DrawConnections();

				// Draw the nodes
				string[] guids = new string[Template.Operators.Count];
				Template.Operators.Keys.CopyTo(guids, 0);
				for (int i = 0; i < guids.Length; i++) {
					Node node = GetNode(guids[i]); // kvp.Key = GUID
					needsRepaint = needsRepaint || node.EventsNeedRepaint(Zoom, this);
					node.Draw(Zoom);
				}

				// Handle left mouse button events
				if (currentEvent.button == 0) {

					// MouseDown
					if (currentEvent.type == EventType.MouseDown && CurrentEvent.Type == GEType.None) {
						CurrentEvent = new GraphEvent(GEType.Unresolved, GEContext.Grid, null, IOOutlet.None, currentEvent.mousePosition);
					}

					// MouseDrag
					if (currentEvent.type == EventType.MouseDrag && CurrentEvent.IsType(GEType.Unresolved, GEType.Drag) && CurrentEvent.Context == GEContext.Grid) {
						if (currentEvent.delta.magnitude > 0) {
							CurrentEvent = new GraphEvent(GEType.Drag, GEContext.Grid, null, IOOutlet.None);
							ScrollPoint.x += - currentEvent.delta.x;
							ScrollPoint.y += - currentEvent.delta.y;
							needsRepaint = true;
						}
					}

					// MouseUp
					if (currentEvent.type == EventType.MouseUp) {
						if (CurrentEvent.Type == GEType.Unresolved && CurrentEvent.Context == GEContext.Grid) {
							Selection.Clear();
							needsRepaint = true;
						}
						needsRepaint = needsRepaint || CurrentEvent.IsConnecting();
						CurrentEvent.Empty();
					}

				} // Left mouse down/drag/up

				// Right mouse button
				if (currentEvent.type == EventType.ContextClick) {
					var menu = new GenericMenu();

					var opTypes = Operator.GetAvailableOperators();
					foreach (var opType in opTypes) {
						string menuLabel = opType.Name;
						var meta = System.Attribute.GetCustomAttribute(opType, typeof(OperatorMetadataAttribute)) as OperatorMetadataAttribute;
						if (meta != null) {
							menuLabel = meta.Category + "/" + (meta.Title != null ? meta.Title : opType.Name);
						}
						var payload = new MenuActionPayload() {OperatorType=opType, Position=currentEvent.mousePosition / Zoom};
						menu.AddItem(new GUIContent(menuLabel), false, MenuAction, payload);
					}

					menu.ShowAsContext();
				}

				if (currentEvent.isKey && currentEvent.keyCode == KeyCode.Delete) {
					foreach (var node in Selection.Nodes) {
						Template.RemoveOperator(node.Operator);
					}
					Selection.Clear();
					needsRepaint = true;
				}

			}

			if (needsRepaint) {
				Repaint();
			}

			GUI.EndScrollView();

			// Current Zoom level
			float zoomLabelWidth = 150f;
			float zoomLabelHeight = 20f;
			Rect zoomLabelRect = new Rect(position.width - SidebarWidth - zoomLabelWidth - 20f, position.height - zoomLabelHeight - 20f, zoomLabelWidth, zoomLabelHeight);
			if (LabelStyle == null) {
				LabelStyle = new GUIStyle();
				LabelStyle.normal.textColor = Color.white;
				LabelStyle.alignment = TextAnchor.LowerRight;
			}
			GUI.Label(zoomLabelRect, string.Format("Zoom: {0}%", Zoom * 100f), LabelStyle);

		} // OnGUI