示例#1
0
 public void OnGUI ()
 {
     switch (Event.current.type)
     {
     case EventType.mouseDown:
         if (nodeRect.Contains (Event.current.mousePosition))
             // Select this node if we clicked it
         {
             selection = this;
             
             if (Event.current.clickCount == 2)
                 // If we double-clicked it, enter connect mode
             {
                 connecting = true;
             }
             
             Event.current.Use ();
         }
         break;
     case EventType.mouseUp:
         // If we released the mouse button...
         if (selection == null)
             // ... with no active selection, ignore the event
         {
             break;
         }
         else if (selection == this)
             // ... while this node was active selection...
         {
             if (!connecting)
                 // ... and we were not in connect mode, clear the selection
             {
                 Selection = null;
                 Event.current.Use ();
             }
         }
         else if (connecting && nodeRect.Contains (Event.current.mousePosition))
             // ... over this component while in connect mode, connect selection to this node and clear selection
         {
             selection.ConnectTo (this);
             Selection = null;
             Event.current.Use ();
         }
         break;
     case EventType.mouseDrag:
         if (selection == this)
             // If doing a mouse drag with this component selected...
         {
             if (connecting)
                 // ... and in connect mode, just use the event as we'll be painting the new connection
             {
                 Event.current.Use ();
             }
             else
                 // ... and not in connect mode, drag the component
             {
                 Position += Event.current.delta;
                 Event.current.Use ();
             }
         }
         break;
     case EventType.repaint:
         GUI.skin.box.Draw (nodeRect, new GUIContent (name), false, false, false, false);
         // The component box
         
         if (selection == this && connecting)
             // The new connection
         {
             GUI.color = Color.red;
             DrawConnection (position, Event.current.mousePosition);
             GUI.color = Color.white;
         }
         break;
     }
 }
示例#2
0
 public void ConnectTo (Node target)
 {
     if (targets.Contains (target))
     {
         return;
     }
     
     targets.Add (target);
 }