File: MenuManager.cpp

package info (click to toggle)
criticalmass 1%3A1.0.0-1.5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 18,740 kB
  • sloc: ansic: 47,628; cpp: 25,167; sh: 12,025; xml: 3,532; perl: 3,271; makefile: 662; python: 66; awk: 40; lisp: 33
file content (523 lines) | stat: -rw-r--r-- 11,471 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// Description:
//   Menu Manager/Controller.
//
// Copyright (C) 2001 Frank Becker
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation;  either version 2 of the License,  or (at your option) any  later
// version.
//
// This program is distributed in the hope that it will be useful,  but  WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
//
#include "SDL.h" //key syms

#include <Trace.hpp>
#include <MenuManager.hpp>
#include <XMLHelper.hpp>
#include <GameState.hpp>
#include <Game.hpp>
#include <Audio.hpp>
#include <Input.hpp>
#include <FontManager.hpp>
#include <SelectableFactory.hpp>
#include <ModelManager.hpp>
#include <Video.hpp>
#include <BitmapManager.hpp>
#include <Config.hpp>

#include <gl++.hpp>

#include <GLExtensionTextureCubeMap.hpp>
#include <GLTextureCubeMap.hpp>
#include <ResourceManager.hpp>
#include <zrwops.hpp>

MenuManager::MenuManager():
    _menu(0),
    _topMenu(0),
    _currentMenu(0),
    _mouseX(200.0),
    _mouseY(650.0),
    _prevContext(Context::eUnknown),
    _delayedExit(false),
    _angle(0.0),
    _prevAngle(0.0),
    _showSparks(true),
    _burst( "SparkBurst", 1000),
    _nextGenShippyCubeMap(0)
{
    XTRACE();

    updateSettings();
}

MenuManager::~MenuManager()
{
    XTRACE();

    clearActiveSelectables();

    SelectableFactory::cleanup();

    delete _menu;
    _menu = 0;

    delete _nextGenShippyCubeMap;
}

bool MenuManager::init( void)
{
    XTRACE();
    _menu = XMLHelper::load( "system/Menu.xml");
    if( !_menu)
    {
	_menu = 0;
	return false;
    }

    _currentMenu = _menu->FirstChild("Menu");
    _topMenu = _currentMenu;

    loadMenuLevel();

    GLBitmapCollection *icons =
        BitmapManagerS::instance()->getBitmap( "bitmaps/menuIcons");
    if( !icons)
    {
	LOG_ERROR << "Unable to load menuIcons." << endl;
	return false;
    }
    _pointer = icons->getIndex( "Pointer");

    GLBitmapCollection *menuBoard =
        BitmapManagerS::instance()->getBitmap( "bitmaps/menuBoard");
    if( !menuBoard)
    {
	LOG_ERROR << "Unable to load menuBoard." << endl;
	return false;
    }
    _board = menuBoard->getIndex( "MenuBoard");

    _mapleLeaf = ModelManagerS::instance()->getModel("models/MapleLeaf");
    if( !_mapleLeaf)
    {
	LOG_ERROR << "Unable to load Maple Leaf, eh?" << endl;
	return false;
    }

    _nextGenShippy = ModelManagerS::instance()->getModel("models/NextGenShippy");
    if( !_nextGenShippy)
    {
	LOG_ERROR << "Unable to sphere."<< endl;
	return false;
    }

    GLExtensionTextureCubeMap _nextGenShippyExt;
    if( _nextGenShippyExt.isSupported())
    {
	SDL_Surface *images[6];
	for( int i=0; i<6; i++)
	{
	    char buf[128];
	    sprintf( buf, "bitmaps/cubemap_%d.png", i);
	    string fileName = buf;
	    if( ResourceManagerS::instance()->selectResource( fileName))
	    {
		ziStream &bminfile1 = ResourceManagerS::instance()->getInputStream();
		SDL_RWops *src = RWops_from_ziStream( bminfile1);
		images[i] = IMG_LoadPNG_RW( src);
		SDL_RWclose( src);
	    }
	    else
	    {
		LOG_ERROR << "Could not load cubemap image\n";
		images[i] = 0;
	    }
	}

	_nextGenShippyCubeMap = new GLTextureCubeMap( images);
    }
    else
    {
	LOG_WARNING << "ARB_texture_cube_map not supported\n";
    }

    _burst.init();

    return true;
}

void MenuManager::updateSettings( void)
{
    ConfigS::instance()->getBoolean( "showSparks", _showSparks);
}

void MenuManager::clearActiveSelectables( void)
{
    list<Selectable*>::iterator i;
    for( i=_activeSelectables.begin(); i!=_activeSelectables.end(); i++)
    {
        delete (*i);
    }
    _activeSelectables.clear();

    Selectable::reset();
}

