-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComboBox.cpp
246 lines (226 loc) · 4.8 KB
/
ComboBox.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "ComboBox.h"
ComboBox::ComboBox(uint32_t width, std::vector<std::string> entries) : Control(width, DEFAULT_HEIGHT * entries.size(), true, true), _selected(0), _current(0), _isOpen(false), _redrawFlag(false)
{
std::sort(entries.begin(), entries.end()); // sort by name.
for (uint32_t i = 0; i < entries.size(); ++i)
_entries.push_back(new Button(width));
_selectedControl = _entries[0];
auto _it = _entries.begin();
for (auto it : entries)
{
(*_it)->setText(it);
(*_it)->addListener(*this);
++_it;
}
}
ComboBox::~ComboBox()
{
Button* ptr;
for (auto it : _entries)
{
ptr = it;
delete ptr;
}
}
size_t ComboBox::getSelectedIndex() const
{
return _selected;
}
void ComboBox::setSelectedIndex(size_t index)
{
if (index >= _entries.size())
throw IndexOutOfBounds(); // Index is out of bounds. throw exception.
_selected = index;
_selectedControl = _entries[index];
}
void ComboBox::draw(Graphics& g, uint32_t x, uint32_t y, uint32_t z) const
{
if (getHidden()) return;
if (z <= getLayer())
{
if (_isOpen) // if ComboBox is open, draw all entries.
{
uint32_t h = this->getCoords().Y;
uint32_t counter = 0;
for (auto it : _entries)
{
if (this != Control::getFocus()) //if we lost focus, repaint control black.
{
if (it->getBackground() == Color::Blue) it->setBackground(getBackground());
it->draw(g, this->getCoords().X, h, z);
h += DEFAULT_HEIGHT;
}
else
{
if (counter == _current)
{
it->setBackground(Color::Blue); // control is focused. paint blue.
}
else
{
if (it->getBackground() == Color::Blue) it->setBackground(getBackground()); // remove blue paint from unfocused control.
}
it->draw(g, this->getCoords().X, h, z);
h += DEFAULT_HEIGHT;
counter++;
}
}
// g.moveCursor(_selectedControl->GetCoords());
}
else // ComboBox is closed. draw only selected entry.
{
if (Control::getFocus() == this)
_selectedControl->setBackground(Color::Blue);
else
_selectedControl->setBackground(getBackground());
g.setBackground(_selectedControl->getBackground());
g.setForeground(_selectedControl->getForeground());
g.write(getCoords().X, getCoords().Y, _selectedControl->getText());
}
drawBorder(g, x, y, z);
}
}
void ComboBox::MousePressed(Control& control, int x, int y, bool isLeft, Control* msCb)
{
if (_isOpen)
{
auto* ptr = dynamic_cast<Button*>(&control);
_selectedControl = ptr;
_selected = std::find(_entries.begin(), _entries.end(), ptr) - _entries.begin();
_isOpen = false;
}
else
{
openBox();
}
}
void ComboBox::mousePressed(uint32_t x, uint32_t y, bool isLeft)
{
if (getHidden()) return;
uint32_t count = 0;
for (auto it : _entries)
{
if (isInside(x, y, it->getLeft(), it->getTop(), it->getWidth(), it->getHeight()))
{
it->mousePressed(x, y, isLeft);
_selected = count;
Control::setFocus(*this);
return;
}
count++;
}
}
void ComboBox::mouseHover(uint32_t x, uint32_t y, bool isLeft)
{
uint32_t count = 0;
if (getHidden()) return;
if (!_isOpen) return;
for (auto it : _entries)
{
if (isInside(x, y, it->getLeft(), it->getTop(), it->getWidth(), it->getHeight()))
{
_current = count;
_redrawFlag = true;
Control::setFocus(*this);
return;
}
count++;
}
}
void ComboBox::keyDown(uint32_t keyCode, char character)
{
if (getHidden()) return;
switch (keyCode)
{
case VK_NUMPAD8:
case VK_UP:
if (_current > 0)
{
_current--;
Control::setFocus(*this);
}
if (!_isOpen)
openBox();
break;
case VK_NUMPAD2:
case VK_DOWN:
if (_current < _entries.size() - 1)
{
_current++;
Control::setFocus(*this);
}
if (!_isOpen)
openBox();
break;
case VK_SPACE:
case VK_RETURN:
if (_isOpen)
{
_selected = _current;
_selectedControl = _entries[_selected];
MousePressed(*_selectedControl, 0, 0, 0);
}
break;
case VK_TAB:
if (_current < _entries.size() - 1)
_current++;
break;
default:
break;
}
}
void ComboBox::setRedrawFlag(bool flag)
{
_redrawFlag = flag;
}
bool ComboBox::getRedrawFlag(void) const
{
return _redrawFlag;
}
void ComboBox::setBackground(Color color)
{
for (auto it : _entries)
{
it->setBackground(color);
}
Control::setBackground(color);
}
void ComboBox::setForeground(Color color)
{
for (auto it : _entries)
{
it->setForeground(color);
}
Control::setForeground(color);
}
void ComboBox::setCoords(uint32_t x, uint32_t y)
{
Control::setCoords(x, y);
uint32_t h = y;
for (auto it : _entries)
{
it->setCoords(x, h);
it->setHeight(1);
h += 1;
}
}
uint32_t ComboBox::getHeight() const
{
if (_isOpen)
return _entries.size() + 1;
return DEFAULT_HEIGHT;
}
void ComboBox::openBox(void)
{
for (auto it : _entries)
{
if (it == _selectedControl) continue;
it->setLayer(1);
}
setLayer(1);
_isOpen = true;
}
void ComboBox::closeBox(void)
{
_isOpen = false;
}