Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

StateInput.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "StateInput.h"
00010 
00011 #include "Keymap.h"
00012 #include "GameState.h"
00013 
00014 #include "Log.h"
00015 #include "KeyStroke.h"
00016 #include "KeyConsole.h"
00017 #include "OptionAgent.h"
00018 
00019 //-----------------------------------------------------------------
00020 /**
00021  * Create state input handler.
00022  * @param state pointer to the leader
00023  */
00024 StateInput::StateInput(GameState *state)
00025 {
00026     m_state = state;
00027     m_keymap = new Keymap();
00028     KeyDesc key_quit(KEY_QUIT, "quit");
00029     KeyDesc key_console(KEY_CONSOLE, "debug console");
00030     KeyDesc key_help(KEY_HELP, "help screen");
00031     KeyDesc key_menu(KEY_MENU, "game menu");
00032     m_keymap->registerKey(KeyStroke(SDLK_ESCAPE, KMOD_NONE), key_quit);
00033     m_keymap->registerKey(KeyStroke(SDLK_BACKQUOTE, KMOD_NONE), key_console);
00034     m_keymap->registerKey(KeyStroke(SDLK_F1, KMOD_NONE), key_help);
00035     m_keymap->registerKey(KeyStroke(SDLK_F10, KMOD_NONE), key_menu);
00036     m_keymap->registerKey(KeyStroke(SDLK_F6, KMOD_NONE),
00037             KeyDesc(KEY_SUBTITLES, "enable subtitles"));
00038 }
00039 //-----------------------------------------------------------------
00040 StateInput::~StateInput()
00041 {
00042     delete m_keymap;
00043 }
00044 //-----------------------------------------------------------------
00045 /**
00046  * Inspects keystroke and call:
00047  * - common function for quit, console, help, menu, subtitles
00048  * - specKey() for defined keystroke but unknown for us
00049  * - specStroke() for other keys
00050  */
00051     void
00052 StateInput::keyEvent(const KeyStroke &stroke)
00053 {
00054     int index = m_keymap->indexPressed(stroke);
00055     switch (index) {
00056         case KEY_QUIT:
00057             quitState();
00058             break;
00059         case KEY_CONSOLE:
00060             enableConsole();
00061             break;
00062         case KEY_HELP:
00063             enableHelp();
00064             break;
00065         case KEY_MENU:
00066             enableMenu();
00067             break;
00068         case KEY_SUBTITLES:
00069             enableSubtitles();
00070             break;
00071         case -1:
00072             specStroke(stroke);
00073             break;
00074         default:
00075             specKey(index);
00076             break;
00077     }
00078 }
00079 //-----------------------------------------------------------------
00080     void
00081 StateInput::specKey(int keyIndex)
00082 {
00083     LOG_WARNING(ExInfo("unknown key")
00084             .addInfo("keyIndex", keyIndex));
00085 }
00086 //-----------------------------------------------------------------
00087 void
00088 StateInput::quitState()
00089 {
00090     m_state->quitState();
00091 }
00092 //-----------------------------------------------------------------
00093     void
00094 StateInput::enableConsole()
00095 {
00096     m_state->pushState(new KeyConsole());
00097 }
00098 //-----------------------------------------------------------------
00099     void
00100 StateInput::enableSubtitles()
00101 {
00102     toggleParam("subtitles");
00103 }
00104 //-----------------------------------------------------------------
00105     void
00106 StateInput::toggleParam(const std::string &param)
00107 {
00108     OptionAgent *option = OptionAgent::agent();
00109     bool enable = option->getAsBool(param);
00110 
00111     option->setPersistent(param, !enable);
00112 }
00113 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2