Bladeren bron

Proper GLFW text input

Thomas Buck 9 jaren geleden
bovenliggende
commit
457c42b1b0
3 gewijzigde bestanden met toevoegingen van 27 en 17 verwijderingen
  1. 1
    0
      ChangeLog.md
  2. 1
    0
      include/system/WindowGLFW.h
  3. 25
    17
      src/system/WindowGLFW.cpp

+ 1
- 0
ChangeLog.md Bestand weergeven

@@ -5,6 +5,7 @@
5 5
     [ 20140318 ]
6 6
     * Updated imgui to version 1.36
7 7
     * Can now display all compiled/linked library versions
8
+    * GLFW windowing interface now supports proper text input
8 9
 
9 10
     [ 20140313 ]
10 11
     * Renderer now limits room list size

+ 1
- 0
include/system/WindowGLFW.h Bestand weergeven

@@ -44,6 +44,7 @@ class WindowGLFW {
44 44
     static void sizeCallback(GLFWwindow* w, int width, int height);
45 45
     static void cursorCallback(GLFWwindow* w, double xpos, double ypos);
46 46
     static void keyCallback(GLFWwindow* w, int key, int scancode, int action, int mods);
47
+    static void charCallback(GLFWwindow* w, unsigned int codepoint);
47 48
     static void buttonCallback(GLFWwindow* w, int button, int action, int mods);
48 49
     static void scrollCallback(GLFWwindow* w, double xoffset, double yoffset);
49 50
 

+ 25
- 17
src/system/WindowGLFW.cpp Bestand weergeven

@@ -5,6 +5,7 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
+#include <codecvt>
8 9
 #include <sstream>
9 10
 
10 11
 #define GLFW_INCLUDE_NONE
@@ -53,6 +54,7 @@ int WindowGLFW::initialize() {
53 54
     glfwSetWindowSizeCallback(window, WindowGLFW::sizeCallback);
54 55
     glfwSetCursorPosCallback(window, WindowGLFW::cursorCallback);
55 56
     glfwSetKeyCallback(window, WindowGLFW::keyCallback);
57
+    glfwSetCharCallback(window, WindowGLFW::charCallback);
56 58
     glfwSetMouseButtonCallback(window, WindowGLFW::buttonCallback);
57 59
     glfwSetScrollCallback(window, WindowGLFW::scrollCallback);
58 60
 
@@ -138,9 +140,7 @@ std::string WindowGLFW::getVersion(bool linked) {
138 140
     }
139 141
 }
140 142
 
141
-void WindowGLFW::inputPositionCallback(int x, int y) {
142
-
143
-}
143
+void WindowGLFW::inputPositionCallback(int x, int y) { }
144 144
 
145 145
 void WindowGLFW::errorCallback(int error, const char* desc) {
146 146
     Log::get(LOG_ERROR) << "GLFW Error (" << error << "): " << desc << Log::endl;
@@ -180,23 +180,31 @@ void WindowGLFW::keyCallback(GLFWwindow* w, int key, int scancode, int action, i
180 180
         UI::handleKeyboard(leftguiKey, modSuper);
181 181
     }
182 182
 
183
-    if (textinput && (action != GLFW_RELEASE)) {
184
-        //! \fixme GLFW does not support UTF8 text input?!
185
-        if ((key >= '0') && (key <= '9')) {
186
-            char s[2] = { (char)key, '\0' };
187
-            UI::handleText(s, false);
188
-        } else if ((key >= 'A') && (key <= 'Z')) {
189
-            key = key - 'A' + 'a';
190
-            char s[2] = { (char)key, '\0' };
191
-            UI::handleText(s, false);
192
-            key = key - 'a' + 'A';
193
-        }
194
-    }
195
-
196 183
     KeyboardButton b = convertAsciiButton(key);
197 184
     UI::handleKeyboard(b, (action != GLFW_RELEASE));
198 185
 }
199 186
 
187
+void WindowGLFW::charCallback(GLFWwindow* w, unsigned int codepoint) {
188
+    static std::codecvt_utf8<char32_t> conv;
189
+    static mbstate_t state;
190
+    static const int bufferSize = 42;
191
+    static char buffer[bufferSize + 1];
192
+
193
+    if (textinput) {
194
+        char32_t inBuff[2] = { codepoint, '\0' };
195
+        const char32_t* in = nullptr;
196
+        char* ex = nullptr;
197
+        auto res = conv.out(state, inBuff, inBuff + 1, in, buffer, buffer + bufferSize, ex);
198
+        if (res != 0) {
199
+            Log::get(LOG_DEBUG) << "UTF-32 to UTF-8 conversion error (" << res << ")" << Log::endl;
200
+            return;
201
+        }
202
+
203
+        *ex = '\0';
204
+        UI::handleText(buffer, false);
205
+    }
206
+}
207
+
200 208
 void WindowGLFW::buttonCallback(GLFWwindow* w, int button, int action, int mods) {
201 209
     if (((mods & GLFW_MOD_SHIFT) != 0) != modShift) {
202 210
         modShift = (mods & GLFW_MOD_SHIFT) != 0;
@@ -253,7 +261,7 @@ KeyboardButton WindowGLFW::convertAsciiButton(int key) {
253 261
         return static_cast<KeyboardButton>(key);
254 262
     }
255 263
 
256
-    //! \fixme GLFW requires keyboard layout? Currently US is hard coded
264
+    //! \fixme GLFW requires keyboard layout, currently US is hard coded!
257 265
     switch (key) {
258 266
         case ' ':
259 267
             return spaceKey;

Laden…
Annuleren
Opslaan