123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #include <iostream>
- #include <utility>
- #include <GLFW/glfw3.h>
- #include "input/TextInput.h"
- TextInput::TextInput() : limit(256), active(false) {
- }
- void TextInput::setLimit(int limit) {
- TextInput::limit = limit;
- while(input.getLength() > limit) {
- input.removeBySwap(limit);
- }
- }
- 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 || input.getLength() >= limit) {
- 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;
- }
|