#include #include #include #include "input/TextInput.h" TextInput::TextInput() : active(false) { } void TextInput::reset() { input.clear(); cursor = 0; } void TextInput::setActive(bool b) { active = b; } void TextInput::onKeyEvent(int key, int scancode, int action, int mods) { if(!active || action == GLFW_RELEASE) { return; } (void)scancode; (void)mods; switch(key) { case GLFW_KEY_BACKSPACE: if(input.getLength() > cursor - 1 && cursor > 0) { input.remove(cursor - 1); cursor--; } break; case GLFW_KEY_LEFT: if(cursor > 0) { cursor--; } break; case GLFW_KEY_RIGHT: if(cursor < input.getLength()) { cursor++; } break; } } void TextInput::onCharEvent(unsigned int codepoint) { if(!active) { return; } input.add(codepoint); for(int i = input.getLength() - 1; i > cursor; i--) { std::swap(input[i], input[i - 1]); } cursor++; } int TextInput::getCursor() const { return cursor; }