-
Notifications
You must be signed in to change notification settings - Fork 914
Expand file tree
/
Copy pathComponentList.cpp
More file actions
341 lines (283 loc) · 8.38 KB
/
Copy pathComponentList.cpp
File metadata and controls
341 lines (283 loc) · 8.38 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "components/ComponentList.h"
#include "Util.h"
#include "Log.h"
#include <string.h>
#define TOTAL_HORIZONTAL_PADDING_PX 20
ComponentList::ComponentList(Window* window) : IList<ComponentListRow, void*>(window, LIST_SCROLL_STYLE_SLOW, LIST_NEVER_LOOP)
{
mSelectorBarOffset = 0;
mCameraOffset = 0;
mFocused = false;
}
void ComponentList::addRow(const ComponentListRow& row, bool setCursorHere)
{
IList<ComponentListRow, void*>::Entry e;
e.name = "";
e.object = NULL;
e.data = row;
this->add(e);
for(auto it = mEntries.back().data.elements.begin(); it != mEntries.back().data.elements.end(); it++)
addChild(it->component.get());
updateElementSize(mEntries.back().data);
updateElementPosition(mEntries.back().data);
if(setCursorHere)
{
mCursor = mEntries.size() - 1;
onCursorChanged(CURSOR_STOPPED);
}
}
void ComponentList::onSizeChanged()
{
for(auto it = mEntries.begin(); it != mEntries.end(); it++)
{
updateElementSize(it->data);
updateElementPosition(it->data);
}
updateCameraOffset();
}
void ComponentList::onFocusLost()
{
mFocused = false;
}
void ComponentList::onFocusGained()
{
mFocused = true;
}
bool ComponentList::input(InputConfig* config, Input input)
{
if(size() == 0)
return false;
// give it to the current row's input handler
if(mEntries.at(mCursor).data.input_handler)
{
if(mEntries.at(mCursor).data.input_handler(config, input))
return true;
}else{
// no input handler assigned, do the default, which is to give it to the rightmost element in the row
auto& row = mEntries.at(mCursor).data;
if(row.elements.size())
{
if(row.elements.back().component->input(config, input))
return true;
}
}
// input handler didn't consume the input - try to scroll
if(config->isMappedTo("up", input))
{
return listInput(input.value != 0 ? -1 : 0);
}else if(config->isMappedTo("down", input))
{
return listInput(input.value != 0 ? 1 : 0);
}else if(config->isMappedTo("pageup", input))
{
return listInput(input.value != 0 ? -7 : 0);
}else if(config->isMappedTo("pagedown", input)){
return listInput(input.value != 0 ? 7 : 0);
}
return false;
}
void ComponentList::update(int deltaTime)
{
listUpdate(deltaTime);
if(size())
{
// update our currently selected row
for(auto it = mEntries.at(mCursor).data.elements.begin(); it != mEntries.at(mCursor).data.elements.end(); it++)
it->component->update(deltaTime);
}
}
void ComponentList::onCursorChanged(const CursorState& state)
{
// update the selector bar position
// in the future this might be animated
mSelectorBarOffset = 0;
for(int i = 0; i < mCursor; i++)
{
mSelectorBarOffset += getRowHeight(mEntries.at(i).data);
}
updateCameraOffset();
// this is terribly inefficient but we don't know what we came from so...
if(size())
{
for(auto it = mEntries.begin(); it != mEntries.end(); it++)
it->data.elements.back().component->onFocusLost();
mEntries.at(mCursor).data.elements.back().component->onFocusGained();
}
if(mCursorChangedCallback)
mCursorChangedCallback(state);
updateHelpPrompts();
}
void ComponentList::updateCameraOffset()
{
// move the camera to scroll
const float totalHeight = getTotalRowHeight();
if(totalHeight > mSize.y())
{
float target = mSelectorBarOffset + getRowHeight(mEntries.at(mCursor).data)/2 - (mSize.y() / 2);
// clamp it
mCameraOffset = 0;
unsigned int i = 0;
while(mCameraOffset < target && i < mEntries.size())
{
mCameraOffset += getRowHeight(mEntries.at(i).data);
i++;
}
if(mCameraOffset < 0)
mCameraOffset = 0;
else if(mCameraOffset + mSize.y() > totalHeight)
mCameraOffset = totalHeight - mSize.y();
}else{
mCameraOffset = 0;
}
}
void ComponentList::render(const Eigen::Affine3f& parentTrans)
{
if(!size())
return;
Eigen::Affine3f trans = roundMatrix(parentTrans * getTransform());
// clip everything to be inside our bounds
Eigen::Vector3f dim(mSize.x(), mSize.y(), 0);
dim = trans * dim - trans.translation();
Renderer::pushClipRect(Eigen::Vector2i((int)trans.translation().x(), (int)trans.translation().y()),
Eigen::Vector2i((int)round(dim.x()), (int)round(dim.y() + 1)));
// scroll the camera
trans.translate(Eigen::Vector3f(0, -round(mCameraOffset), 0));
// draw our entries
std::vector<GuiComponent*> drawAfterCursor;
bool drawAll;
for(unsigned int i = 0; i < mEntries.size(); i++)
{
auto& entry = mEntries.at(i);
drawAll = !mFocused || i != mCursor;
for(auto it = entry.data.elements.begin(); it != entry.data.elements.end(); it++)
{
if(drawAll || it->invert_when_selected)
{
it->component->render(trans);
}else{
drawAfterCursor.push_back(it->component.get());
}
}
}
// custom rendering
Renderer::setMatrix(trans);
// draw selector bar
if(mFocused)
{
// inversion: src * (1 - dst) + dst * 0 = where src = 1
// need a function that goes roughly 0x777777 -> 0xFFFFFF
// and 0xFFFFFF -> 0x777777
// (1 - dst) + 0x77
const float selectedRowHeight = getRowHeight(mEntries.at(mCursor).data);
Renderer::drawRect(0.0f, mSelectorBarOffset, mSize.x(), selectedRowHeight, 0xFFFFFFFF,
GL_ONE_MINUS_DST_COLOR, GL_ZERO);
Renderer::drawRect(0.0f, mSelectorBarOffset, mSize.x(), selectedRowHeight, 0x777777FF,
GL_ONE, GL_ONE);
// hack to draw 2px dark on left/right of the bar
Renderer::drawRect(0.0f, mSelectorBarOffset, 2.0f, selectedRowHeight, 0x878787FF);
Renderer::drawRect(mSize.x() - 2.0f, mSelectorBarOffset, 2.0f, selectedRowHeight, 0x878787FF);
for(auto it = drawAfterCursor.begin(); it != drawAfterCursor.end(); it++)
(*it)->render(trans);
// reset matrix if one of these components changed it
if(drawAfterCursor.size())
Renderer::setMatrix(trans);
}
// draw separators
float y = 0;
for(unsigned int i = 0; i < mEntries.size(); i++)
{
Renderer::drawRect(0.0f, y, mSize.x(), 1.0f, 0xC6C7C6FF);
y += getRowHeight(mEntries.at(i).data);
}
Renderer::drawRect(0.0f, y, mSize.x(), 1.0f, 0xC6C7C6FF);
Renderer::popClipRect();
}
float ComponentList::getRowHeight(const ComponentListRow& row) const
{
// returns the highest component height found in the row
float height = 0;
for(unsigned int i = 0; i < row.elements.size(); i++)
{
if(row.elements.at(i).component->getSize().y() > height)
height = row.elements.at(i).component->getSize().y();
}
return height;
}
float ComponentList::getTotalRowHeight() const
{
float height = 0;
for(auto it = mEntries.begin(); it != mEntries.end(); it++)
{
height += getRowHeight(it->data);
}
return height;
}
void ComponentList::updateElementPosition(const ComponentListRow& row)
{
float yOffset = 0;
for(auto it = mEntries.begin(); it != mEntries.end() && &it->data != &row; it++)
{
yOffset += getRowHeight(it->data);
}
// assumes updateElementSize has already been called
float rowHeight = getRowHeight(row);
float x = TOTAL_HORIZONTAL_PADDING_PX / 2;
for(unsigned int i = 0; i < row.elements.size(); i++)
{
const auto comp = row.elements.at(i).component;
// center vertically
comp->setPosition(x, (rowHeight - comp->getSize().y()) / 2 + yOffset);
x += comp->getSize().x();
}
}
void ComponentList::updateElementSize(const ComponentListRow& row)
{
float width = mSize.x() - TOTAL_HORIZONTAL_PADDING_PX;
std::vector< std::shared_ptr<GuiComponent> > resizeVec;
for(auto it = row.elements.begin(); it != row.elements.end(); it++)
{
if(it->resize_width)
resizeVec.push_back(it->component);
else
width -= it->component->getSize().x();
}
// redistribute the "unused" width equally among the components with resize_width set to true
width = width / resizeVec.size();
for(auto it = resizeVec.begin(); it != resizeVec.end(); it++)
{
(*it)->setSize(width, (*it)->getSize().y());
}
}
void ComponentList::textInput(const char* text)
{
if(!size())
return;
mEntries.at(mCursor).data.elements.back().component->textInput(text);
}
std::vector<HelpPrompt> ComponentList::getHelpPrompts()
{
if(!size())
return std::vector<HelpPrompt>();
std::vector<HelpPrompt> prompts = mEntries.at(mCursor).data.elements.back().component->getHelpPrompts();
if(size() > 1)
{
bool addMovePrompt = true;
for(auto it = prompts.begin(); it != prompts.end(); it++)
{
if(strcmp((it->first) , "up/down") == 0 || strcmp((it->first) , "up/down/left/right") == 0)
{
addMovePrompt = false;
break;
}
}
if(addMovePrompt)
prompts.push_back(HelpPrompt("up/down", "choose"));
}
return prompts;
}
bool ComponentList::moveCursor(int amt)
{
bool ret = listInput(amt);
listInput(0);
return ret;
}