]> git.jsancho.org Git - lugaru.git/blob - Source/Menu.cpp
License: Update GPLv2+ header to match current FSF recommendation
[lugaru.git] / Source / Menu.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <vector>
21 #include <string>
22 #include "gamegl.h"
23
24 #include "Menu.h"
25 using namespace Menu;
26
27 extern float multiplier;
28
29 struct MenuItem {
30     enum MenuItemType {NONE, LABEL, BUTTON, IMAGE, IMAGEBUTTON, MAPMARKER, MAPLINE, MAPLABEL} type;
31     int id;
32     string text;
33     Texture texture;
34     int x, y, w, h;
35     float r, g, b;
36     float effectfade;
37
38     float linestartsize;
39     float lineendsize;
40
41     void init(MenuItemType _type, int _id, const string& _text, Texture _texture,
42               int _x, int _y, int _w, int _h, float _r, float _g, float _b,
43               float _linestartsize = 1, float _lineendsize = 1) {
44         type = _type;
45         id = _id;
46         text = _text;
47         texture = _texture;
48         x = _x;
49         y = _y;
50         w = _w;
51         h = _h;
52         r = _r;
53         g = _g;
54         b = _b;
55         effectfade = 0;
56         linestartsize = _linestartsize;
57         lineendsize = _lineendsize;
58         if (type == MenuItem::BUTTON) {
59             if (w == -1)
60                 w = text.length() * 10;
61             if (h == -1)
62                 h = 20;
63         }
64     }
65 };
66
67 vector<MenuItem> items;
68
69
70
71
72 void Menu::clearMenu()
73 {
74     items.clear();
75 }
76
77 void Menu::addLabel(int id, const string& text, int x, int y, float r, float g, float b)
78 {
79     items.push_back(MenuItem());
80     items.back().init(MenuItem::LABEL, id, text, Texture(), x, y, -1, -1, r, g, b);
81 }
82 void Menu::addButton(int id, const string& text, int x, int y, float r, float g, float b)
83 {
84     items.push_back(MenuItem());
85     items.back().init(MenuItem::BUTTON, id, text, Texture(), x, y, -1, -1, r, g, b);
86 }
87 void Menu::addImage(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
88 {
89     items.push_back(MenuItem());
90     items.back().init(MenuItem::IMAGE, id, "", texture, x, y, w, h, r, g, b);
91 }
92 void Menu::addButtonImage(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
93 {
94     items.push_back(MenuItem());
95     items.back().init(MenuItem::IMAGEBUTTON, id, "", texture, x, y, w, h, r, g, b);
96 }
97 void Menu::addMapLine(int x, int y, int w, int h, float startsize, float endsize, float r, float g, float b)
98 {
99     items.push_back(MenuItem());
100     items.back().init(MenuItem::MAPLINE, -1, "", Texture(), x, y, w, h, r, g, b, startsize, endsize);
101 }
102 void Menu::addMapMarker(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
103 {
104     items.push_back(MenuItem());
105     items.back().init(MenuItem::MAPMARKER, id, "", texture, x, y, w, h, r, g, b);
106 }
107 void Menu::addMapLabel(int id, const string& text, int x, int y, float r, float g, float b)
108 {
109     items.push_back(MenuItem());
110     items.back().init(MenuItem::MAPLABEL, id, text, Texture(), x, y, -1, -1, r, g, b);
111 }
112
113 void Menu::setText(int id, const string& text)
114 {
115     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++)
116         if (it->id == id) {
117             it->text = text;
118             it->w = it->text.length() * 10;
119             break;
120         }
121 }
122
123 void Menu::setText(int id, const string& text, int x, int y, int w, int h)
124 {
125     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++)
126         if (it->id == id) {
127             it->text = text;
128             it->x = x;
129             it->y = y;
130             if (w == -1)
131                 it->w = it->text.length() * 10;
132             if (h == -1)
133                 it->h = 20;
134             break;
135         }
136 }
137
138 int Menu::getSelected(int mousex, int mousey)
139 {
140     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++)
141         if (it->type == MenuItem::BUTTON || it->type == MenuItem::IMAGEBUTTON || it->type == MenuItem::MAPMARKER) {
142             int mx = mousex;
143             int my = mousey;
144             if (it->type == MenuItem::MAPMARKER) {
145                 mx -= 1;
146                 my += 2;
147             }
148             if (mx >= it->x && mx < it->x + it->w && my >= it->y && my < it->y + it->h)
149                 return it->id;
150         }
151     return -1;
152 }
153
154 void GUITick()
155 {
156     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++) {
157         if (it->id == Game::selected) {
158             it->effectfade += multiplier * 5;
159             if (it->effectfade > 1)
160                 it->effectfade = 1;
161         } else {
162             it->effectfade -= multiplier * 5;
163             if (it->effectfade < 0)
164                 it->effectfade = 0;
165         }
166     }
167 }
168
169 void Menu::drawItems()
170 {
171     GUITick();
172     glEnable(GL_TEXTURE_2D);
173     glEnable(GL_ALPHA_TEST);
174     glEnable(GL_BLEND);
175     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++) {
176         switch (it->type) {
177         case MenuItem::IMAGE:
178         case MenuItem::IMAGEBUTTON:
179         case MenuItem::MAPMARKER:
180             glColor4f(it->r, it->g, it->b, 1);
181             glPushMatrix();
182             if (it->type == MenuItem::MAPMARKER) {
183                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
184                 glTranslatef(2.5, -4.5, 0); //from old code
185             } else {
186                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
187             }
188             it->texture.bind();
189             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
190             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
191             glBegin(GL_QUADS);
192             glTexCoord2f(0, 0);
193             glVertex3f(it->x, it->y, 0);
194             glTexCoord2f(1, 0);
195             glVertex3f(it->x + it->w, it->y, 0);
196             glTexCoord2f(1, 1);
197             glVertex3f(it->x + it->w, it->y + it->h, 0);
198             glTexCoord2f(0, 1);
199             glVertex3f(it->x, it->y + it->h, 0);
200             glEnd();
201             if (it->type != MenuItem::IMAGE) {
202                 //mouseover highlight
203                 for (int i = 0; i < 10; i++) {
204                     if (1 - ((float)i) / 10 - (1 - it->effectfade) > 0) {
205                         glColor4f(it->r, it->g, it->b, (1 - ((float)i) / 10 - (1 - it->effectfade))*.25);
206                         glBegin(GL_QUADS);
207                         glTexCoord2f(0, 0);
208                         glVertex3f(it->x - ((float)i) * 1 / 2, it->y - ((float)i) * 1 / 2, 0);
209                         glTexCoord2f(1, 0);
210                         glVertex3f(it->x + it->w + ((float)i) * 1 / 2, it->y - ((float)i) * 1 / 2, 0);
211                         glTexCoord2f(1, 1);
212                         glVertex3f(it->x + it->w + ((float)i) * 1 / 2, it->y + it->h + ((float)i) * 1 / 2, 0);
213                         glTexCoord2f(0, 1);
214                         glVertex3f(it->x - ((float)i) * 1 / 2, it->y + it->h + ((float)i) * 1 / 2, 0);
215                         glEnd();
216                     }
217                 }
218             }
219             glPopMatrix();
220             break;
221         case MenuItem::LABEL:
222         case MenuItem::BUTTON:
223             glColor4f(it->r, it->g, it->b, 1);
224             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
225             Game::text->glPrint(it->x, it->y, it->text.c_str(), 0, 1, 640, 480);
226             if (it->type != MenuItem::LABEL) {
227                 //mouseover highlight
228                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
229                 for (int i = 0; i < 15; i++) {
230                     if (1 - ((float)i) / 15 - (1 - it->effectfade) > 0) {
231                         glColor4f(it->r, it->g, it->b, (1 - ((float)i) / 10 - (1 - it->effectfade))*.25);
232                         Game::text->glPrint(it->x - ((float)i), it->y, it->text.c_str(), 0, 1 + ((float)i) / 70, 640, 480);
233                     }
234                 }
235             }
236             break;
237         case MenuItem::MAPLABEL:
238             Game::text->glPrintOutlined(0.9, 0, 0, it->x, it->y, it->text.c_str(), 0, 0.6, 640, 480);
239             break;
240         case MenuItem::MAPLINE: {
241             XYZ linestart;
242             linestart.x = it->x;
243             linestart.y = it->y;
244             linestart.z = 0;
245             XYZ lineend;
246             lineend.x = it->x + it->w;
247             lineend.y = it->y + it->h;
248             lineend.z = 0;
249             XYZ offset = lineend - linestart;
250             XYZ fac = offset;
251             Normalise(&fac);
252             offset = DoRotation(offset, 0, 0, 90);
253             Normalise(&offset);
254
255             linestart += fac * 4 * it->linestartsize;
256             lineend -= fac * 4 * it->lineendsize;
257
258             glDisable(GL_TEXTURE_2D);
259             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
260             glColor4f(it->r, it->g, it->b, 1);
261             glPushMatrix();
262             glTranslatef(2, -5, 0); //from old code
263             glBegin(GL_QUADS);
264             glVertex3f(linestart.x - offset.x * it->linestartsize, linestart.y - offset.y * it->linestartsize, 0.0f);
265             glVertex3f(linestart.x + offset.x * it->linestartsize, linestart.y + offset.y * it->linestartsize, 0.0f);
266             glVertex3f(lineend.x + offset.x * it->lineendsize, lineend.y + offset.y * it->lineendsize, 0.0f);
267             glVertex3f(lineend.x - offset.x * it->lineendsize, lineend.y - offset.y * it->lineendsize, 0.0f);
268             glEnd();
269             glPopMatrix();
270             glEnable(GL_TEXTURE_2D);
271         }
272         break;
273         default:
274         case MenuItem::NONE:
275         break;
276         }
277     }
278 }
279