~ubuntu-branches/ubuntu/quantal/zaz/quantal

« back to all changes in this revision

Viewing changes to src/menu.cpp

  • Committer: Package Import Robot
  • Author(s): Miriam Ruiz
  • Date: 2011-11-16 02:28:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111116022810-d3tehh974q280vit
Tags: 1.0.0~dfsg1-1
* New Upstream Release
* Upgraded Standards-Version from 3.8.3 to 3.9.2
* New homepage: http://phuzzboxmedia.com/index.php/games/open-sourced-zaz
* Refreshed patches
* Moved package to DebSrc 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 */
18
18
 
19
19
#include <iostream>
 
20
 
20
21
#include "menu.h"
21
22
 
22
23
#define RENDERHEIGHT 5
23
24
 
24
25
Menu::Menu(float x, float y, float width)
25
 
        : x(x), y(y), width(width), posCalculated(false), lastClickedItem(0)
 
26
        : x(x), y(y), lmx(0.0), lmy(0.0), width(width), hoverItem(0), posCalculated(false), lastClickedItem(0)
26
27
{
27
28
};
28
29
 
29
30
Menu::Menu()
30
 
        : posCalculated(false)
 
31
        : lmx(0.0), lmy(0.0), hoverItem(0), posCalculated(false), lastClickedItem(0)
31
32
{
32
33
 
33
34
};
34
35
 
35
36
Menu::~Menu()
36
37
{
37
 
    std::vector<MenuItem *>::iterator i;
38
 
 
39
 
    for (i = items.begin(); i != items.end(); ++i)
40
 
        delete (*i);
 
38
    Clear();
41
39
};
42
40
 
43
41
void Menu::Add(const std::string txt)
52
50
    this->width = width;
53
51
    CalculatePositions();
54
52
    posCalculated = true;
55
 
};
 
53
 
 
54
    if (items.size() > 0)
 
55
    {
 
56
        if (hoverItem < items.size() - 1)
 
57
        {
 
58
            items[hoverItem]->hover = true;
 
59
        }
 
60
        else
 
61
        {
 
62
            items[0]->hover = true;
 
63
        }
 
64
    }
 
65
}
56
66
 
57
67
void Menu::Render()
58
68
{
81
91
    }
82
92
};
83
93
 
84
 
void Menu::Logic(double mx, double my, bool click)
 
94
void Menu::setHoverItem(uint i)
 
95
{
 
96
    hoverItem = i;
 
97
    for (uint f = 0; f < items.size(); ++f)
 
98
    {
 
99
        items[f]->hover = false;
 
100
    }
 
101
 
 
102
    if (items.size() > 0)
 
103
        items[hoverItem]->hover = true;
 
104
}
 
