-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComboBox.cs
More file actions
88 lines (73 loc) · 3 KB
/
ComboBox.cs
File metadata and controls
88 lines (73 loc) · 3 KB
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
// from http://wiki.unity3d.com/index.php?title=PopupList
// and some changes to integrate it in config window
using System;
using UnityEngine;
public class ComboBox
{
private static bool forceToUnShow;
private static int useControlID = -1;
private bool isClickedComboButton;
private Vector2 scrollVector;
private int selectedItemIndex;
public int List(string buttonText, GUIContent[] listContent, GUIStyle listStyle, int defaultSelectedItemIndex)
{
return List(new GUIContent(buttonText), listContent, "button", "box", listStyle, defaultSelectedItemIndex);
}
public int List(GUIContent buttonContent, GUIContent[] listContent, GUIStyle buttonStyle, GUIStyle boxStyle, GUIStyle listStyle, int defaultSelectedItemIndex)
{
selectedItemIndex = defaultSelectedItemIndex;
if (forceToUnShow)
{
forceToUnShow = false;
isClickedComboButton = false;
}
bool done = false;
int controlID = GUIUtility.GetControlID(FocusType.Passive);
switch (Event.current.GetTypeForControl(controlID))
{
case EventType.mouseUp:
if (isClickedComboButton)
done = true;
break;
}
if (GUILayout.Button(buttonContent, buttonStyle))
{
if (useControlID == -1)
{
useControlID = controlID;
isClickedComboButton = false;
}
if (useControlID != controlID)
{
forceToUnShow = true;
useControlID = controlID;
}
isClickedComboButton = true;
}
if (isClickedComboButton)
{
Rect rect = GUILayoutUtility.GetLastRect();
Rect listRect = new Rect(rect.x, rect.y + listStyle.CalcHeight(listContent[0], 1.0f), rect.width - 20, listStyle.CalcHeight(listContent[0], 1.0f)*listContent.Length);
Rect viewtRect = new Rect(rect.x, rect.y + listStyle.CalcHeight(listContent[0], 1.0f), rect.width, listStyle.CalcHeight(listContent[0], 1.0f)*Math.Min(4, listContent.Length));
scrollVector = GUI.BeginScrollView(viewtRect, scrollVector, listRect, false, true);
//GUI.Box(listRect, "", boxStyle);
int newSelectedItemIndex = GUI.SelectionGrid(listRect, selectedItemIndex, listContent, 1, listStyle);
GUI.EndScrollView(true);
if (done)
{
Vector2 mousePosition = Event.current.mousePosition;
if (!listRect.Contains(mousePosition) && viewtRect.Contains(mousePosition))
done = false;
else
selectedItemIndex = newSelectedItemIndex;
}
}
if (done)
isClickedComboButton = false;
return GetSelectedItemIndex();
}
public int GetSelectedItemIndex()
{
return selectedItemIndex;
}
}