Search Unity

How to make rubber drag-and-drop list in EditorWindow Unity?

Discussion in 'General Discussion' started by ivantriumphov, Jun 19, 2020.

  1. ivantriumphov

    ivantriumphov

    Joined:
    Jun 24, 2019
    Posts:
    5
    Gui-drag-drop used this code as the basis.

    I implemented the logic of a "drag-and-drop" list.

    I have a 2-level list.

    And now the list below is limited by the size of the cell:

    How to make a cell in the drag-and-drop list be rubber? And did not violate the ability to drag list items with the mouse.
    My code is:

    DataObject.cs

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEditor;
    5. public class DataObject : GUIDraggableObject
    6. // This class just has the capability of being dragged in GUI - it could be any type of generic data class
    7. {
    8.    public string m_Name;
    9.    public int m_Value;
    10.    public int m_Processing_Order;
    11.    public int m_OrderValue;
    12.  
    13.    static internal GUIStyleState styleStateHeadlineblack = new GUIStyleState()
    14.    {
    15.        textColor = Color.white,
    16.        background = LofP.MakeTex(Screen.width, 100, Color.black)
    17.    };
    18.    static internal GUIStyle styleHeadlineblack = new GUIStyle()
    19.    {
    20.        normal = styleStateHeadlineblack,
    21.        fixedHeight = 20
    22.    };
    23.  
    24.    static internal GUIStyleState styleStateHeadlinegray = new GUIStyleState()
    25.    {
    26.        textColor = Color.white,
    27.        background = LofP.MakeTex(Screen.width, 100, Color.gray)
    28.    };
    29.    static internal GUIStyle styleHeadlinegray = new GUIStyle()
    30.    {
    31.        normal = styleStateHeadlinegray,
    32.        fixedHeight = 17
    33.    };
    34.  
    35.    internal GUIStyle style = new GUIStyle()
    36.    {
    37.        padding = new RectOffset(20, 0, 0, 0)
    38.    };
    39.  
    40.    public DataObject(string name, int value, int processing_order, Vector2 position) : base(position)
    41.    {
    42.        m_Name = name;
    43.        m_Value = value;
    44.        m_Processing_Order = processing_order;
    45.        m_OrderValue = m_Processing_Order;
    46.    }
    47.  
    48.    internal bool valueBool = false;
    49.  
    50.    public void OnGUI()
    51.    {
    52.        Rect drawRect = new Rect(m_Position.x, m_Position.y, Screen.width, 100.0f), dragRect;
    53.        EditorGUILayout.BeginVertical();
    54.  
    55.        GUILayout.BeginArea(drawRect, GUI.skin.GetStyle("Box"));
    56.        GUILayout.Label(m_Name, style, GUILayout.ExpandWidth(true));
    57.        dragRect = GUILayoutUtility.GetLastRect();
    58.        dragRect = new Rect(dragRect.x + m_Position.x, dragRect.y + m_Position.y, Screen.width, dragRect.height);
    59.  
    60.        EditorGUILayout.BeginHorizontal();
    61.        GUILayout.Label(m_Name, styleHeadlineblack);
    62.        m_OrderValue = EditorGUILayout.IntField("Order:", m_OrderValue, styleHeadlineblack);
    63.        if (Dragging)
    64.        {
    65.            GUILayout.Label("Drag to section...");
    66.        }
    67.        else if (GUILayout.Button("To apply", styleHeadlineblack, GUILayout.Width(70)))
    68.        {
    69.            int max = Main.getMaxOrder();
    70.            if (max < m_OrderValue) { m_OrderValue = max; }
    71.            Main.CountOrder(m_OrderValue, this);
    72.        }
    73.        EditorGUILayout.EndHorizontal();
    74.  
    75.        valueBool = EditorGUILayout.Foldout(valueBool, "objects");
    76.        if (valueBool)
    77.        {
    78.            for (int l = 0; l < PanoramaObjInspector.PanoramaObjList.Count;)
    79.            {
    80.                if (PanoramaObjInspector.PanoramaObjList[l].IdLayerNumber == m_Value && PanoramaObjInspector.PanoramaObjList[l].Toggle == true)
    81.                {
    82.                    goto Build;
    83.                }
    84.                else
    85.                {
    86.                    goto Next;
    87.                }
    88.            Build:
    89.                SetSection(PanoramaObjInspector.PanoramaObjList[l], l);
    90.            Next:
    91.                l++;
    92.            }
    93.        }
    94.        GUILayout.EndArea();
    95.        EditorGUILayout.EndVertical();
    96.  
    97.        Drag(dragRect);
    98.    }
    99.  
    100.    void SetSection(PanoramaObjInspector.PanoramaObj mapObj, int index)
    101.    {
    102.        EditorGUILayout.BeginVertical(style);
    103.        EditorGUILayout.BeginHorizontal();
    104.        GUILayout.Label(mapObj.Name + "(" + mapObj.Key + ")", styleHeadlinegray);
    105.  
    106.        m_OrderValue = EditorGUILayout.IntField("Order:", m_OrderValue, styleHeadlinegray);
    107.        if (Dragging)
    108.        {
    109.            GUILayout.Label("drag-and-drop...");
    110.        }
    111.        else if (GUILayout.Button("To apply", styleHeadlinegray, GUILayout.Width(70)))
    112.        {
    113.            int max = Main.getMaxOrder();
    114.            if (max < m_OrderValue) { m_OrderValue = max; }
    115.            Main.CountOrder(m_OrderValue, this);
    116.        }
    117.        EditorGUILayout.EndHorizontal();
    118.        if (GUILayout.Button("Select"))
    119.        {
    120.            PanoramaObjInspector.SelectObject(index, true);
    121.            Main.selectedTabPObj = 2;
    122.        }
    123.        EditorGUILayout.EndVertical();
    124.        LofP.HorizontalLine(Color.green);
    125.    }
    126.  
    127. }
    Main.cs

    Code (CSharp):
    1. public class BoxSection {
    2.  
    3.     public int m_Order;
    4.     public Rect m_SectionLObj;
    5.     public Color m_color;
    6.     public BoxSection(int Order,Rect SectionLObj) {
    7.         m_Order = Order;
    8.         m_SectionLObj = SectionLObj;
    9.         m_color = Color.green;
    10.     }
    11.  
    12.     public void OnGUI ()
    13.     {
    14.         //EditorGUILayout.BeginVertical();
    15.         GUI.Box(m_SectionLObj, "Order="+m_Order ,GUI.skin.GetStyle("HelpBox"));
    16.         //GUI.color = m_color;
    17.         //EditorGUILayout.EndVertical();
    18.     }
    19.  
    20. }
    21.  
    22. Vector2 _DragAndDrop;
    23.  
    24. public void DrawOnGUIDragAndDrop () {
    25.     DataObject dropSelected;
    26.     bool previousState, flipRepaint;
    27.     Color color;
    28.  
    29.     GUI.Box(HeaderSection, "Die");
    30.  
    31.     GUILayout.BeginArea (BodySection);
    32.     EditorGUILayout.BeginVertical ();
    33.  
    34.     _DragAndDrop = EditorGUILayout.BeginScrollView (_DragAndDrop, false, true, GUILayout.Width (BodySection.width), GUILayout.Height(BodySection.height));
    35.  
    36.     dropSelected = null;
    37.     // doRepaint = false;
    38.     doRepaint = flipRepaint = false;
    39.     int count = 0;
    40.     int target_Order = -1;
    41.  
    42.     foreach (var itemBoxSection in listKeyLayer)
    43.     {
    44.         itemBoxSection.Value.OnGUI ();
    45.         foreach (DataObject data in m_Data_Layer.ToList())
    46.         {
    47.  
    48.             previousState = data.Dragging;
    49.  
    50.             color = GUI.color;
    51.  
    52.             if (!data.Dragging) {
    53.                 foreach (var item in listKeyLayer)
    54.                 {
    55.                     if(data.m_Processing_Order == item.Key) {
    56.                         data.m_Position = new Vector2( item.Value.m_SectionLObj.x, item.Value.m_SectionLObj.y );
    57.                     }
    58.                 }
    59.             }
    60.  
    61.             if (previousState)
    62.             {
    63.                 foreach (var item in listKeyLayer)
    64.                 {
    65.                     if(item.Value.m_SectionLObj.Contains (Event.current.mousePosition)) {
    66.                         GUI.color = Color.red;
    67.                     } else {
    68.                         GUI.color = color;
    69.                     }
    70.                 }
    71.             }
    72.  
    73.             data.OnGUI ();
    74.  
    75.             GUI.color = color;
    76.  
    77.             if (previousState)
    78.             {
    79.                 flipRepaint = true;
    80.                 foreach (var item in listKeyLayer)
    81.                 {
    82.                     if (item.Value.m_SectionLObj.Contains (Event.current.mousePosition))
    83.                     {
    84.                         target_Order = item.Value.m_Order;
    85.                         dropSelected = data;
    86.                     }
    87.                 }
    88.             }
    89.             count++;
    90.             LofP.HorizontalLine( Color.green );
    91.         }
    92.     }
    93.     EditorGUILayout.EndScrollView();
    94.     EditorGUILayout.EndVertical ();
    95.     GUILayout.EndArea ();
    96.  
    97.     if (dropSelected != null && EventType.MouseUp == Event.current.type)
    98.     {
    99.         CountOrder (target_Order,dropSelected);
    100.         dropSelected = null;
    101.     }
    102.  
    103.     if (flipRepaint)
    104.     // If some object just stopped being dragged, we should repaing for the state change
    105.     {
    106.         Repaint ();
    107.     }
    108. }
    109. static public int getMaxOrder() {
    110.     int max =0;
    111.     for (int j = 0;j<m_Data_Layer.Count;j++) {
    112.         if(m_Data_Layer[j].m_Processing_Order>max) { max=m_Data_Layer[j].m_Processing_Order; }
    113.     }
    114.     return max;
    115. }
    116. static public void CountOrder(int targetOrder, DataObject selected) {
    117.     if (targetOrder < selected.m_Processing_Order) {
    118.         if(targetOrder+1==selected.m_Processing_Order) {
    119.             for (int j = 0;j<m_Data_Layer.Count;j++) {
    120.                 if(m_Data_Layer[j].m_Processing_Order == targetOrder) {
    121.                     m_Data_Layer[j].m_Processing_Order = targetOrder+1;
    122.                     m_Data_Layer[j].m_OrderValue = m_Data_Layer[j].m_Processing_Order;
    123.                 } else if(m_Data_Layer[j].m_Processing_Order == targetOrder+1) {
    124.                     m_Data_Layer[j].m_Processing_Order = targetOrder;
    125.                     m_Data_Layer[j].m_OrderValue = m_Data_Layer[j].m_Processing_Order;
    126.                 }
    127.             }
    128.         } else {
    129.             m_Data_Layer.Remove (selected);
    130.             for (int i = selected.m_Processing_Order;i>=targetOrder;i--) {
    131.                 for (int j = 0;j<m_Data_Layer.Count;j++) {
    132.                     if(m_Data_Layer[j].m_Processing_Order == i) {
    133.                         m_Data_Layer[i].m_Processing_Order = m_Data_Layer[j].m_Processing_Order+1;
    134.                         m_Data_Layer[j].m_OrderValue = m_Data_Layer[j].m_Processing_Order;
    135.                     }
    136.                 }
    137.             }
    138.             selected.m_Processing_Order = targetOrder;
    139.             selected.m_OrderValue = selected.m_Processing_Order;
    140.             m_Data_Layer.Add (selected);
    141.         }
    142.     } else if (targetOrder > selected.m_Processing_Order) {
    143.         if(targetOrder-1==selected.m_Processing_Order) {
    144.             for (int j = 0;j<m_Data_Layer.Count;j++) {
    145.                 if(m_Data_Layer[j].m_Processing_Order == targetOrder-1) {
    146.                     m_Data_Layer[j].m_Processing_Order = targetOrder;
    147.                     m_Data_Layer[j].m_OrderValue = m_Data_Layer[j].m_Processing_Order;
    148.                 } else if(m_Data_Layer[j].m_Processing_Order == targetOrder) {
    149.                     m_Data_Layer[j].m_Processing_Order = targetOrder-1;
    150.                     m_Data_Layer[j].m_OrderValue = m_Data_Layer[j].m_Processing_Order;
    151.                 }
    152.             }
    153.         } else {
    154.             m_Data_Layer.Remove (selected);
    155.             for (int i = selected.m_Processing_Order; i<=targetOrder; i++) {
    156.                 for (int j = 0;j<m_Data_Layer.Count;j++) {
    157.                     if(m_Data_Layer[j].m_Processing_Order == i) {
    158.                         m_Data_Layer[j].m_Processing_Order = m_Data_Layer[j].m_Processing_Order-1;
    159.                         m_Data_Layer[j].m_OrderValue = m_Data_Layer[j].m_Processing_Order;
    160.                     }
    161.                 }
    162.             }
    163.             selected.m_Processing_Order = targetOrder;
    164.             selected.m_OrderValue = selected.m_Processing_Order;
    165.             m_Data_Layer.Add (selected);
    166.         }
    167.     }
    168. }
    GUIDraggableObject.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GUIDraggableObject
    5. {
    6.     public Vector2 m_Position;
    7.     private Vector2 m_DragStart;
    8.     private bool m_Dragging;
    9.  
    10.     public GUIDraggableObject(Vector2 position)
    11.     {
    12.         m_Position = position;
    13.     }
    14.  
    15.     public bool Dragging
    16.     {
    17.         get
    18.         {
    19.             return m_Dragging;
    20.         }
    21.     }
    22.  
    23.     public Vector2 Position
    24.     {
    25.         get
    26.         {
    27.             return m_Position;
    28.         }
    29.  
    30.         set
    31.         {
    32.             m_Position = value;
    33.         }
    34.     }
    35.  
    36.     public void Drag(Rect draggingRect)
    37.     {
    38.         if (Event.current.type == EventType.MouseUp)
    39.         {
    40.             m_Dragging = false;
    41.         }
    42.         else if (Event.current.type == EventType.MouseDown && draggingRect.Contains(Event.current.mousePosition))
    43.         {
    44.             m_Dragging = true;
    45.             m_DragStart = Event.current.mousePosition - m_Position;
    46.             Event.current.Use();
    47.         }
    48.  
    49.         if (m_Dragging)
    50.         {
    51.             m_Position = Event.current.mousePosition - m_DragStart;
    52.         }
    53.     }
    54. }