more elaborate keyboard input handling, and debug gui improvements
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 28 Nov 2017 05:07:19 +0000 (07:07 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 28 Nov 2017 05:07:19 +0000 (07:07 +0200)
src/app.cc
src/app.h
src/dbg_gui.cc
src/dbg_gui.h
src/main.cc

index 0356104..a03fd34 100644 (file)
@@ -500,9 +500,9 @@ void app_keyboard(int key, bool pressed)
 {
        unsigned int mod = app_get_modifiers();
 
-       if(!debug_gui_key(key, pressed, mod)) {
-               // the debug gui indicated that we should ignore the event
-               return;
+       if(show_debug_gui && !(pressed && (key == '`' || key == 27))) {
+               debug_gui_key(key, pressed, mod);
+               return; // ignore all keystrokes when GUI is visible
        }
 
        if(pressed) {
@@ -518,12 +518,12 @@ void app_keyboard(int key, bool pressed)
                        }
                        break;
 
-               case '\t':
+               case '`':
                        show_debug_gui = !show_debug_gui;
                        show_message("debug gui %s", show_debug_gui ? "enabled" : "disabled");
                        break;
 
-               case '`':
+               case 'm':
                        app_toggle_grab_mouse();
                        show_message("mouse %s", app_is_mouse_grabbed() ? "grabbed" : "released");
                        break;
@@ -592,9 +592,9 @@ void app_keyboard(int key, bool pressed)
 
 void app_mouse_button(int bn, bool pressed, int x, int y)
 {
-       if(!debug_gui_mbutton(bn, pressed, x, y)) {
-               // the debug GUI indicated we should ignore the event
-               return;
+       if(show_debug_gui) {
+               debug_gui_mbutton(bn, pressed, x, y);
+               return; // ignore mouse events while GUI is visible
        }
 
        prev_mx = x;
@@ -620,9 +620,9 @@ static void mouse_zoom(float dx, float dy)
 
 void app_mouse_motion(int x, int y)
 {
-       if(!debug_gui_mmotion(x, y)) {
-               // the debug GUI indicated we should ignore the event
-               return;
+       if(show_debug_gui) {
+               debug_gui_mmotion(x, y);
+               return; // ignore mouse events while GUI is visible
        }
 
        int dx = x - prev_mx;
@@ -649,6 +649,13 @@ void app_mouse_delta(int dx, int dy)
        }
 }
 
+void app_mouse_wheel(int dir)
+{
+       if(show_debug_gui) {
+               debug_gui_wheel(dir);
+       }
+}
+
 void app_gamepad_axis(int axis, float val)
 {
        switch(axis) {
index 5e7554f..83680ec 100644 (file)
--- a/src/app.h
+++ b/src/app.h
@@ -21,6 +21,31 @@ enum {
        MOD_CTRL        = 4
 };
 
+/* special keys */
+enum {
+       KEY_DEL = 127,
+       KEY_LEFT,
+       KEY_RIGHT,
+       KEY_UP,
+       KEY_DOWN,
+       KEY_PGUP,
+       KEY_PGDOWN,
+       KEY_HOME,
+       KEY_END,
+       KEY_F1,
+       KEY_F2,
+       KEY_F3,
+       KEY_F4,
+       KEY_F5,
+       KEY_F6,
+       KEY_F7,
+       KEY_F8,
+       KEY_F9,
+       KEY_F10,
+       KEY_F11,
+       KEY_F12
+};
+
 /* XXX make sure these match with SDL_GameControllerButton */
 enum {
        GPAD_A,
@@ -50,6 +75,7 @@ void app_keyboard(int key, bool pressed);
 void app_mouse_button(int bn, bool pressed, int x, int y);
 void app_mouse_motion(int x, int y);
 void app_mouse_delta(int dx, int dy);
+void app_mouse_wheel(int dir);
 
 void app_gamepad_axis(int axis, float val);
 void app_gamepad_button(int bn, bool pressed);
index c0c7984..a968b29 100644 (file)
@@ -1,3 +1,4 @@
+#include <ctype.h>
 #include "dbg_gui.h"
 #include "imgui/imgui.h"
 #include "app.h"
@@ -16,6 +17,26 @@ bool init_debug_gui()
        io->DisplaySize.y = win_height;
        io->RenderDrawListsFn = render_func;
 
+       io->KeyMap[ImGuiKey_Tab] = '\t';
+       io->KeyMap[ImGuiKey_LeftArrow] = KEY_LEFT;
+       io->KeyMap[ImGuiKey_RightArrow] = KEY_RIGHT;
+       io->KeyMap[ImGuiKey_UpArrow] = KEY_UP;
+       io->KeyMap[ImGuiKey_DownArrow] = KEY_DOWN;
+       io->KeyMap[ImGuiKey_PageUp] = KEY_PGUP;
+       io->KeyMap[ImGuiKey_PageDown] = KEY_PGDOWN;
+       io->KeyMap[ImGuiKey_Home] = KEY_HOME;
+       io->KeyMap[ImGuiKey_End] = KEY_END;
+       io->KeyMap[ImGuiKey_Delete] = KEY_DEL;
+       io->KeyMap[ImGuiKey_Backspace] = '\b';
+       io->KeyMap[ImGuiKey_Enter] = '\n';
+       io->KeyMap[ImGuiKey_Escape] = 27;
+       io->KeyMap[ImGuiKey_A] = 'a';
+       io->KeyMap[ImGuiKey_C] = 'c';
+       io->KeyMap[ImGuiKey_V] = 'v';
+       io->KeyMap[ImGuiKey_X] = 'x';
+       io->KeyMap[ImGuiKey_Y] = 'y';
+       io->KeyMap[ImGuiKey_Z] = 'z';
+
        unsigned char *pixels;
        int width, height;
        io->Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
@@ -42,32 +63,37 @@ void debug_gui_reshape(int x, int y)
        io->DisplaySize.y = y;
 }
 
-bool debug_gui_key(int key, bool press, unsigned int modstate)
+void debug_gui_key(int key, bool press, unsigned int modstate)
 {
-       io->KeysDown[key] = press;
+       if(key < 512) {
+               io->KeysDown[key] = press;
+       }
        io->KeyShift = (modstate & MOD_SHIFT) != 0;
        io->KeyCtrl = (modstate & MOD_CTRL) != 0;
        io->KeyAlt = (modstate & MOD_ALT) != 0;
        io->KeySuper = false;
 
-       return true;    // TODO
+       if(press && key < 256 && isprint(key)) {
+               io->AddInputCharacter(key);
+       }
 }
 
-bool debug_gui_mbutton(int bn, bool press, int x, int y)
+void debug_gui_mbutton(int bn, bool press, int x, int y)
 {
        io->MouseDown[bn] = press;
        io->MousePos.x = x;
        io->MousePos.y = y;
-
-       return true;    // TODO
 }
 
-bool debug_gui_mmotion(int x, int y)
+void debug_gui_mmotion(int x, int y)
 {
        io->MousePos.x = x;
        io->MousePos.y = y;
+}
 
-       return true;    // TODO
+void debug_gui_wheel(int dir)
+{
+       io->MouseWheel = dir;
 }
 
 static void render_func(ImDrawData *ddat)
index 5374d78..739e1af 100644 (file)
@@ -7,8 +7,9 @@ bool init_debug_gui();
 void cleanup_debug_gui();
 
 void debug_gui_reshape(int x, int y);
-bool debug_gui_key(int key, bool press, unsigned int modstate);
-bool debug_gui_mbutton(int bn, bool press, int x, int y);
-bool debug_gui_mmotion(int x, int y);
+void debug_gui_key(int key, bool press, unsigned int modstate);
+void debug_gui_mbutton(int bn, bool press, int x, int y);
+void debug_gui_mmotion(int x, int y);
+void debug_gui_wheel(int dir);
 
 #endif // DBG_GUI_H_
index d24428c..c497dc1 100644 (file)
@@ -8,6 +8,7 @@
 static bool init(int argc, char **argv);
 static void process_event(SDL_Event *ev);
 static void proc_modkeys();
+static int translate_keysym(SDL_Keycode sym);
 
 static SDL_Window *win;
 static SDL_GLContext ctx;
@@ -165,6 +166,8 @@ static bool init(int argc, char **argv)
 
 static void process_event(SDL_Event *ev)
 {
+       int key;
+
        switch(ev->type) {
        case SDL_QUIT:
                quit = true;
@@ -173,7 +176,9 @@ static void process_event(SDL_Event *ev)
        case SDL_KEYDOWN:
        case SDL_KEYUP:
                proc_modkeys();
-               app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
+               if((key = translate_keysym(ev->key.keysym.sym)) != -1) {
+                       app_keyboard(key, ev->key.state == SDL_PRESSED);
+               }
                break;
 
        case SDL_MOUSEBUTTONDOWN:
@@ -191,6 +196,10 @@ static void process_event(SDL_Event *ev)
                }
                break;
 
+       case SDL_MOUSEWHEEL:
+               app_mouse_wheel(ev->wheel.y);
+               break;
+
        case SDL_WINDOWEVENT:
                if(ev->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
                        SDL_GL_GetDrawableSize(win, &win_width, &win_height);
@@ -225,3 +234,39 @@ static void proc_modkeys()
                modkeys |= MOD_CTRL;
        }
 }
+
+static int translate_keysym(SDL_Keycode sym)
+{
+       switch(sym) {
+       case SDLK_RETURN:
+               return '\n';
+       case SDLK_DELETE:
+               return KEY_DEL;
+       case SDLK_LEFT:
+               return KEY_LEFT;
+       case SDLK_RIGHT:
+               return KEY_RIGHT;
+       case SDLK_UP:
+               return KEY_UP;
+       case SDLK_DOWN:
+               return KEY_DOWN;
+       case SDLK_PAGEUP:
+               return KEY_PGUP;
+       case SDLK_PAGEDOWN:
+               return KEY_PGDOWN;
+       case SDLK_HOME:
+               return KEY_HOME;
+       case SDLK_END:
+               return KEY_END;
+       default:
+               break;
+       }
+
+       if(sym < 127) {
+               return sym;
+       }
+       if(sym >= SDLK_F1 && sym <= SDLK_F12) {
+               return KEY_F1 + sym - SDLK_F1;
+       }
+       return -1;
+}