Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

gui: delete event

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