SUNENGINE 0.0.2
A simple and bright C++ game engine.
 
Loading...
Searching...
No Matches
Dropdown.h
1#ifndef DROPDOWN_H
2#define DROPDOWN_H
3
4#include "UIComponent.h"
5#include "UIDirection.h"
6#include <vector>
7#include <string>
8
9class Button;
10class UICommand;
12
13class Dropdown : public UIComponent
14{
15public:
16 Dropdown(const std::string& textureKey, const std::vector<std::string>& optionLabels);
17 Dropdown(const std::string& textureKey);
18 Dropdown(const Dropdown&) = delete;
19 Dropdown& operator=(const Dropdown&) = delete;
20 ~Dropdown();
21
28 void AddOption(UIComponent* comp, const std::string& value, UIDirection dir = UIDirection::Down);
29
35 void SetPosition(float x, float y);
36
42 void SetScale(float x, float y);
43
48 void SetLayer(int layer) override;
49
55 void SetOnSelectCommand(int index, UICommand* cmd);
56
61 Button* GetMainButton() const;
62
67 UIComponent* GetComponent(int index) const;
68
73 const std::string& GetSelectedValue() const;
74
75private:
76 friend class UIDropdownCommand;
77
79 UIDropdownCommand* selectCommand;
80 std::vector<UIComponent*> optionItems;
81 std::vector<std::string> optionValues;
82 std::vector<UIDirection> optionDirs;
83 std::vector<UICommand*> ownedCommands;
85
86 float posX;
87 float posY;
88 float scaleX;
89 float scaleY;
90
91 float heldX;
92 float heldY;
93
94 std::string selectedValue;
95
99 void Render() override;
100
104 void OnPress() override;
105
112 bool IsMouseOver(float x, float y) override;
113
117 void ToggleDropdown();
118
123 void SelectOption(int index);
124
129
136 UIComponent* GetHoveredItem(float x, float y);
137
138};
139
140#endif
A clickable UI button component.
Definition Button.h:30
std::vector< UIDirection > optionDirs
Directions for positioning each option.
Definition Dropdown.h:82
void SetScale(float x, float y)
Sets the scale of the dropdown.
Definition Dropdown.cpp:119
void ToggleDropdown()
Toggles the expanded/collapsed state of the dropdown.
Definition Dropdown.cpp:186
float scaleY
Y scale factor.
Definition Dropdown.h:89
UIComponent * GetHoveredItem(float x, float y)
Gets the option item currently hovered by the mouse.
Definition Dropdown.cpp:292
void Render() override
Renders the dropdown and its options.
Definition Dropdown.cpp:146
float posX
X position of the dropdown.
Definition Dropdown.h:86
void SetLayer(int layer) override
Sets the layer of the dropdown for rendering.
Definition Dropdown.cpp:138
float heldX
Last held/clicked X coordinate.
Definition Dropdown.h:91
void OnPress() override
Handles the press event on the dropdown.
Definition Dropdown.cpp:156
float heldY
Last held/clicked Y coordinate.
Definition Dropdown.h:92
std::vector< UIComponent * > optionItems
List of option UI components.
Definition Dropdown.h:80
void AddOption(UIComponent *comp, const std::string &value, UIDirection dir=UIDirection::Down)
Adds an option to the dropdown.
Definition Dropdown.cpp:43
std::vector< std::string > optionValues
Values associated with each option.
Definition Dropdown.h:81
float scaleX
X scale factor.
Definition Dropdown.h:88
std::string selectedValue
The currently selected value.
Definition Dropdown.h:94
const std::string & GetSelectedValue() const
Gets the currently selected value.
Definition Dropdown.cpp:106
Button * GetMainButton() const
Gets the main button of the dropdown.
Definition Dropdown.cpp:74
float posY
Y position of the dropdown.
Definition Dropdown.h:87
void SetOnSelectCommand(int index, UICommand *cmd)
Sets the command to be executed when an option is selected.
Definition Dropdown.cpp:86
void UpdateOptionTransforms()
Updates the transforms (position/scale) of the dropdown options.
Definition Dropdown.cpp:200
Button * mainButton
The main button that toggles the dropdown.
Definition Dropdown.h:78
UIDropdownCommand * selectCommand
Command for handling selection/toggle.
Definition Dropdown.h:79
bool IsMouseOver(float x, float y) override
Checks if the mouse is over the dropdown or its options.
Definition Dropdown.cpp:181
void SelectOption(int index)
Selects the option at the given index.
Definition Dropdown.cpp:191
UIComponent * GetComponent(int index) const
Gets the component at the specified index in the Dropdown.
Definition Dropdown.cpp:79
std::vector< UICommand * > ownedCommands
Commands owned and managed by the dropdown.
Definition Dropdown.h:83
bool isExpanded
Whether the dropdown is currently expanded.
Definition Dropdown.h:84
void SetPosition(float x, float y)
Sets the position of the dropdown.
Definition Dropdown.cpp:112
Abstract base class for UI-related command objects.
Definition UICommand.h:32
Abstract base class for all UI components.
Definition UIComponent.h:32
Command class for handling dropdown selection events.
Definition UIDropdownCommand.h:23