From 8e694e6ddeb42110a00233d7ff7345c57ee5d90d Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Sun, 6 Oct 2019 23:06:45 +0300 Subject: [PATCH] add text input for SDL 1.2 --- src/engine/e_input_stub.inc | 4 ++++ src/game/sdl/g_system.pas | 37 +++++++++++++++++++++++++++---------- src/shared/utils.pas | 6 ++++++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/engine/e_input_stub.inc b/src/engine/e_input_stub.inc index b2ca332..670390a 100644 --- a/src/engine/e_input_stub.inc +++ b/src/engine/e_input_stub.inc @@ -138,6 +138,8 @@ interface IK_Y = 106; IK_Z = 107; IK_MINUS = 108; + IK_RMETA = 109; + IK_LMETA = 110; // TODO: think of something better than this shit IK_LASTKEY = e_MaxKbdKeys-1; @@ -350,6 +352,8 @@ begin e_KeyNames[IK_DOT] := '.'; e_KeyNames[IK_MINUS] := '-'; e_KeyNames[IK_EQUALS] := '='; + e_KeyNames[IK_RMETA] := 'RMETA'; + e_KeyNames[IK_LMETA] := 'LMETA'; // joysticks for j := 0 to e_MaxJoys-1 do diff --git a/src/game/sdl/g_system.pas b/src/game/sdl/g_system.pas index a72f658..81b1ca3 100644 --- a/src/game/sdl/g_system.pas +++ b/src/game/sdl/g_system.pas @@ -18,9 +18,10 @@ unit g_system; interface (* To fix: - * - Text input * - Joystick support * - Window resizing using SDL_VIDEORESIZE + * -- Linux: GL drawing area have wrong size + * -- OSX: GL context are recreated *) uses Utils; @@ -48,7 +49,7 @@ implementation uses SysUtils, SDL, GL, e_log, e_graphics, e_input, - g_options, g_window, g_console, g_game, g_menu; + g_options, g_window, g_console, g_game, g_menu, g_gui, g_main; var screen: PSDL_Surface; @@ -236,6 +237,8 @@ implementation SDLK_PAUSE: x := IK_PAUSE; SDLK_A..SDLK_Z: x := IK_A + (key - SDLK_A); SDLK_MINUS: x := IK_MINUS; + SDLK_RMETA: x := IK_RMETA; + SDLK_LMETA: x := IK_LMETA; else x := IK_INVALID end; @@ -243,16 +246,28 @@ implementation end; procedure HandleKeyboard (var ev: TSDL_KeyboardEvent); - var down: Boolean; key, stb: Integer; + var down, repeated: Boolean; key: Integer; ch: Char; begin + key := Key2Stub(ev.keysym.sym); down := (ev.type_ = SDL_KEYDOWN); - key := ev.keysym.sym; - stb := Key2Stub(key); + repeated := down and e_KeyPressed(key); + ch := wchar2win(WideChar(ev.keysym.unicode)); if g_dbg_input then - e_LogWritefln('Input Debug: keysym, press=%s, scancode=%s SDL.ev.key.state=%s', [down, key, ev.state]); - e_KeyUpDown(stb, down); - g_Console_ProcessBind(stb, down); - (* !!! need text input -- console, cheaats, fields *) + e_LogWritefln('Input Debug: keysym, down=%s, sym=%s, state=%s, unicode=%s, stubsym=%s, cp1251=%s', [down, ev.keysym.sym, ev.state, ev.keysym.unicode, key, Ord(ch)]); + if not repeated then + begin + e_KeyUpDown(key, down); + g_Console_ProcessBind(key, down); + end + else if gConsoleShow or gChatShow or (g_ActiveWindow <> nil) then + begin + KeyPress(key) + end; + if down and (ev.keysym.unicode <= $44F) then + begin + if IsPrintable1251(ch) then + CharPress(ch) + end end; function sys_HandleInput (): Boolean; @@ -288,7 +303,9 @@ implementation raise Exception.Create('SDL: Init failed: ' + SDL_GetError()); ok := InitWindow(gScreenWidth, gScreenHeight, gBPP, gFullScreen); if not ok then - raise Exception.Create('SDL: failed to set videomode: ' + SDL_GetError) + raise Exception.Create('SDL: failed to set videomode: ' + SDL_GetError); + SDL_EnableUNICODE(1); + SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); end; procedure sys_Final; diff --git a/src/shared/utils.pas b/src/shared/utils.pas index 867f0dc..4c4db7b 100644 --- a/src/shared/utils.pas +++ b/src/shared/utils.pas @@ -87,6 +87,7 @@ function int64ToStrComma (i: Int64): AnsiString; function upcase1251 (ch: AnsiChar): AnsiChar; inline; function locase1251 (ch: AnsiChar): AnsiChar; inline; +function IsPrintable1251 (ch: AnsiChar): Boolean; function toLowerCase1251 (const s: AnsiString): AnsiString; @@ -961,6 +962,11 @@ begin result := ch; end; +function IsPrintable1251 (ch: AnsiChar): Boolean; +begin + result := (ch >= #32) and (ch <> #127) +end; + function strEquCI1251 (const s0, s1: AnsiString): Boolean; var -- 2.29.2