105
 
 
106
void Menu::Logic(Scenes::FrameEvents events)//double mx, double my, bool click)
85
107
{
86
108
    if (!posCalculated)
87
109
    {
89
111
        posCalculated = true;
90
112
    }
91
113
 
92
 
    // fix hovers
 
114
    double mx = events.mouseX;
 
115
    double my = events.mouseY;
 
116
    bool click = false;
 
117
    bool mouseMoved = false;
 
118
 
 
119
    if (lmx != mx)
 
120
        mouseMoved = true;
 
121
 
 
122
    if (lmy != my)
 
123
        mouseMoved = true;
 
124
 
 
125
    if (events.buttDown[0])
 
126
    {
 
127
        click = true;
 
128
        mouseMoved = true;
 
129
    }
 
130
 
 
131
    // fix hovers triggered by mouse
 
132
    if (mouseMoved)
 
133
        for (uint f = 0; f < items.size(); ++f)
 
134
        {
 
135
            //std::cout << items[f]->x << ":" << items[f]->y << " - " << items[f]->width << ":" << items[f]->height << " --- " << mx << ":" << my << std::endl;
 
136
 
 
137
            if ((items[f]->x < mx)
 
138
                    && (items[f]->x + items[f]->width > mx)
 
139
                    && (items[f]->y > my)
 
140
                    && (items[f]->y - items[f]->height < my))
 
141
            {
 
142
                hoverItem = f;
 
143
            }
 
144
        }
 
145
 
 
146
    // fix hovers by keyboard
 
147
    if (!events.empty)
 
148
    {
 
149
        if (events.keyDown.size() > 0)
 
150
            for (vector<SDLKey>::iterator i = events.keyDown.begin(); i != events.keyDown.end(); ++i)
 
151
            {
 
152
                bool sendKey = true;
 
153
 
 
154
                if (*i == SDLK_UP)
 
155
                {
 
156
                    sendKey = false;
 
157
                    if (hoverItem > 0)
 
158
                    {
 
159
                        hoverItem--;
 
160
                    }
 
161
                    else hoverItem = items.size() - 1;
 
162
                }
 
163
 
 
164
                if (*i == SDLK_DOWN)
 
165
                {
 
166
                    sendKey = false;
 
167
                    hoverItem++;
 
168
                    if (hoverItem == items.size())
 
169
                        hoverItem = 0;
 
170
                }
 
171
 
 
172
                if (!items.empty())
 
173
                    if (sendKey && hoverItem < items.size())
 
174
                        items[hoverItem]->Key(*i);
 
175
            }
 
176
    }
 
177
 
93
178
    for (uint f = 0; f < items.size(); ++f)
94
179
    {
95
 
        //std::cout << items[f]->x << ":" << items[f]->y << " - " << items[f]->width << ":" << items[f]->height << " --- " << mx << ":" << my << std::endl;
96
 
 
97
 
        if ((items[f]->x < mx)
98
 
                && (items[f]->x + width > mx)
99
 
                && (items[f]->y > my)
100
 
                && (items[f]->y - items[f]->height < my))
101
 
        {
102
 
            items[f]->hover = true;
103
 
        }
104
 
        else
105
 
        {
106
 
            items[f]->hover = false;
107
 
        }
 
180
        items[f]->hover = false;
108
181
    }
109
182
 
 
183
    if (items.size() > 0)
 
184
        items[hoverItem]->hover = true;
 
185
 
110
186
    if (click)
111
187
    {
112
188
        for (uint f = 0; f < items.size(); ++f)
114
190
            //std::cout << items[f]->x << ":" << items[f]->y << " - " << items[f]->width << ":" << items[f]->height << " --- " << mx << ":" << my << std::endl;
115
191
 
116
192
            if ((items[f]->x < mx)
117
 
                    && (items[f]->x + width > mx)
 
193
                    && (items[f]->x + items[f]->width > mx)
118
194
                    && (items[f]->y > my)
119
195
                    && (items[f]->y - items[f]->height < my))
120
196
                items[f]->Click((int)mx - items[f]->x, (int)my - items[f]->y);
122
198
            lastClickedItem = f;
123
199
        }
124
200
    }
 
201
 
 
202
    lmx = mx;
 
203
    lmy = my;
125
204
};
126
205
 
127
206
void OptionMenuItem::Render()
128
207
{
 
208
    if (!show)
 
209
        return;
 
210
 
129
211
    GenericMenuItem::Render();
130
212
    glPushMatrix();
131
 
    FTBBox b = font->BBox(values[v].c_str());
 
213
    const char *val = values[v].c_str();
 
214
 
 
215
    if (useDescriptions)
 
216
    {
 
217
        val = descriptions[v].c_str();
 
218
    }
 
219
 
 
220
    FTBBox b = font->BBox(val);
132
221
    glTranslated(x + (width - (b.Upper().X() / 6.67)) - 2, y - height / 2 - 1, RENDERZHEIGHT + 1);
133
222
    glScaled(0.15, 0.15, 0.15);
134
 
    font->Render(values[v].c_str());
 
223
    font->Render(val);
135
224
    glPopMatrix();
136
225
}
137
226
 
