Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

gui: slider work

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