]> git.lizzy.rs Git - irrlicht.git/commitdiff
Refactor SDL input code to fix menu exit (#146)
authorndren <andreien@proton.me>
Sat, 18 Feb 2023 15:16:17 +0000 (15:16 +0000)
committerGitHub <noreply@github.com>
Sat, 18 Feb 2023 15:16:17 +0000 (16:16 +0100)
source/Irrlicht/CIrrDeviceSDL.cpp
source/Irrlicht/CIrrDeviceSDL.h

index 83c763c61d91a3260b85e98110ca0a0034103c81..23f00d2579a92a48a60c5a7e92888597dbf66f37 100644 (file)
@@ -8,6 +8,8 @@
 \r
 #include "CIrrDeviceSDL.h"\r
 #include "IEventReceiver.h"\r
+#include "IGUIElement.h"\r
+#include "IGUIEnvironment.h"\r
 #include "os.h"\r
 #include "CTimer.h"\r
 #include "irrString.h"\r
@@ -105,6 +107,98 @@ EM_BOOL CIrrDeviceSDL::MouseLeaveCallback(int eventType, const EmscriptenMouseEv
 }\r
 #endif\r
 \r
+\r
+bool CIrrDeviceSDL::keyIsKnownSpecial(EKEY_CODE key)\r
+{\r
+       switch ( key )\r
+       {\r
+               // keys which are known to have safe special character interpretation\r
+               // could need changes over time (removals and additions!)\r
+               case KEY_RETURN:\r
+               case KEY_PAUSE:\r
+               case KEY_ESCAPE:\r
+               case KEY_PRIOR:\r
+               case KEY_NEXT:\r
+               case KEY_HOME:\r
+               case KEY_END:\r
+               case KEY_LEFT:\r
+               case KEY_UP:\r
+               case KEY_RIGHT:\r
+               case KEY_DOWN:\r
+               case KEY_TAB:\r
+               case KEY_PRINT:\r
+               case KEY_SNAPSHOT:\r
+               case KEY_INSERT:\r
+               case KEY_BACK:\r
+               case KEY_DELETE:\r
+               case KEY_HELP:\r
+               case KEY_APPS:\r
+               case KEY_SLEEP:\r
+               case KEY_F1:\r
+               case KEY_F2:\r
+               case KEY_F3:\r
+               case KEY_F4:\r
+               case KEY_F5:\r
+               case KEY_F6:\r
+               case KEY_F7:\r
+               case KEY_F8:\r
+               case KEY_F9:\r
+               case KEY_F10:\r
+               case KEY_F11:\r
+               case KEY_F12:\r
+               case KEY_F13:\r
+               case KEY_F14:\r
+               case KEY_F15:\r
+               case KEY_F16:\r
+               case KEY_F17:\r
+               case KEY_F18:\r
+               case KEY_F19:\r
+               case KEY_F20:\r
+               case KEY_F21:\r
+               case KEY_F22:\r
+               case KEY_F23:\r
+               case KEY_F24:\r
+               case KEY_NUMLOCK:\r
+               case KEY_SCROLL:\r
+               case KEY_LCONTROL:\r
+               case KEY_RCONTROL:\r
+                       return true;\r
+\r
+               default:\r
+                       return false;\r
+       }\r
+}\r
+\r
+int CIrrDeviceSDL::findCharToPassToIrrlicht(int assumedChar, EKEY_CODE key) {\r
+       // SDL in-place ORs values with no character representation with 1<<30\r
+       // https://wiki.libsdl.org/SDL2/SDLKeycodeLookup\r
+       if (assumedChar & (1<<30))\r
+               return 0;\r
+\r
+       switch (key) {\r
+               case KEY_PRIOR:\r
+               case KEY_NEXT:\r
+               case KEY_HOME:\r
+               case KEY_END:\r
+               case KEY_LEFT:\r
+               case KEY_UP:\r
+               case KEY_RIGHT:\r
+               case KEY_DOWN:\r
+               case KEY_NUMLOCK:\r
+                       return 0;\r
+               default:\r
+                       return assumedChar;\r
+       }\r
+}\r
+\r
+void CIrrDeviceSDL::resetReceiveTextInputEvents() {\r
+       gui::IGUIElement *elem = GUIEnvironment->getFocus();\r
+       if (elem && elem->acceptsIME())\r
+               SDL_StartTextInput();\r
+       else\r
+               SDL_StopTextInput();\r
+}\r
+\r
 //! constructor\r
 CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)\r
        : CIrrDeviceStub(param),\r
@@ -611,6 +705,10 @@ bool CIrrDeviceSDL::run()
                                else\r
                                        key = (EKEY_CODE)KeyMap[idx].Win32Key;\r
 \r
+                               // Make sure to only input special characters if something is in focus, as SDL_TEXTINPUT handles normal unicode already\r
+                               if (SDL_IsTextInputActive() && !keyIsKnownSpecial(key) && (SDL_event.key.keysym.mod & KMOD_CTRL) == 0)\r
+                                       break;\r
+\r
 #ifdef _IRR_WINDOWS_API_\r
                                // handle alt+f4 in Windows, because SDL seems not to\r
                                if ( (SDL_event.key.keysym.mod & KMOD_LALT) && key == KEY_F4)\r
@@ -624,12 +722,7 @@ bool CIrrDeviceSDL::run()
                                irrevent.KeyInput.PressedDown = (SDL_event.type == SDL_KEYDOWN);\r
                                irrevent.KeyInput.Shift = (SDL_event.key.keysym.mod & KMOD_SHIFT) != 0;\r
                                irrevent.KeyInput.Control = (SDL_event.key.keysym.mod & KMOD_CTRL ) != 0;\r
-                               // These keys are handled differently in CGUIEditBox.cpp (may become out of date!)\r
-                               // Control key is used in special character combinations, so keep that too\r
-                               // Pass through the keysym only then so no extra text gets input\r
-                               irrevent.KeyInput.Char = 0;\r
-                               if (mp.SDLKey == SDLK_DELETE || mp.SDLKey == SDLK_RETURN || mp.SDLKey == SDLK_BACKSPACE || irrevent.KeyInput.Control)\r
-                                       irrevent.KeyInput.Char = mp.SDLKey;\r
+                               irrevent.KeyInput.Char = findCharToPassToIrrlicht(mp.SDLKey, key);\r
                                postEventFromUser(irrevent);\r
                        }\r
                        break;\r
@@ -663,7 +756,7 @@ bool CIrrDeviceSDL::run()
                default:\r
                        break;\r
                } // end switch\r
-\r
+       resetReceiveTextInputEvents();\r
        } // end while\r
 \r
 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)\r
index 299271823d1d4dc177ecc848cd758ce873375af7..7657718fcbbda3f506c2e3c85f29a1471d1556b4 100644 (file)
@@ -264,6 +264,15 @@ namespace irr
        static EM_BOOL MouseLeaveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);\r
 \r
 #endif\r
+               // Check if a key is a known special character with no side effects on text boxes.\r
+               static bool keyIsKnownSpecial(EKEY_CODE key);\r
+\r
+               // Return the Char that should be sent to Irrlicht for the given key (either the one passed in or 0).\r
+               static int findCharToPassToIrrlicht(int assumedChar, EKEY_CODE key);\r
+\r
+               // Check if a text box is in focus. Enable or disable SDL_TEXTINPUT events only if in focus.\r
+               void resetReceiveTextInputEvents();\r
+\r
                //! create the driver\r
                void createDriver();\r
 \r