#ifndef DROPDOWN_HPP
#define DROPDOWN_HPP
#include "raylib.h"
#include <string>
#include <vector>
class Dropdown {
private:
Rectangle bounds; std::vector<std::string> items; int selectedIndex; bool isOpen; Color boxColor; Color borderColor; Color textColor; Color hoverColor; int fontSize;
public:
Dropdown(float x, float y, float width, float height, const std::vector<std::string>& items)
: bounds{x, y, width, height}, items(items), selectedIndex(-1), isOpen(false),
boxColor(WHITE), borderColor(BLACK), textColor(BLACK), hoverColor(LIGHTGRAY), fontSize(20) {}
void SetColors(Color box, Color border, Color text, Color hover) {
boxColor = box;
borderColor = border;
textColor = text;
hoverColor = hover;
}
void SetFontSize(int size) {
fontSize = size;
}
void Update() {
Vector2 mousePos = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
if (CheckCollisionPointRec(mousePos, bounds)) {
isOpen = !isOpen;
} else if (isOpen) {
for (size_t i = 0; i < items.size(); ++i) {
Rectangle itemBounds = {bounds.x, bounds.y + bounds.height * (i + 1), bounds.width, bounds.height};
if (CheckCollisionPointRec(mousePos, itemBounds)) {
selectedIndex = static_cast<int>(i);
isOpen = false;
break;
}
}
}
}
}
void Draw() const {
DrawRectangleRec(bounds, boxColor);
DrawRectangleLinesEx(bounds, 2, borderColor);
if (selectedIndex >= 0 && selectedIndex < static_cast<int>(items.size())) {
DrawText(items[selectedIndex].c_str(), static_cast<int>(bounds.x + 5), static_cast<int>(bounds.y + (bounds.height - fontSize) / 2), fontSize, textColor);
}
DrawText("v", static_cast<int>(bounds.x + bounds.width - fontSize), static_cast<int>(bounds.y + (bounds.height - fontSize) / 2), fontSize, textColor);
if (isOpen) {
for (size_t i = 0; i < items.size(); ++i) {
Rectangle itemBounds = {bounds.x, bounds.y + bounds.height * (i + 1), bounds.width, bounds.height};
DrawRectangleRec(itemBounds, hoverColor);
DrawRectangleLinesEx(itemBounds, 2, borderColor);
DrawText(items[i].c_str(), static_cast<int>(itemBounds.x + 5), static_cast<int>(itemBounds.y + (itemBounds.height - fontSize) / 2), fontSize, textColor);
}
}
}
int GetSelectedIndex() const {
return selectedIndex;
}
std::string GetSelectedItem() const {
if (selectedIndex >= 0 && selectedIndex < static_cast<int>(items.size())) {
return items[selectedIndex];
}
return "";
}
};
#endif