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
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
66  /// GUI
67  ///(this is as modular as it is possible).
68  OrxGui::GLGuiPushButton* pb = new OrxGui::GLGuiPushButton("PUSH ME");
69  pb->show();
70  pb->setAbsCoor2D(50, 50);
71
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);
79  rdnpb->connectSignal(OrxGui::Signal_release, this, createExecutor<SimpleGameMenu>(&SimpleGameMenu::quitMenu));
80
81  OrxGui::GLGuiInputLine* input = new OrxGui::GLGuiInputLine();
82  input->show();
83
84  OrxGui::GLGuiHandler::getInstance()->activateCursor();
85  /////
86  if (root != NULL)
87    this->loadParams(root);
88
89  State::setMenuID(this->getNextStoryID());
90}
91
92
93/**
94 *  @brief remove the SimpleGameMenu from memory
95 *
96 *  delete everything explicitly, that isn't contained in the parenting tree!
97 *  things contained in the tree are deleted automaticaly
98 */
99SimpleGameMenu::~SimpleGameMenu ()
100{
101  PRINTF(3)("SimpleGameMenu::~SimpleGameMenu() - deleting current world\n");
102
103  if( this->dataTank)
104    delete this->dataTank;
105  delete OrxGui::GLGuiHandler::getInstance( );
106}
107
108
109/**
110 * @brief loads the parameters of a SimpleGameMenu from an XML-element
111 * @param root the XML-element to load from
112 */
113void SimpleGameMenu::loadParams(const TiXmlElement* root)
114{
115  /* skip the GameWorld, since it does not define any useful loadParams for this class */
116  //static_cast<GameWorld*>(this)->loadParams(root);
117  GameWorld::loadParams(root);
118
119  PRINTF(4)("Loaded SimpleGameMenu specific stuff\n");
120}
121
122
123/**
124 * @brief this is executed just before load
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
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);
139
140  this->dataTank->localCamera->setRelCoor(this->cameraVector);
141
142  GraphicsEngine::getInstance()->displayFPS(false);
143
144  this->layerIndex = 0;
145  this->menuSelectedIndex = 0;
146}
147
148
149/**
150 * @brief load the data
151 */
152ErrorMessage SimpleGameMenu::loadData()
153{
154  GameWorld::loadData();
155
156  if (this->dataXML != NULL)
157  {
158    LoadParam(dataXML, "selector-sound", this, SimpleGameMenu, setSelectorSound);
159
160    TiXmlElement* element = this->dataXML->FirstChildElement("Elements");
161
162
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 )
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        }
181        element = element->NextSiblingElement();
182      }
183      PRINTF(4)("Done loading Elements\n");
184    }
185  }
186
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);
196      this->menuSelector->setBindNode((const PNode*)NULL);
197    }
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()))
204    {
205      this->menuStartGame = dynamic_cast<TextElement*>(*entity);
206      this->menuStartGame->setBindNode((const PNode*)NULL);
207      this->menuStartGame->setRelCoor2D(State::getResX() / 2.0f,
208                                        State::getResY() / 2.0f - 60.0f);
209      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
210
211    }
212    else if( !strcmp( "Multiplayer_Menu", (*entity)->getName()))
213    {
214      this->menuStartMultiplayerGame = dynamic_cast<TextElement*>(*entity);
215      this->menuStartMultiplayerGame->setBindNode((const PNode*)NULL);
216      this->menuStartMultiplayerGame->setRelCoor2D(State::getResX() / 2.0f,
217                                                   State::getResY() / 2.0f + ((this->menuLayers[0].menuList.size() -1 ) * 60.0f));
218      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
219    }
220    else if( !strcmp( "Quit_Menu", (*entity)->getName()))
221    {
222      this->menuQuitGame = dynamic_cast<TextElement*>(*entity);
223      this->menuQuitGame->setBindNode((const PNode*)NULL);
224      this->menuQuitGame->setRelCoor2D(State::getResX() / 2.0f,
225                                       State::getResY() / 2.0f + ((this->menuLayers[0].menuList.size() -1 )* 60.0f));
226      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
227    }
228  }
229  this->menuSelected->getNullElement()->update2D(0.1f);
230  this->menuSelectedIndex = 0;
231  this->menuSelected = this->menuLayers[0].menuList[this->menuSelectedIndex];
232  this->sliderTo(this->menuSelected, 0.0f);
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    {
243      this->menuLayers[1].storyList.push_back(se);
244
245      // generating menu item
246      TextElement* te = new TextElement();
247      te->setVisibility(false);
248      te->setText(se->getName());
249      te->setRelCoor2D(State::getResX() / 2.0f - 200.0f, State::getResY() / 2.0f + ((this->menuLayers[1].menuList.size() - 2.0f) * 60.0f));
250      this->menuLayers[1].menuList.push_back(te);
251
252      // generating screenshoot item
253      ImageEntity* ie = new ImageEntity();
254      ie->setVisibility(false);
255      ie->setBindNode((const PNode*)NULL);
256      ie->setTexture(se->getMenuScreenshoot());
257      ie->setRelCoor2D(State::getResX() / 2.0f + 250.0f, State::getResY() / 2.0f);
258      ie->setSize2D(140.0f, 105.0f);
259      this->menuLayers[1].screenshootList.push_back(ie);
260    }
261  }
262}
263
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{
270  this->selectorSource = OrxSound::SoundEngine::getInstance()->createSource(selectorSound, NULL);
271}
272
273ErrorMessage SimpleGameMenu::unloadData()
274{
275  this->unsubscribeEvents(ES_MENU);
276
277  std::vector<MenuLayer>::iterator mit;
278  for(mit = this->menuLayers.begin(); mit != this->menuLayers.end(); mit++)
279  {
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();
288    (*mit).screenshootList.clear();
289  }
290
291  // delete the SoundSource.
292  if (this->selectorSource != NULL)
293    delete this->selectorSource;
294  this->selectorSource = NULL;
295
296  GameWorld::unloadData();
297}
298
299
300/**
301 * @brief start the menu
302 */
303bool SimpleGameMenu::start()
304{
305  EventHandler::getInstance()->pushState(ES_MENU);
306
307  /* now call the underlying*/
308  GameWorld::start();
309}
310
311
312
313/**
314 * stop the menu
315 */
316bool SimpleGameMenu::stop()
317{
318  EventHandler::getInstance()->popState();
319
320  /* now call the underlying*/
321  GameWorld::stop();
322}
323
324
325/**
326 *  override the standard tick for more functionality
327 */
328void SimpleGameMenu::tick()
329{
330  GameWorld::tick();
331
332  // Make the GLGui tick.
333  OrxGui::GLGuiHandler::getInstance()->tick(this->dtS);
334
335  this->animateScene(this->dtS);
336}
337
338
339/**
340 * @brief no collision detection in the menu
341 */
342void SimpleGameMenu::collide()
343{
344//   this->dataTank->localCamera->
345}
346
347
348/**
349 * @brief animate the scene
350 */
351void SimpleGameMenu::animateScene(float dt)
352{
353  Quaternion q(/*0.00005*/ dt * .1, Vector(0.0, 1.0, 0.0));
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
359void SimpleGameMenu::quitMenu()
360{
361  this->setNextStoryID(WORLD_ID_GAMEEND);
362  this->stop();
363}
364
365
366/**
367 * @brief event dispatcher funciton
368 * @param event the incoming event
369 */
370void SimpleGameMenu::process(const Event &event)
371{
372  /* ----------------- LAYER 1 ---------------*/
373  if( this->layerIndex == 0)
374  {
375    if( event.type == SDLK_RETURN && event.bPressed == true)
376    {
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
385        if( this->menuLayers[1].menuList.size() == 0)
386        {
387          PRINTF(1)("Haven't got any StoryEntities to play!\n");
388          return;
389        }
390
391        this->switchMenuLayer(this->layerIndex, 1);
392      }
393    }
394    if( event.type == SDLK_ESCAPE && event.bPressed == true)
395    {
396      this->setNextStoryID(WORLD_ID_GAMEEND);
397      this->stop();
398    }
399  }  /* ----------------- LAYER 2 ---------------*/
400  else if( this->layerIndex == 1)
401  {
402    if( event.type == SDLK_RETURN && event.bPressed == true)
403    {
404      this->setNextStoryID( this->menuLayers[1].storyList[this->menuSelectedIndex]->getStoryID());
405      this->stop();
406    }
407    if( event.type == SDLK_ESCAPE && event.bPressed == true)
408    {
409      this->switchMenuLayer(this->layerIndex, 0);
410    }
411  }
412
413
414
415  // The menu selction cursor
416  if( event.type == SDLK_DOWN && event.bPressed == true)
417  {
418    if(this->menuSelectedIndex < (this->menuLayers[this->layerIndex].menuList.size() - 1))
419    {
420      this->menuSelected = this->menuLayers[this->layerIndex].menuList[++this->menuSelectedIndex];
421      this->sliderTo(this->menuSelected, 5.0f);
422      if (this->selectorSource != NULL)
423        this->selectorSource->play();
424
425      if( this->layerIndex == 1)
426      {
427        this->menuLayers[1].screenshootList[this->menuSelectedIndex]->setVisibility(true);
428        this->menuLayers[1].screenshootList[this->menuSelectedIndex-1]->setVisibility(false);
429      }
430    }
431  }
432  else if( event.type == SDLK_UP && event.bPressed == true)
433  {
434    if(this->menuSelectedIndex > 0)
435    {
436      this->menuSelected = this->menuLayers[this->layerIndex].menuList[--this->menuSelectedIndex];
437      this->sliderTo(this->menuSelected, 5.0f);
438      if (this->selectorSource != NULL)
439        this->selectorSource->play();
440
441      if( this->layerIndex == 1)
442      {
443        this->menuLayers[1].screenshootList[this->menuSelectedIndex]->setVisibility(true);
444        this->menuLayers[1].screenshootList[this->menuSelectedIndex+1]->setVisibility(false);
445      }
446    }
447  }
448}
449
450
451/**
452 * @brief switches to from one meny layer to an other
453 * @param layer1 from layer
454 * @param layer2 to layer
455 */
456void SimpleGameMenu::switchMenuLayer(int layer1, int layer2)
457{
458  // wrong sizes
459  if(layer1 >= this->menuLayers.size() || layer2 >= this->menuLayers.size())
460    return;
461
462
463  PRINTF(0)("Removing layer %i\n", layer1);
464  std::vector<TextElement*>::iterator te;
465  // fade old menu
466  for( te = this->menuLayers[layer1].menuList.begin(); te != this->menuLayers[layer1].menuList.end(); te++)
467  {
468    (*te)->setVisibility(false);
469  }
470
471  std::vector<ImageEntity*>::iterator it;
472
473  //also fade the screenshots if in level choosement mode
474  for( it = this->menuLayers[layer1].screenshootList.begin(); it != this->menuLayers[layer1].screenshootList.end(); it++)
475  {
476    (*it)->setVisibility(false);
477  }
478
479
480  PRINTF(0)("Showing layer %i\n", layer1);
481  // beam here the new menu
482  for( te = this->menuLayers[layer2].menuList.begin(); te != this->menuLayers[layer2].menuList.end(); te++ )
483  {
484    (*te)->setVisibility(true);
485  }
486
487
488  this->layerIndex = layer2;
489  this->menuSelected = this->menuLayers[layer2].menuList[0];
490  this->menuSelector->setAbsCoor2D(this->menuSelected->getAbsCoor2D() + Vector2D(0, this->menuSelected->getSizeY2D() *.5));
491  this->menuSelector->setSize2D(this->menuSelected->getSizeX2D()*.7, this->menuSelected->getSizeY2D());
492  this->menuSelectedIndex = 0;
493
494  if( layer2 == 1)
495    this->menuLayers[layer2].screenshootList[0]->setVisibility(true);
496}
497
498void SimpleGameMenu::sliderTo(const Element2D* element, float bias)
499{
500  if (bias > 0.0)
501  {
502    this->menuSelector->setAbsCoorSoft2D(element->getAbsCoor2D() + Vector2D(0, element->getSizeY2D() *.5), bias);
503    this->menuSelector->setSizeSoft2D(element->getSizeX2D()*.7, element->getSizeY2D(), bias);
504  }
505  else
506  {
507    this->menuSelector->setAbsCoor2D(element->getAbsCoor2D() + Vector2D(0, element->getSizeY2D() *.5));
508    this->menuSelector->setSize2D(element->getSizeX2D()*.7, element->getSizeY2D());
509  }
510}
511
512
513
514/**********************************************************************************************
515    SimpleGameMenuData
516 **********************************************************************************************/
517
518
519/**
520 * SimpleGameMenuData constructor
521 */
522SimpleGameMenuData::SimpleGameMenuData()
523{}
524
525/**
526 * SimpleGameMenuData decontructor
527 */
528SimpleGameMenuData::~SimpleGameMenuData()
529{}
530
531
532/**
533 *  initialize the GameWorldDataData
534 */
535ErrorMessage SimpleGameMenuData::init()
536{
537  /* call underlying function */
538  GameWorldData::init();
539}
540
541
542/**
543 *  loads the GUI data
544 * @param root reference to the xml root element
545 */
546ErrorMessage SimpleGameMenuData::loadGUI(const TiXmlElement* root)
547{
548  /* call underlying function */
549  GameWorldData::loadGUI(root);
550}
551
552
553/**
554 *  unloads the GUI data
555 */
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 */
567ErrorMessage SimpleGameMenuData::loadWorldEntities(const TiXmlElement* root)
568{
569  GameWorldData::loadWorldEntities(root);
570  /*
571  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
572
573  if( element != NULL)
574  {
575    element = element->FirstChildElement();
576    PRINTF(4)("Loading WorldEntities\n");
577    while(element != NULL)
578    {
579      BaseObject* created = Factory::fabricate(element);
580      if( created != NULL )
581        printf("Created a %s: %s\n", created->getClassName(), created->getName());
582
583      if( element->Value() == "SkyBox")
584        this->sky = dynamic_cast<WorldEntity*>(created);
585      if( element->Value() == "Terrain")
586        this->terrain = dynamic_cast<Terrain*>(created);
587      element = element->NextSiblingElement();
588    }
589
590    PRINTF(4)("Done loading WorldEntities\n");
591  }
592
593  // init the pnode tree
594  PNode::getNullParent()->init();
595  */
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 */
613ErrorMessage SimpleGameMenuData::loadScene(const TiXmlElement* root)
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.