TextInput.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <iostream>
  2. #include <utility>
  3. #include <GLFW/glfw3.h>
  4. #include "input/TextInput.h"
  5. TextInput::TextInput() : active(false) {
  6. }
  7. void TextInput::reset() {
  8. input.clear();
  9. cursor = 0;
  10. }
  11. void TextInput::setActive(bool b) {
  12. active = b;
  13. }
  14. void TextInput::onKeyEvent(int key, int scancode, int action, int mods) {
  15. if(!active || action == GLFW_RELEASE) {
  16. return;
  17. }
  18. (void)scancode;
  19. (void)mods;
  20. switch(key) {
  21. case GLFW_KEY_BACKSPACE:
  22. if(input.getLength() > cursor - 1 && cursor > 0) {
  23. input.remove(cursor - 1);
  24. cursor--;
  25. }
  26. break;
  27. case GLFW_KEY_LEFT:
  28. if(cursor > 0) {
  29. cursor--;
  30. }
  31. break;
  32. case GLFW_KEY_RIGHT:
  33. if(cursor < input.getLength()) {
  34. cursor++;
  35. }
  36. break;
  37. }
  38. }
  39. void TextInput::onCharEvent(unsigned int codepoint) {
  40. if(!active) {
  41. return;
  42. }
  43. input.add(codepoint);
  44. for(int i = input.getLength() - 1; i > cursor; i--) {
  45. std::swap(input[i], input[i - 1]);
  46. }
  47. cursor++;
  48. }
  49. int TextInput::getCursor() const {
  50. return cursor;
  51. }