1#include <allegro5/allegro.h>
2#include <allegro5/allegro_font.h>
3#include <allegro5/allegro_ttf.h>
4#include <allegro5/allegro_native_dialog.h>
5#include <string>
6#include <vector>
7
8namespace framework
9{
10 /********************************************\
11 *
12 utility functions
13 *
14 ********************************************/
15
16 std::string changeCase
(std::string word
);
17 bool findWord
(std::string searchThis, std::string target
);
18 std::string getFileName
(std::string filePath
);
19 std::string intToStr
(int value,
int base
);
20 std::string floatToString
(float number
);
21 int showMessageBox
(std::string header, std::string message,
int flag
);
22 std::string getXmlData
(std::string data, std::string element
);
23
24 /********************************************\
25 *
26 enum declarations
27 *
28 ********************************************/
29
30 enum LOAD_RESULT
31 {
32 LOAD_FAIL
= -1,
33 LOAD_SUCCESS
= 0
34 };
35
36 enum BUTTON_LAYOUT
37 {
38 LAYOUT_OVERLAP,
39 LAYOUT_VERTICAL,
40 LAYOUT_HORIZONTAL,
41 };
42
43 enum PROGRESSBAR_DISPLAY_FLAG
44 {
45 PB_DISPLAY_NOTHING,
46 PB_DISPLAY_PERCENTAGES,
47 PB_DISPLAY_NUMBERS
48 };
49
50 enum INPUT_TYPE
51 {
52 INPUT_ALL,
53 INPUT_ALPHA,
54 INPUT_ALPHA_PLUS_COM_APO_DASH,
//short for comma and apostrophe
55 INPUT_NUMERIC,
56 INPUT_COMPARISON,
57 INPUT_NUMERIC_NO_SPACE,
58 INPUT_COMPARISON_NO_SPACE,
59 INPUT_ALPHA_PLUS_NUMERIC,
60 INPUT_ALPHA_NO_SPACE,
61 INPUT_ALPHA_PLUS_NUMERIC_NO_SPACE,
62 };
63
64 /*******************************************\
65 *
66 general namespace variable declarations
67 *
68 *******************************************/
69
70 const int NO_BUTTON_SELECTED
= -1;
71
72 /********************************************\
73 *
74 class declarations
75 *
76 ********************************************/
77
78 class fontHandler
79 {
80 static ALLEGRO_FONT *size12Font
;
81 static ALLEGRO_FONT *size64Font
;
82 static ALLEGRO_FONT *size16Font
;
83
84 public:
85
86 fontHandler
(){}
87 ~fontHandler
(){}
88
89 LOAD_RESULT initialize
();
90 void cleanup
();
91 ALLEGRO_FONT *size12
();
92 ALLEGRO_FONT *size64
();
93 ALLEGRO_FONT *size16
();
94 };
95
96 class vector2d
97 {
98 float x, y
;
99
100 public:
101
102 vector2d
(): x
(0.
0), y
(0.
0){}
103 vector2d
(float xpos,
float ypos
)
104 {
105 x
= xpos
;
106 y
= ypos
;
107 }
108
109 float getX
();
110 float getY
();
111
112 bool operator== (vector2d
&pos
)
113 {
114 return (x
== pos.x
&& y
== pos.y
);
115 }
116
117 bool operator!= (vector2d
&pos
)
118 {
119 return !(x
== pos.x
&& y
== pos.y
);
120 }
121 };
122
123 /********************************************\
124 rectangle - class that defines the basics
125 of an objects dimensions.
126 ********************************************/
127 class rectangle
128 {
129 vector2d position
;
130 float height, width
;
131
132 public:
133
134 ~rectangle
(){}
135 rectangle
(): position
(vector2d
()), height
(0.
0), width
(0.
0){}
136 rectangle
(float xpos,
float ypos,
float w,
float h
)
137 {
138 position
= vector2d
(xpos, ypos
);
139 width
= w
;
140 height
= h
;
141 }
142
143 rectangle
(framework::vector2d newPosition,
float w,
float h
)
144 {
145 position
= newPosition
;
146 width
= w
;
147 height
= h
;
148 }
149
150 vector2d getCenter
();
151 vector2d getPosition
();
152 float getX1
();
153 float getX2
();
154 float getY1
();
155 float getY2
();
156 float getWidth
();
157 float getHeight
();
158 void setPosition
(vector2d newPosition
);
159 void setPosition
(float x,
float y
);
160 };
161
162 class Tooltip
163 {
164 rectangle dimensions
;
165 std::string text
;
166 int maxWidth
;
167 ALLEGRO_TIMER *enableTimer
;
168 bool enabled
;
169
170 public:
171
172 Tooltip
(): maxWidth
(100), enableTimer
(NULL
), enabled
(false){}
173
174 Tooltip
(std::string newText,
int width
): enabled
(false)
175 {
176 text
= newText
;
177 maxWidth
= width
;
178 }
179
180 void Load
(ALLEGRO_EVENT_QUEUE *eventQueue
);
181 void HandleEvents
(ALLEGRO_EVENT &event,
bool isMouseOverObject
);
182 void Cleanup
();
183 void Draw
();
184 };
185
186 class basicImage
187 {
188 rectangle dimensions
;
189 ALLEGRO_BITMAP *image
;
190 bool over, scalable
;
191
192 public:
193
194 ~basicImage
(){}
195 basicImage
(): dimensions
(rectangle
()), image
(NULL
), over
(false), scalable
(false){}
196 basicImage
(rectangle newDimensions
): image
(NULL
), over
(false), scalable
(false)
197 {
198 dimensions
= newDimensions
;
199 }
200
201 void draw
();
202 LOAD_RESULT load
(std::string path
);
203 void isMouseOver
(ALLEGRO_EVENT &event
);
204 bool isClicked
(ALLEGRO_EVENT &event
);
205 void cleanup
();
206
207 void setPosition
(vector2d newPosition
);
208 void highlight
();
209 bool isImageOver
(vector2d position
);
210 rectangle getDimensions
();
211 void setDimensions
(rectangle newDimensions
);
212 void setScalable
(bool state
);
213 };
214
215
216 /********************************************\
217 button - class that represents a button
218 in the program.
219 ********************************************/
220
221 class button
222 {
223 rectangle dimensions
;
224 ALLEGRO_BITMAP *upImage,
*overImage,
*upPiece,
*upLeftPiece,
*upRightPiece,
*overPiece,
*overLeftPiece,
*overRightPiece
;
225 bool over, scalable, tooltipEnabled
;
226 int minimumWidth
;
227 Tooltip tooltip
;
228
229 public:
230
231 ~button
(){}
232 button
(): dimensions
(rectangle
()), upImage
(NULL
), overImage
(NULL
), upPiece
(NULL
), upLeftPiece
(NULL
),
233 upRightPiece
(NULL
), overPiece
(NULL
), overLeftPiece
(NULL
), overRightPiece
(NULL
),
234 over
(false), scalable
(false), tooltipEnabled
(false), minimumWidth
(0){}
235
236 button
(rectangle newDimensions
):
237 upImage
(NULL
), overImage
(NULL
), upPiece
(NULL
), upLeftPiece
(NULL
), upRightPiece
(NULL
),
238 overPiece
(NULL
), overLeftPiece
(NULL
), overRightPiece
(NULL
),
239 over
(false), scalable
(false), tooltipEnabled
(false), minimumWidth
(0)
240 {
241 dimensions
= newDimensions
;
242 }
243
244 button
(rectangle newDimensions, std::string text
):
245 upImage
(NULL
), overImage
(NULL
), upPiece
(NULL
), upLeftPiece
(NULL
), upRightPiece
(NULL
),
246 overPiece
(NULL
), overLeftPiece
(NULL
), overRightPiece
(NULL
),
247 over
(false), scalable
(false), tooltipEnabled
(false), minimumWidth
(0)
248 {
249 dimensions
= newDimensions
;
250 tooltip
= Tooltip
(text,
100);
251 }
252
253 void draw
();
254 LOAD_RESULT load
(std::string upPath, std::string overPath
);
255 LOAD_RESULT load
(std::string upPath, std::string overPath,
ALLEGRO_EVENT_QUEUE *eventQueue
);
256 void isMouseOver
(ALLEGRO_EVENT &event
);
257 bool isClicked
(ALLEGRO_EVENT &event
);
258 void cleanup
();
259 void scale
(int newWidth
);
260 LOAD_RESULT enableScaling
(std::string upPiecePath, std::string upLeftPath, std::string upRightPath, std::string overPiecePath, std::string overLeftPath, std::string overRightPath,
int minimum
);
261 rectangle getDimensions
();
262 void setPosition
(vector2d newPosition
);
263 ALLEGRO_BITMAP* getUpImage
();
264 void EnableToolTips
(ALLEGRO_EVENT_QUEUE *eventQueue
);
265 };
266
267 class selectableButton
268 {
269 rectangle dimensions
;
270 ALLEGRO_BITMAP *upImage,
*selectedImage
;
271 bool over, selected, tooltipEnabled
;
272 bool irregularShape
;
273 Tooltip tooltip
;
274
275 public:
276
277 ~selectableButton
(){}
278 selectableButton
(): dimensions
(rectangle
()),
279 upImage
(NULL
), selectedImage
(NULL
),
280 over
(false), selected
(false), tooltipEnabled
(false){}
281
282 selectableButton
(rectangle newDimensions,
bool shape
):
283 upImage
(NULL
), selectedImage
(NULL
),
284 over
(false), selected
(false), tooltipEnabled
(false)
285 {
286 dimensions
= newDimensions
;
287 irregularShape
= shape
;
288 }
289
290 selectableButton
(rectangle newDimensions,
bool shape, std::string text
):
291 upImage
(NULL
), selectedImage
(NULL
),
292 over
(false), selected
(false), tooltipEnabled
(false)
293 {
294 dimensions
= newDimensions
;
295 irregularShape
= shape
;
296 tooltip
= Tooltip
(text,
100);
297 }
298
299 void draw
();
300 LOAD_RESULT load
(std::string upPath, std::string selectedPath
);
301 LOAD_RESULT load
(std::string upPath, std::string selectedPath,
ALLEGRO_EVENT_QUEUE *eventQueue
);
302 bool handleEvents
(ALLEGRO_EVENT &event
);
303 bool isSelected
();
304 void cleanup
();
305 void setSelectedState
(bool state
);
306 void setY1
(float y
);
307 void setX1
(float x
);
308 void setImage
(ALLEGRO_BITMAP *img1,
ALLEGRO_BITMAP *img2
);
309 };
310
311 class selectableButtonSet
312 {
313 std::vector
<selectableButton> buttons
;
314 rectangle dimensions
;
315
316 int selectedButton
;
317 float xSpace, ySpace
;
318 BUTTON_LAYOUT layout
;
319 bool irregularShape
;
320
321 public:
322
323 ~selectableButtonSet
(){}
324 selectableButtonSet
(rectangle newDimensions,
float x_space,
float y_space, BUTTON_LAYOUT newLayout,
bool shape
): selectedButton
(NO_BUTTON_SELECTED
)
325 {
326 irregularShape
= shape
;
327 layout
= newLayout
;
328 xSpace
= x_space
;
329 ySpace
= y_space
;
330 dimensions
= newDimensions
;
331 }
332
333 selectableButtonSet
(): selectedButton
(NO_BUTTON_SELECTED
)
334 {
335 layout
= LAYOUT_HORIZONTAL
;
336 xSpace
= 0;
337 ySpace
= 0;
338 dimensions
= rectangle
();
339 }
340
341 void draw
();
342 LOAD_RESULT addButton
(std::string upPath, std::string selectedPath
);
343 LOAD_RESULT load
(std::string upPath, std::string selectedPath,
int targetButton
);
344 bool handleEvents
(ALLEGRO_EVENT &event
);
345 void cleanup
();
346 int getSelectedButton
();
347 int getNumButtons
();
348 void setNoButtonSelected
();
349 void setSelectedButton
(int selected
);
350 void setBaseY
(float y
);
351 void setBaseX
(float x
);
352 };
353
354 class progressBar
355 {
356 rectangle dimensions
;
357 int maxItems, numItems
;
358 float increaseRate, percentageIncrease, barProgress, percentageProgress
;
359 ALLEGRO_COLOR barColor
;
360 std::string text
;
361
362 public:
363
364 progressBar
(){}
365 progressBar
(rectangle newDimensions, std::string newText,
int maxi,
ALLEGRO_COLOR color
): numItems
(0), barProgress
(0.
0), percentageProgress
(0.
0)
366 {
367 dimensions
= newDimensions
;
368 text
= newText
;
369 barColor
= color
;
370 maxItems
= maxi
;
371 increaseRate
= dimensions.getWidth
() / maxItems
;
372 percentageIncrease
= 100.
0 / maxItems
;
373 }
374
375 void draw
(PROGRESSBAR_DISPLAY_FLAG displayFlag
);
376 void increaseItems
(int updates
= 1);
377 void setMaxItems
(int newMax
);
378 void setDimensions
(rectangle newDimensions
);
379 rectangle getDimensions
();
380 void setNumItems
(int num
);
381 };
382
383 class scrollbar
384 {
385 ALLEGRO_BITMAP *piece,
*topPiece,
*bottomPiece,
*bar
;
386
387 float minimumBarHeight, maximumBarHeight, moveRate
;
388 int numItems, numItemsDisplayed, topListItem, spaceFromTop
;
389
390 rectangle barDimensions, bgBarDimensions, targetArea
;
391
392 button upButton, downButton
;
393 bool dragging, overScrollBar, mouseButtonDown
;
394
395 public:
396
397 scrollbar
(rectangle bgBar, rectangle target,
float mini,
int displayed
):
398 piece
(NULL
), topPiece
(NULL
), bottomPiece
(NULL
), bar
(NULL
), numItems
(0), topListItem
(0), spaceFromTop
(0),
399 dragging
(false), overScrollBar
(false), mouseButtonDown
(false)
400 {
401 bgBarDimensions
= bgBar
;
402 barDimensions
= bgBar
;
403 targetArea
= target
;
404
405 minimumBarHeight
= mini
;
406 maximumBarHeight
= bgBarDimensions.getHeight
();
407 numItemsDisplayed
= displayed
;
408 }
409
410 scrollbar
():
411
412 piece
(NULL
), topPiece
(NULL
), bottomPiece
(NULL
), bar
(NULL
), numItems
(0), topListItem
(0), spaceFromTop
(0),
413 dragging
(false), overScrollBar
(false), mouseButtonDown
(false)
414 {
415 bgBarDimensions
= rectangle
();
416 barDimensions
= rectangle
();;
417 targetArea
= rectangle
();;
418
419 minimumBarHeight
= 0;
420 maximumBarHeight
= 0;
421 numItemsDisplayed
= 0;
422 }
423
424 LOAD_RESULT load
(std::string piecePath, std::string topPath, std::string bottomPath,
int num
);
425 LOAD_RESULT loadButtons
(std::string upButtonUpPath, std::string upButtonOverPath, std::string DownButtonUpPath, std::string DownButtonOverPath
);
426 void cleanup
();
427 void draw
();
428
429 int handleEvents
(ALLEGRO_EVENT &event
);
430 int getTopListItem
();
431 void recalcuate
(rectangle newDimensions, rectangle newTargetArea,
int displayed,
int num
);
432 void recalcuate
(int displayed,
int num
);
433 void resetTopListItem
();
434 void setTopListItem
(int num
);
435 };
436
437 class textBox
438 {
439 rectangle dimensions
;
440 std::string text
;
441 bool shiftMod, over, isActive, drawCursor, disableClicks
; // so textbox cant be disabled if clicked off
442 unsigned int maxTextLength
;
443 ALLEGRO_TIMER *cursorTimer
;
444
445 public:
446
447 textBox
(rectangle newDimensions,
bool click
):
448 shiftMod
(false), over
(false), isActive
(false), drawCursor
(false),
449 maxTextLength
(20)
450 {
451 disableClicks
= click
;
452 dimensions
= newDimensions
;
453 }
454
455 textBox
():
456 dimensions
(rectangle
()),
457 shiftMod
(false), over
(false), isActive
(false), drawCursor
(false), disableClicks
(false),
458 maxTextLength
(20){}
459
460 bool handleEvents
(ALLEGRO_EVENT &event, INPUT_TYPE inputType
);
461
462 void setActiveState
(bool newState
);
463 bool getActiveState
();
464
465 void draw
();
466 void cleanup
();
467 int load
(int limit,
ALLEGRO_EVENT_QUEUE *eventQueue
);
468 void setDimensions
(rectangle newDimensions
);
469 rectangle getDimensions
();
470
471 void clearText
();
472 std::string getText
();
473 void setText
(std::string newText
);
474 };
475
476 class selectionMenu
477 {
478 std::vector
<button> menuItems
;
479 selectableButton menuBase
;
480 int selectedButton
;
481 rectangle dimensions
;
482
483 public:
484
485 selectionMenu
(){}
486 selectionMenu
(rectangle position
): selectedButton
(0)
487 {
488 dimensions
= position
;
489 }
490
491 int getSelectedItem
();
492 int addButton
(std::string upPath, std::string overPath
);
493 int loadBase
(std::string upPath, std::string selectedPath
);
494 void draw
();
495 void handleEvents
(ALLEGRO_EVENT &event
);
496 void setSelectedItem
(int item
);
497 };
498}