-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.cpp
227 lines (203 loc) · 6.69 KB
/
ui.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "ui.hpp"
namespace TerraNova {
/*void UIElement::AddValue(const int val){
values.push_back(val);
}*/
/*void UIElement::SetValue(const unsigned int entry, const int newVal){
if(entry >= values.size()){
std::cout << "Attempted to set a UI element's invalid value " << entry
<< " to " << newVal << "." << std::endl;
} else {
values[entry] = newVal;
}
}*/
/*int UIElement::Value(const unsigned int entry) const{
if(entry >= values.size()){
std::cout << "Attempted to access a UI element's invalid value "
<< entry << "." << std::endl;
} else {
return values[entry];
}
return 0;
}*/
void UIElement::AddText(const std::string& text, const SDL_Rect newLayout,
TTF_Font* font, const textcolor_t color){
textLayout = newLayout;
textLayout.x += layout.x;
textLayout.y += layout.y;
if(textLayout.w == 0) textLayout.w = layout.w;
if(textLayout.h == 0) textLayout.h = layout.h;
textLayout.x -= textLayout.w/2;
textLayout.y -= textLayout.h/2;
SetText(text, font, color);
/* std::cout << "Text \"" << text << "\" added at position (" << textLayout.x
<< "," << textLayout.y << ")." << std::endl;*/
}
void UIElement::SetText(const std::string& text, TTF_Font* font,
const textcolor_t color){
if(font == nullptr) font = defaultFont;
SDL_Color colorCode = Sprite::SDLifyTextColor(color);
/*switch(color){
case BLACK: colorCode.r = 0;
colorCode.g = 0;
colorCode.b = 0;
colorCode.a = 255;
break;
default: colorCode.r = 0;
colorCode.g = 0;
colorCode.b = 0;
colorCode.a = 255;
break;
}*/
/* std::cout << "Text \"" << text << "\" set at position (" << textLayout.x
<< "," << textLayout.y << ")." << std::endl;*/
textLayout.x += textLayout.w/2;
textLayout.y += textLayout.h/2;
// setting width and height so textSprite knows how wide the box is
//if(textLayout.w == 0) textLayout.w = layout.w;
//if(textLayout.h == 0) textLayout.h = layout.h;
textSprite = std::make_unique<Sprite>(ren, text, textLayout, colorCode, font);
if(!textSprite) std::cout << "Error constructing textSprite!" << std::endl;
textSprite->MakeDefaultSize(textLayout);
textLayout.x -= textLayout.w/2;
textLayout.y -= textLayout.h/2;
}
// WARNING: you have to make sure the source outlives this UI element!
void UIElement::AddDynamicText(const int& source, const int x, const int y,
TTF_Font* font, const textcolor_t color){
dynamicTextLayout.x = layout.x + x;
dynamicTextLayout.y = layout.y + y;
dynamicTextLayout.w = 0;
dynamicTextLayout.h = 0;
SetDynamicText(source, font, color);
}
// WARNING: you have to make sure the source outlives this UI element!
void UIElement::SetDynamicText(const int& source, TTF_Font* font,
const textcolor_t color){
if(font == nullptr) font = defaultFont;
dynamicTextSource = &source;
dynamicTextCached = source;
dynamicTextFont = font;
dynamicTextColor = Sprite::SDLifyTextColor(color);
dynamicTextSprite = std::make_unique<Sprite>(ren, std::to_string(source),
dynamicTextLayout, dynamicTextColor, font);
dynamicTextSprite->MakeDefaultSize(dynamicTextLayout);
dynamicTextLayout.x -= dynamicTextLayout.w/2;
dynamicTextLayout.y -= dynamicTextLayout.h/2;
}
void UIElement::UpdateDynamicText() const{
if(*dynamicTextSource != dynamicTextCached){
dynamicTextCached = *dynamicTextSource;
dynamicTextSprite = std::make_unique<Sprite>(ren,
std::to_string(dynamicTextCached), dynamicTextLayout,
dynamicTextColor, dynamicTextFont);
dynamicTextLayout.x += dynamicTextLayout.w/2;
dynamicTextLayout.y += dynamicTextLayout.h/2;
dynamicTextSprite->MakeDefaultSize(dynamicTextLayout);
dynamicTextLayout.x -= dynamicTextLayout.w/2;
dynamicTextLayout.y -= dynamicTextLayout.h/2;
}
}
/*void UIElement::EnableButton(const button_t type){
button = true;
this->type = type;
}
void UIElement::DisableButton(){
button = false;
}
int UIElement::Select(){
if(button){
switch(type){
case END_TURN: return static_cast<int>(NEXT_TURN);
case BUILDING: if(values.size() < 1){
std::cerr << "Error: Building button not \
given an ID." << std::endl;
return static_cast<int>(ERROR);
}
return static_cast<int>(TRY_BUILD) + values[0];
case LEAVE_COLONY: return static_cast<int>(SCREEN_CHANGE);
}
}
return static_cast<int>(ERROR);
}*/
void UIElement::Render() const {
if(!visible) return;
sprite->RenderTo(ren, layout);
if(textSprite){
textSprite->RenderTo(ren, textLayout);
// std::cout << "Rendering an object with text." << std::endl;
} else {
// std::cout << "Rendering an object without text." << std::endl;
}
if(dynamicTextSprite){
UpdateDynamicText();
dynamicTextSprite->RenderTo(ren, dynamicTextLayout);
}
}
void UIElement::MoveTo(int x, int y){
if(textSprite){
textLayout.x += x - layout.x;
textLayout.y += y - layout.y;
}
layout.x = x;
layout.y = y;
}
void UIElement::MoveTo(SDL_Rect newLayout){
MoveTo(newLayout.x, newLayout.y);
}
//UIAggregate::~UIAggregate() {}
void UIAggregate::Render() const {
std::cerr << "UIAggregate::Render() shouldn't run!" << std::endl;
}
UnitOrderPanel::UnitOrderPanel(SDL_Renderer* ren, Unit* source): GFXObject(ren,
File::SpritePath() / "unit_order_panel",
0, SCREEN_HEIGHT - ORDER_PANEL_HEIGHT) {
Update(*source);
activeButton = -1u;
}
void UnitOrderPanel::Render() const{
GFXObject::Render();
for(auto& button : buttons) button.Render();
}
// the source argument is not const because the buttons can make it do stuff
void UnitOrderPanel::Update(Unit& source){
buttons.clear();
for(auto order : source.AvailableOrders()){
int buttonX = X() + 5 + (ORDER_BUTTON_WIDTH+5)*(buttons.size()%3);
int buttonY = Y() + 5 + (ORDER_BUTTON_HEIGHT+5)*(buttons.size()/3);
buttons.emplace_back(ren, "button_" + order.name, buttonX, buttonY,
order.func);
}
}
void UnitOrderPanel::AddButton(Button&& newButton){
newButton.MoveTo(X() + 5 + (ORDER_BUTTON_WIDTH+5)*(buttons.size()%3),
Y() + 5 + (ORDER_BUTTON_HEIGHT+5)*(buttons.size()/3));
buttons.push_back(std::move(newButton));
}
// Note that this returns false if you click on the frame rather than a button
bool UnitOrderPanel::InsideQ(const int x, const int y){
for(auto i = 0u; i < buttons.size(); ++i){
if(buttons[i].InsideQ(x, y)){
activeButton = i;
return true;
}
}
return false;
}
bool UnitOrderPanel::Click(){
if(activeButton >= buttons.size()){
std::cerr << "Error: UnitOrderPanel clicked, but it does not have an "
<< "active button set by InsideQ()." << std::endl;
}
const bool ret = buttons[activeButton].Click();
activeButton = -1u;
return ret;
}
void UnitOrderPanel::MoveTo(int x, int y){
GFXObject::MoveTo(x,y);
for(auto i = 0u; i < buttons.size(); ++i){
buttons[i].MoveTo(x + 5 + (ORDER_BUTTON_WIDTH+5)*(i%3),
y + 5 + (ORDER_BUTTON_HEIGHT+5)*(i/3));
}
}
} // namespace TerraNova