]> git.jsancho.org Git - lugaru.git/blob - Source/Menu.cpp
6e2df4c5a412a9ff7abb22c7a173b274be15315b
[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 MenuItem::MenuItem(MenuItemType _type, int _id, const string& _text, Texture _texture,
32           int _x, int _y, int _w, int _h, float _r, float _g, float _b,
33           float _linestartsize, float _lineendsize):
34     type(_type),
35     id(_id),
36     text(_text),
37     texture(_texture),
38     x(_x),
39     y(_y),
40     w(_w),
41     h(_h),
42     r(_r),
43     g(_g),
44     b(_b),
45     effectfade(0),
46     linestartsize(_linestartsize),
47     lineendsize(_lineendsize)
48 {
49     if (type == MenuItem::BUTTON) {
50         if (w == -1) {
51             w = text.length() * 10;
52         }
53         if (h == -1) {
54             h = 20;
55         }
56     }
57 }
58
59 void Menu::clearMenu()
60 {
61     items.clear();
62 }
63
64 void Menu::addLabel(int id, const string& text, int x, int y, float r, float g, float b)
65 {
66     items.emplace_back(MenuItem::LABEL, id, text, Texture(), x, y, -1, -1, r, g, b);
67 }
68 void Menu::addButton(int id, const string& text, int x, int y, float r, float g, float b)
69 {
70     items.emplace_back(MenuItem::BUTTON, id, text, Texture(), x, y, -1, -1, r, g, b);
71 }
72 void Menu::addImage(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
73 {
74     items.emplace_back(MenuItem::IMAGE, id, "", texture, x, y, w, h, r, g, b);
75 }
76 void Menu::addButtonImage(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
77 {
78     items.emplace_back(MenuItem::IMAGEBUTTON, id, "", texture, x, y, w, h, r, g, b);
79 }
80 void Menu::addMapLine(int x, int y, int w, int h, float startsize, float endsize, float r, float g, float b)
81 {
82     items.emplace_back(MenuItem::MAPLINE, -1, "", Texture(), x, y, w, h, r, g, b, startsize, endsize);
83 }
84 void Menu::addMapMarker(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
85 {
86     items.emplace_back(MenuItem::MAPMARKER, id, "", texture, x, y, w, h, r, g, b);
87 }
88 void Menu::addMapLabel(int id, const string& text, int x, int y, float r, float g, float b)
89 {
90     items.emplace_back(MenuItem::MAPLABEL, id, text, Texture(), x, y, -1, -1, r, g, b);
91 }
92
93 void Menu::setText(int id, const string& text)
94 {
95     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++)
96         if (it->id == id) {
97             it->text = text;
98             it->w = it->text.length() * 10;
99             break;
100         }
101 }
102
103 void Menu::setText(int id, const string& text, int x, int y, int w, int h)
104 {
105     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++)
106         if (it->id == id) {
107             it->text = text;
108             it->x = x;
109             it->y = y;
110             if (w == -1)
111                 it->w = it->text.length() * 10;
112             if (h == -1)
113                 it->h = 20;
114             break;
115         }
116 }
117
118 int Menu::getSelected(int mousex, int mousey)
119 {
120     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++)
121         if (it->type == MenuItem::BUTTON || it->type == MenuItem::IMAGEBUTTON || it->type == MenuItem::MAPMARKER) {
122             int mx = mousex;
123             int my = mousey;
124             if (it->type == MenuItem::MAPMARKER) {
125                 mx -= 1;
126                 my += 2;
127             }
128             if (mx >= it->x && mx < it->x + it->w && my >= it->y && my < it->y + it->h)
129                 return it->id;
130         }
131     return -1;
132 }
133
134 void Menu::handleFadeEffect()
135 {
136     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++) {
137         if (it->id == Game::selected) {
138             it->effectfade += multiplier * 5;
139             if (it->effectfade > 1)
140                 it->effectfade = 1;
141         } else {
142             it->effectfade -= multiplier * 5;
143             if (it->effectfade < 0)
144                 it->effectfade = 0;
145         }
146     }
147 }
148
149 void Menu::drawItems()
150 {
151     handleFadeEffect();
152     glEnable(GL_TEXTURE_2D);
153     glEnable(GL_ALPHA_TEST);
154     glEnable(GL_BLEND);
155     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++) {
156         switch (it->type) {
157         case MenuItem::IMAGE:
158         case MenuItem::IMAGEBUTTON:
159         case MenuItem::MAPMARKER:
160             glColor4f(it->r, it->g, it->b, 1);
161             glPushMatrix();
162             if (it->type == MenuItem::MAPMARKER) {
163                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
164                 glTranslatef(2.5, -4.5, 0); //from old code
165             } else {
166                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
167             }
168             it->texture.bind();
169             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
170             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
171             glBegin(GL_QUADS);
172             glTexCoord2f(0, 0);
173             glVertex3f(it->x, it->y, 0);
174             glTexCoord2f(1, 0);
175             glVertex3f(it->x + it->w, it->y, 0);
176             glTexCoord2f(1, 1);
177             glVertex3f(it->x + it->w, it->y + it->h, 0);
178             glTexCoord2f(0, 1);
179             glVertex3f(it->x, it->y + it->h, 0);
180             glEnd();
181             if (it->type != MenuItem::IMAGE) {
182                 //mouseover highlight
183                 for (int i = 0; i < 10; i++) {
184                     if (1 - ((float)i) / 10 - (1 - it->effectfade) > 0) {
185                         glColor4f(it->r, it->g, it->b, (1 - ((float)i) / 10 - (1 - it->effectfade))*.25);
186                         glBegin(GL_QUADS);
187                         glTexCoord2f(0, 0);
188                         glVertex3f(it->x - ((float)i) * 1 / 2, it->y - ((float)i) * 1 / 2, 0);
189                         glTexCoord2f(1, 0);
190                         glVertex3f(it->x + it->w + ((float)i) * 1 / 2, it->y - ((float)i) * 1 / 2, 0);
191                         glTexCoord2f(1, 1);
192                         glVertex3f(it->x + it->w + ((float)i) * 1 / 2, it->y + it->h + ((float)i) * 1 / 2, 0);
193                         glTexCoord2f(0, 1);
194                         glVertex3f(it->x - ((float)i) * 1 / 2, it->y + it->h + ((float)i) * 1 / 2, 0);
195                         glEnd();
196                     }
197                 }
198             }
199             glPopMatrix();
200             break;
201         case MenuItem::LABEL:
202         case MenuItem::BUTTON:
203             glColor4f(it->r, it->g, it->b, 1);
204             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
205             Game::text->glPrint(it->x, it->y, it->text.c_str(), 0, 1, 640, 480);
206             if (it->type != MenuItem::LABEL) {
207                 //mouseover highlight
208                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
209                 for (int i = 0; i < 15; i++) {
210                     if (1 - ((float)i) / 15 - (1 - it->effectfade) > 0) {
211                         glColor4f(it->r, it->g, it->b, (1 - ((float)i) / 10 - (1 - it->effectfade))*.25);
212                         Game::text->glPrint(it->x - ((float)i), it->y, it->text.c_str(), 0, 1 + ((float)i) / 70, 640, 480);
213                     }
214                 }
215             }
216             break;
217         case MenuItem::MAPLABEL:
218             Game::text->glPrintOutlined(0.9, 0, 0, it->x, it->y, it->text.c_str(), 0, 0.6, 640, 480);
219             break;
220         case MenuItem::MAPLINE: {
221             XYZ linestart;
222             linestart.x = it->x;
223             linestart.y = it->y;
224             linestart.z = 0;
225             XYZ lineend;
226             lineend.x = it->x + it->w;
227             lineend.y = it->y + it->h;
228             lineend.z = 0;
229             XYZ offset = lineend - linestart;
230             XYZ fac = offset;
231             Normalise(&fac);
232             offset = DoRotation(offset, 0, 0, 90);
233             Normalise(&offset);
234
235             linestart += fac * 4 * it->linestartsize;
236             lineend -= fac * 4 * it->lineendsize;
237
238             glDisable(GL_TEXTURE_2D);
239             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
240             glColor4f(it->r, it->g, it->b, 1);
241             glPushMatrix();
242             glTranslatef(2, -5, 0); //from old code
243             glBegin(GL_QUADS);
244             glVertex3f(linestart.x - offset.x * it->linestartsize, linestart.y - offset.y * it->linestartsize, 0.0f);
245             glVertex3f(linestart.x + offset.x * it->linestartsize, linestart.y + offset.y * it->linestartsize, 0.0f);
246             glVertex3f(lineend.x + offset.x * it->lineendsize, lineend.y + offset.y * it->lineendsize, 0.0f);
247             glVertex3f(lineend.x - offset.x * it->lineendsize, lineend.y - offset.y * it->lineendsize, 0.0f);
248             glEnd();
249             glPopMatrix();
250             glEnable(GL_TEXTURE_2D);
251         }
252         break;
253         default:
254         case MenuItem::NONE:
255         break;
256         }
257     }
258 }
259