1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #include <iostream>
- #include <utility>
- #include <GLFW/glfw3.h>
- #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;
- }
|