summaryrefslogtreecommitdiff
path: root/Runtime/IMGUI/IDList.cpp
blob: 896c49898ab4d76576936dcd9253af4af4b817b0 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "UnityPrefix.h"
#include "Runtime/IMGUI/IDList.h"
#include "Runtime/IMGUI/GUIState.h"
#include "Runtime/Misc/InputEvent.h"

IDList::IDList ()
{
	m_Idx = 0;
}

int IDList::CalculateNextFromHintList (GUIState &state, int hint, bool isKeyboard)
{
	int retval = 0;
	// Ok - we're searching: start at idx and search to end.
	for (int searchIdx = m_Idx; searchIdx < m_IDs.size(); searchIdx++)
	{
		if (m_IDs[searchIdx].hint == hint)
		{
			m_Idx = searchIdx + 1;
			retval = m_IDs[searchIdx].value; 
			break;
		}
	}
	
	// We still couldn't find it, so we just add to end...
	if (retval == 0)
	{
		retval = state.m_EternalGUIState->GetNextUniqueID();
		m_IDs.push_back (ID (hint, retval, isKeyboard));
		m_Idx = m_IDs.size();
	}
	
	return retval;
}

void IDList::BeginOnGUI () 
{
	m_Idx = 0;
	m_FirstKeyControl = -1;
	m_LastKeyControl = -1;
	m_PreviousKeyControl = -1;	
	m_NextKeyControl = -1;
	m_HasKeyboardControl = false;
	m_TabControlSearchStatus = kLookingForPrevious;
}

int IDList::GetNext (GUIState &state, int hint, FocusType focusType, const Rectf &rect)
{
	int retval = GetNext (state, hint, focusType);
	if (state.m_CurrentEvent->type != InputEvent::kLayout && state.m_CurrentEvent->type != InputEvent::kUsed && ShouldBeKeyboardControl(focusType))
	{
		Assert (m_Idx > 0);
		m_IDs[m_Idx-1].rect = rect;
	}
	return retval;
}

int IDList::GetNext (GUIState &state, int hint, FocusType focusType)
{
	InputEvent::Type type = state.m_CurrentEvent->type;

	bool isKeyboard = ShouldBeKeyboardControl(focusType);
	int retval = 0;
	if (type != InputEvent::kUsed)
		retval = CalculateNextFromHintList(state, hint, isKeyboard);
	else
	{
		return -1;
	}
	
	if (type != InputEvent::kLayout) 
	{
		if (type == InputEvent::kKeyDown && state.m_OnGUIState.m_Enabled == (int)true)
		{
			if (isKeyboard)
			{
				switch (m_TabControlSearchStatus)
				{
					case kNotActive:
						break;
					case kLookingForPrevious:
						if (m_FirstKeyControl == -1)
							m_FirstKeyControl = retval;
						if (retval != state.m_MultiFrameGUIState.m_KeyboardControl)
							m_PreviousKeyControl = retval;
						else
						{
							m_TabControlSearchStatus = kLookingForNext;
							m_HasKeyboardControl = true;
						}
						break;
					case kLookingForNext:
						m_NextKeyControl = retval;
						m_TabControlSearchStatus = kFound;
						break;
					default:
						break;
				}
				m_LastKeyControl = retval;				
			}
		}			
	}
	return retval;
}

bool IDList::GetRectOfControl (int id, Rectf &out) const
{
	for (dynamic_array<ID>::const_iterator i = m_IDs.begin(); i != m_IDs.end(); i++)
	{
		if (i->value == id && i->rect.width != -1.0f)
		{
			out = i->rect;
			return true;
		}
	}
	return false;
}

bool IDList::CanHaveKeyboardFocus (int id) const
{
	for (dynamic_array<ID>::const_iterator i = m_IDs.begin(); i != m_IDs.end(); i++)
	{
		if (i->value == id)
		{
			return i->isKeyboard;
		}
	}
	return false;
}

bool IDList::ShouldBeKeyboardControl (FocusType focus) {
	switch (focus) {
		case kPassive:
			return false;
		case kKeyboard:
			return true;
		case kNative:
			return false;
			// TODO: Move this back in during 2.x when we accept keyboard input on the various GUI controls.
			/*			PlatformSelection platform = GUI.skin.settings.keyboardFocus;
			 if (platform == PlatformSelection.Native) {
			 if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer || Application.platform == RuntimePlatform.WindowsEditor)
			 platform = PlatformSelection.Windows;
			 else
			 platform = PlatformSelection.Mac;
			 }
			 return platform == PlatformSelection.Windows;
			 */		}
	return true;
}

void IDList::SetSearchIndex (int index)
{
	if (index >= 0 && index < m_IDs.size())
		m_Idx = index;
	else
		AssertString (Format("Invalid index %d (size is %zd)", index, m_IDs.size()));
}

int IDList::GetSearchIndex () const
{
	return m_Idx;
}