-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLabel.cpp
92 lines (65 loc) · 2.33 KB
/
GLabel.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
#include "GLabel.h"
GLabel::GLabel(std::string ID, SDL_Renderer* renderer, SDL_Rect texture_rect)
:GElement(ID, renderer, texture_rect) {
type = "LABEL";
text = "";
this->text_rect = SDL_Rect{ 0,0,texture_rect.w, texture_rect.h };
this->active_index = 0;
texture_repository.resize(2);
}
bool GLabel::CheckEvent() {
return false;
}
void GLabel::PrepareTexture() {
if (!updated) return;
SDL_Texture* prev_target = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, element_texture);
SDL_RenderCopy(renderer, texture_repository[BG_IMAGE], 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 GLabel::SetTextRect(SDL_Rect rect){
this->text_rect = rect;
}
bool GLabel::InitializeTextures_from_stylesheet(){
if (!GElement::InitializeTextures_from_stylesheet())
return false;
CreateRoundedBox(
renderer,
texture_repository[BG_IMAGE],
0, 0, texture_rect.w, texture_rect.h,20,
this->stylesheet->background->r,
this->stylesheet->background->g,
this->stylesheet->background->b,
this->stylesheet->background->a,
SDL_BLENDMODE_BLEND
);
if (this->text != "") {
if (texture_repository[TEXT_IMAGE])SDL_DestroyTexture(this->texture_repository[TEXT_IMAGE]);
texture_repository[TEXT_IMAGE] = load_texture_from_text(renderer, stylesheet->font, this->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;
return true;
}
void GLabel::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 GLabel::GetText() {
return this->text;
}