-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonGUI.cpp
More file actions
64 lines (57 loc) · 2.46 KB
/
Copy pathButtonGUI.cpp
File metadata and controls
64 lines (57 loc) · 2.46 KB
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
#include "include/ButtonGUI.hpp"
#include "include/raylib/raylib.h"
ButtonGUI::ButtonGUI(int buttonSize, int iconSize, int buttonMargin, int screenHalfway) {
ButtonGUI::buttonSize = buttonSize;
ButtonGUI::iconSize = iconSize;
ButtonGUI::buttonMargin = buttonMargin;
ButtonGUI::screenHalfway = screenHalfway;
}
ButtonGUI::~ButtonGUI() {
for (int i = 0; i < iconTextures.size(); i++) {
UnloadTexture(iconTextures[i]);
}
}
void ButtonGUI::addButton(std::string name, bool* buttonBool) {
buttons.push_back({name, buttonBool});
Image iconIMG = LoadImage((std::string("icons/") + name + std::string(".png")).c_str());
ImageResize(&iconIMG, iconSize, iconSize);
iconTextures.push_back(LoadTextureFromImage(iconIMG));
UnloadImage(iconIMG);
}
void ButtonGUI::updateButtons() {
float totalSize = buttonSize * buttons.size() +
buttonMargin * (buttons.size() + 1);
for (int i = 0; i < buttons.size(); i++) {
float x = 20.f + (float)buttonMargin/2;
float y = (float)(screenHalfway - totalSize/2 + buttonMargin + (buttonSize + buttonMargin) * i);
Rectangle buttonRec = (Rectangle){x, y, (float)buttonSize, (float)buttonSize};
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(GetMousePosition(), buttonRec)) {
for (int j = 0; j < buttons.size(); j++) {
*buttons[j].second = (i==j);
}
}
}
}
Rectangle ButtonGUI::getTotalRectangle() {
float totalSize = buttonSize * buttons.size() +
buttonMargin * (buttons.size() + 1);
return (Rectangle){15.f, (float)(screenHalfway - totalSize/2), (float)(buttonSize + buttonMargin) + 10, totalSize};
}
void ButtonGUI::render() {
float totalSize = buttonSize * buttons.size() +
buttonMargin * (buttons.size() + 1);
DrawRectangleRounded(
getTotalRectangle(),
0.8, 20, (Color){130,130,130,255});
for (int i = 0; i < buttons.size(); i++) {
float x = 20.f + (float)buttonMargin/2;
float y = (float)(screenHalfway - totalSize/2 + buttonMargin + (buttonSize + buttonMargin) * i);
Rectangle buttonRec = (Rectangle){x, y, (float)buttonSize, (float)buttonSize};
DrawRectangleRounded(buttonRec, 0.8, 20, *buttons[i].second ? (Color){75,75,75,255} : (Color){100,100,100,255});
if (*buttons[i].second || CheckCollisionPointRec(GetMousePosition(), buttonRec)) {
DrawRectangleRoundedLinesEx(
(Rectangle){x, y, (float)buttonSize, (float)buttonSize}, 0.8, 20, 2, BLACK);
}
DrawTexture(iconTextures[i], x + (float)buttonSize / 2 - (float)iconSize / 2, y + (float)buttonSize / 2 - (float)iconSize / 2, WHITE);
}
}