Player12
An open source football management simulator
Loading...
Searching...
No Matches
dropdown.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <SDL3/SDL.h>
12#include <SDL3_ttf/SDL_ttf.h>
13
14#include <functional>
15#include <string>
16#include <vector>
17
22{
23 SDL_Color backgroundColor = {60, 60, 60, 255};
24 SDL_Color borderColor = {100, 100, 100, 255};
25 SDL_Color textColor = {255, 255, 255, 255};
26 SDL_Color hoverColor = {80, 80, 80, 255};
27 SDL_Color arrowColor = {200, 200, 200, 255};
28};
29
34{
35 public:
43 Dropdown(SDL_Renderer* dropdown_renderer, TTF_Font* dropdown_font,
44 const SDL_FRect& rect,
45 const std::vector<std::string>& dropdown_options);
46
50 ~Dropdown();
51
52 // Disable copy and move to prevent accidental memory issues
53 Dropdown(const Dropdown&) = delete;
54 Dropdown& operator=(const Dropdown&) = delete;
55 Dropdown(Dropdown&&) = delete;
57
63 bool handleEvent(const SDL_Event& event);
64
68 void render();
69
74 void update(float deltaTime);
75
79 void close();
80
85 bool isDropdownOpen() const;
86
91 int getSelectedIndex() const;
92
97 std::string getSelectedOption() const;
98
103 void setSelectedIndex(int index);
104
110 std::function<void(int, const std::string&)> callback);
111
112 private:
113 void createTextures();
114 void freeTextures();
115 void renderArrow(const SDL_FRect& buttonRect) const;
116
117 SDL_Renderer* renderer;
118 TTF_Font* font;
119 SDL_FRect mainRect;
120 std::vector<std::string> options;
121 std::vector<SDL_FRect> optionRects;
123
124 bool isOpen = false;
126 int hoveredIndex = -1;
127
128 SDL_Texture* selectedOptionTexture = nullptr;
129 std::vector<SDL_Texture*> optionTextures;
130
131 std::function<void(int, const std::string&)> onSelectionChangedCallback;
132};
Represents a UI dropdown menu.
Definition dropdown.h:34
Dropdown & operator=(Dropdown &&)=delete
DropdownStyle style
Definition dropdown.h:122
void renderArrow(const SDL_FRect &buttonRect) const
Definition dropdown.cpp:239
std::vector< std::string > options
Definition dropdown.h:120
void render()
Renders the dropdown.
Definition dropdown.cpp:90
void setOnSelectionChanged(std::function< void(int, const std::string &)> callback)
Sets the callback invoked when the selection changes.
Definition dropdown.cpp:186
bool isDropdownOpen() const
Checks if the dropdown is currently open.
Definition dropdown.cpp:36
Dropdown(Dropdown &&)=delete
std::vector< SDL_Texture * > optionTextures
Definition dropdown.h:129
void freeTextures()
Definition dropdown.cpp:221
~Dropdown()
Destroys the Dropdown.
Definition dropdown.cpp:32
void setSelectedIndex(int index)
Sets the selected option by index.
Definition dropdown.cpp:158
int selectedIndex
Definition dropdown.h:125
TTF_Font * font
Definition dropdown.h:118
SDL_FRect mainRect
Definition dropdown.h:119
void update(float deltaTime)
Updates the dropdown logic.
Definition dropdown.cpp:145
bool handleEvent(const SDL_Event &event)
Handles SDL events for the dropdown.
Definition dropdown.cpp:38
SDL_Renderer * renderer
Definition dropdown.h:117
int getSelectedIndex() const
Gets the index of the currently selected option.
Definition dropdown.cpp:147
SDL_Texture * selectedOptionTexture
Definition dropdown.h:128
Dropdown & operator=(const Dropdown &)=delete
Dropdown(const Dropdown &)=delete
void createTextures()
Definition dropdown.cpp:192
int hoveredIndex
Definition dropdown.h:126
void close()
Closes the dropdown if it is open.
Definition dropdown.cpp:34
std::string getSelectedOption() const
Gets the text of the currently selected option.
Definition dropdown.cpp:149
std::vector< SDL_FRect > optionRects
Definition dropdown.h:121
std::function< void(int, const std::string &)> onSelectionChangedCallback
Definition dropdown.h:131
bool isOpen
Definition dropdown.h:124
int index
Definition runtime_editor.cpp:94
Style configuration for a dropdown.
Definition dropdown.h:22
SDL_Color arrowColor
Definition dropdown.h:27
SDL_Color borderColor
Definition dropdown.h:24
SDL_Color backgroundColor
Definition dropdown.h:23
SDL_Color hoverColor
Definition dropdown.h:26
SDL_Color textColor
Definition dropdown.h:25