Browse Source

text input filling

Kajetan Johannes Hammerle 3 years ago
parent
commit
eb3c259056
2 changed files with 38 additions and 3 deletions
  1. 35 1
      input/TextInput.cpp
  2. 3 2
      input/TextInput.h

+ 35 - 1
input/TextInput.cpp

@@ -49,7 +49,7 @@ void TextInput::onKeyEvent(int key, int scancode, int action, int mods) {
     }
 }
 
-void TextInput::onCharEvent(unsigned int codepoint) {
+void TextInput::onCharEvent(uint32 codepoint) {
     if(!active || input.getLength() >= limit) {
         return;
     }
@@ -60,6 +60,40 @@ void TextInput::onCharEvent(unsigned int codepoint) {
     cursor++;
 }
 
+static uint32 read(int& index, const char* s) {
+    if(s[index] == '\0') {
+        return '\0';
+    }
+    return s[index++];
+}
+
+static uint32 readUnicode(int& index, const char* s) {
+    uint32 c = read(index, s);
+    if((c & 0xE0) == 0xC0) {
+        c = ((c & 0x1F) << 6) | (read(index, s) & 0x3F);
+    } else if((c & 0xF0) == 0xE0) {
+        c = ((c & 0xF) << 12) | ((read(index, s) & 0x3F) << 6);
+        c |= read(index, s) & 0x3F;
+    } else if((c & 0xF8) == 0xF0) {
+        c = ((c & 0x7) << 18) | ((read(index, s) & 0x3F) << 12);
+        c |= (read(index, s) & 0x3F) << 6;
+        c |= read(index, s) & 0x3F;
+    }
+    return c;
+}
+
+void TextInput::fill(const char* s) {
+    int index = 0;
+    reset();
+    while(true) {
+        uint32 c = readUnicode(index, s);
+        if(c == '\0') {
+            break;
+        }
+        onCharEvent(c);
+    }
+}
+
 int TextInput::getCursor() const {
     return cursor;
 }

+ 3 - 2
input/TextInput.h

@@ -4,7 +4,7 @@
 #include "utils/List.h"
 
 class TextInput final {
-    List<unsigned int> input;
+    List<uint32> input;
     int cursor;
     int limit;
     bool active;
@@ -16,7 +16,8 @@ public:
     void reset();
     void setActive(bool b);
     void onKeyEvent(int key, int scancode, int action, int mods);
-    void onCharEvent(unsigned int codepoint);
+    void onCharEvent(uint32);
+    void fill(const char* s);
 
     template<int N>
     void toString(StringBuffer<N>& s) const {