Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

gui: introduce inputline

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