forked from stephanietfong/DietaryMacroSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dropdown.h
117 lines (103 loc) · 3.03 KB
/
Dropdown.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <string>
#include "Data.h"
using namespace std;
class Dropdown {
vector<string> items;
int selectedItemIndex;
bool isExpanded;
sf::Font font;
sf::RectangleShape box;
float width, height;
int scrollOffset;
int maxDisplayItems;
public:
Dropdown(float width, float height, const vector<string>& macros) : width(width), height(height), selectedItemIndex(-1), isExpanded(false), scrollOffset(0), maxDisplayItems(3) {
box.setSize(sf::Vector2f(width, height));
box.setFillColor(sf::Color::White);
box.setOutlineColor(sf::Color::Black);
box.setOutlineThickness(1.0f);
box.setPosition(sf::Vector2f(100, 400));
if (!font.loadFromFile("font.ttf")) {
cout << "Error loading font in Dropdown!" << endl;
}
for (const auto& macro : macros) {
addItem(macro);
}
}
void addItem(const string& item) {
items.push_back(item);
}
void draw(sf::RenderWindow& window) {
window.draw(box);
sf::Text text;
text.setFont(font);
text.setCharacterSize(20);
text.setFillColor(sf::Color::Black);
if (selectedItemIndex >= 0 && !isExpanded) {
text.setString(items[selectedItemIndex]);
text.setPosition(sf::Vector2f(box.getPosition().x, box.getPosition().y + 10.5));
window.draw(text);
}
if (isExpanded) {
int displayCount = 0;
for (size_t i = scrollOffset; i < items.size() && displayCount < maxDisplayItems; ++i) {
text.setString(items[i]);
text.setPosition(box.getPosition().x, box.getPosition().y + height * (displayCount + 1));
window.draw(text);
++displayCount;
}
}
}
void clear (sf::RenderWindow& window){
window.draw(box);
selectedItemIndex = -1;
}
void handleEvent(const sf::Event& event) {
if (event.type == sf::Event::MouseButtonPressed) {
sf::Vector2f mousePos(event.mouseButton.x, event.mouseButton.y);
if (box.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y)) {
isExpanded = !isExpanded;
}
else if (isExpanded) {
float itemHeight = height;
sf::Vector2f boxPos = box.getPosition();
for (int i = 0; i < maxDisplayItems; ++i) {
sf::FloatRect itemRect(boxPos.x, boxPos.y + itemHeight * (i + 1), width, itemHeight);
if (itemRect.contains(mousePos)) {
selectedItemIndex = scrollOffset + i;
isExpanded = false;
break;
}
}
}
}
else if (event.type == sf::Event::MouseWheelScrolled && isExpanded) {
if (event.mouseWheelScroll.delta > 0) {
scrollOffset = max(0, scrollOffset - 1);
}
else if (event.mouseWheelScroll.delta < 0) {
scrollOffset = min(static_cast<int>(items.size()) - maxDisplayItems, scrollOffset + 1);
}
}
}
sf::Vector2f getPosition() const {
return box.getPosition();
}
float getWidth() const {
return box.getSize().x;
}
void setPosition(float x, float y) {
box.setPosition(x, y);
}
const std::string& getSelectedItem() const {
if (selectedItemIndex >= 0 && selectedItemIndex < items.size()) {
return items[selectedItemIndex];
}
static std::string empty = "";
return empty;
}
};