TDME2 1.9.121
GUISliderVController.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
38GUISliderVController::GUISliderVController(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");
53 setValue(MutableString(required_dynamic_cast<GUIElementNode*>(node)->getValue()));
54
55 //
56 GUIElementController::initialize();
57}
58
60 GUIElementController::dispose();
61}
62
65}
66
68 GUIElementController::handleMouseEvent(node, event);
69 Vector2 nodeMousePosition;
70 if (node == this->node &&
71 event->getType() == GUIMouseEvent::MOUSEEVENT_RELEASED == true) {
72 this->node->getScreenNode()->getGUI()->setFoccussedNode(required_dynamic_cast<GUIElementNode*>(this->node));
73 event->setProcessed(true);
74 } else
75 if (event->getButton() == MOUSE_BUTTON_LEFT &&
76 ((node == this->node && event->getType() == GUIMouseEvent::MOUSEEVENT_PRESSED == true && node->isEventBelongingToNode(event) == true) ||
77 event->getType() == GUIMouseEvent::MOUSEEVENT_DRAGGED == true)) {
78 event->setProcessed(true);
79 node->getEventNodePosition(event, nodeMousePosition);
80 auto sliderPosition = Math::clamp(
81 static_cast<int>(nodeMousePosition[1]) - sliderNode->getContentHeight() / 2 - this->node->getPadding().top,
82 0,
83 this->node->getComputedConstraints().height - this->node->getPadding().top - this->node->getPadding().bottom - sliderNode->getContentHeight()
84 );
85 valueFloat = 1.0f - static_cast<float>(sliderPosition) / static_cast<float>(this->node->getComputedConstraints().height - this->node->getPadding().top - this->node->getPadding().bottom - sliderNode->getContentHeight());
87 node->getScreenNode()->delegateValueChanged(required_dynamic_cast<GUIElementNode*>(this->node));
88 }
89}
90
92 GUIElementController::handleKeyboardEvent(event);
93 switch (event->getKeyCode()) {
94 case GUIKeyboardEvent::KEYCODE_UP: {
95 event->setProcessed(true);
96 if (event->getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_PRESSED) {
97 this->valueFloat = Math::clamp(valueFloat + 0.1f, 0.0f, 1.0f);
99 node->getScreenNode()->delegateValueChanged(required_dynamic_cast<GUIElementNode*>(this->node));
100 }
101 }
102 break;
103 case GUIKeyboardEvent::KEYCODE_DOWN: {
104 event->setProcessed(true);
105 if (event->getType() == GUIKeyboardEvent::KEYBOARDEVENT_KEY_PRESSED) {
106 this->valueFloat = Math::clamp(valueFloat - 0.1f, 0.0f, 1.0f);
107 updateSlider();
108 node->getScreenNode()->delegateValueChanged(required_dynamic_cast<GUIElementNode*>(this->node));
109 }
110 }
111 break;
112 }
113}
114
116 GUIElementController::tick();
117}
118
120}
121
123}
124
126 return true;
127}
128
130 return value.set(valueFloat, 4);
131}
132
134 this->value.set(value);
135 this->valueFloat = Math::clamp(Float::parse(this->value.getString()), 0.0f, 1.0f);
136 updateSlider();
137}
138
140 auto sliderPosition = static_cast<int>((1.0f - valueFloat) * (this->node->getComputedConstraints().height - this->node->getPadding().top - this->node->getPadding().bottom - sliderNode->getContentHeight()));
143}
#define MOUSE_BUTTON_LEFT
GUI module class.
Definition: GUI.h:66
void setFoccussedNode(GUIElementNode *newFoccussedNode)
Set focussed node.
Definition: GUI.cpp:359
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
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