Browse Source

text input limit

Kajetan Johannes Hammerle 3 years ago
parent
commit
35425beaa4
2 changed files with 11 additions and 2 deletions
  1. 9 2
      input/TextInput.cpp
  2. 2 0
      input/TextInput.h

+ 9 - 2
input/TextInput.cpp

@@ -5,7 +5,14 @@
 
 #include "input/TextInput.h"
 
-TextInput::TextInput() : active(false) {
+TextInput::TextInput() : limit(256), active(false) {
+}
+
+void TextInput::setLimit(int limit) {
+    TextInput::limit = limit;
+    while(input.getLength() > limit) {
+        input.removeBySwap(limit);
+    }
 }
 
 void TextInput::reset() {
@@ -44,7 +51,7 @@ void TextInput::onKeyEvent(int key, int scancode, int action, int mods) {
 }
 
 void TextInput::onCharEvent(unsigned int codepoint) {
-    if(!active) {
+    if(!active || input.getLength() >= limit) {
         return;
     }
     input.add(codepoint);

+ 2 - 0
input/TextInput.h

@@ -6,11 +6,13 @@
 class TextInput final {
     List<unsigned int> input;
     int cursor;
+    int limit;
     bool active;
 
 public:
     TextInput();
 
+    void setLimit(int limit);
     void reset();
     void setActive(bool b);
     void onKeyEvent(int key, int scancode, int action, int mods);