Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

gui: delete event

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