void MenuManager::loadMenuLevel( void)
{
    clearActiveSelectables();

    TiXmlNode *node = _currentMenu->FirstChild();
    while( node)
    {
//	LOG_INFO << "MenuItem: [" << node->Value() << "]" << endl;

	SelectableFactory *selF = SelectableFactory::getFactory( node->Value());
	if( selF)
	{
	    Selectable *sel = selF->createSelectable( node);
	    if( sel)
	    {
		_activeSelectables.insert( _activeSelectables.end(), sel);
	    }
	}
	else
	{
	    LOG_WARNING << "No Factory found for:" << node->Value() << endl;
	}
	node = node->NextSibling();
    }

    //add escape button
    {
	BoundingBox r;
	r.min.x = 860;
	r.min.y = 530;
	r.max.x = 875;
	r.max.y = 545;

	Selectable *sel = new EscapeSelectable( r, 2.0);
	_activeSelectables.insert( _activeSelectables.end(), sel);
    }

    _currentSelectable = _activeSelectables.begin();
    if( _currentSelectable != _activeSelectables.end())
    {
	(*_currentSelectable)->activate();
    }
}

void MenuManager::makeMenu( TiXmlNode *_node)
{
    _currentMenu = _node;
    loadMenuLevel();
}

bool MenuManager::update( void)
{
    static float nextTime = Timer::getTime()+0.5f;
    float thisTime = Timer::getTime();
    if( thisTime > nextTime)
    {
	updateSettings();
	nextTime = thisTime+0.5f;
    }

    _onlineUpdateDisplay.update();

    _prevAngle = _angle;
    _angle += 10.0f;

    if( _delayedExit)
    {
	if( !Exit())
	{
	    turnMenuOff();
	}
	_delayedExit = false;
    }
    list<Selectable*>::iterator i;
    for( i=_activeSelectables.begin(); i!=_activeSelectables.end(); i++)
    {
        (*i)->update();
    }

    if( _showSparks)
    {
	GLBitmapCollection *icons =
	    BitmapManagerS::instance()->getBitmap( "bitmaps/menuIcons");
	float w = _mouseX + icons->getWidth( _pointer)*0.4f;
	float h = _mouseY - icons->getHeight( _pointer)*0.4f;
	for( int i=0; i<10; i++)
	{
	    _burst.newParticle( "Spark", w-i, h+i, 0);
	}

	_burst.update();
    }

    return true;
}

bool MenuManager::draw( void)
{
    glEnable( GL_LIGHTING);
    glEnable( GL_DEPTH_TEST);

    GLfloat light_position[] = { 820.0, 620.0, 500.0, 0.0 };
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glPushMatrix();
    glTranslatef( 820.0, 620.0, 0.0);
    float iAngle = _prevAngle+(_angle-_prevAngle)*GameState::frameFractionOther;
    glRotatef(iAngle, 0,1.0,0);
    _mapleLeaf->draw();
    glPopMatrix();

    if( _nextGenShippyCubeMap)
    {
	glPushMatrix();
	glTranslatef( 200.0, 620.0, 0.0);
	iAngle = _prevAngle+(_angle-_prevAngle)*GameState::frameFractionOther;
	glRotatef(iAngle/10.0, 0.0,1.0,0);
	_nextGenShippyCubeMap->bind();
	_nextGenShippyCubeMap->enable();
	_nextGenShippy->draw();
	_nextGenShippyCubeMap->disable();
	glPopMatrix();
    }

    glDisable( GL_DEPTH_TEST);
    glDisable( GL_LIGHTING);

    _onlineUpdateDisplay.draw();

    GLBitmapCollection *menuBoard =
        BitmapManagerS::instance()->getBitmap( "bitmaps/menuBoard");
    menuBoard->bind();
    glColor4f(1.0, 1.0, 1.0, 0.7f);
    glEnable(GL_TEXTURE_2D);
    menuBoard->Draw( _board, 100.0, 50.0, 800.0/256.0, 2.0);
    glDisable(GL_TEXTURE_2D);

    TiXmlElement* elem = _currentMenu->ToElement();
    const string* val = elem->Attribute("Text");
    if( val)
    {
	GLBitmapFont &fontWhite = 
	  *(FontManagerS::instance()->getFont( "bitmaps/menuWhite"));
	glColor4f(1.0, 1.0, 1.0, 1.0);
        fontWhite.DrawString( (*val).c_str(), 122, 527, 0.5, 0.5);
    }

    list<Selectable*>::iterator i;
    for( i=_activeSelectables.begin(); i!=_activeSelectables.end(); i++)
    {
        (*i)->draw();
    }

    glEnable(GL_TEXTURE_2D);
    if( _showSparks)
    {
	_burst.draw();
    }

    GLBitmapCollection *icons =
        BitmapManagerS::instance()->getBitmap( "bitmaps/menuIcons");
    icons->bind();
    glColor4f(1.0, 1.0, 1.0, 1.0);
    icons->Draw( _pointer, _mouseX, _mouseY, 0.5, 0.5);
    glDisable(GL_TEXTURE_2D);

    return true;
}

