/
guiWidgets.cpp
295 lines (249 loc) · 8.36 KB
/
guiWidgets.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "gui.h"
void GUI::questionPrompt(std::string question, std::vector<std::string> options, void (GUI::*caller)(std::string))
{
Menu *questionPrompt = addMenu(38,6.5,(double)al_get_text_width(czcionka,question.c_str())/(double)dispx*100+10,
(options.size()+1)*6.5,Y_LAYOUT,"questionPrompt");
questionPrompt->fillWithLayouts(options.size()+1, 5, X_LAYOUT);
questionPrompt->findLayoutByVector({1})->addLabel(question);
movingBlocked=true;
for(int i=0; i<options.size(); i++)
{
questionPrompt->findLayoutByVector({i+2})->addButton(options.at(i), caller, options.at(i));
}
}
void GUI::removeQuestionPrompt()
{
if(movingBlocked)
movingBlocked = false;
removeMenu("questionPrompt");
}
void GUI::showNotification(std::string notification)
{
Menu *notificationMenu = addMenu(38,6.5,(double)al_get_text_width(czcionka,notification.c_str())/(double)dispx*100+10,13,Y_LAYOUT,"notification");
notificationMenu->fillWithLayouts(2, 5, X_LAYOUT);
notificationMenu->findLayoutByVector({1})->addLabel(notification);
movingBlocked=true;
notificationMenu->findLayoutByVector({2})->addButton("OK", &GUI::removeNotification, "");
}
void GUI::removeNotification(std::string arg)
{
movingBlocked = false;
removeMenu("notification");
}
void GUI::quantityQuestionPrompt(std::string question, double min, double max, double step, void (GUI::*caller)(std::string))
{
Menu *quantityMenu = addMenu(38,6.5,(double)al_get_text_width(czcionka,question.c_str())/(double)dispx*100+10,13,Y_LAYOUT,"quantityQuestion");
quantityMenu->fillWithLayouts(3, 5, X_LAYOUT);
quantityMenu->findLayoutByVector({1})->addLabel(question);
movingBlocked=true;
quantityMenu->findLayoutByVector({2})->addSlider(min, max, step);
quantityMenu->findLayoutByVector({3})->addButton("OK", caller, "something");
}
void GUI::removeQuantityQuestionPrompt()
{
removeMenu("quantityQuestion");
movingBlocked=false;
quantityReturnValue = 0;
}
//DO PRZENIESIENIA!!!
void Menu::fillWithLayouts(int layoutsNumber, int marginSize, eLayout layoutsType)
{
if(this->layout == X_LAYOUT)
{
for(int i=0;i<layoutsNumber;i++)
{
Layout layout;
layout.height = this->height;
layout.width = (this->width/layoutsNumber);
layout.marginSize = marginSize;
layout.layoutType = layoutsType;
layout.relX = ((this->width/layoutsNumber)*i)+this->x;
layout.relY = this->y;
layout.parent = NULL;
layout.parentMenu = this;
layout.highlight = false;
this->layouts.push_back(layout);
}
}
else if(this->layout == Y_LAYOUT)
{
for(int i=0;i<layoutsNumber;i++)
{
Layout layout;
layout.height = (this->height/layoutsNumber);
layout.width = this->width;
layout.marginSize = marginSize;
layout.layoutType = layoutsType;
layout.relX = this->x;
layout.relY = ((this->height/layoutsNumber)*i)+this->y;
layout.parent = NULL;
layout.parentMenu = this;
layout.highlight = false;
this->layouts.push_back(layout);
}
}
}
void Layout::fillWithLayouts(int layoutsNumber, int marginSize, eLayout layoutsType)
{
if(this->layoutType == X_LAYOUT)
{
for(int i=0;i<layoutsNumber;i++)
{
Layout layout;
layout.height = this->height;
layout.width = (this->width/layoutsNumber);
layout.marginSize = marginSize;
layout.layoutType = layoutsType;
layout.relX = ((this->width/layoutsNumber)*i)+this->relX;
layout.relY = this->relY;
layout.parent = this;
layout.parentMenu = this->parentMenu;
layout.highlight = false;
this->layouts.push_back(layout);
}
}
else if(this->layoutType == Y_LAYOUT)
{
for(int i=0;i<layoutsNumber;i++)
{
Layout layout;
layout.height = (this->height/layoutsNumber);
layout.width = this->width;
layout.marginSize = marginSize;
layout.layoutType = layoutsType;
layout.relX = this->relX;
layout.relY = ((this->height/layoutsNumber)*i)+this->relY;
layout.parent = this;
layout.parentMenu = this->parentMenu;
layout.highlight = false;
this->layouts.push_back(layout);
}
}
}
void Layout::changeLabel(std::string label)
{
this->labels.at(0).text = label;
}
void Layout::highlightElement()
{
this->highlight = true;
}
void Layout::clearHighlight()
{
this->highlight = false;
}
void Layout::addButton(std::string label, void (GUI::*function)(std::string), std::string functionArg, std::string font)
{
Button przycisk;
przycisk.function = function;
przycisk.label = label;
przycisk.font = font;
przycisk.blocked = false;
przycisk.functionArg = functionArg;
this->buttons.push_back(przycisk);
}
void Layout::removeButton()
{
this->buttons.clear();
}
void Layout::addImageButton(std::string imageDir, void (GUI::*function)(std::string), std::string functionArg)
{
ImageButton przycisk;
ALLEGRO_BITMAP *image = al_load_bitmap(imageDir.c_str());
przycisk.function = function;
przycisk.image = image;
przycisk.blocked = false;
przycisk.functionArg = functionArg;
this->imageButtons.push_back(przycisk);
this->parentMenu->imagesInMenuList.push_back(image);
}
void Layout::addLabel(std::string text, std::string font)
{
Label label;
label.text = text;
label.font = font;
this->labels.push_back(label);
}
void Layout::addImage(std::string imageDir, std::string nameId)
{
ALLEGRO_BITMAP *imagePtr = al_load_bitmap(imageDir.c_str());
Image image;
image.image=imagePtr;
image.nameId=nameId;
this->images.push_back(image);
this->parentMenu->imagesInMenuList.push_back(imagePtr);
}
void Layout::removeImage()
{
std::list<ALLEGRO_BITMAP*>::iterator it;
for(it=this->parentMenu->imagesInMenuList.begin();it!=this->parentMenu->imagesInMenuList.end();it++)
{
if(this->images.at(0).image == *it)
{
ALLEGRO_BITMAP *bmp = *it;
this->parentMenu->imagesInMenuList.erase(it);
al_destroy_bitmap(bmp);
break;
}
}
this->images.clear();
}
void Layout::addSlider(double min, double max, double step)
{
this->fillWithLayouts(10, 0 , X_LAYOUT);
std::vector<int> conn;
for(int i=2; i<9; i++)
{
conn.push_back(i);
}
this->connectLayouts(conn);
this->at(0)->addButton("b", &GUI::sliderLeftButton, this->parentMenu->nameId, "arrows");
//this->at(0)->addImageButton("./res/arrowl.png", &GUI::sliderLeftButton, this->parentMenu->nameId);
Slider slider;
slider.max = max;
slider.min = min;
slider.step = step;
slider.value = min;
this->at(1)->sliders.push_back(slider);
this->at(2)->addLabel("0");
//this->at(3)->addImageButton("./res/arrowr.png", &GUI::sliderRightButton, this->parentMenu->nameId);
this->at(3)->addButton("a", &GUI::sliderRightButton, this->parentMenu->nameId, "arrows");
}
void Layout::connectLayouts(std::vector<int> layoutsToConnect)
{
int connectedHeight = this->at(layoutsToConnect.at(0)-1)->height;
int connectedWidth = this->at(layoutsToConnect.at(0)-1)->width;
for(unsigned is=1;is<layoutsToConnect.size();is++)
{
if(this->layoutType == X_LAYOUT)
{
connectedWidth += this->at(layoutsToConnect.at(is)-1)->width;
}
else if(this->layoutType == Y_LAYOUT)
{
connectedHeight += this->at(layoutsToConnect.at(is)-1)->height;
}
}
for(unsigned iu=1;iu<layoutsToConnect.size();iu++)
{
std::list<Layout>::iterator it;
it = this->layouts.begin();
std::advance(it,layoutsToConnect.at(iu)-iu);
this->layouts.erase(it);
}
Layout *targetLay = this->at(layoutsToConnect.at(0)-1);
targetLay->height = connectedHeight;
targetLay->width = connectedWidth;
}
void Layout::setButtonBlock(bool block)
{
Button *button = &this->buttons.at(0);
button->blocked = block;
}
Layout* Layout::at(int num)
{
std::list<Layout>::iterator it;
it = this->layouts.begin();
std::advance(it, num);
return &*it;
}