138
227
int OptionMenuItem::GetVNum()
139
228
{
 
229
    if (!settings)
 
230
        return 0;
 
231
 
140
232
    std::string opt = settings->get(option, "");
141
233
 
142
234
    for (uint f = 0; f < values.size(); ++f)
150
242
 
151
243
void BooleanMenuItem::Render()
152
244
{
 
245
    if (!show)
 
246
        return;
 
247
 
153
248
    GenericMenuItem::Render();
154
249
 
155
250
    // draw tickbox
178
273
 
179
274
void ValueMenuItem::Render()
180
275
{
 
276
    if (!show)
 
277
        return;
 
278
 
181
279
    GenericMenuItem::Render();
182
280
    glLoadIdentity();
183
281
    glLineWidth(2.0);
223
321
    settings->set(option, vs.str());
224
322
};
225
323
 
 
324
void ValueMenuItem::Key(SDLKey k)
 
325
{
 
326
    std::stringstream vs;
 
327
 
 
328
    if (k == SDLK_RIGHT)
 
329
        value++;
 
330
 
 
331
    if (k == SDLK_LEFT)
 
332
        value--;
 
333
 
 
334
    if (k == SDLK_END)
 
335
        value = maximum;
 
336
 
 
337
    if (k == SDLK_HOME)
 
338
        value = minimum;
 
339
 
 
340
    if (k == SDLK_PAGEUP)
 
341
        value += (maximum - minimum) / 10;
 
342
 
 
343
    if (k == SDLK_PAGEDOWN)
 
344
        value -= (maximum - minimum) / 10;
 
345
 
 
346
    if (value < minimum)
 
347
        value = minimum;
 
348
 
 
349
    if (value > maximum)
 
350
        value = maximum;
 
351
 
 
352
    vs << value;
 
353
 
 
354
    settings->set(option, vs.str());
 
355
}
226
356
 
227
357
void GenericMenuItem::Render()
228
358
{
 
359
    if (!show)
 
360
        return;
 
361
 
229
362
    glLoadIdentity();
230
363
    if (hover)
231
364
    {
232
365
        glPushMatrix();
233
366
        glTranslatef(x, y, RENDERZHEIGHT);
234
367
        glBegin(GL_QUADS);
235
 
        glColor3f( 0, 0, 1.0);
 
368
        glColor3f( .3, .3, 1.0);
236
369
        glVertex3d(0, 0, 0);
237
370
        glVertex3d(0, -height, 0);
238
371
        glColor3d( .1, .1, .7);
239
372
        glVertex3d(width, -height, 0);
240
373
        glVertex3d(width, 0, 0);
241
374
        glEnd();
 
375
 
 
376
        glTranslatef(0.5, -0.5, -1);
 
377
        glBegin(GL_QUADS);
 
378
        glColor3f( 0, 0, .2);
 
379
        glVertex3d(0, 0, 0);
 
380
        glVertex3d(0, -height, 0);
 
381
        glVertex3d(width, -height, 0);
 
382
        glVertex3d(width, 0, 0);
 
383
        glEnd();
 
384
 
 
385
 
242
386
        glPopMatrix();
243
387
    }
244
388
    else
253
397
        glVertex3d(width, -height, 0);
254
398
        glVertex3d(width, 0, 0);
255
399
        glEnd();
 
400
 
 
401
        glTranslatef(0.5, -0.5, -1);
 
402
        glBegin(GL_QUADS);
 
403
        glColor3f( 0, 0, .2);
 
404
        glVertex3d(0, 0, 0);
 
405
        glVertex3d(0, -height, 0);
 
406
        glVertex3d(width, -height, 0);
 
407
        glVertex3d(width, 0, 0);
 
408
        glEnd();
 
409
 
 
410
 
256
411
        glPopMatrix();
257
412
    }
258
413