Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

gui: connected a new Signal, man it is so easy now :)

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