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

ConsoleInput.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 "ConsoleInput.h"
00010 
00011 #include "Keymap.h"
00012 #include "KeyConsole.h"
00013 
00014 #include "Log.h"
00015 #include "KeyDesc.h"
00016 #include "KeyStroke.h"
00017 
00018 #include <ctype.h> // isprint()
00019 
00020 //-----------------------------------------------------------------
00021 ConsoleInput::ConsoleInput(KeyConsole *console)
00022     : StateInput(console)
00023 {
00024     KeyDesc key_history(KEY_HISTORY, "input history");
00025     KeyDesc key_backspace(KEY_BACKSPACE, "backspace");
00026     KeyDesc key_clear(KEY_CLEAR, "clear");
00027     KeyDesc key_enter(KEY_ENTER, "enter");
00028 
00029     m_keymap->registerKey(KeyStroke(SDLK_UP, KMOD_NONE), key_history);
00030     m_keymap->registerKey(KeyStroke(SDLK_BACKSPACE, KMOD_NONE), key_backspace);
00031     m_keymap->registerKey(KeyStroke(SDLK_u, KMOD_LCTRL), key_clear);
00032     m_keymap->registerKey(KeyStroke(SDLK_u, KMOD_RCTRL), key_clear);
00033     m_keymap->registerKey(KeyStroke(SDLK_RETURN, KMOD_NONE), key_enter);
00034 }
00035 //-----------------------------------------------------------------
00036 KeyConsole *
00037 ConsoleInput::getConsole()
00038 {
00039     return dynamic_cast<KeyConsole*>(m_state);
00040 }
00041 //-----------------------------------------------------------------
00042 /**
00043  * Toggle console.
00044  */
00045 void
00046 ConsoleInput::enableConsole()
00047 {
00048     quitState();
00049 }
00050 //-----------------------------------------------------------------
00051     void
00052 ConsoleInput::specKey(int keyIndex)
00053 {
00054     //TODO: simulate key repeat in console
00055     switch (keyIndex) {
00056         case KEY_HISTORY:
00057             getConsole()->setInput(m_history);
00058             break;
00059         case KEY_BACKSPACE:
00060             {
00061                 std::string input = getConsole()->getInput();
00062                 if (!input.empty()) {
00063                     input.erase(input.end() - 1);
00064                     getConsole()->setInput(input);
00065                 }
00066             }
00067             break;
00068         case KEY_CLEAR:
00069             getConsole()->setInput("");
00070             break;
00071         case KEY_ENTER:
00072             {
00073                 std::string input = getConsole()->getInput();
00074                 if (!input.empty()) {
00075                     if (getConsole()->sendCommand()) {
00076                         m_history = input;
00077                         getConsole()->setInput("");
00078                     }
00079                 }
00080                 else {
00081                     quitState();
00082                 }
00083             }
00084             break;
00085         default:
00086             StateInput::specKey(keyIndex);
00087             break;
00088     }
00089 }
00090 //-----------------------------------------------------------------
00091 void
00092 ConsoleInput::specStroke(const KeyStroke &stroke)
00093 {
00094     //TODO: support UTF-8
00095     char c = stroke.getUnicode() & 0x7F;
00096     if (isprint(c)) {
00097         std::string input = getConsole()->getInput();
00098         input.push_back(c);
00099         getConsole()->setInput(input);
00100     }
00101 }
00102 

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