Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/heathaze/src/story_entities/menu/game_menu.cc @ 10616

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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#include "class_id_DEPRECATED.h"
44//! This creates a Factory to fabricate a GameMenu
45ObjectListDefinitionID(GameMenu, CL_GAME_MENU);
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  this->dataTank->localCamera->setRelCoor(this->cameraVector);
113
114  GraphicsEngine::getInstance()->displayFPS(false);
115
116  return ErrorMessage();
117}
118
119
120/**
121* @brief load the data
122*/
123ErrorMessage GameMenu::loadData()
124{
125  this->mainMenuBox = NULL;
126
127  this->levelsBox = NULL;
128  this->networkBox = NULL;
129
130  this->clientNetworkBox = NULL;
131  this->serverNetworkBox = NULL;
132
133  this->optionsBox = NULL;
134  this->generalBox = NULL;
135  this->audioBox = NULL;
136  this->videoBox = NULL;
137  this->controlBox = NULL;
138  this->levelsBox = NULL;
139
140  this->currentlyOpened = NULL;
141
142  return GameWorld::loadData();
143}
144
145
146void GameMenu::showMainMenu()
147{
148  if (mainMenuBox == NULL)
149  {
150    this->mainMenuBox = new OrxGui::GLGuiBox();
151    {
152      OrxGui::GLGuiButton* startButton = new OrxGui::GLGuiPushButton("Play");
153      startButton->released.connect(this, &GameMenu::showCampaigns);
154      this->mainMenuBox->pack(startButton);
155      startButton->select();
156
157      OrxGui::GLGuiButton* networkButton = new OrxGui::GLGuiPushButton("MultiPlayer");
158      networkButton->released.connect(this, &GameMenu::showMultiPlayer);
159      this->mainMenuBox->pack(networkButton);
160
161      OrxGui::GLGuiButton* optionsButton = new OrxGui::GLGuiPushButton("Options");
162      optionsButton->released.connect(this, &GameMenu::showOptionsMenu);
163      this->mainMenuBox->pack(optionsButton);
164
165
166      OrxGui::GLGuiButton* quitButton = new OrxGui::GLGuiPushButton("Quit");
167      this->mainMenuBox->pack(quitButton);
168      quitButton->released.connect(this, &GameMenu::quitMenu);
169    }
170  }
171  this->mainMenuBox->showAll();
172
173
174  this->mainMenuBox->setRelCoor2D(200, 100);
175}
176
177
178void GameMenu::showCampaigns()
179{
180  if (this->levelsBox == NULL)
181  {
182    this->levelsBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
183    {
184      OrxGui::GLGuiBox* labelBox = new OrxGui::GLGuiBox();
185
186      OrxGui::GLGuiImage* image = new OrxGui::GLGuiImage();
187      image->show();
188      image->setWidgetSize( 250, 200);
189      image->setAbsCoor2D(400, 150);
190      image->setForegroundColor(Color( 1,1,1,.6));
191
192      bool first = true;
193      for(ObjectList<StoryEntity>::const_iterator it = StoryEntity::objectList().begin();
194          it != StoryEntity::objectList().end();
195          ++it)
196      {
197        StoryEntity* se = *it;
198        if( se->isContainedInMenu())
199        {
200
201          PRINTF(3)("Load image %s\n", se->getMenuScreenshoot().c_str());
202          OrxGui::GLGuiImageButton* button = new OrxGui::GLGuiImageButton(se->getName(), se->getStoryID(), se->getMenuScreenshoot(), image);
203          button->startLevel.connect(this, &GameMenu::startLevel);
204          labelBox->pack(button);
205
206          if (first)
207          {
208            first = !first;
209            button->select();
210          }
211
212          // generating screenshoot item
213          /*
214          ImageEntity* ie = new ImageEntity();
215          ie->setVisibility(false);
216          ie->setBindNode((const PNode*)NULL);
217          ie->setTexture(se->getMenuScreenshoot());
218          ie->setRelCoor2D(State::getResX() / 2.0f + 250.0f, State::getResY() / 2.0f);
219          ie->setSize2D(140.0f, 105.0f);
220          this->menuLayers[1].screenshootList.push_back(ie);
221          */
222        }
223      }
224
225      this->levelsBox->pack(labelBox);
226      this->levelsBox->pack(image);
227    }
228
229  }
230
231  this->showSecondLevelElement(this->levelsBox);
232
233}
234
235void GameMenu::showMultiPlayer()
236{
237  if (this->networkBox == NULL)
238  {
239    this->networkBox = new OrxGui::GLGuiBox( OrxGui::Horizontal );
240    {
241      OrxGui::GLGuiBox * box = new OrxGui::GLGuiBox();
242
243      OrxGui::GLGuiButton* clientButton = new OrxGui::GLGuiPushButton("Client");
244      box->pack(clientButton);
245      clientButton->released.connect(this, &GameMenu::showClientMenu);
246
247      OrxGui::GLGuiButton* serverButton = new OrxGui::GLGuiPushButton("Server");
248      box->pack(serverButton);
249      serverButton->released.connect(this, &GameMenu::showServerMenu);
250
251      networkBox->pack( box );
252    }
253  }
254
255  this->showSecondLevelElement(this->networkBox);
256
257}
258
259void GameMenu::showOptionsMenu()
260{
261  if (this->optionsBox == NULL)
262  {
263    this->optionsBox = new OrxGui::GLGuiBox();
264    {
265      OrxGui::GLGuiTextfield* WARNtext = new OrxGui::GLGuiTextfield();
266      WARNtext->setText("PLEASE USE THE EXTERNAL GUI\n FOR ORXONOX CONFIGURATION\n (start with './orxonox -g')");
267      optionsBox->pack(WARNtext);
268
269
270      OrxGui::GLGuiButton* generalButton = new OrxGui::GLGuiPushButton("General");
271      optionsBox->pack(generalButton);
272
273      OrxGui::GLGuiButton* audioButton = new OrxGui::GLGuiPushButton("Audio");
274      optionsBox->pack(audioButton);
275
276      OrxGui::GLGuiButton* videoButton = new OrxGui::GLGuiPushButton("Video");
277      optionsBox->pack(videoButton);
278
279      OrxGui::GLGuiButton* controlButton = new OrxGui::GLGuiPushButton("Control");
280      optionsBox->pack(controlButton);
281
282
283      //      for (unsigned int i = 0; i <
284      //OrxGui::GLGuiButton*
285
286    }
287  }
288
289  this->showSecondLevelElement(this->optionsBox);
290}
291
292
293void GameMenu::showSecondLevelElement(OrxGui::GLGuiBox* element)
294{
295  if (this->currentlyOpened != NULL && this->currentlyOpened != element)
296    this->currentlyOpened->hideAll();
297
298  element->showAll();
299  element->setRelCoor2D(200, 100);
300
301  this->currentlyOpened = element;
302
303  this->mainMenuBox->setRelCoorSoft2D(50, 100, 5);
304}
305
306
307
308
309
310void GameMenu::startLevel(int levelID)
311{
312  this->setNextStoryID( levelID);
313  this->stop();
314}
315
316/**
317* @brief set the Sound to play when switching menu entry.
318* @param selectorSound the sound to load.
319*/
320void GameMenu::setSelectorSound(const std::string& selectorSound)
321{
322  this->selectorSource = OrxSound::SoundEngine::getInstance()->createSource(selectorSound, NULL);
323}
324
325ErrorMessage GameMenu::unloadData()
326{
327  this->unsubscribeEvents(ES_MENU);
328
329  return GameWorld::unloadData();
330}
331
332
333/**
334* @brief start the menu
335*/
336bool GameMenu::start()
337{
338  EventHandler::getInstance()->pushState(ES_MENU);
339
340  this->showMainMenu();
341  OrxGui::GLGuiHandler::getInstance()->activateCursor();
342  OrxGui::GLGuiHandler::getInstance()->activate();
343
344  /* now call the underlying*/
345  return GameWorld::start();
346}
347
348
349
350/**
351* stop the menu
352*/
353bool GameMenu::stop()
354{
355  EventHandler::getInstance()->popState();
356
357  /* now call the underlying*/
358  return GameWorld::stop();
359}
360
361
362/**
363*  override the standard tick for more functionality
364*/
365void GameMenu::tick()
366{
367  GameWorld::tick();
368
369  // Make the GLGui tick.
370  OrxGui::GLGuiHandler::getInstance()->tick(this->dtS);
371
372  this->animateScene(this->dtS);
373}
374
375
376/**
377* @brief no collision detection in the menu
378*/
379void GameMenu::collide()
380{
381  //   this->dataTank->localCamera->
382}
383
384
385/**
386* @brief animate the scene
387*/
388void GameMenu::animateScene(float dt)
389{
390  Quaternion q(/*0.00005*/ dt * .1, Vector(0.0, 1.0, 0.0));
391  this->cameraVector = q.apply(this->cameraVector);
392  this->dataTank->localCamera->setRelCoor(this->cameraVector);
393  this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0);
394}
395
396void GameMenu::quitMenu()
397{
398  this->setNextStoryID(WORLD_ID_GAMEEND);
399  this->stop();
400}
401
402
403/**
404* @brief event dispatcher funciton
405* @param event the incoming event
406*/
407void GameMenu::process(const Event &event)
408{
409  if( event.type == SDLK_ESCAPE && event.bPressed == true)
410  {
411    this->setNextStoryID(WORLD_ID_GAMEEND);
412    this->stop();
413  }
414
415}
416
417
418
419/**********************************************************************************************
420GameMenuData
421**********************************************************************************************/
422
423
424/**
425* GameMenuData constructor
426*/
427GameMenuData::GameMenuData()
428{}
429
430/**
431* GameMenuData decontructor
432*/
433GameMenuData::~GameMenuData()
434{}
435
436
437/**
438*  initialize the GameWorldDataData
439*/
440ErrorMessage GameMenuData::init()
441{
442  /* call underlying function */
443  return GameWorldData::init();
444}
445
446
447/**
448*  loads the GUI data
449* @param root reference to the xml root element
450*/
451ErrorMessage GameMenuData::loadGUI(const TiXmlElement* root)
452{
453  /* call underlying function */
454  return GameWorldData::loadGUI(root);
455}
456
457
458/**
459*  unloads the GUI data
460*/
461ErrorMessage GameMenuData::unloadGUI()
462{
463  /* call underlying function */
464  return GameWorldData::unloadGUI();
465}
466
467
468/**
469*  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
470* @param root reference to the xml root parameter
471*/
472ErrorMessage GameMenuData::loadWorldEntities(const TiXmlElement* root)
473{
474  return GameWorldData::loadWorldEntities(root);
475}
476
477
478/**
479*  unloads the world entities from the xml file
480*/
481ErrorMessage GameMenuData::unloadWorldEntities()
482{
483  /* call underlying function */
484  return GameWorldData::unloadWorldEntities();
485}
486
487
488/**
489*  loads the scene data
490* @param root reference to the xml root element
491*/
492ErrorMessage GameMenuData::loadScene(const TiXmlElement* root)
493{
494  /* call underlying function */
495  return GameWorldData::loadScene(root);
496}
497
498
499/**
500*  unloads the scene data
501*/
502ErrorMessage GameMenuData::unloadScene()
503{
504  /* call underlying function */
505  return GameWorldData::unloadScene();
506}
507
508/**
509 * show controls to join network game
510 */
511void GameMenu::showClientMenu( )
512{
513  if ( this->serverNetworkBox )
514  {
515    this->networkBox->unpack( this->serverNetworkBox );
516    this->serverNetworkBox->hideAll();
517    //delete this->serverNetworkBox;
518    //this->serverNetworkBox = NULL;
519  }
520
521  if ( !this->clientNetworkBox )
522  {
523    this->clientNetworkBox = new OrxGui::GLGuiBox();
524    {
525      OrxGui::GLGuiText * text = new OrxGui::GLGuiText();
526      text->setText( "Host:" );
527      this->clientNetworkBox->pack( text );
528
529      this->ipInputLine = new OrxGui::GLGuiInputLine( );
530      this->ipInputLine->setText( Preferences::getInstance()->getString( "multiplayer", "lastVisitedServer", "localhost" ) );
531      this->clientNetworkBox->pack( this->ipInputLine );
532      //this->ipInputLine->enterPushed.connect(this, &GameMenu::connectToServer); /// redo this.
533      this->ipInputLine->select();
534
535      OrxGui::GLGuiButton* connectButton = new OrxGui::GLGuiPushButton("Connect");
536      clientNetworkBox->pack(connectButton);
537      connectButton->released.connect(this, &GameMenu::connectToServer);
538    }
539  }
540
541  this->networkBox->pack( this->clientNetworkBox );
542
543  this->clientNetworkBox->showAll();
544
545  //this->clientNetworkBox->setAbsCoor2D( 300.0f, 100.0f );
546}
547
548/**
549 * show controls to create new network game
550 */
551void GameMenu::showServerMenu( )
552{
553  if ( this->clientNetworkBox )
554  {
555    this->networkBox->unpack( this->clientNetworkBox );
556    this->clientNetworkBox->hideAll();
557    //delete this->clientNetworkBox;
558    //this->clientNetworkBox = NULL;
559  }
560
561  if ( !this->serverNetworkBox )
562  {
563    this->serverNetworkBox = new OrxGui::GLGuiBox();
564    {
565      OrxGui::GLGuiText * text = new OrxGui::GLGuiText();
566      text->setText( "Map:" );
567      this->serverNetworkBox->pack( text );
568
569      OrxGui::GLGuiText * text2 = new OrxGui::GLGuiText();
570      text2->setText( "Multiplayer TeamDeathMatch Arena" );
571      this->serverNetworkBox->pack( text2 );
572
573      OrxGui::GLGuiButton* createButton = new OrxGui::GLGuiPushButton("Create Server");
574      serverNetworkBox->pack(createButton);
575      createButton->released.connect(this, &GameMenu::createMasterServer);
576    }
577  }
578
579  this->networkBox->pack( this->serverNetworkBox );
580
581  this->serverNetworkBox->showAll();
582
583  //this->serverNetworkBox->setAbsCoor2D( 300.0f, 100.0f );
584}
585
586/**
587 * connect to host
588 */
589void GameMenu::connectToServer( )
590{
591  PRINTF(0)("Connecting to %s\n", this->ipInputLine->_getText().c_str() );
592
593  State::setOnline(true);
594  NetworkManager::getInstance()->createClient( this->ipInputLine->_getText(), 9999 );
595
596  Preferences::getInstance()->setString( "multiplayer", "lastVisitedServer", this->ipInputLine->_getText() );
597
598  this->startLevel( 5 );
599}
600
601void GameMenu::createMasterServer( )
602{
603  PRINTF(0)("Create server\n" );
604
605  State::setOnline(true);
606  NetworkManager::getInstance()->createMasterServer( 9999 );
607
608  this->startLevel( 5 );
609}
610
611
612
Note: See TracBrowser for help on using the repository browser.