| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "ConsoleInput.h" |
| 10 |
|
| 11 |
#include "Keymap.h" |
| 12 |
#include "KeyConsole.h" |
| 13 |
|
| 14 |
#include "Log.h" |
| 15 |
#include "KeyDesc.h" |
| 16 |
#include "KeyStroke.h" |
| 17 |
|
| 18 |
#include <ctype.h> // isprint() |
| 19 |
|
| 20 |
//----------------------------------------------------------------- |
| 21 |
ConsoleInput::ConsoleInput(KeyConsole *console) |
| 22 |
: StateInput(console) |
| 23 |
{ |
| 24 |
KeyDesc key_history(KEY_HISTORY, "input history"); |
| 25 |
KeyDesc key_backspace(KEY_BACKSPACE, "backspace"); |
| 26 |
KeyDesc key_clear(KEY_CLEAR, "clear"); |
| 27 |
KeyDesc key_enter(KEY_ENTER, "enter"); |
| 28 |
|
| 29 |
m_keymap->registerKey(KeyStroke(SDLK_UP, KMOD_NONE), key_history); |
| 30 |
m_keymap->registerKey(KeyStroke(SDLK_BACKSPACE, KMOD_NONE), key_backspace); |
| 31 |
m_keymap->registerKey(KeyStroke(SDLK_u, KMOD_LCTRL), key_clear); |
| 32 |
m_keymap->registerKey(KeyStroke(SDLK_u, KMOD_RCTRL), key_clear); |
| 33 |
m_keymap->registerKey(KeyStroke(SDLK_RETURN, KMOD_NONE), key_enter); |
| 34 |
} |
| 35 |
//----------------------------------------------------------------- |
| 36 |
KeyConsole * |
| 37 |
ConsoleInput::getConsole() |
| 38 |
{ |
| 39 |
return dynamic_cast<KeyConsole*>(m_state); |
| 40 |
} |
| 41 |
//----------------------------------------------------------------- |
| 42 |
/** |
| 43 |
* Toggle console. |
| 44 |
*/ |
| 45 |
void |
| 46 |
ConsoleInput::enableConsole() |
| 47 |
{ |
| 48 |
quitState(); |
| 49 |
} |
| 50 |
//----------------------------------------------------------------- |
| 51 |
void |
| 52 |
ConsoleInput::specKey(int keyIndex) |
| 53 |
{ |
| 54 |
//TODO: simulate key repeat in console |
| 55 |
switch (keyIndex) { |
| 56 |
case KEY_HISTORY: |
| 57 |
getConsole()->setInput(m_history); |
| 58 |
break; |
| 59 |
case KEY_BACKSPACE: |
| 60 |
{ |
| 61 |
std::string input = getConsole()->getInput(); |
| 62 |
if (!input.empty()) { |
| 63 |
input.erase(input.end() - 1); |
| 64 |
getConsole()->setInput(input); |
| 65 |
} |
| 66 |
} |
| 67 |
break; |
| 68 |
case KEY_CLEAR: |
| 69 |
getConsole()->setInput(""); |
| 70 |
break; |
| 71 |
case KEY_ENTER: |
| 72 |
{ |
| 73 |
std::string input = getConsole()->getInput(); |
| 74 |
if (!input.empty()) { |
| 75 |
if (getConsole()->sendCommand()) { |
| 76 |
m_history = input; |
| 77 |
getConsole()->setInput(""); |
| 78 |
} |
| 79 |
} |
| 80 |
else { |
| 81 |
quitState(); |
| 82 |
} |
| 83 |
} |
| 84 |
break; |
| 85 |
default: |
| 86 |
StateInput::specKey(keyIndex); |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
//----------------------------------------------------------------- |
| 91 |
void |
| 92 |
ConsoleInput::specStroke(const KeyStroke &stroke) |
| 93 |
{ |
| 94 |
//TODO: support UTF-8 |
| 95 |
char c = stroke.getUnicode() & 0x7F; |
| 96 |
if (isprint(c)) { |
| 97 |
std::string input = getConsole()->getInput(); |
| 98 |
input.append(1, c); |
| 99 |
getConsole()->setInput(input); |
| 100 |
} |
| 101 |
} |
| 102 |
|