Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/story_entities/menu/game_menu.cc @ 9115

Last change on this file since 9115 was 9115, checked in by rennerc, 18 years ago

last server is saved now

File size: 14.6 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#include "class_list.h"
25
26#include "util/loading/load_param.h"
27#include "util/loading/factory.h"
28#include "util/loading/resource_manager.h"
29
30#include "graphics_engine.h"
31#include "camera.h"
32#include "sound_engine.h"
33
34#include "sound_source.h"
35
36#include "glgui.h"
37#include "menu/glgui_imagebutton.h"
38
39#include "glgui_text.h"
40
41#include "network_manager.h"
42
43#include "preferences.h"
44
45//! This creates a Factory to fabricate a GameMenu
46CREATE_FACTORY(GameMenu, CL_GAME_MENU);
47
48
49
50GameMenu::GameMenu(const TiXmlElement* root)
51    : GameWorld()
52{
53  this->setClassID(CL_GAME_MENU, "GameMenu");
54  this->setName("GameMenu uninitialized");
55
56  this->dataTank = new GameMenuData();
57
58  this->cameraVector = Vector(50.0, 0.0, 0.0);
59
60  if (root != NULL)
61    this->loadParams(root);
62
63  State::setMenuID(this->getNextStoryID());
64}
65
66/**
67*  @brief remove the GameMenu from memory
68*
69*  delete everything explicitly, that isn't contained in the parenting tree!
70*  things contained in the tree are deleted automaticaly
71*/
72GameMenu::~GameMenu ()
73{
74  PRINTF(3)("GameMenu::~GameMenu() - deleting current world\n");
75
76  if( this->dataTank)
77    delete this->dataTank;
78  delete OrxGui::GLGuiHandler::getInstance( );
79}
80
81
82/**
83* @brief loads the parameters of a GameMenu from an XML-element
84* @param root the XML-element to load from
85*/
86void GameMenu::loadParams(const TiXmlElement* root)
87{
88  /* skip the GameWorld, since it does not define any useful loadParams for this class */
89  //static_cast<GameWorld*>(this)->loadParams(root);
90  GameWorld::loadParams(root);
91
92  PRINTF(4)("Loaded GameMenu specific stuff\n");
93}
94
95
96/**
97* @brief this is executed just before load
98*
99* since the load function sometimes needs data, that has been initialized
100* before the load and after the proceeding storyentity has finished
101*/
102ErrorMessage GameMenu::init()
103{
104  /* call underlying init funciton */
105  GameWorld::init();
106
107  this->subscribeEvent(ES_MENU, SDLK_UP);
108  this->subscribeEvent(ES_MENU, SDLK_DOWN);
109  this->subscribeEvent(ES_MENU, SDLK_RETURN);
110  this->subscribeEvent(ES_MENU, SDLK_SPACE);
111  this->subscribeEvent(ES_MENU, SDLK_ESCAPE);
112
113  this->dataTank->localCamera->setRelCoor(this->cameraVector);
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("Play");
154      startButton->connect(SIGNAL(startButton, released), this, SLOT(GameMenu, showCampaigns));
155      this->mainMenuBox->pack(startButton);
156      startButton->select();
157
158      OrxGui::GLGuiButton* networkButton = new OrxGui::GLGuiPushButton("MultiPlayer");
159      networkButton->connect(SIGNAL(networkButton, released), this, SLOT(GameMenu, showMultiPlayer));
160      this->mainMenuBox->pack(networkButton);
161
162      OrxGui::GLGuiButton* optionsButton = new OrxGui::GLGuiPushButton("Options");
163      optionsButton->connect(SIGNAL(optionsButton, released), this, SLOT(GameMenu, showOptionsMenu));
164      this->mainMenuBox->pack(optionsButton);
165
166
167      OrxGui::GLGuiButton* quitButton = new OrxGui::GLGuiPushButton("Quit");
168      this->mainMenuBox->pack(quitButton);
169      quitButton->connect(SIGNAL(quitButton, released), this, SLOT(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      const std::list<BaseObject*>* storyEntities = ClassList::getList(CL_STORY_ENTITY);
194      std::list<BaseObject*>::const_iterator it;
195      bool first = true;
196      for( it = storyEntities->begin(); it != storyEntities->end(); it++)
197      {
198        StoryEntity* se = dynamic_cast<StoryEntity*>(*it);
199        if( se->isContainedInMenu())
200        {
201
202          printf("%s\n", se->getMenuScreenshoot().c_str());
203          OrxGui::GLGuiImageButton* button = new OrxGui::GLGuiImageButton(se->getName(), se->getStoryID(), se->getMenuScreenshoot(), image);
204          button->connect(SIGNAL(button, startLevel), this, SLOT(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->connect(SIGNAL(clientButton, released), this, SLOT(GameMenu, showClientMenu));
247
248      OrxGui::GLGuiButton* serverButton = new OrxGui::GLGuiPushButton("Server");
249      box->pack(serverButton);
250      serverButton->connect(SIGNAL(serverButton, released), this, SLOT(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  OrxGui::GLGuiHandler::getInstance()->cursor()->loadTextureSequence(ResourceManager::getInstance()->getDataDir() + "/" + "maps/reap_mouse/reap_mouse_##.png", 1, 49);
345
346  /* now call the underlying*/
347  return GameWorld::start();
348}
349
350
351
352/**
353* stop the menu
354*/
355bool GameMenu::stop()
356{
357  EventHandler::getInstance()->popState();
358
359  /* now call the underlying*/
360  return GameWorld::stop();
361}
362
363
364/**
365*  override the standard tick for more functionality
366*/
367void GameMenu::tick()
368{
369  GameWorld::tick();
370
371  // Make the GLGui tick.
372  OrxGui::GLGuiHandler::getInstance()->tick(this->dtS);
373
374  this->animateScene(this->dtS);
375}
376
377
378/**
379* @brief no collision detection in the menu
380*/
381void GameMenu::collide()
382{
383  //   this->dataTank->localCamera->
384}
385
386
387/**
388* @brief animate the scene
389*/
390void GameMenu::animateScene(float dt)
391{
392  Quaternion q(/*0.00005*/ dt * .1, Vector(0.0, 1.0, 0.0));
393  this->cameraVector = q.apply(this->cameraVector);
394  this->dataTank->localCamera->setRelCoor(this->cameraVector);
395  this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0);
396}
397
398void GameMenu::quitMenu()
399{
400  this->setNextStoryID(WORLD_ID_GAMEEND);
401  this->stop();
402}
403
404
405/**
406* @brief event dispatcher funciton
407* @param event the incoming event
408*/
409void GameMenu::process(const Event &event)
410{
411  if( event.type == SDLK_ESCAPE && event.bPressed == true)
412  {
413    this->setNextStoryID(WORLD_ID_GAMEEND);
414    this->stop();
415  }
416
417}
418
419
420
421/**********************************************************************************************
422GameMenuData
423**********************************************************************************************/
424
425
426/**
427* GameMenuData constructor
428*/
429GameMenuData::GameMenuData()
430{}
431
432/**
433* GameMenuData decontructor
434*/
435GameMenuData::~GameMenuData()
436{}
437
438
439/**
440*  initialize the GameWorldDataData
441*/
442ErrorMessage GameMenuData::init()
443{
444  /* call underlying function */
445  return GameWorldData::init();
446}
447
448
449/**
450*  loads the GUI data
451* @param root reference to the xml root element
452*/
453ErrorMessage GameMenuData::loadGUI(const TiXmlElement* root)
454{
455  /* call underlying function */
456  return GameWorldData::loadGUI(root);
457}
458
459
460/**
461*  unloads the GUI data
462*/
463ErrorMessage GameMenuData::unloadGUI()
464{
465  /* call underlying function */
466  return GameWorldData::unloadGUI();
467}
468
469
470/**
471*  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
472* @param root reference to the xml root parameter
473*/
474ErrorMessage GameMenuData::loadWorldEntities(const TiXmlElement* root)
475{
476  return GameWorldData::loadWorldEntities(root);
477}
478
479
480/**
481*  unloads the world entities from the xml file
482*/
483ErrorMessage GameMenuData::unloadWorldEntities()
484{
485  /* call underlying function */
486  return GameWorldData::unloadWorldEntities();
487}
488
489
490/**
491*  loads the scene data
492* @param root reference to the xml root element
493*/
494ErrorMessage GameMenuData::loadScene(const TiXmlElement* root)
495{
496  /* call underlying function */
497  return GameWorldData::loadScene(root);
498}
499
500
501/**
502*  unloads the scene data
503*/
504ErrorMessage GameMenuData::unloadScene()
505{
506  /* call underlying function */
507  return GameWorldData::unloadScene();
508}
509
510/**
511 * show controls to join network game
512 */
513void GameMenu::showClientMenu( )
514{
515  if ( this->serverNetworkBox )
516  {
517    this->networkBox->unpack( this->serverNetworkBox );
518    this->serverNetworkBox->hideAll();
519    //delete this->serverNetworkBox;
520    //this->serverNetworkBox = NULL;
521  }
522 
523  if ( !this->clientNetworkBox )
524  {
525    this->clientNetworkBox = new OrxGui::GLGuiBox();
526    {
527      OrxGui::GLGuiText * text = new OrxGui::GLGuiText();
528      text->setText( "Host:" );
529      this->clientNetworkBox->pack( text );
530     
531      this->ipInputLine = new OrxGui::GLGuiInputLine( );
532      this->ipInputLine->setText( Preferences::getInstance()->getString( "multiplayer", "lastVisitedServer", "localhost" ) );
533      this->clientNetworkBox->pack( this->ipInputLine );
534      this->ipInputLine->connect(SIGNAL(ipInputLine, enterPushed), this, SLOT(GameMenu, connectToServer));
535      this->ipInputLine->select();
536     
537      OrxGui::GLGuiButton* connectButton = new OrxGui::GLGuiPushButton("Connect");
538      clientNetworkBox->pack(connectButton);
539      connectButton->connect(SIGNAL(connectButton, released), this, SLOT(GameMenu, connectToServer));
540    }
541  }
542 
543  this->networkBox->pack( this->clientNetworkBox );
544 
545  this->clientNetworkBox->showAll();
546 
547  //this->clientNetworkBox->setAbsCoor2D( 300.0f, 100.0f );
548}
549
550/**
551 * show controls to create new network game
552 */
553void GameMenu::showServerMenu( )
554{
555  if ( this->clientNetworkBox )
556  {
557    this->networkBox->unpack( this->clientNetworkBox );
558    this->clientNetworkBox->hideAll();
559    //delete this->clientNetworkBox;
560    //this->clientNetworkBox = NULL;
561  }
562 
563  if ( !this->serverNetworkBox )
564  {
565    this->serverNetworkBox = new OrxGui::GLGuiBox();
566    {
567      OrxGui::GLGuiText * text = new OrxGui::GLGuiText();
568      text->setText( "Map:" );
569      this->serverNetworkBox->pack( text );
570     
571      OrxGui::GLGuiText * text2 = new OrxGui::GLGuiText();
572      text2->setText( "Multiplayer TeamDeathMatch Arena" );
573      this->serverNetworkBox->pack( text2 );
574     
575      OrxGui::GLGuiButton* createButton = new OrxGui::GLGuiPushButton("Create Server");
576      serverNetworkBox->pack(createButton);
577      createButton->connect(SIGNAL(createButton, released), this, SLOT(GameMenu, createServer));
578    }
579  }
580 
581  this->networkBox->pack( this->serverNetworkBox );
582 
583  this->serverNetworkBox->showAll();
584 
585  //this->serverNetworkBox->setAbsCoor2D( 300.0f, 100.0f );
586}
587
588/**
589 * connect to host
590 */
591void GameMenu::connectToServer( )
592{
593  PRINTF(0)("Connecting to %s\n", this->ipInputLine->_getText().c_str() );
594 
595  State::setOnline(true);
596  NetworkManager::getInstance()->establishConnection( this->ipInputLine->_getText(), 9999 );
597 
598  Preferences::getInstance()->setString( "multiplayer", "lastVisitedServer", this->ipInputLine->_getText() );
599 
600  this->startLevel( 5 );
601}
602
603void GameMenu::createServer( )
604{
605  PRINTF(0)("Create server\n" );
606 
607  State::setOnline(true);
608  NetworkManager::getInstance()->createServer( 9999 );
609 
610  this->startLevel( 5 );
611}
612
613
614
Note: See TracBrowser for help on using the repository browser.