Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/orxonox/gamestates/GSLevel.cc @ 2869

Last change on this file since 2869 was 2869, checked in by bknecht, 15 years ago

Including some features to use only mouse or keys in a GUI, also cleaning up the calls to GUIManager, also trying to show and hide a GUI just by tabbing a key. Anyways, more features to come soonsvn status

  • Property svn:eol-style set to native
File size: 10.2 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      Fabian 'x3n' Landau
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "GSLevel.h"
31
32#include "core/input/InputManager.h"
33#include "core/input/SimpleInputState.h"
34#include "core/input/KeyBinder.h"
35#include "core/Loader.h"
36#include "core/XMLFile.h"
37#include "core/CommandExecutor.h"
38#include "core/ConsoleCommand.h"
39#include "core/CommandLine.h"
40#include "core/ConfigValueIncludes.h"
41#include "core/Core.h"
42#include "core/CoreIncludes.h"
43#include "core/Game.h"
44#include "core/GameMode.h"
45#include "objects/Tickable.h"
46#include "objects/Radar.h"
47#include "CameraManager.h"
48#include "GraphicsManager.h"
49#include "LevelManager.h"
50#include "PlayerManager.h"
51#include "gui/GUIManager.h"
52
53namespace orxonox
54{
55    AddGameState(GSLevel, "level");
56
57    SetCommandLineArgument(level, "presentation.oxw").shortcut("l");
58    SetConsoleCommand(GSLevel, showIngameGUI, true).keybindMode(KeybindMode::OnPress).keybindMode(KeybindMode::OnRelease);
59
60    GSLevel::GSLevel(const std::string& name)
61        : GameState(name)
62        , keyBinder_(0)
63        , gameInputState_(0)
64        , guiMouseOnlyInputState_(0)
65        , guiKeysOnlyInputState_(0)
66        , radar_(0)
67        , startFile_(0)
68        , cameraManager_(0)
69        , levelManager_(0)
70    {
71        RegisterObject(GSLevel);
72
73        this->ccKeybind_ = 0;
74        this->ccTkeybind_ = 0;
75    }
76
77    GSLevel::~GSLevel()
78    {
79    }
80
81    void GSLevel::setConfigValues()
82    {
83        SetConfigValue(keyDetectorCallbackCode_, "KeybindBindingStringKeyName=");
84    }
85
86    void GSLevel::activate()
87    {
88        setConfigValues();
89
90        if (GameMode::showsGraphics())
91        {
92            gameInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("game");
93            keyBinder_ = new KeyBinder();
94            keyBinder_->loadBindings("keybindings.ini");
95            gameInputState_->setHandler(keyBinder_);
96
97            guiMouseOnlyInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("guiMouseOnly");
98            guiMouseOnlyInputState_->setMouseHandler(GUIManager::getInstancePtr());
99
100            guiKeysOnlyInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("guiKeysOnly");
101            guiKeysOnlyInputState_->setKeyHandler(GUIManager::getInstancePtr());
102
103            // create the global CameraManager
104            this->cameraManager_ = new CameraManager(GraphicsManager::getInstance().getViewport());
105
106            // Start the Radar
107            this->radar_ = new Radar();
108        }
109
110        this->playerManager_ = new PlayerManager();
111
112        if (GameMode::isMaster())
113        {
114            // create the global LevelManager
115            this->levelManager_ = new LevelManager();
116
117            this->loadLevel();
118        }
119
120        if (GameMode::showsGraphics())
121        {
122            // keybind console command
123            FunctorMember<GSLevel>* functor1 = createFunctor(&GSLevel::keybind);
124            functor1->setObject(this);
125            ccKeybind_ = createConsoleCommand(functor1, "keybind");
126            CommandExecutor::addConsoleCommandShortcut(ccKeybind_);
127            FunctorMember<GSLevel>* functor2 = createFunctor(&GSLevel::tkeybind);
128            functor2->setObject(this);
129            ccTkeybind_ = createConsoleCommand(functor2, "tkeybind");
130            CommandExecutor::addConsoleCommandShortcut(ccTkeybind_);
131            // set our console command as callback for the key detector
132            InputManager::getInstance().setKeyDetectorCallback(std::string("keybind ") + keyDetectorCallbackCode_);
133
134            // InGame GUI test
135            GUIManager::getInstance().showGUI("inGameTest");
136
137            // level is loaded: we can start capturing the input
138            InputManager::getInstance().requestEnterState("game");
139        }
140    }
141
142    void GSLevel::showIngameGUI(bool show)
143    {
144        COUT(0) << "*** Call works with \"" << (show ? "true" : "false") << "\" as param" << std::endl;
145        if (show)
146        {
147            GUIManager::getInstancePtr()->showGUI("inGameTest");
148            InputManager::getInstance().requestEnterState("guiMouseOnly");
149        }
150        else
151        {
152            GUIManager::getInstancePtr()->executeCode("hideGUI(inGameTest)");
153            InputManager::getInstance().requestLeaveState("guiMouseOnly");
154        }
155    }
156
157    void GSLevel::deactivate()
158    {
159        // destroy console commands
160        if (this->ccKeybind_)
161        {
162            delete this->ccKeybind_;
163            this->ccKeybind_ = 0;
164        }
165        if (this->ccTkeybind_)
166        {
167            delete this->ccTkeybind_;
168            this->ccTkeybind_ = 0;
169        }
170
171
172        // this call will delete every BaseObject!
173        // But currently this will call methods of objects that exist no more
174        // The only 'memory leak' is the ParticleSpawer. They would be deleted here
175        // and call a sceneNode method that has already been destroy by the corresponding space ship.
176        //Loader::close();
177
178        if (GameMode::showsGraphics())
179            InputManager::getInstance().requestLeaveState("game");
180
181        if (GameMode::isMaster())
182            this->unloadLevel();
183
184        if (this->radar_)
185        {
186            delete this->radar_;
187            this->radar_ = 0;
188        }
189
190        if (this->cameraManager_)
191        {
192            delete this->cameraManager_;
193            this->cameraManager_ = 0;
194        }
195
196        if (this->levelManager_)
197        {
198            delete this->levelManager_;
199            this->levelManager_ = 0;
200        }
201
202        if (this->playerManager_)
203        {
204            delete this->playerManager_;
205            this->playerManager_ = 0;
206        }
207
208        if (GameMode::showsGraphics())
209        {
210            gameInputState_->setHandler(0);
211            guiMouseOnlyInputState_->setHandler(0);
212            guiKeysOnlyInputState_->setHandler(0);
213            InputManager::getInstance().requestDestroyState("game");
214            if (this->keyBinder_)
215            {
216                delete this->keyBinder_;
217                this->keyBinder_ = 0;
218            }
219        }
220    }
221
222    void GSLevel::update(const Clock& time)
223    {
224        // Note: Temporarily moved to GSGraphics.
225        //// Call the scene objects
226        //for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it)
227        //    it->tick(time.getDeltaTime() * this->timeFactor_);
228    }
229
230    void GSLevel::loadLevel()
231    {
232        // call the loader
233        COUT(0) << "Loading level..." << std::endl;
234        std::string levelName;
235        CommandLine::getValue("level", &levelName);
236        startFile_ = new XMLFile(Core::getMediaPathString() + "levels" + '/' + levelName);
237        Loader::open(startFile_);
238    }
239
240    void GSLevel::unloadLevel()
241    {
242        //////////////////////////////////////////////////////////////////////////////////////////
243        // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO // TODO //
244        //////////////////////////////////////////////////////////////////////////////////////////
245        // Loader::unload(startFile_); // TODO: REACTIVATE THIS IF LOADER::UNLOAD WORKS PROPERLY /
246        //////////////////////////////////////////////////////////////////////////////////////////
247
248        delete this->startFile_;
249    }
250
251    void GSLevel::keybind(const std::string &command)
252    {
253        this->keybindInternal(command, false);
254    }
255
256    void GSLevel::tkeybind(const std::string &command)
257    {
258        this->keybindInternal(command, true);
259    }
260
261    /**
262    @brief
263        Assigns a command string to a key/button/axis. The name is determined via KeyDetector.
264    @param command
265        Command string that can be executed by the CommandExecutor
266        OR: Internal string "KeybindBindingStringKeyName=" used for the second call to identify
267        the key/button/axis that has been activated. This is configured above in activate().
268    */
269    void GSLevel::keybindInternal(const std::string& command, bool bTemporary)
270    {
271        if (GameMode::showsGraphics())
272        {
273            static std::string bindingString = "";
274            static bool bTemporarySaved = false;
275            static bool bound = true;
276            // note: We use a long name to make 'sure' that the user doesn't use it accidentally.
277            // Howerver there will be no real issue if it happens anyway.
278            if (command.find(keyDetectorCallbackCode_) != 0)
279            {
280                if (bound)
281                {
282                    COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
283                    InputManager::getInstance().requestEnterState("detector");
284                    bindingString = command;
285                    bTemporarySaved = bTemporary;
286                    bound = false;
287                }
288                //else:  We're still in a keybind command. ignore this call.
289            }
290            else
291            {
292                if (!bound)
293                {
294                    // user has pressed the key
295                    std::string name = command.substr(this->keyDetectorCallbackCode_.size());
296                    COUT(0) << "Binding string \"" << bindingString << "\" on key '" << name << "'" << std::endl;
297                    this->keyBinder_->setBinding(bindingString, name, bTemporarySaved);
298                    InputManager::getInstance().requestLeaveState("detector");
299                    bound = true;
300                }
301                // else: A key was pressed within the same tick, ignore it.
302            }
303        }
304    }
305}
Note: See TracBrowser for help on using the repository browser.