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