Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

gui: introducing rect into gui

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