Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/story_entities/simple_game_menu.cc @ 7880

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

focusable widgets

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