Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

gui: introducing a Box

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