TDME2 1.9.121
GUIDropDownController.cpp
Go to the documentation of this file.
2
3#include <string>
4
5#include <tdme/tdme.h>
16#include <tdme/gui/GUI.h>
19
20using std::string;
21using std::to_string;
22
34using tdme::gui::GUI;
37
38string GUIDropDownController::CONDITION_DISABLED = "disabled";
39string GUIDropDownController::CONDITION_ENABLED = "enabled";
40string GUIDropDownController::CONDITION_OPENED = "opened";
41string GUIDropDownController::CONDITION_CLOSED = "closed";
42
43GUIDropDownController::GUIDropDownController(GUINode* node)
45{
46 open = false;
47 this->disabled = required_dynamic_cast<GUIElementNode*>(node)->isDisabled();
48}
49
51{
52 return disabled;
53}
54
56{
57 auto& nodeConditions = required_dynamic_cast<GUIElementNode*>(node)->getActiveConditions();
58 auto& nodeConditionsTextElement = textElementNode->getActiveConditions();
59 nodeConditions.remove(this->disabled == true?CONDITION_DISABLED:CONDITION_ENABLED);
60 nodeConditionsTextElement.remove(this->disabled == true?CONDITION_DISABLED:CONDITION_ENABLED);
61 this->disabled = disabled;
62 nodeConditions.add(this->disabled == true?CONDITION_DISABLED:CONDITION_ENABLED);
63 nodeConditionsTextElement.add(this->disabled == true ?CONDITION_DISABLED:CONDITION_ENABLED);
64 if (disabled == true && isOpen() == true) {
66 }
67}
68
70{
71 dropDownContainerNode = required_dynamic_cast<GUIParentNode*>(node->getScreenNode()->getInnerNodeById(node->getId()));
72 arrowNode = required_dynamic_cast<GUIElementNode*>(node->getScreenNode()->getNodeById(node->getId() + "_arrow"));
73 textElementNode = required_dynamic_cast<GUIElementNode*>(node->getScreenNode()->getNodeById(node->getId() + "_layout_horizontal_element"));
74 required_dynamic_cast<GUIElementNode*>(node)->getActiveConditions().add(open == true?CONDITION_OPENED:CONDITION_CLOSED);
76
77 //
78 GUIElementController::initialize();
79
80 //
82}
83
85{
86 GUIElementController::dispose();
87}
88
90{
91}
92
94{
95 return open;
96}
97
99{
100 if (selectedDropDownOptionControllerIdx == -1) return;
102}
103
105 unselect();
107 if (selectedDropDownOptionControllerIdx == -1) return;
111}
112
113void GUIDropDownController::select(GUIElementNode* selectedDropDownOptionElementNode) {
114 unselect();
115 auto selectedDropDownOptionElementController = selectedDropDownOptionElementNode->getController();
116 auto i = 0;
117 for (auto dropDownOptionController: dropDownOptionControllers) {
118 if (dropDownOptionController == selectedDropDownOptionElementController) {
119 select(i);
120 break;
121 }
122 i++;
123 }
124}
125
127{
128 if (open == false) {
130 }
131 required_dynamic_cast<GUIElementNode*>(node)->getActiveConditions().remove(open == true?CONDITION_OPENED:CONDITION_CLOSED);
133 open = open == true?false:true;
134 required_dynamic_cast<GUIElementNode*>(node)->getActiveConditions().add(open == true?CONDITION_OPENED:CONDITION_CLOSED);
136 search.clear();
137 doSearch();
138}
139
141{
142 vector<GUINode*> childControllerNodes;
144 required_dynamic_cast<GUIParentNode*>(node)->getChildControllerNodes(childControllerNodes);
145 for (auto i = 0; i < childControllerNodes.size(); i++) {
146 auto childControllerNode = childControllerNodes[i];
147 auto childController = dynamic_cast<GUIDropDownOptionController*>(childControllerNode->getController());
148 if (childController != nullptr) dropDownOptionControllers.push_back(childController);
149 }
150}
151
153{
154 unselect();
155 if (dropDownOptionControllers.size() == 0)
156 return;
157
158 auto disabledCount = 0;
159 while (disabledCount < dropDownOptionControllers.size()) {
163 disabledCount++;
164 }
165 if (disabledCount == dropDownOptionControllers.size()) {
167 return;
168 }
169
173}
174
176{
177 unselect();
178 if (dropDownOptionControllers.size() == 0)
179 return;
180
182
183 auto disabledCount = 0;
184 while (disabledCount < dropDownOptionControllers.size()) {
190 disabledCount++;
191 }
192 if (disabledCount == dropDownOptionControllers.size()) {
194 return;
195 }
196
197
201}
202
204{
207 return;
208 }
209 unselect();
210
214}
215
218 string selectedDropDownOptionText;
220 selectedDropDownOptionText = required_dynamic_cast<GUITextNode*>(node->getScreenNode()->getNodeById(dropDownOptionControllers[selectedDropDownOptionControllerIdx]->getNode()->getId() + "_unselected"))->getText().getString();
221 }
222 auto dropDownTextNodeEnabled = required_dynamic_cast<GUITextNode*>(node->getScreenNode()->getNodeById(node->getId() + "_text_enabled"));
223 dropDownTextNodeEnabled->setText(search.empty() == false?MutableString(search):MutableString(selectedDropDownOptionText));
224 auto dropDownTextNodeDisabled = required_dynamic_cast<GUITextNode*>(node->getScreenNode()->getNodeById(node->getId() + "_text_disabled"));
225 dropDownTextNodeDisabled->setText(search.empty() == false?MutableString(search):MutableString(selectedDropDownOptionText));
226 if (dropDownOptionControllers.size() > 0) {
227 auto scrollToDropDownOption = -1;
228 auto searchLowerCase = StringTools::toLowerCase(search);
229 auto i = 0;
230 for (auto dropDownOptionController: dropDownOptionControllers) {
231 auto match = dropDownOptionController->search(searchLowerCase);
232 if (match == true && (scrollToDropDownOption == -1 || i == lastSelectedDropDownOptionControllerIdx)) scrollToDropDownOption = i;
233 i++;
234 }
235 if (scrollToDropDownOption != -1) {
236 dropDownOptionControllers[scrollToDropDownOption]->getNode()->scrollToNodeX(dropDownContainerNode);
237 dropDownOptionControllers[scrollToDropDownOption]->getNode()->scrollToNodeY(dropDownContainerNode);
238 }
239 }
240}
241
243{
244 GUIElementController::handleMouseEvent(node, event);
245 if (disabled == true) return;
246 auto elementNode = required_dynamic_cast<GUIElementNode*>(this->node);
247 if (event->getButton() == MOUSE_BUTTON_LEFT) {
248 if (node == this->node && node->isEventBelongingToNode(event) == true) {
249 if (event->getType() == GUIMouseEvent::MOUSEEVENT_RELEASED) {
250 event->setProcessed(true);
252 node->getScreenNode()->getGUI()->setFoccussedNode(elementNode);
253 }
254 } else {
255 if (open == true) {
256 auto innerNode = this->node->getScreenNode()->getNodeById(this->node->getId() + "_inner");
257 if (node == this->node && innerNode->isEventBelongingToNode(event) == false) {
258 event->setProcessed(true);
260 }
261 }
262 }
263 }
264}
265
267{
268 GUIElementController::handleKeyboardEvent(event);
269 if (disabled == false) {
270 auto keyChar = event->getKeyChar();
271 if (keyChar > 32 && keyChar < 127 && keyChar >= 32) {
272 event->setProcessed(true);
273 if (event->getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_TYPED) {
274 if (isOpen() == false) toggleOpenState();
275 search+= event->getKeyChar();
276 doSearch();
277 }
278 } else {
279 switch (event->getKeyCode()) {
280 case GUIKeyboardEvent::KEYCODE_ESCAPE:
281 if (event->getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_PRESSED) {
282 if (isOpen() == true) {
283 selectLast();
285 }
286 }
287 break;
288 case GUIKeyboardEvent::KEYCODE_BACKSPACE:
289 case GUIKeyboardEvent::KEYCODE_DELETE: {
290 if (event->getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_PRESSED) {
291 search.clear();
292 }
293 if (isOpen() == false) toggleOpenState();
294 break;
295 case GUIKeyboardEvent::KEYCODE_UP: {
296 event->setProcessed(true);
297 if (event->getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_PRESSED) {
298 if (isOpen() == false) {
300 } else {
302 }
303 }
304 }
305 break;
306 case GUIKeyboardEvent::KEYCODE_DOWN: {
307 event->setProcessed(true);
308 if (event->getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_PRESSED) {
309 if (isOpen() == false) {
311 } else {
312 selectNext();
313 }
314 }
315 }
316 break;
317 case GUIKeyboardEvent::KEYCODE_SPACE: {
318 event->setProcessed(true);
319 if (event->getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_PRESSED) {
321 }
322 if (open == false) {
323 node->getScreenNode()->delegateValueChanged(required_dynamic_cast<GUIElementNode*>(node));
324 }
325 }
326 break;
327 }
328 }
329 }
330 }
331}
332
334{
335 GUIElementController::tick();
336 if (open == true) node->getScreenNode()->getGUI()->addMouseOutClickCandidateElementNode(required_dynamic_cast<GUIElementNode*>(this->node));
337}
338
340{
341}
342
344{
345 if (isOpen() == true) {
347 }
348}
349
351{
352 return true;
353}
354
356{
357 value.reset();
359 return value;
360}
361
363{
364 unselect();
365 auto i = 0;
366 for (auto dropDownOptionController: dropDownOptionControllers) {
367 auto dropDownOptionNode = required_dynamic_cast<GUIElementNode*>(dropDownOptionController->getNode());
368 if (value.equals(dropDownOptionNode->getValue())) {
369 select(i);
370 break;
371 }
372 i++;
373 }
374}
375
379 auto i = 0;
380 for (auto dropDownOptionController: dropDownOptionControllers) {
381 auto dropDownOptionNode = required_dynamic_cast<GUIElementNode*>(dropDownOptionController->getNode());
382 dropDownOptionController->unselect();
383 if (dropDownOptionNode->isSelected() == true) {
385 break;
386 }
387 i++;
388 }
391 }
392}
#define MOUSE_BUTTON_LEFT
GUI module class.
Definition: GUI.h:66
void addMouseOutClickCandidateElementNode(GUINode *node)
Add node that is a possible mouse click out candidate as it received a mouse click.
Definition: GUI.h:118
void setFoccussedNode(GUIElementNode *newFoccussedNode)
Set focussed node.
Definition: GUI.cpp:359
void dispose() override
Dispose controller.
static STATIC_DLL_IMPEXT string CONDITION_CLOSED
void postLayout() override
Post layout event.
void initialize() override
Initialize controller after element has been created.
void handleKeyboardEvent(GUIKeyboardEvent *event) override
Handle keyboard event.
void setValue(const MutableString &value) override
Set value.
void handleMouseEvent(GUINode *node, GUIMouseEvent *event) override
Handle mouse event.
vector< GUIDropDownOptionController * > dropDownOptionControllers
void tick() override
Tick method will be executed once per frame.
void determineDropDownOptionControllers()
Determine drop down option controllers.
static STATIC_DLL_IMPEXT string CONDITION_DISABLED
static STATIC_DLL_IMPEXT string CONDITION_ENABLED
void setDisabled(bool disabled) override
Set disabled.
void onSubTreeChange() override
On sub tree change.
static STATIC_DLL_IMPEXT string CONDITION_OPENED
GUINodeConditions & getActiveConditions()
GUI element node conditions.
bool add(const string &condition)
Add a condition.
bool remove(const string &condition)
Remove a condition.
GUI node base class.
Definition: GUINode.h:63
const string & getId()
Definition: GUINode.h:329
bool isEventBelongingToNode(GUIMouseEvent *event, Vector2 &position)
Is event belonging to node.
Definition: GUINode.cpp:973
GUIScreenNode * getScreenNode()
Definition: GUINode.h:315
GUINodeController * getController()
Definition: GUINode.h:586
GUI parent node base class thats supporting child nodes.
Definition: GUIParentNode.h:43
GUI screen node that represents a screen that can be rendered via GUI system.
Definition: GUIScreenNode.h:57
GUINode * getNodeById(const string &nodeId)
Get GUI node by id.
void delegateValueChanged(GUIElementNode *node)
Delegate value changed.
GUINode * getInnerNodeById(const string &nodeId)
Get inner GUI node by id.
Mutable string class.
Definition: MutableString.h:16
MutableString & append(char c)
Append character.
MutableString & reset()
Reset.
bool equals(const string &s2) const
Equals.
String tools class.
Definition: StringTools.h:20