Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

EVENTS WORK :)

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