From 5c53cc30550b59d512a4bb73888af8cbc43ebb4f Mon Sep 17 00:00:00 2001 From: Christoph Sommer Date: Mon, 5 Feb 2007 23:24:54 +0000 Subject: [PATCH 1/1] Some more keys for the console SVN-Revision: 4815 --- src/console.cpp | 28 ++++++++++++++++++++++++---- src/console.hpp | 2 ++ src/control/joystickkeyboardcontroller.cpp | 13 +++++++++++-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index 1408bbde5..fc5c1cc0f 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "console.hpp" #include "video/drawing_context.hpp" #include "video/surface.hpp" @@ -73,9 +74,7 @@ Console::flush(ConsoleStreamBuffer* buffer) std::string s = inputBuffer.str(); if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) { while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1); - addLines("> "+s); - parse(s); - inputBuffer.str(std::string()); + enter(); } } } @@ -158,6 +157,15 @@ Console::backspace() } void +Console::enter() +{ + std::string s = inputBuffer.str(); + addLines("> "+s); + parse(s); + inputBuffer.str(std::string()); +} + +void Console::scroll(int numLines) { offset += numLines; @@ -183,6 +191,14 @@ Console::show_history(int offset) } } +void +Console::move_cursor(int offset) +{ + if (offset == -65535) inputBuffer.pubseekoff(0, std::ios_base::beg, std::ios_base::out); + if (offset == +65535) inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out); + inputBuffer.pubseekoff(offset, std::ios_base::cur, std::ios_base::out); +} + // Helper functions for Console::autocomplete // TODO: Fix rough documentation namespace { @@ -458,7 +474,11 @@ Console::draw(DrawingContext& context) if (focused) { lineNo++; float py = height-4-1 * font->get_height(); - context.draw_text(font.get(), "> "+inputBuffer.str()+"_", Vector(4, py), ALIGN_LEFT, layer); + context.draw_text(font.get(), "> "+inputBuffer.str(), Vector(4, py), ALIGN_LEFT, layer); + if (SDL_GetTicks() % 1000 < 750) { + int cursor_px = 2 + inputBuffer.pubseekoff(0, std::ios_base::cur, std::ios_base::out); + context.draw_text(font.get(), "_", Vector(4 + (cursor_px * font->get_text_width("X")), py), ALIGN_LEFT, layer); + } } int skipLines = -offset; diff --git a/src/console.hpp b/src/console.hpp index 83ea2fe07..94043e80d 100644 --- a/src/console.hpp +++ b/src/console.hpp @@ -49,9 +49,11 @@ public: void init_graphics(); void backspace(); /**< delete last character sent to the input stream */ + void enter(); /**< process and clear input stream */ void scroll(int offset); /**< scroll console text up or down by @c offset lines */ void autocomplete(); /**< autocomplete current command */ void show_history(int offset); /**< move @c offset lines forward through history; Negative offset moves backward */ + void move_cursor(int offset); /**< move the cursor @c offset chars to the right; Negative offset moves backward; 0xFFFF moves to the end */ void draw(DrawingContext& context); /**< draw the console in a DrawingContext */ void update(float elapsed_time); diff --git a/src/control/joystickkeyboardcontroller.cpp b/src/control/joystickkeyboardcontroller.cpp index 5e2455af5..d386a80a4 100644 --- a/src/control/joystickkeyboardcontroller.cpp +++ b/src/control/joystickkeyboardcontroller.cpp @@ -392,7 +392,7 @@ JoystickKeyboardController::process_console_key_event(const SDL_Event& event) switch (event.key.keysym.sym) { case SDLK_RETURN: - Console::instance->input << std::endl; + Console::instance->enter(); break; case SDLK_BACKSPACE: Console::instance->backspace(); @@ -406,8 +406,11 @@ JoystickKeyboardController::process_console_key_event(const SDL_Event& event) case SDLK_PAGEDOWN: Console::instance->scroll(+1); break; + case SDLK_HOME: + Console::instance->move_cursor(-65535); + break; case SDLK_END: - Console::instance->scroll(+65535); + Console::instance->move_cursor(+65535); break; case SDLK_UP: Console::instance->show_history(-1); @@ -415,6 +418,12 @@ JoystickKeyboardController::process_console_key_event(const SDL_Event& event) case SDLK_DOWN: Console::instance->show_history(+1); break; + case SDLK_LEFT: + Console::instance->move_cursor(-1); + break; + case SDLK_RIGHT: + Console::instance->move_cursor(+1); + break; default: int c = event.key.keysym.unicode; if ((c >= 32) && (c <= 126)) { -- 2.11.0