-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGButton.cpp
151 lines (118 loc) · 3.45 KB
/
GButton.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "GButton.h"
#include "Functor.h"
GButton::GButton(std::string ID, SDL_Renderer* renderer, SDL_Rect rect) : GElement(ID, renderer, rect) {
type = "BUTTON";
text = "";
this->texture_repository.resize(4);
active_index = IDLE_IMAGE;
}
bool GButton::InitializeTextures_from_stylesheet() {
if (!GElement::InitializeTextures_from_stylesheet()) return false;
CreateRectangle(
renderer,
texture_repository[CLICKED_IMAGE],
0, 0, texture_rect.w, texture_rect.h,
this->stylesheet->foreground->r,
this->stylesheet->foreground->g,
this->stylesheet->foreground->b,
this->stylesheet->foreground->a,
SDL_BLENDMODE_BLEND
);
CreateRectangle(
renderer,
texture_repository[IDLE_IMAGE],
0, 0, texture_rect.w, texture_rect.h,
this->stylesheet->background->r,
this->stylesheet->background->g,
this->stylesheet->background->b,
this->stylesheet->background->a,
SDL_BLENDMODE_BLEND
);
CreateRectangle(
renderer,
texture_repository[HIGHLIGHT_IMAGE],
0, 0, texture_rect.w, texture_rect.h,
this->stylesheet->accent->r,
this->stylesheet->accent->g,
this->stylesheet->accent->b,
this->stylesheet->accent->a,
SDL_BLENDMODE_BLEND
);
if (text != "") this->SetText(text);
updated = true;
return true;
}
void GButton::OnMouseDown_left() {
if (last_event.type != SDL_MOUSEBUTTONDOWN) GElement::OnMouseDown_left();
SDL_FlushEvent(SDL_MOUSEBUTTONDOWN);
active_index = CLICKED_IMAGE;
return;
}
void GButton::OnMouseUp() {
GElement::OnMouseUp();
SDL_FlushEvent(SDL_MOUSEBUTTONUP);
active_index = HIGHLIGHT_IMAGE;
return;
}
void GButton::OnMouseMotion_Hover() {
GElement::OnMouseMotion_Hover();
active_index = HIGHLIGHT_IMAGE;
return;
}
bool GButton::OnFocusLostMotion() {
if (!GElement::OnFocusLostMotion()) return false;
active_index = IDLE_IMAGE;
return true;
}
bool GButton::CheckEvent(SDL_Event event) {
if (!GElement::CheckEvent(event)) return false;
switch (event.type) {
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
if (CheckCursor()) {
if (event.type == SDL_MOUSEMOTION)
this->focussed_motion = true;
return true;
}
break;
case SDL_MOUSEWHEEL:
return false;
break;
default:
return false;
break;
}
return false;
}
void GButton::PrepareTexture() {
if (!updated) return;
SDL_Texture* prev_target = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, element_texture);
SDL_RenderCopy(renderer, texture_repository[active_index], NULL, &rect_maker(0, 0, texture_rect.w, texture_rect.h));
if (this->text != "")SDL_RenderCopy(renderer, texture_repository[TEXT_IMAGE], NULL, &text_rect);
SDL_SetRenderTarget(renderer, prev_target);
}
void GButton::SetText(std::string text) {
this->text = text;
if (!this->stylesheet) {
return;
}
this->text = text;
if (this->texture_repository[TEXT_IMAGE]) {
SDL_DestroyTexture(this->texture_repository[TEXT_IMAGE]);
texture_repository[TEXT_IMAGE] = NULL;
}
this->texture_repository[TEXT_IMAGE] = load_texture_from_text(renderer, this->stylesheet->font, text);
int w, h;
SDL_QueryTexture(texture_repository[TEXT_IMAGE], NULL, NULL, &w, &h);
this->text_rect = rect_maker((texture_rect.w - w) / 2, (texture_rect.h - h) / 2, w, h);
updated = true;
}
std::string GButton::GetText() {
return this->text;
}
void GButton::operator()() {
(*Function_click_left)();
return;
}