-
Notifications
You must be signed in to change notification settings - Fork 188
/
Menu.cpp
143 lines (119 loc) · 3.54 KB
/
Menu.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
/*
Copyright © 2011-2012 kitano
Copyright © 2013-2014 Henrik Andersson
Copyright © 2013 Kurt Rinnert
Copyright © 2012-2016 Justin Jacobs
This file is part of FLARE.
FLARE is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
FLARE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
FLARE. If not, see http://www.gnu.org/licenses/
*/
/**
* class Menu
*
* The base class for Menu objects
*/
#include "Menu.h"
#include "RenderDevice.h"
#include "SharedResources.h"
#include "SoundManager.h"
#include "Utils.h"
#include "UtilsParsing.h"
Menu::Menu()
: visible(false)
, alignment(Utils::ALIGN_TOPLEFT)
, sfx_open(0)
, sfx_close(0)
, background(NULL) {
}
Menu::~Menu() {
if (background) delete background;
}
void Menu::setBackground(const std::string& background_image) {
Image *graphics;
if (background) {
delete background;
background = NULL;
}
graphics = render_device->loadImage(background_image, RenderDevice::ERROR_NORMAL);
if (graphics) {
background = graphics->createSprite();
background->setClip(0, 0, window_area.w, window_area.h);
background->setDestFromRect(window_area);
graphics->unref();
}
}
void Menu::setBackgroundDest(const Rect &dest) {
if (background) background->setDestFromRect(dest);
}
void Menu::setBackgroundClip(const Rect &clip) {
if (background) background->setClipFromRect(clip);
}
void Menu::render() {
if (background)
render_device->render(background);
}
/**
* Aligns the menu relative to one of these positions:
* topleft, top, topright, left, center, right, bottomleft, bottom, bottomright
*/
void Menu::align() {
window_area.x = window_area_base.x;
window_area.y = window_area_base.y;
Utils::alignToScreenEdge(alignment, &window_area);
if (background) {
background->setClip(0, 0, window_area.w, window_area.h);
background->setDestFromRect(window_area);
}
}
void Menu::setWindowPos(int x, int y) {
window_area_base.x = x;
window_area_base.y = y;
}
/**
* When reading menu config files, we use this to set common variables
*/
bool Menu::parseMenuKey(const std::string &key, const std::string &val) {
// @CLASS Menu|Description of menus in menus/
std::string value = val;
if (key == "pos") {
// @ATTR pos|rectangle|Menu position and dimensions
value = value + ',';
window_area = Parse::toRect(value);
setWindowPos(window_area.x, window_area.y);
}
else if (key == "align") {
// @ATTR align|alignment|Position relative to screen edges
alignment = Parse::toAlignment(value);
}
else if (key == "soundfx_open") {
// @ATTR soundfx_open|filename|Filename of a sound to play when opening this menu.
sfx_open = snd->load(value, "Menu open tab");
}
else if (key == "soundfx_close") {
// @ATTR soundfx_close|filename|Filename of a sound to play when closing this menu.
sfx_close = snd->load(value, "Menu close tab");
}
else if (key == "background") {
// @ATTR background|filename|Filename of the background image for this menu.
setBackground(value);
}
else {
//not a common key
return false;
}
return true;
}
TabList* Menu::getCurrentTabList() {
if (tablist.getCurrent() != -1)
return (&tablist);
return NULL;
}
void Menu::defocusTabLists() {
tablist.defocus();
}