Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/menu/game_menu.cc

Last change on this file was 10618, checked in by bknecht, 17 years ago

merged cleanup into trunk (only improvements)

File size: 14.1 KB
RevLine 
[6501]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
[8673]19#include "game_menu.h"
[6501]20
[7868]21#include "event_handler.h"
22
[6502]23#include "state.h"
24
[7193]25#include "util/loading/load_param.h"
26#include "util/loading/factory.h"
[6502]27
[8688]28#include "graphics_engine.h"
[10618]29#include "tools/camera.h"
[8688]30#include "sound_engine.h"
[6502]31
[7318]32#include "sound_source.h"
[6504]33
[7919]34#include "glgui.h"
[8691]35#include "menu/glgui_imagebutton.h"
[6502]36
[9059]37#include "glgui_text.h"
38
39#include "network_manager.h"
40
[9869]41#include "parser/preferences/preferences.h"
[9235]42
[10114]43
[8673]44//! This creates a Factory to fabricate a GameMenu
[10114]45ObjectListDefinition(GameMenu);
[9869]46CREATE_FACTORY(GameMenu);
[6502]47
48
[8673]49GameMenu::GameMenu(const TiXmlElement* root)
[7919]50    : GameWorld()
[6502]51{
[9869]52  this->registerObject(this, GameMenu::_objectList);
[8673]53  this->setName("GameMenu uninitialized");
[6502]54
[8673]55  this->dataTank = new GameMenuData();
[6502]56
[6521]57  this->cameraVector = Vector(50.0, 0.0, 0.0);
[6862]58
[7221]59  if (root != NULL)
60    this->loadParams(root);
[6878]61
[7032]62  State::setMenuID(this->getNextStoryID());
[6502]63}
64
[6501]65/**
[8673]66*  @brief remove the GameMenu from memory
[7919]67*
68*  delete everything explicitly, that isn't contained in the parenting tree!
69*  things contained in the tree are deleted automaticaly
70*/
[8673]71GameMenu::~GameMenu ()
[6502]72{
[8673]73  PRINTF(3)("GameMenu::~GameMenu() - deleting current world\n");
[7287]74
75  if( this->dataTank)
76    delete this->dataTank;
[9656]77  OrxGui::GLGuiHandler::deleteInstance( );
[6502]78}
[6501]79
[6502]80
81/**
[8673]82* @brief loads the parameters of a GameMenu from an XML-element
[7919]83* @param root the XML-element to load from
84*/
[8673]85void GameMenu::loadParams(const TiXmlElement* root)
[6502]86{
87  /* skip the GameWorld, since it does not define any useful loadParams for this class */
[6696]88  //static_cast<GameWorld*>(this)->loadParams(root);
89  GameWorld::loadParams(root);
[6501]90
[8673]91  PRINTF(4)("Loaded GameMenu specific stuff\n");
[6502]92}
[6501]93
[6502]94
[6501]95/**
[7919]96* @brief this is executed just before load
97*
98* since the load function sometimes needs data, that has been initialized
99* before the load and after the proceeding storyentity has finished
100*/
[8673]101ErrorMessage GameMenu::init()
[6504]102{
103  /* call underlying init funciton */
104  GameWorld::init();
105
[7868]106  this->subscribeEvent(ES_MENU, SDLK_UP);
107  this->subscribeEvent(ES_MENU, SDLK_DOWN);
108  this->subscribeEvent(ES_MENU, SDLK_RETURN);
109  this->subscribeEvent(ES_MENU, SDLK_SPACE);
110  this->subscribeEvent(ES_MENU, SDLK_ESCAPE);
[6521]111
[10379]112  State::getCamera()->setRelCoor(this->cameraVector);
[10368]113  //this->dataTank->localCamera->setFovy(60);
[6524]114
115  GraphicsEngine::getInstance()->displayFPS(false);
[6862]116
[8677]117  return ErrorMessage();
[6504]118}
119
120
[6524]121/**
[7919]122* @brief load the data
123*/
[8673]124ErrorMessage GameMenu::loadData()
[6504]125{
[8677]126  this->mainMenuBox = NULL;
127
128  this->levelsBox = NULL;
129  this->networkBox = NULL;
[9235]130
[9059]131  this->clientNetworkBox = NULL;
132  this->serverNetworkBox = NULL;
[8677]133
134  this->optionsBox = NULL;
[8688]135  this->generalBox = NULL;
[8677]136  this->audioBox = NULL;
137  this->videoBox = NULL;
138  this->controlBox = NULL;
139  this->levelsBox = NULL;
140
[8688]141  this->currentlyOpened = NULL;
142
[8677]143  return GameWorld::loadData();
144}
145
146
147void GameMenu::showMainMenu()
148{
149  if (mainMenuBox == NULL)
[8674]150  {
[8677]151    this->mainMenuBox = new OrxGui::GLGuiBox();
152    {
[10403]153      OrxGui::GLGuiButton* startButton = new OrxGui::GLGuiPushButton("Start");
[9406]154      startButton->released.connect(this, &GameMenu::showCampaigns);
[8677]155      this->mainMenuBox->pack(startButton);
[9014]156      startButton->select();
[8674]157
[10403]158//       OrxGui::GLGuiButton* networkButton = new OrxGui::GLGuiPushButton("MultiPlayer");
159//       networkButton->released.connect(this, &GameMenu::showMultiPlayer);
160//       this->mainMenuBox->pack(networkButton);
[8674]161
[10403]162//       OrxGui::GLGuiButton* optionsButton = new OrxGui::GLGuiPushButton("Options");
163//       optionsButton->released.connect(this, &GameMenu::showOptionsMenu);
164//       this->mainMenuBox->pack(optionsButton);
[8674]165
166
[8677]167      OrxGui::GLGuiButton* quitButton = new OrxGui::GLGuiPushButton("Quit");
168      this->mainMenuBox->pack(quitButton);
[9406]169      quitButton->released.connect(this, &GameMenu::quitMenu);
[8677]170    }
[8674]171  }
[8677]172  this->mainMenuBox->showAll();
[8674]173
[9014]174
[8677]175  this->mainMenuBox->setRelCoor2D(200, 100);
176}
[8674]177
[8688]178
[8677]179void GameMenu::showCampaigns()
180{
181  if (this->levelsBox == NULL)
182  {
[8696]183    this->levelsBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
[8677]184    {
[8696]185      OrxGui::GLGuiBox* labelBox = new OrxGui::GLGuiBox();
186
187      OrxGui::GLGuiImage* image = new OrxGui::GLGuiImage();
188      image->show();
189      image->setWidgetSize( 250, 200);
190      image->setAbsCoor2D(400, 150);
[8719]191      image->setForegroundColor(Color( 1,1,1,.6));
[8696]192
[9014]193      bool first = true;
[9869]194      for(ObjectList<StoryEntity>::const_iterator it = StoryEntity::objectList().begin();
195          it != StoryEntity::objectList().end();
196          ++it)
[8677]197      {
[9869]198        StoryEntity* se = *it;
[8677]199        if( se->isContainedInMenu())
200        {
[8691]201
[9869]202          PRINTF(3)("Load image %s\n", se->getMenuScreenshoot().c_str());
[8691]203          OrxGui::GLGuiImageButton* button = new OrxGui::GLGuiImageButton(se->getName(), se->getStoryID(), se->getMenuScreenshoot(), image);
[9406]204          button->startLevel.connect(this, &GameMenu::startLevel);
[8696]205          labelBox->pack(button);
[8677]206
[9014]207          if (first)
208          {
209            first = !first;
210            button->select();
211          }
212
[8677]213          // generating screenshoot item
214          /*
215          ImageEntity* ie = new ImageEntity();
216          ie->setVisibility(false);
217          ie->setBindNode((const PNode*)NULL);
218          ie->setTexture(se->getMenuScreenshoot());
219          ie->setRelCoor2D(State::getResX() / 2.0f + 250.0f, State::getResY() / 2.0f);
220          ie->setSize2D(140.0f, 105.0f);
221          this->menuLayers[1].screenshootList.push_back(ie);
222          */
223        }
224      }
[8696]225
226      this->levelsBox->pack(labelBox);
227      this->levelsBox->pack(image);
[8677]228    }
[8696]229
[8677]230  }
231
[8688]232  this->showSecondLevelElement(this->levelsBox);
[8677]233
[6521]234}
[6520]235
[8677]236void GameMenu::showMultiPlayer()
[8742]237{
238  if (this->networkBox == NULL)
239  {
[9110]240    this->networkBox = new OrxGui::GLGuiBox( OrxGui::Horizontal );
[8742]241    {
[9110]242      OrxGui::GLGuiBox * box = new OrxGui::GLGuiBox();
[9235]243
[8742]244      OrxGui::GLGuiButton* clientButton = new OrxGui::GLGuiPushButton("Client");
[9110]245      box->pack(clientButton);
[9406]246      clientButton->released.connect(this, &GameMenu::showClientMenu);
[8677]247
[8742]248      OrxGui::GLGuiButton* serverButton = new OrxGui::GLGuiPushButton("Server");
[9110]249      box->pack(serverButton);
[9406]250      serverButton->released.connect(this, &GameMenu::showServerMenu);
[9235]251
[9110]252      networkBox->pack( box );
[8742]253    }
254  }
255
256  this->showSecondLevelElement(this->networkBox);
257
258}
259
[8677]260void GameMenu::showOptionsMenu()
261{
262  if (this->optionsBox == NULL)
263  {
264    this->optionsBox = new OrxGui::GLGuiBox();
265    {
[9015]266      OrxGui::GLGuiTextfield* WARNtext = new OrxGui::GLGuiTextfield();
267      WARNtext->setText("PLEASE USE THE EXTERNAL GUI\n FOR ORXONOX CONFIGURATION\n (start with './orxonox -g')");
268      optionsBox->pack(WARNtext);
269
270
[8677]271      OrxGui::GLGuiButton* generalButton = new OrxGui::GLGuiPushButton("General");
272      optionsBox->pack(generalButton);
273
274      OrxGui::GLGuiButton* audioButton = new OrxGui::GLGuiPushButton("Audio");
275      optionsBox->pack(audioButton);
276
277      OrxGui::GLGuiButton* videoButton = new OrxGui::GLGuiPushButton("Video");
278      optionsBox->pack(videoButton);
279
280      OrxGui::GLGuiButton* controlButton = new OrxGui::GLGuiPushButton("Control");
281      optionsBox->pack(controlButton);
282
283
284      //      for (unsigned int i = 0; i <
285      //OrxGui::GLGuiButton*
286
287    }
288  }
289
[8688]290  this->showSecondLevelElement(this->optionsBox);
[8677]291}
292
293
[8688]294void GameMenu::showSecondLevelElement(OrxGui::GLGuiBox* element)
295{
296  if (this->currentlyOpened != NULL && this->currentlyOpened != element)
297    this->currentlyOpened->hideAll();
[8677]298
[8688]299  element->showAll();
300  element->setRelCoor2D(200, 100);
[8677]301
[8688]302  this->currentlyOpened = element;
[8677]303
[8688]304  this->mainMenuBox->setRelCoorSoft2D(50, 100, 5);
305}
[8677]306
307
308
309
310
[8691]311void GameMenu::startLevel(int levelID)
312{
313  this->setNextStoryID( levelID);
314  this->stop();
315}
[8688]316
[7318]317/**
[7919]318* @brief set the Sound to play when switching menu entry.
319* @param selectorSound the sound to load.
320*/
[8673]321void GameMenu::setSelectorSound(const std::string& selectorSound)
[7318]322{
[7460]323  this->selectorSource = OrxSound::SoundEngine::getInstance()->createSource(selectorSound, NULL);
[7318]324}
[6521]325
[8673]326ErrorMessage GameMenu::unloadData()
[6862]327{
[7868]328  this->unsubscribeEvents(ES_MENU);
[6862]329
[8677]330  return GameWorld::unloadData();
[6862]331}
332
333
[6524]334/**
[7919]335* @brief start the menu
336*/
[8673]337bool GameMenu::start()
[6521]338{
339  EventHandler::getInstance()->pushState(ES_MENU);
340
[8677]341  this->showMainMenu();
[8689]342  OrxGui::GLGuiHandler::getInstance()->activateCursor();
343  OrxGui::GLGuiHandler::getInstance()->activate();
[8674]344
[6504]345  /* now call the underlying*/
[8677]346  return GameWorld::start();
[6504]347}
348
349
350
[6524]351/**
[7919]352* stop the menu
353*/
[8673]354bool GameMenu::stop()
[6504]355{
356  EventHandler::getInstance()->popState();
357
358  /* now call the underlying*/
[8677]359  return GameWorld::stop();
[6504]360}
361
362
[6521]363/**
[7919]364*  override the standard tick for more functionality
365*/
[8673]366void GameMenu::tick()
[6521]367{
368  GameWorld::tick();
[6504]369
[7919]370  // Make the GLGui tick.
371  OrxGui::GLGuiHandler::getInstance()->tick(this->dtS);
372
[7131]373  this->animateScene(this->dtS);
[6521]374}
375
376
[6504]377/**
[7919]378* @brief no collision detection in the menu
379*/
[8673]380void GameMenu::collide()
[6521]381{
[7919]382  //   this->dataTank->localCamera->
[6521]383}
[6501]384
385
[6504]386/**
[7919]387* @brief animate the scene
388*/
[8673]389void GameMenu::animateScene(float dt)
[6521]390{
[7131]391  Quaternion q(/*0.00005*/ dt * .1, Vector(0.0, 1.0, 0.0));
[6521]392  this->cameraVector = q.apply(this->cameraVector);
[10379]393  State::getCamera()->setRelCoor(this->cameraVector);
394  State::getCamera()->getTarget()->setRelCoorSoft(0,0,0);
[6521]395}
396
[8673]397void GameMenu::quitMenu()
[7919]398{
399  this->setNextStoryID(WORLD_ID_GAMEEND);
400  this->stop();
401}
[6521]402
[7919]403
[6521]404/**
[7919]405* @brief event dispatcher funciton
406* @param event the incoming event
407*/
[8673]408void GameMenu::process(const Event &event)
[6504]409{
[8674]410  if( event.type == SDLK_ESCAPE && event.bPressed == true)
411  {
412    this->setNextStoryID(WORLD_ID_GAMEEND);
413    this->stop();
414  }
415
[6504]416}
[6502]417
418
419
420/**********************************************************************************************
[8673]421GameMenuData
[7919]422**********************************************************************************************/
[6502]423
424
[6501]425/**
[8673]426* GameMenuData constructor
[7919]427*/
[8673]428GameMenuData::GameMenuData()
[6501]429{}
430
431/**
[8673]432* GameMenuData decontructor
[7919]433*/
[8673]434GameMenuData::~GameMenuData()
[6501]435{}
436
437
438/**
[7919]439*  initialize the GameWorldDataData
440*/
[8673]441ErrorMessage GameMenuData::init()
[6502]442{
443  /* call underlying function */
[8677]444  return GameWorldData::init();
[6502]445}
[6501]446
447
448/**
[7919]449*  loads the GUI data
450* @param root reference to the xml root element
451*/
[8673]452ErrorMessage GameMenuData::loadGUI(const TiXmlElement* root)
[6502]453{
454  /* call underlying function */
[8677]455  return GameWorldData::loadGUI(root);
[6502]456}
[6501]457
458
459/**
[7919]460*  unloads the GUI data
461*/
[8673]462ErrorMessage GameMenuData::unloadGUI()
[6502]463{
464  /* call underlying function */
[8677]465  return GameWorldData::unloadGUI();
[6502]466}
467
468
469/**
[7919]470*  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
471* @param root reference to the xml root parameter
472*/
[8673]473ErrorMessage GameMenuData::loadWorldEntities(const TiXmlElement* root)
[6502]474{
[8677]475  return GameWorldData::loadWorldEntities(root);
[6502]476}
477
478
479/**
[7919]480*  unloads the world entities from the xml file
481*/
[8673]482ErrorMessage GameMenuData::unloadWorldEntities()
[6502]483{
484  /* call underlying function */
[8677]485  return GameWorldData::unloadWorldEntities();
[6502]486}
487
488
489/**
[7919]490*  loads the scene data
491* @param root reference to the xml root element
492*/
[8673]493ErrorMessage GameMenuData::loadScene(const TiXmlElement* root)
[6502]494{
495  /* call underlying function */
[8677]496  return GameWorldData::loadScene(root);
[6502]497}
498
499
500/**
[7919]501*  unloads the scene data
502*/
[8673]503ErrorMessage GameMenuData::unloadScene()
[6502]504{
505  /* call underlying function */
[8677]506  return GameWorldData::unloadScene();
[6502]507}
508
[9059]509/**
510 * show controls to join network game
511 */
512void GameMenu::showClientMenu( )
513{
514  if ( this->serverNetworkBox )
515  {
[9110]516    this->networkBox->unpack( this->serverNetworkBox );
517    this->serverNetworkBox->hideAll();
518    //delete this->serverNetworkBox;
519    //this->serverNetworkBox = NULL;
[9059]520  }
[9235]521
[9059]522  if ( !this->clientNetworkBox )
523  {
524    this->clientNetworkBox = new OrxGui::GLGuiBox();
525    {
526      OrxGui::GLGuiText * text = new OrxGui::GLGuiText();
527      text->setText( "Host:" );
528      this->clientNetworkBox->pack( text );
[9235]529
[9059]530      this->ipInputLine = new OrxGui::GLGuiInputLine( );
[9235]531      this->ipInputLine->setText( Preferences::getInstance()->getString( "multiplayer", "lastVisitedServer", "localhost" ) );
[9059]532      this->clientNetworkBox->pack( this->ipInputLine );
[9406]533      //this->ipInputLine->enterPushed.connect(this, &GameMenu::connectToServer); /// redo this.
[9059]534      this->ipInputLine->select();
[9235]535
[9059]536      OrxGui::GLGuiButton* connectButton = new OrxGui::GLGuiPushButton("Connect");
537      clientNetworkBox->pack(connectButton);
[9406]538      connectButton->released.connect(this, &GameMenu::connectToServer);
[9059]539    }
540  }
[9235]541
[9110]542  this->networkBox->pack( this->clientNetworkBox );
[9235]543
[9059]544  this->clientNetworkBox->showAll();
[9235]545
[9110]546  //this->clientNetworkBox->setAbsCoor2D( 300.0f, 100.0f );
[9059]547}
[6502]548
[9059]549/**
550 * show controls to create new network game
551 */
552void GameMenu::showServerMenu( )
553{
554  if ( this->clientNetworkBox )
555  {
[9110]556    this->networkBox->unpack( this->clientNetworkBox );
557    this->clientNetworkBox->hideAll();
558    //delete this->clientNetworkBox;
559    //this->clientNetworkBox = NULL;
[9059]560  }
[9235]561
[9059]562  if ( !this->serverNetworkBox )
563  {
564    this->serverNetworkBox = new OrxGui::GLGuiBox();
565    {
566      OrxGui::GLGuiText * text = new OrxGui::GLGuiText();
567      text->setText( "Map:" );
568      this->serverNetworkBox->pack( text );
[9235]569
[9059]570      OrxGui::GLGuiText * text2 = new OrxGui::GLGuiText();
571      text2->setText( "Multiplayer TeamDeathMatch Arena" );
572      this->serverNetworkBox->pack( text2 );
[9235]573
[9059]574      OrxGui::GLGuiButton* createButton = new OrxGui::GLGuiPushButton("Create Server");
575      serverNetworkBox->pack(createButton);
[9406]576      createButton->released.connect(this, &GameMenu::createMasterServer);
[9059]577    }
578  }
[9235]579
[9110]580  this->networkBox->pack( this->serverNetworkBox );
[9235]581
[9059]582  this->serverNetworkBox->showAll();
[9235]583
[9110]584  //this->serverNetworkBox->setAbsCoor2D( 300.0f, 100.0f );
[9059]585}
[6502]586
[9059]587/**
588 * connect to host
589 */
590void GameMenu::connectToServer( )
591{
592  PRINTF(0)("Connecting to %s\n", this->ipInputLine->_getText().c_str() );
[9235]593
[9059]594  State::setOnline(true);
[9406]595  NetworkManager::getInstance()->createClient( this->ipInputLine->_getText(), 9999 );
[9235]596
597  Preferences::getInstance()->setString( "multiplayer", "lastVisitedServer", this->ipInputLine->_getText() );
598
[9059]599  this->startLevel( 5 );
600}
601
[9406]602void GameMenu::createMasterServer( )
[9059]603{
604  PRINTF(0)("Create server\n" );
[9235]605
[9059]606  State::setOnline(true);
[9406]607  NetworkManager::getInstance()->createMasterServer( 9999 );
[9235]608
[9059]609  this->startLevel( 5 );
610}
611
612
613
Note: See TracBrowser for help on using the repository browser.