Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Window Drawing Help

Discussion in 'Immediate Mode GUI (IMGUI)' started by Zalosath, Mar 4, 2018.

  1. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    Hi there, I can't seem to get to grips with the UnityEditor.
    Nothing seems to make sense,
    What I want is a draggable box that I can link to other boxes, each box will have two text fields, but I do not know how I would go about doing this. I have the basic nodes set up (The boxes) but I can't figure out how to, firstly, put a text field in them, and secondly, how to know if they are linked.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System;
    6. using System.Reflection;
    7. using System.Text;
    8.  
    9. public class NodeEditor  : EditorWindow
    10. {
    11.     static NodeEditor  window;
    12.  
    13.     public Rect window1, window2, _handleArea;
    14.     private bool _nodeOption, _options, _handleActive, _action;
    15.     private Texture2D _resizeHandle, _aaLine;
    16.     private GUIContent _icon;
    17.     private float _winMinX, _winMinY;
    18.     private int _mainwindowID;
    19.  
    20.     [MenuItem("Window/Node Editor")]
    21.     static void Init()
    22.     {
    23.         window = (NodeEditor )EditorWindow.GetWindow(typeof(NodeEditor));
    24.         window.title = "Node Editor";
    25.         window.ShowNodes();
    26.     }
    27.  
    28.     private void ShowNodes()
    29.     {
    30.         _winMinX = 100f;
    31.         _winMinY = 100f;
    32.         window1 = new Rect(30, 30, _winMinX, _winMinY);
    33.         window2 = new Rect(210, 210, _winMinX, _winMinY);
    34.  
    35.         _resizeHandle = AssetDatabase.LoadAssetAtPath("Assets/Textures/ResizeHandle.png", typeof(Texture2D)) as Texture2D;
    36.         _aaLine = AssetDatabase.LoadAssetAtPath("Assets/Textures/AA1x5.png", typeof(Texture2D)) as Texture2D;
    37.         _icon = new GUIContent(_resizeHandle);
    38.         _mainwindowID = GUIUtility.GetControlID(FocusType.Passive); //grab primary editor window controlID
    39.     }
    40.  
    41.     void OnGUI()
    42.     {
    43.         BeginWindows();
    44.         window1 = GUI.Window(1, window1, DrawNodeWindow, "Window 1");   // Updates the Rect's when these are dragged
    45.         window2 = GUI.Window(2, window2, DrawNodeWindow, "Window 2");
    46.         EndWindows();
    47.  
    48.         DrawNodeCurve(window1, window2);
    49.  
    50.         GUILayout.BeginHorizontal(EditorStyles.toolbar);
    51.         _options = GUILayout.Toggle(_options, "Toggle Me", EditorStyles.toolbarButton);
    52.         GUILayout.FlexibleSpace();
    53.         GUILayout.EndHorizontal();
    54.  
    55.         //if drag extends inner window bounds _handleActive remains true as event gets lost to parent window
    56.         if ((Event.current.rawType == EventType.MouseUp) && (GUIUtility.hotControl != _mainwindowID))
    57.         {
    58.             GUIUtility.hotControl = 0;
    59.         }
    60.     }
    61.  
    62.     private void DrawNodeWindow(int id)
    63.     {
    64.         if (GUIUtility.hotControl == 0)  //mouseup event outside parent window?
    65.         {
    66.             _handleActive = false; //make sure handle is deactivated
    67.         }
    68.  
    69.         float _cornerX = 0f;
    70.         float _cornerY = 0f;
    71.         switch (id) //case which window this is and nab size info
    72.         {
    73.         case 1:
    74.             _cornerX = window1.width;
    75.             _cornerY = window1.height;
    76.             break;
    77.         case 2:
    78.             _cornerX = window2.width;
    79.             _cornerY = window2.height;
    80.             break;
    81.         }
    82.  
    83.         //begin layout of contents
    84.         GUILayout.BeginArea(new Rect(1, 16, _cornerX - 3, _cornerY - 1));
    85.         GUILayout.BeginHorizontal(EditorStyles.toolbar);
    86.         _nodeOption = GUILayout.Toggle(_nodeOption, "Node Toggle", EditorStyles.toolbarButton);
    87.         GUILayout.FlexibleSpace();
    88.         GUILayout.EndHorizontal();
    89.         GUILayout.EndArea();
    90.  
    91.         GUILayout.BeginArea(new Rect(1, _cornerY - 16, _cornerX - 3, 14));
    92.         GUILayout.BeginHorizontal(EditorStyles.toolbarTextField, GUILayout.ExpandWidth(true));
    93.         GUILayout.FlexibleSpace();
    94.  
    95.         //grab corner area based on content reference
    96.         _handleArea = GUILayoutUtility.GetRect(_icon, GUIStyle.none);
    97.         GUI.DrawTexture(new Rect(_handleArea.xMin + 6, _handleArea.yMin - 3, 20, 20), _resizeHandle); //hacky placement
    98.         _action = (Event.current.type == EventType.MouseDown) || (Event.current.type == EventType.MouseDrag);
    99.         if (!_handleActive && _action)
    100.         {
    101.             if (_handleArea.Contains(Event.current.mousePosition, true))
    102.             {
    103.                 _handleActive = true; //active when cursor is in contact area
    104.                 GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive); //set handle hot
    105.             }
    106.         }
    107.  
    108.         EditorGUIUtility.AddCursorRect(_handleArea, MouseCursor.ResizeUpLeft);
    109.         GUILayout.EndHorizontal();
    110.         GUILayout.EndArea();
    111.  
    112.         //resize window
    113.         if (_handleActive && (Event.current.type == EventType.MouseDrag))
    114.         {
    115.             ResizeNode(id, Event.current.delta.x, Event.current.delta.y);
    116.             Repaint();
    117.             Event.current.Use();
    118.         }
    119.  
    120.         //enable drag for node
    121.         if (!_handleActive)
    122.         {
    123.             GUI.DragWindow();
    124.         }
    125.     }
    126.  
    127.     private void ResizeNode(int id, float deltaX, float deltaY)
    128.     {
    129.         switch (id)
    130.         {
    131.         case 1:
    132.             if ((window1.width + deltaX) > _winMinX) { window1.xMax += deltaX; }
    133.             if ((window1.height + deltaY) > _winMinY) { window1.yMax += deltaY; }
    134.             break;
    135.         case 2:
    136.             if ((window2.width + deltaX) > _winMinX) { window2.xMax += deltaX; }
    137.             if ((window2.height + deltaY) > _winMinY) { window2.yMax += deltaY; }
    138.             break;
    139.         }
    140.     }
    141.  
    142.     void DrawNodeCurve(Rect start, Rect end)
    143.     {
    144.         Vector3 startPos = new Vector3(start.x + start.width, start.y + start.height / 2, 0);
    145.         Vector3 endPos = new Vector3(end.x, end.y + end.height / 2, 0);
    146.         Vector3 startTan = startPos + Vector3.right * 50;
    147.         Vector3 endTan = endPos + Vector3.left * 50;
    148.         Handles.DrawBezier(startPos, endPos, startTan, endTan, Color.black, _aaLine, 1.5f);
    149.     }
    150. }