Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10555 was 10403, checked in by patrick, 18 years ago

fading now works

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