#include <config.h>
#include <iostream>
+#include <SDL_timer.h>
#include "console.hpp"
#include "video/drawing_context.hpp"
#include "video/surface.hpp"
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();
}
}
}
}
void
+Console::enter()
+{
+ std::string s = inputBuffer.str();
+ addLines("> "+s);
+ parse(s);
+ inputBuffer.str(std::string());
+}
+
+void
Console::scroll(int numLines)
{
offset += numLines;
}
}
+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 {
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;
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);
switch (event.key.keysym.sym) {
case SDLK_RETURN:
- Console::instance->input << std::endl;
+ Console::instance->enter();
break;
case SDLK_BACKSPACE:
Console::instance->backspace();
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);
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)) {