Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7911 was 7908, checked in by bensch, 19 years ago

no default shit-text for text anymore

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