TDME2 1.9.121
GUISliderHController.cpp
Go to the documentation of this file.
2
3#include <array>
4#include <string>
5
6#include <tdme/tdme.h>
13#include <tdme/gui/GUI.h>
14#include <tdme/math/Math.h>
15#include <tdme/math/Vector2.h>
18#include <tdme/utilities/Time.h>
19
20using std::array;
21using std::to_string;
22
29using tdme::gui::GUI;
35
37
38GUISliderHController::GUISliderHController(GUINode* node): GUIElementController(node)
39{
40 // TODO: maybe introduce a custom relative node alignment, I use content alignment for now
41}
42
44 return false;
45}
46
48
49}
50
52 sliderNode = this->node->getScreenNode()->getNodeById(this->node->getId() + "_slider");
54 setValue(MutableString(required_dynamic_cast<GUIElementNode*>(node)->getValue()));
55 GUIElementController::initialize();
56}
57
59 GUIElementController::dispose();
60}
61
64}
65
67 GUIElementController::handleMouseEvent(node, event);
68 Vector2 nodeMousePosition;
69 if (node == this->node &&
70 event->getType() == GUIMouseEvent::MOUSEEVENT_RELEASED == true) {
71 this->node->getScreenNode()->getGUI()->setFoccussedNode(required_dynamic_cast<GUIElementNode*>(this->node));
72 event->setProcessed(true);
73 } else
74 if (event->getButton() == MOUSE_BUTTON_LEFT &&
75 ((node == this->node && event->getType() == GUIMouseEvent::MOUSEEVENT_PRESSED == true && node->isEventBelongingToNode(event) == true) ||
76 event->getType() == GUIMouseEvent::MOUSEEVENT_DRAGGED == true)) {
77 event->setProcessed(true);
78 node->getEventNodePosition(event, nodeMousePosition);
79 auto sliderPosition = Math::clamp(
80 static_cast<int>(nodeMousePosition[0]) - sliderNode->getContentWidth() / 2 - this->node->getPadding().left,
81 0,
83 );
84 valueFloat = static_cast<float>(sliderPosition) / static_cast<float>(sliderParentNode->getComputedConstraints().width - sliderParentNode->getPadding().left - sliderParentNode->getPadding().right - sliderNode->getContentWidth());
86 node->getScreenNode()->delegateValueChanged(required_dynamic_cast<GUIElementNode*>(this->node));
87 }
88}
89
91 GUIElementController::handleKeyboardEvent(event);
92 switch (event->getKeyCode()) {
93 case GUIKeyboardEvent::KEYCODE_LEFT: {
94 event->setProcessed(true);
95 if (event->getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_PRESSED) {
96 this->valueFloat = Math::clamp(valueFloat - 0.1f, 0.0f, 1.0f);
98 node->getScreenNode()->delegateValueChanged(required_dynamic_cast<GUIElementNode*>(this->node));
99 }
100 }
101 break;
102 case GUIKeyboardEvent::KEYCODE_RIGHT: {
103 event->setProcessed(true);
104 if (event->getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_PRESSED) {
105 this->valueFloat = Math::clamp(valueFloat + 0.1f, 0.0f, 1.0f);
106 updateSlider();
107 node->getScreenNode()->delegateValueChanged(required_dynamic_cast<GUIElementNode*>(this->node));
108 }
109 }
110 break;
111 }
112}
113
115 GUIElementController::tick();
116}
117
119}
120
122}
123
125 return true;
126}
127
129 return value.set(valueFloat, 4);
130}
131
133 this->value.set(value);
134 this->valueFloat = Math::clamp(Float::parse(this->value.getString()), 0.0f, 1.0f);
135 updateSlider();
136}
137
142}
#define MOUSE_BUTTON_LEFT
GUI module class.
Definition: GUI.h:66
void setFoccussedNode(GUIElementNode *newFoccussedNode)
Set focussed node.
Definition: GUI.cpp:359
GUI horizontal slider controller.
void onFocusGained() override
On focus gained.
void dispose() override
Dispose controller.
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.
void tick() override
Tick method will be executed once per frame.
void setDisabled(bool disabled) override
Set disabled.
const MutableString & getValue() override
GUI node base class.
Definition: GUINode.h:63
virtual int getContentHeight()=0
GUINode_Padding & getPadding()
Definition: GUINode.h:373
virtual int getContentWidth()=0
const string & getId()
Definition: GUINode.h:329
bool isEventBelongingToNode(GUIMouseEvent *event, Vector2 &position)
Is event belonging to node.
Definition: GUINode.cpp:973
GUINode_ComputedConstraints & getComputedConstraints()
Definition: GUINode.h:387
GUIScreenNode * getScreenNode()
Definition: GUINode.h:315
void getEventNodePosition(GUIMouseEvent *event, Vector2 &position)
Get event position clamped to node constraints TODO: use Vector2 instead of array<float,...
Definition: GUINode.cpp:1040
GUIParentNode * getParentNode()
Definition: GUINode.h:322
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.
Standard math functions.
Definition: Math.h:21
2D vector 2 class
Definition: Vector2.h:19
Float class.
Definition: Float.h:23
Mutable string class.
Definition: MutableString.h:16
MutableString & set(char c)
Set character.
const string & getString() const
Time utility class.
Definition: Time.h:21