/46ac05b2146b3be59c
Created 1 year, 1 month ago...
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 |
118 |
119 |
120 |
121 |
122 |
123 |
#include <iostream>
#include <functional>
#include <string>
#include "include/ui.hpp"
using namespace std;
Button::Button(SDL_Renderer *renderer, const string custom_id, const char* staticImage, const char* readyImage, const char* clickedImage, tuple<int, int> xy) {
buttonTextureStatic = IMG_LoadTexture(renderer, staticImage);
buttonTextureReady = IMG_LoadTexture(renderer, readyImage);
buttonTexturePressed = IMG_LoadTexture(renderer, clickedImage);
// If failed to load button textures
if (!buttonTextureStatic || !buttonTextureReady || !buttonTexturePressed) {
cerr << "Failed to load button textures for " << custom_id << endl;
return;
}
int x, y;
buttonRect.x = get<0>(xy);
buttonRect.y = get<1>(xy);
SDL_QueryTexture(buttonTextureStatic, NULL, NULL, &buttonRect.w, &buttonRect.h);
on_ready = false;
is_clicked = false;
onClickFunction = nullptr;
offset_x_t = 0;
offset_y_t = 0;
offset_w_t = 0;
offset_h_t = 0;
}
void Button::handleEvent(SDL_Event &event) {
int ox, oy;
SDL_GetMouseState(&ox, &oy);
// If window loses focus
if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_LEAVE) {
is_clicked = false;
on_ready = false;
}
}
if (event.type == SDL_MOUSEMOTION) {
// if button in focus
if (ox >= (buttonRect.x + offset_x_t) && ox <= buttonRect.x + (buttonRect.w + offset_w_t) && oy >= (buttonRect.y + offset_y_t) && oy <= buttonRect.y + (buttonRect.h + offset_h_t)) {
if (is_clicked == false) {
on_ready = true;
}
}
// if button out of focus
else {
if (is_clicked == false) {
on_ready = false;
}
}
}
if (event.type == SDL_MOUSEBUTTONDOWN) {
int x, y;
SDL_GetMouseState(&x, &y);
// if button pressed
if (x >= (buttonRect.x + offset_x_t) && x <= buttonRect.x + (buttonRect.w + offset_w_t) && y >= (buttonRect.y + offset_y_t) && y <= buttonRect.y + (buttonRect.h + offset_h_t)) {
is_clicked = true;
if (onClickFunction)
onClickFunction();
}
}
// if left mouse button released
else if (event.type == SDL_MOUSEBUTTONUP) {
int x, y;
SDL_GetMouseState(&x, &y);
is_clicked = false;
if (x >= (buttonRect.x + offset_x_t) && x <= buttonRect.x + (buttonRect.w + offset_w_t) && y >= (buttonRect.y + offset_y_t) && y <= buttonRect.y + (buttonRect.h + offset_h_t)) {
cout << "pressed" << endl;
on_ready = true;
}
}
}
void Button::render(SDL_Renderer *renderer) {
// Rendering buttons
if (is_clicked == true){
SDL_RenderCopy(renderer, buttonTexturePressed, NULL, &buttonRect);
} else if (on_ready == true) {
SDL_RenderCopy(renderer, buttonTextureReady, NULL, &buttonRect);
} else {
SDL_RenderCopy(renderer, buttonTextureStatic, NULL, &buttonRect);
}
}
void Button::setOriginImage(const string& origin) {
// Previous coordinates to save
int prevX = buttonRect.x;
int prevY = buttonRect.y;
// Correction of coordinates depending on the origin
if (origin == "top-left") { buttonRect.x = prevX; buttonRect.y = prevY; }
else if (origin == "top-center") { buttonRect.x = prevX - buttonRect.w / 2; buttonRect.y = prevY; }
else if (origin == "top-right") { buttonRect.x = prevX - buttonRect.w; buttonRect.y = prevY; }
else if (origin == "middle-left") { buttonRect.x = prevX; buttonRect.y = prevY - buttonRect.h / 2; }
else if (origin == "middle-center") { buttonRect.x = prevX - buttonRect.w / 2; buttonRect.y = prevY - buttonRect.h / 2; }
else if (origin == "middle-right") { buttonRect.x = prevX - buttonRect.w; buttonRect.y = prevY - buttonRect.h / 2; }
else if (origin == "bottom-left") { buttonRect.x = prevX; buttonRect.y = prevY - buttonRect.h; }
else if (origin == "bottom-center") { buttonRect.x = prevX - buttonRect.w / 2; buttonRect.y = prevY - buttonRect.h; }
else if (origin == "bottom-right") { buttonRect.x = prevX - buttonRect.w; buttonRect.y = prevY - buttonRect.h; }
}
Button::~Button() {
// Destroy all textures
SDL_DestroyTexture(buttonTextureStatic);
SDL_DestroyTexture(buttonTextureReady);
SDL_DestroyTexture(buttonTexturePressed);
}