diff options
Diffstat (limited to 'src/client')
-rw-r--r-- | src/client/console.cc | 113 | ||||
-rw-r--r-- | src/client/console.h | 1 |
2 files changed, 94 insertions, 20 deletions
diff --git a/src/client/console.cc b/src/client/console.cc index 83d69de..71f3d5a 100644 --- a/src/client/console.cc +++ b/src/client/console.cc @@ -41,17 +41,18 @@ public: // private client console object Console console; -// client console input -std::string input; - // console text data std::deque<std::string> text; +// input history +std::deque<std::string> history; +std::deque<std::string>::reverse_iterator history_pos; +size_t input_pos = 0; + // console visibility bool console_visible; - size_t console_scroll = 0; -size_t input_pos = 0; + //--- engine functions -------------------------------------------- @@ -65,11 +66,19 @@ extern "C" void func_con_toggle(std::stringstream &args) void init() { con_print << "Initializing console..." << std::endl; - + console_visible = false; - + // add engine functions core::func::add("con_toggle", func_con_toggle); + + text.clear(); + console_scroll = 0; + + history.clear(); + history.push_back(""); + history_pos = history.rbegin(); + input_pos = 0; } void shutdown() @@ -78,6 +87,12 @@ void shutdown() // remove engine functions core::func::remove("con_toggle"); + + text.clear(); + console_scroll = 0; + + history.clear(); + input_pos = 0; } void draw() @@ -140,12 +155,12 @@ void draw() // draw the console input gl::color(0.0f, 1.0f, 0.0f, 1.0f); - draw_text(CHARWIDTH, video::height*con_height - CHARHEIGHT - 4, input); + draw_text(CHARWIDTH, video::height*con_height - CHARHEIGHT - 4, (*history_pos)); // draw cursor if ((core::time() - ::floorf(core::time())) < 0.5f) { std::string cursor("_"); - draw_text(CHARWIDTH*(input.size()+1), video::height*con_height - CHARHEIGHT - 4 , cursor); + draw_text(CHARWIDTH*(input_pos+1), video::height*con_height - CHARHEIGHT - 4 , cursor); } gl::disable(GL_TEXTURE_2D); } @@ -168,23 +183,76 @@ void toggle() { console_visible = !console_visible; setkeyboardmode(console_visible); - console_scroll = 0; - input.clear(); - input_pos = 0; + + if (console_visible) { + console_scroll = 0; + input_pos = 0; + + history_pos = history.rbegin(); + (*history_pos).clear(); + } } void keypressed(const SDL_keysym &keysym) { + std::deque<std::string>::reverse_iterator upit; + switch( keysym.sym ) { + case SDLK_TAB: + core::commandbuffer::complete( (*history_pos), input_pos); + break; case SDLK_RETURN: - if (input.size()) { - core::cmd << input << std::endl; - input.clear(); + if ((*history_pos).size()) { + // store input into history + while (history.size() >= MAXHISTOLINES) { + history.pop_front(); + } + + core::cmd << (*history_pos) << std::endl; + + history.push_back(""); + history_pos = history.rbegin(); + input_pos = 0; + } + break; + case SDLK_KP8: + case SDLK_UP: + upit = history_pos; + ++upit; + if (upit != history.rend()) { + history_pos = upit; + input_pos = (*history_pos).size(); + } + break; + case SDLK_KP2: + case SDLK_DOWN: + if (history_pos != history.rbegin()) { + --history_pos; + input_pos = (*history_pos).size(); } break; + case SDLK_KP7: + case SDLK_HOME: + input_pos = 0; + break; + case SDLK_KP1: + case SDLK_END: + input_pos = (*history_pos).size(); + break; + case SDLK_KP4: + case SDLK_LEFT: + if (input_pos > 0) + input_pos--; + break; + case SDLK_KP6: + case SDLK_RIGHT: + if (input_pos <= (*history_pos).size()) + input_pos++; + break; case SDLK_BACKSPACE: - if (input.size()) { - input.erase(input.size()-1, 1); + if ((*history_pos).size() && input_pos) { + (*history_pos).erase(input_pos-1, 1); + input_pos--; } break; case SDLK_PAGEUP: @@ -196,12 +264,17 @@ void keypressed(const SDL_keysym &keysym) else console_scroll = 0; break; default: + if ((keysym.sym >= 32 ) && (keysym.sym <175)) { + if (input_pos == (*history_pos).size()) + (*history_pos) += keysym_to_char(keysym); + else + (*history_pos).insert(input_pos, 1, keysym_to_char(keysym)); + input_pos++; + } break; } - if ((keysym.sym >= 32 ) && (keysym.sym <175)) { - input += keysym_to_char(keysym); - } + } bool visible() diff --git a/src/client/console.h b/src/client/console.h index e558438..e848720 100644 --- a/src/client/console.h +++ b/src/client/console.h @@ -15,6 +15,7 @@ #include <deque> const size_t MAXCONLINES=2048; +const size_t MAXHISTOLINES=512; namespace client { |