Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/simple_game_menu.cc @ 7019

Last change on this file since 7019 was 7019, checked in by bensch, 18 years ago

orxonox/trunk: Using TextEngine in the Menu

File size: 14.8 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Patrick Boenzli
13
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
17
18
19#include "simple_game_menu.h"
20
21#include "state.h"
22#include "class_list.h"
23
24#include "load_param.h"
25#include "fast_factory.h"
26#include "factory.h"
27
28#include "p_node.h"
29#include "world_entity.h"
30#include "elements/image_entity.h"
31#include "terrain.h"
32#include "camera.h"
33
34#include "event_handler.h"
35#include "graphics_engine.h"
36#include "object_manager.h"
37
38
39#include "cd_engine.h"
40
41
42using namespace std;
43
44
45//! This creates a Factory to fabricate a SimpleGameMenu
46CREATE_FACTORY(SimpleGameMenu, CL_SIMPLE_GAME_MENU);
47
48
49
50SimpleGameMenu::SimpleGameMenu(const TiXmlElement* root)
51  : GameWorld()
52{
53  this->setClassID(CL_SIMPLE_GAME_MENU, "SimpleGameMenu");
54  this->setName("SimpleGameMenu uninitialized");
55
56  this->dataTank = new SimpleGameMenuData();
57
58  this->cameraVector = Vector(50.0, 0.0, 0.0);
59  this->menuLayers.push_back(MenuLayer());
60  this->menuLayers.push_back(MenuLayer());
61
62  this->layerIndex = 0;
63  this->menuSelectedIndex = 0;
64
65  this->loadParams(root);
66
67  State::setMenuMode(true);
68}
69
70
71/**
72 *  remove the SimpleGameMenu from memory
73 *
74 *  delete everything explicitly, that isn't contained in the parenting tree!
75 *  things contained in the tree are deleted automaticaly
76 */
77SimpleGameMenu::~SimpleGameMenu ()
78{
79  PRINTF(3)("SimpleGameMenu::~SimpleGameMenu() - deleting current world\n");
80
81  if( this->dataTank)
82    delete this->dataTank;
83}
84
85
86/**
87 * loads the parameters of a SimpleGameMenu from an XML-element
88 * @param root the XML-element to load from
89 */
90void SimpleGameMenu::loadParams(const TiXmlElement* root)
91{
92  /* skip the GameWorld, since it does not define any useful loadParams for this class */
93  //static_cast<GameWorld*>(this)->loadParams(root);
94  GameWorld::loadParams(root);
95
96  PRINTF(4)("Loaded SimpleGameMenu specific stuff\n");
97}
98
99
100/**
101 * this is executed just before load
102 *
103 * since the load function sometimes needs data, that has been initialized
104 * before the load and after the proceeding storyentity has finished
105 */
106ErrorMessage SimpleGameMenu::init()
107{
108  /* call underlying init funciton */
109  GameWorld::init();
110
111  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_UP);
112  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_DOWN);
113  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_RETURN);
114  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_SPACE);
115  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_ESCAPE);
116
117  this->dataTank->localCamera->setRelCoor(this->cameraVector);
118
119  GraphicsEngine::getInstance()->displayFPS(false);
120
121  this->layerIndex = 0;
122  this->menuSelectedIndex = 0;
123}
124
125
126/**
127 * load the data
128 */
129ErrorMessage SimpleGameMenu::loadData()
130{
131  GameWorld::loadData();
132
133  if (this->dataXML != NULL)
134  {
135    TiXmlElement* element = this->dataXML->FirstChildElement("Elements");
136
137    if( element == NULL)
138    {
139      PRINTF(1)("SimpleGameMenu is missing 'Elements'\n");
140    }
141    else
142    {
143      element = element->FirstChildElement();
144    // load Players/Objects/Whatever
145      PRINTF(4)("Loading Elements\n");
146      while( element != NULL)
147      {
148        BaseObject* created = Factory::fabricate(element);
149        if( created != NULL )
150        {
151          PRINTF(4)("Created a %s::%s\n", created->getClassName(), created->getName());
152          if (!created->isA(CL_ELEMENT_2D))
153            PRINTF(2)("Error the Created Entity is not an Element2D but an %s::%s\n", created->getClassName(), created->getName());
154        }
155        element = element->NextSiblingElement();
156      }
157      PRINTF(4)("Done loading Elements\n");
158    }
159  }
160
161  /* get the menu list */
162  const std::list<BaseObject*>* imageEntityList = ClassList::getList(CL_IMAGE_ENTITY);
163  std::list<BaseObject*>::const_iterator entity;
164  for (entity = imageEntityList->begin(); entity != imageEntityList->end(); entity++)
165  {
166
167    if( !strcmp("Selector_Menu", (*entity)->getName()))
168    {
169      this->menuSelector = dynamic_cast<ImageEntity*>(*entity);
170      this->menuSelector->setBindNode((const PNode*)NULL);
171    }
172  }
173
174  imageEntityList = ClassList::getList(CL_TEXT_ELEMENT);
175  for (entity = imageEntityList->begin(); entity != imageEntityList->end(); entity++)
176  {
177    if( !strcmp( "StartGame_Menu", (*entity)->getName()))
178    {
179      this->menuStartGame = dynamic_cast<TextElement*>(*entity);
180      this->menuStartGame->setBindNode((const PNode*)NULL);
181      this->menuStartGame->setRelCoor2D(State::getResX() / 2.0f,
182                                        State::getResY() / 2.0f - 60.0f,
183                                        0.0f);
184      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
185
186    }
187    else if( !strcmp( "Multiplayer_Menu", (*entity)->getName()))
188    {
189      this->menuStartMultiplayerGame = dynamic_cast<TextElement*>(*entity);
190      this->menuStartMultiplayerGame->setBindNode((const PNode*)NULL);
191      this->menuStartMultiplayerGame->setRelCoor2D(State::getResX() / 2.0f,
192                                                   State::getResY() / 2.0f + ((this->menuLayers[0].menuList.size() -1 ) * 60.0f),
193                                                   0.0f);
194      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
195    }
196    else if( !strcmp( "Quit_Menu", (*entity)->getName()))
197    {
198      this->menuQuitGame = dynamic_cast<TextElement*>(*entity);
199      this->menuQuitGame->setBindNode((const PNode*)NULL);
200      this->menuQuitGame->setRelCoor2D(State::getResX() / 2.0f,
201                                       State::getResY() / 2.0f + ((this->menuLayers[0].menuList.size() -1 )* 60.0f),
202                                       0.0f);
203      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
204    }
205  }
206  this->menuSelected->getNullElement()->update2D(0.1f);
207  this->menuSelectedIndex = 0;
208  this->menuSelected = this->menuLayers[0].menuList[this->menuSelectedIndex];
209  this->menuSelector->setAbsCoor2D(this->menuSelected->getAbsCoor2D());
210
211
212  // loading the storyentities submenu (singleplayer)
213  const std::list<BaseObject*>* storyEntities = ClassList::getList(CL_STORY_ENTITY);
214  std::list<BaseObject*>::const_iterator it;
215  for( it = storyEntities->begin(); it != storyEntities->end(); it++)
216  {
217    StoryEntity* se = dynamic_cast<StoryEntity*>(*it);
218    if( se->isContainedInMenu())
219    {
220      this->menuLayers[1].storyList.push_back(se);
221
222      // generating menu item
223      TextElement* te = new TextElement();
224      te->setVisibility(false);
225      te->setText(se->getName());
226      te->setRelCoor2D(State::getResX() / 2.0f - 200.0f, State::getResY() / 2.0f + ((this->menuLayers[1].menuList.size() - 2.0f) * 60.0f), 0.0f);
227      this->menuLayers[1].menuList.push_back(te);
228
229      // generating screenshoot item
230      ImageEntity* ie = new ImageEntity();
231      ie->setVisibility(false);
232      ie->setBindNode((const PNode*)NULL);
233      ie->setTexture(se->getMenuScreenshoot());
234      ie->setRelCoor2D(State::getResX() / 2.0f + 250.0f, State::getResY() / 2.0f, 0.0f);
235      ie->setSize2D(140.0f, 105.0f);
236      this->menuLayers[1].screenshootList.push_back(ie);
237    }
238  }
239}
240
241
242ErrorMessage SimpleGameMenu::unloadData()
243{
244  GameWorld::unloadData();
245
246  EventHandler::getInstance()->unsubscribe(this, ES_MENU);
247
248  std::vector<MenuLayer>::iterator mit;
249  for(mit = this->menuLayers.begin(); mit != this->menuLayers.end(); mit++)
250  {
251    while(!(*mit).menuList.empty())
252    {
253      delete (*mit).menuList.back();
254      (*mit).menuList.pop_back();
255    }
256
257    (*mit).menuList.clear();
258    (*mit).storyList.clear();
259    (*mit).screenshootList.clear();
260  }
261}
262
263
264/**
265 * start the menu
266 */
267bool SimpleGameMenu::start()
268{
269  EventHandler::getInstance()->pushState(ES_MENU);
270
271  /* now call the underlying*/
272  GameWorld::start();
273}
274
275
276
277/**
278 * stop the menu
279 */
280bool SimpleGameMenu::stop()
281{
282  EventHandler::getInstance()->popState();
283
284  /* now call the underlying*/
285  GameWorld::stop();
286}
287
288
289/**
290 *  override the standard tick for more functionality
291 */
292void SimpleGameMenu::tick()
293{
294  GameWorld::tick();
295
296  this->animateScene(this->dt);
297}
298
299
300/**
301 *  no collision detection in the menu
302 */
303void SimpleGameMenu::collide()
304{
305//   this->dataTank->localCamera->
306}
307
308
309/**
310 *  animate the scene
311 */
312void SimpleGameMenu::animateScene(float dt)
313{
314  Quaternion q(/*0.00005*/ 0.0001* dt, Vector(0.0, 1.0, 0.0));
315  this->cameraVector = q.apply(this->cameraVector);
316  this->dataTank->localCamera->setRelCoor(this->cameraVector);
317  this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0);
318}
319
320
321/**
322 * event dispatcher funciton
323 * @param event the incoming event
324 */
325void SimpleGameMenu::process(const Event &event)
326{
327  /* ----------------- LAYER 1 ---------------*/
328  if( this->layerIndex == 0)
329  {
330    if( event.type == SDLK_RETURN && event.bPressed == true)
331    {
332      if( this->menuSelected == this->menuQuitGame)
333      {
334        this->setNextStoryID(WORLD_ID_GAMEEND);
335        this->stop();
336      }
337      if( this->menuSelected == this->menuStartGame)
338      {
339        // switch to first submenu
340        if( this->menuLayers[1].menuList.size() == 0)
341        {
342          PRINTF(1)("Haven't got any StoryEntities to play!\n");
343          return;
344        }
345
346        this->switchMenuLayer(this->layerIndex, 1);
347      }
348    }
349    if( event.type == SDLK_ESCAPE && event.bPressed == true)
350    {
351      this->setNextStoryID(WORLD_ID_GAMEEND);
352      this->stop();
353    }
354  }  /* ----------------- LAYER 2 ---------------*/
355  else if( this->layerIndex == 1)
356  {
357    if( event.type == SDLK_RETURN && event.bPressed == true)
358    {
359      this->setNextStoryID( this->menuLayers[1].storyList[this->menuSelectedIndex]->getStoryID());
360      this->stop();
361    }
362    if( event.type == SDLK_ESCAPE && event.bPressed == true)
363    {
364      this->switchMenuLayer(this->layerIndex, 0);
365    }
366  }
367
368
369
370  // The menu selction cursor
371  if( event.type == SDLK_DOWN && event.bPressed == true)
372  {
373    if(this->menuSelectedIndex < (this->menuLayers[this->layerIndex].menuList.size() - 1))
374    {
375      this->menuSelected = this->menuLayers[this->layerIndex].menuList[++this->menuSelectedIndex];
376      this->menuSelector->setAbsCoor2D(this->menuSelected->getAbsCoor2D());
377
378      if( this->layerIndex == 1)
379      {
380        this->menuLayers[1].screenshootList[this->menuSelectedIndex]->setVisibility(true);
381        this->menuLayers[1].screenshootList[this->menuSelectedIndex-1]->setVisibility(false);
382      }
383    }
384  }
385  else if( event.type == SDLK_UP && event.bPressed == true)
386  {
387    if(this->menuSelectedIndex > 0)
388    {
389      this->menuSelected = this->menuLayers[this->layerIndex].menuList[--this->menuSelectedIndex];
390      this->menuSelector->setAbsCoor2D(this->menuSelected->getAbsCoor2D());
391
392      if( this->layerIndex == 1)
393      {
394        this->menuLayers[1].screenshootList[this->menuSelectedIndex]->setVisibility(true);
395        this->menuLayers[1].screenshootList[this->menuSelectedIndex+1]->setVisibility(false);
396      }
397    }
398  }
399}
400
401
402/**
403 *  switches to from one meny layer to an other
404 * @param layer1 from layer
405 * @param layer2 to layer
406 */
407void SimpleGameMenu::switchMenuLayer(int layer1, int layer2)
408{
409  // wrong sizes
410  if(layer1 >= this->menuLayers.size() || layer2 >= this->menuLayers.size())
411    return;
412
413
414  PRINTF(0)("Removing layer %i\n", layer1);
415  std::vector<TextElement*>::iterator te;
416  // fade old menu
417  for( te = this->menuLayers[layer1].menuList.begin(); te != this->menuLayers[layer1].menuList.end(); te++)
418  {
419    (*te)->setVisibility(false);
420  }
421
422  std::vector<ImageEntity*>::iterator it;
423
424  //also fade the screenshots if in level choosement mode
425  for( it = this->menuLayers[layer1].screenshootList.begin(); it != this->menuLayers[layer1].screenshootList.end(); it++)
426  {
427    (*it)->setVisibility(false);
428  }
429
430
431  PRINTF(0)("Showing layer %i\n", layer1);
432  // beam here the new menu
433  for( te = this->menuLayers[layer2].menuList.begin(); te != this->menuLayers[layer2].menuList.end(); te++ )
434  {
435    (*te)->setVisibility(true);
436  }
437
438
439  this->layerIndex = layer2;
440  this->menuSelected = this->menuLayers[layer2].menuList[0];
441  this->menuSelector->setAbsCoor2D(this->menuSelected->getAbsCoor2D());
442  this->menuSelectedIndex = 0;
443
444  if( layer2 == 1)
445    this->menuLayers[layer2].screenshootList[0]->setVisibility(true);
446}
447
448
449
450
451/**********************************************************************************************
452    SimpleGameMenuData
453 **********************************************************************************************/
454
455
456/**
457 * SimpleGameMenuData constructor
458 */
459SimpleGameMenuData::SimpleGameMenuData()
460{}
461
462/**
463 * SimpleGameMenuData decontructor
464 */
465SimpleGameMenuData::~SimpleGameMenuData()
466{}
467
468
469/**
470 *  initialize the GameWorldDataData
471 */
472ErrorMessage SimpleGameMenuData::init()
473{
474  /* call underlying function */
475  GameWorldData::init();
476}
477
478
479/**
480 *  loads the GUI data
481 * @param root reference to the xml root element
482 */
483ErrorMessage SimpleGameMenuData::loadGUI(TiXmlElement* root)
484{
485  /* call underlying function */
486  GameWorldData::loadGUI(root);
487}
488
489
490/**
491 *  unloads the GUI data
492 */
493ErrorMessage SimpleGameMenuData::unloadGUI()
494{
495  /* call underlying function */
496  GameWorldData::unloadGUI();
497}
498
499
500/**
501 *  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
502 * @param root reference to the xml root parameter
503 */
504ErrorMessage SimpleGameMenuData::loadWorldEntities(TiXmlElement* root)
505{
506  TiXmlElement* element = root->FirstChildElement("WorldEntities");
507
508  if( element != NULL)
509  {
510    element = element->FirstChildElement();
511    PRINTF(4)("Loading WorldEntities\n");
512    while( element != NULL)
513    {
514      BaseObject* created = Factory::fabricate(element);
515      if( created != NULL )
516        printf("Created a %s: %s\n", created->getClassName(), created->getName());
517
518      if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
519        this->sky = dynamic_cast<WorldEntity*>(created);
520      if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
521        this->terrain = dynamic_cast<Terrain*>(created);
522      element = element->NextSiblingElement();
523    }
524    PRINTF(4)("Done loading WorldEntities\n");
525  }
526
527  /* init the pnode tree */
528  PNode::getNullParent()->init();
529}
530
531
532/**
533 *  unloads the world entities from the xml file
534 */
535ErrorMessage SimpleGameMenuData::unloadWorldEntities()
536{
537  /* call underlying function */
538  GameWorldData::unloadWorldEntities();
539}
540
541
542/**
543 *  loads the scene data
544 * @param root reference to the xml root element
545 */
546ErrorMessage SimpleGameMenuData::loadScene(TiXmlElement* root)
547{
548  /* call underlying function */
549  GameWorldData::loadScene(root);
550}
551
552
553/**
554 *  unloads the scene data
555 */
556ErrorMessage SimpleGameMenuData::unloadScene()
557{
558  /* call underlying function */
559  GameWorldData::unloadScene();
560}
561
562
563
Note: See TracBrowser for help on using the repository browser.