-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathComboBox.cs
134 lines (112 loc) · 4.72 KB
/
ComboBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Popup list created by Eric Haines
// ComboBox Extended by Hyungseok Seo.(Jerry) sdragoon@nate.com
// this oop version of ComboBox is refactored by zhujiangbo jumbozhu@gmail.com
// Modified by MarC0 / ManlyMarco
using System;
using UnityEngine;
namespace RuntimeUnityEditor.Core.Utils
{
internal class ComboBox
{
private static bool forceToUnShow;
private static int useControlID = -1;
private readonly string boxStyle;
private readonly string buttonStyle;
private bool isClickedComboButton;
private readonly GUIContent[] listContent;
private readonly GUIStyle listStyle;
private readonly int _windowYmax;
public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, GUIStyle listStyle, float windowYmax)
{
Rect = rect;
ButtonContent = buttonContent;
this.listContent = listContent;
buttonStyle = "button";
boxStyle = "box";
this.listStyle = listStyle;
_windowYmax = (int)windowYmax;
}
public ComboBox(Rect rect, GUIContent buttonContent, GUIContent[] listContent, string buttonStyle,
string boxStyle, GUIStyle listStyle)
{
Rect = rect;
ButtonContent = buttonContent;
this.listContent = listContent;
this.buttonStyle = buttonStyle;
this.boxStyle = boxStyle;
this.listStyle = listStyle;
}
public Rect Rect { get; set; }
public GUIContent ButtonContent { get; set; }
public void Show(Action<int> onItemSelected)
{
if (forceToUnShow)
{
forceToUnShow = false;
isClickedComboButton = false;
}
var done = false;
var controlID = GUIUtility.GetControlID(FocusType.Passive);
Vector2 currentMousePosition = Vector2.zero;
if (Event.current.GetTypeForControl(controlID) == EventType.MouseUp)
{
if (isClickedComboButton)
{
done = true;
currentMousePosition = Event.current.mousePosition;
}
}
if (GUI.Button(Rect, ButtonContent, buttonStyle))
{
if (useControlID == -1)
{
useControlID = controlID;
isClickedComboButton = false;
}
if (useControlID != controlID)
{
forceToUnShow = true;
useControlID = controlID;
}
isClickedComboButton = true;
}
if (isClickedComboButton)
{
GUI.enabled = false;
GUI.color = new Color(1, 1, 1, 2);
var location = GUIUtility.GUIToScreenPoint(new Vector2(Rect.x, Rect.y + listStyle.CalcHeight(listContent[0], 1.0f)));
var size = new Vector2(Rect.width, listStyle.CalcHeight(listContent[0], 1.0f) * listContent.Length);
var innerRect = new Rect(0, 0, size.x, size.y);
var outerRectScreen = new Rect(location.x, location.y, size.x, size.y);
if (outerRectScreen.yMax > _windowYmax)
{
outerRectScreen.height = _windowYmax - outerRectScreen.y;
outerRectScreen.width += 20;
}
if (currentMousePosition != Vector2.zero && outerRectScreen.Contains(GUIUtility.GUIToScreenPoint(currentMousePosition)))
done = false;
CurrentDropdownDrawer = () =>
{
GUI.enabled = true;
var outerRectLocal = GUIUtility.ScreenToGUIRect(outerRectScreen);
GUI.Box(outerRectLocal, GUIContent.none, GUI.skin.box);
_scrollPosition = GUI.BeginScrollView(outerRectLocal, _scrollPosition, innerRect, false, false);
{
const int initialSelectedItem = -1;
var newSelectedItemIndex = GUI.SelectionGrid(innerRect, initialSelectedItem, listContent, 1, listStyle);
if (newSelectedItemIndex != initialSelectedItem)
{
onItemSelected(newSelectedItemIndex);
isClickedComboButton = false;
}
}
GUI.EndScrollView(true);
};
}
if (done)
isClickedComboButton = false;
}
private Vector2 _scrollPosition = Vector2.zero;
public static Action CurrentDropdownDrawer { get; set; }
}
}