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