Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

PushButton Testing

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