void MenuManager::reload( void)
{
    _nextGenShippyCubeMap->reload();
}

void MenuManager::turnMenuOn( void)
{
    AudioS::instance()->playSample( "sounds/humm.wav");
    _prevContext = GameState::context;
    GameState::context = Context::eMenu;

    //ask input system to forward all input to us
    InputS::instance()->enableInterceptor( this);
    GameState::stopwatch.pause();
}

void MenuManager::turnMenuOff( void)
{
    if( _prevContext == Context::eUnknown) return;

    AudioS::instance()->playSample( "sounds/humm.wav");
    GameState::context = _prevContext;

    //don't want anymore input
    InputS::instance()->disableInterceptor();

    if( GameState::context == Context::eInGame)
    {
	GameState::stopwatch.start();
    }
}

void MenuManager::input( const Trigger &trigger, const bool &isDown)
{
    Trigger t = trigger;
    if( isDown)
    {
	switch( trigger.type)
	{
	    case eKeyTrigger:
		switch( trigger.data1)
		{
		    case SDLK_RETURN:
			Enter();
			break;
		    case SDLK_ESCAPE:
			if( !Exit())
			{
			    turnMenuOff();
			}
			break;
		    case SDLK_UP:
			Up();
			break;
		    case SDLK_DOWN:
			Down();
			break;

		    case SDLK_F12:
			VideoS::instance()->takeSnapshot();
			break;

		    default:
			break;
		}
		break;

	    case eButtonTrigger:
		break;

	    case eMotionTrigger:
		_mouseX += (trigger.fData1*10.0f);
		_mouseY += (trigger.fData2*10.0f);

		Clamp( _mouseX, 0.0f, 1000.0f);
		Clamp( _mouseY, 0.0f, 750.0f);

		break;

	    default:
		break;
	}
    }

    //put the absolute mouse position in to trigger
    t.fData1 = _mouseX;
    t.fData2 = _mouseY;

    list<Selectable*>::iterator i;
    list<Selectable*>::iterator check = _activeSelectables.begin();
    for( i=_activeSelectables.begin(); i!=_activeSelectables.end(); i++)
    {
	Selectable *sel = *i;
	if( !sel)
	{
	    LOG_ERROR << "Selectable is 0 !!!" << endl;
	    continue;
	}
	const BoundingBox &r = sel->getInputBox();

	if( (_mouseX >= r.min.x) &&
	    (_mouseX <= r.max.x) &&
	    (_mouseY >= r.min.y) &&
	    (_mouseY <= r.max.y))
	{
	    sel->input( t, isDown);
	    //one of the selectables may trigger a loadMenuLevel and our
	    //iterator will be invalid. Drop out of this loop!
	    if( check != _activeSelectables.begin())
	    {
//		LOG_INFO << "active selectables have changed..." << endl;
		break;
	    }
	}
    }
}

void MenuManager::Down( void)
{
    XTRACE();
    if( _currentSelectable == _activeSelectables.end()) return;

    _currentSelectable++;
    if( _currentSelectable == _activeSelectables.end())
    {
	_currentSelectable = _activeSelectables.begin();
    }

    (*_currentSelectable)->activate();
}

void MenuManager::Up( void)
{
    XTRACE();
    if( _currentSelectable == _activeSelectables.end()) return;

    if( _currentSelectable == _activeSelectables.begin())
    {
	_currentSelectable = _activeSelectables.end();
    }
    _currentSelectable--;

    (*_currentSelectable)->activate();
}

void MenuManager::Goto( Selectable *s)
{
    list<Selectable*>::iterator i;
    for( i=_activeSelectables.begin(); i!=_activeSelectables.end(); i++)
    {
        if( (*i) == s)
	{
//	    LOG_INFO << "Goto found match" << endl;
	    break;
	}
    }
    _currentSelectable = i;
}

void MenuManager::Enter( void)
{
    XTRACE();
    (*_currentSelectable)->select();
}

bool MenuManager::Exit( bool delayed)
{
    XTRACE();
    if( delayed)
    {
	//while iterating over the selectables we dont want to loadMenuLevel
	_delayedExit = true;
	return true;
    }
    if( _currentMenu != _topMenu)
    {
	_currentMenu = _currentMenu->Parent();
	loadMenuLevel();
	AudioS::instance()->playSample( "sounds/humm.wav");
	return true;
    }

    //at the top level menu
    return false;
}