Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/simple_game_menu.cc @ 7952

Last change on this file since 7952 was 7919, checked in by bensch, 19 years ago

orxonox/trunk: merged the gui branche back
merged with command:
https://svn.orxonox.net/orxonox/branches/gui
no conflicts

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