From 5ff315211d9cab4ed4b38c339857a06611d9b0ed Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Thu, 27 Jan 2022 22:20:14 +0300 Subject: [PATCH] render: draw touch controls via render --- src/game/Doom2DF.lpr | 7 +- src/game/g_console.pas | 18 +- src/game/g_game.pas | 3 - src/game/g_gui.pas | 10 +- src/game/g_menu.pas | 2 +- src/game/g_options.pas | 16 +- src/game/opengl/r_game.pas | 6 +- src/game/opengl/r_render.pas | 18 ++ .../{sdl2/g_touch.pas => opengl/r_touch.pas} | 280 ++++-------------- src/game/sdl/g_system.pas | 17 ++ src/game/sdl/g_touch.pas | 53 ---- src/game/sdl2/g_system.pas | 161 +++++++++- src/game/stub/g_system.pas | 16 + src/game/stub/g_touch.pas | 53 ---- src/shared/a_modes.inc | 16 + 15 files changed, 324 insertions(+), 352 deletions(-) rename src/game/{sdl2/g_touch.pas => opengl/r_touch.pas} (55%) delete mode 100644 src/game/sdl/g_touch.pas delete mode 100644 src/game/stub/g_touch.pas diff --git a/src/game/Doom2DF.lpr b/src/game/Doom2DF.lpr index 1db0109..4131108 100644 --- a/src/game/Doom2DF.lpr +++ b/src/game/Doom2DF.lpr @@ -140,15 +140,12 @@ uses {$IFNDEF HEADLESS} {$IFDEF USE_SYSSTUB} g_system in 'stub/g_system.pas', - g_touch in 'stub/g_touch.pas', {$ENDIF} {$IFDEF USE_SDL} g_system in 'sdl/g_system.pas', - g_touch in 'sdl/g_touch.pas', {$ENDIF} {$IFDEF USE_SDL2} g_system in 'sdl2/g_system.pas', - g_touch in 'sdl2/g_touch.pas', {$ENDIF} {$ENDIF} @@ -170,6 +167,9 @@ uses r_textures in 'opengl/r_textures.pas', r_weapons in 'opengl/r_weapons.pas', r_window in 'opengl/r_window.pas', + {$IFDEF ENABLE_TOUCH} + r_touch in 'opengl/r_touch.pas', + {$ENDIF} {$IFDEF ENABLE_MENU} g_gui in 'g_gui.pas', g_menu in 'g_menu.pas', @@ -1001,7 +1001,6 @@ end; g_Language_Set(gLanguage); {$IFNDEF HEADLESS} r_Render_Initialize; - g_Touch_Init; {$ENDIF} DebugOptions; g_Net_InitLowLevel; diff --git a/src/game/g_console.pas b/src/game/g_console.pas index 13bf81f..c872e8e 100644 --- a/src/game/g_console.pas +++ b/src/game/g_console.pas @@ -105,8 +105,8 @@ uses {$IFDEF ENABLE_MENU} g_gui, g_menu, {$ENDIF} - {$IFNDEF HEADLESS} - g_touch, + {$IFDEF ENABLE_TOUCH} + g_system, {$ENDIF} g_textures, e_input, g_game, g_gfx, g_player, g_items, SysUtils, g_basic, g_options, Math, e_res, @@ -169,8 +169,8 @@ begin gChatShow := False; gConsoleShow := not gConsoleShow; InputReady := False; - {$IFNDEF HEADLESS} - g_Touch_ShowKeyboard(gConsoleShow or gChatShow); + {$IFDEF ENABLE_TOUCH} + sys_ShowKeyboard(gConsoleShow or gChatShow); {$ENDIF} end; @@ -183,8 +183,8 @@ begin InputReady := False; Line := ''; CPos := 1; - {$IFNDEF HEADLESS} - g_Touch_ShowKeyboard(gConsoleShow or gChatShow); + {$IFDEF ENABLE_TOUCH} + sys_ShowKeyboard(gConsoleShow or gChatShow); {$ENDIF} end; @@ -879,11 +879,11 @@ begin 'unbindall': for i := 0 to e_MaxInputKeys - 1 do g_Console_BindKey(i, ''); -{$IFNDEF HEADLESS} +{$IFDEF ENABLE_TOUCH} 'showkeyboard': - g_Touch_ShowKeyboard(True); + sys_ShowKeyboard(True); 'hidekeyboard': - g_Touch_ShowKeyboard(False); + sys_ShowKeyboard(False); {$ENDIF} 'togglemenu': begin diff --git a/src/game/g_game.pas b/src/game/g_game.pas index 2debca1..c15aeaa 100644 --- a/src/game/g_game.pas +++ b/src/game/g_game.pas @@ -21,9 +21,6 @@ interface {$IFDEF ENABLE_MENU} g_gui, {$ENDIF} - {$IFNDEF HEADLESS} - g_touch, - {$ENDIF} SysUtils, Classes, MAPDEF, g_base, g_basic, g_player, g_res_downloader, g_sound, utils, md5, mempool, xprofiler, diff --git a/src/game/g_gui.pas b/src/game/g_gui.pas index 5da0d5a..fe15869 100644 --- a/src/game/g_gui.pas +++ b/src/game/g_gui.pas @@ -19,7 +19,7 @@ interface uses {$IFDEF USE_MEMPOOL}mempool,{$ENDIF} - g_base, r_graphics, e_input, e_log, g_playermodel, g_basic, g_touch, MAPDEF, utils; + g_base, r_graphics, e_input, e_log, g_playermodel, g_basic, MAPDEF, utils; const MAINMENU_HEADER_COLOR: TRGB = (R:255; G:255; B:255); @@ -552,6 +552,9 @@ procedure g_GUI_LoadMenuPos(); implementation uses + {$IFDEF ENABLE_TOUCH} + g_system, + {$ENDIF} g_sound, SysUtils, e_res, r_textures, g_game, Math, StrUtils, g_player, g_options, g_console, r_playermodel, g_map, g_weapons, xdynrec, wadreader; @@ -2439,7 +2442,10 @@ begin end; g_GUIGrabInput := (@FOnEnterEvent = nil) and (FWindow.FActiveControl = Self); - g_Touch_ShowKeyboard(g_GUIGrabInput) + + {$IFDEF ENABLE_TOUCH} + sys_ShowKeyboard(g_GUIGrabInput) + {$ENDIF} end; procedure TGUIEdit.SetText(Text: string); diff --git a/src/game/g_menu.pas b/src/game/g_menu.pas index 80f2731..2824c92 100644 --- a/src/game/g_menu.pas +++ b/src/game/g_menu.pas @@ -44,7 +44,7 @@ uses e_log, SysUtils, CONFIG, g_playermodel, DateUtils, MAPDEF, Math, g_saveload, g_language, e_res, - g_net, g_netmsg, g_netmaster, g_items, e_input, g_touch, + g_net, g_netmsg, g_netmaster, g_items, e_input, utils, wadreader, g_system, r_render, r_game; diff --git a/src/game/g_options.pas b/src/game/g_options.pas index aba2764..dea1ca3 100644 --- a/src/game/g_options.pas +++ b/src/game/g_options.pas @@ -90,6 +90,13 @@ var g_dbg_aimline_on: Boolean = false; g_dbg_input: Boolean = False; + var (* touch *) + g_touch_enabled: Boolean = False; + g_touch_size: Single = 1.0; + g_touch_offset: Single = 50.0; + g_touch_fire: Boolean = True; + g_touch_alt: Boolean = False; + {--- Read-only dirs ---} GameWAD: string; DataDirs: SSArray; @@ -405,5 +412,12 @@ initialization conRegVar('g_screenshot_stats', @gScreenshotStats, '', ''); conRegVar('g_lastmap', @gsMap, '', ''); - conRegVar('d_input', @g_dbg_input, '', '') + conRegVar('d_input', @g_dbg_input, '', ''); + + (* touch *) + conRegVar('touch_enable', @g_touch_enabled, 'enable/disable virtual buttons', 'draw buttons'); + conRegVar('touch_fire', @g_touch_fire, 'enable/disable fire when press virtual up/down', 'fire when press up/down'); + conRegVar('touch_size', @g_touch_size, 0.1, 10, 'size of virtual buttons', 'button size'); + conRegVar('touch_offset', @g_touch_offset, 0, 100, '', ''); + conRegVar('touch_alt', @g_touch_alt, 'althernative virtual buttons layout', 'althernative layout'); end. diff --git a/src/game/opengl/r_game.pas b/src/game/opengl/r_game.pas index ed21eaa..734eb1d 100644 --- a/src/game/opengl/r_game.pas +++ b/src/game/opengl/r_game.pas @@ -46,7 +46,7 @@ implementation {$ENDIF} SysUtils, Classes, Math, g_base, g_basic, r_graphics, - g_system, g_touch, + g_system, MAPDEF, xprofiler, utils, wadreader, CONFIG, e_input, e_sound, g_language, g_console, g_triggers, g_player, g_options, g_monsters, g_map, g_panel, @@ -2180,10 +2180,6 @@ begin e_SetRendertarget(False); e_SetViewPort(0, 0, gWinSizeX, gWinSizeY); e_BlitFramebuffer(gWinSizeX, gWinSizeY); - - // draw the overlay stuff on top of it - - g_Touch_Draw; end; end. diff --git a/src/game/opengl/r_render.pas b/src/game/opengl/r_render.pas index a9c4639..2e356dd 100644 --- a/src/game/opengl/r_render.pas +++ b/src/game/opengl/r_render.pas @@ -39,12 +39,20 @@ interface function r_Render_GetGibRect (m, id: Integer): TRectWH; procedure r_Render_QueueEffect (AnimType, X, Y: Integer); +{$IFDEF ENABLE_TOUCH} + // touch screen button location and size + procedure r_Render_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean); +{$ENDIF} + procedure r_Render_DrawLoading (force: Boolean); // !!! remove it implementation uses {$INCLUDE ../../nogl/noGLuses.inc} + {$IFDEF ENABLE_TOUCH} + r_touch, + {$ENDIF} SysUtils, Classes, Math, e_log, g_system, utils, g_game, g_options, g_console, @@ -156,6 +164,9 @@ implementation procedure r_Render_Draw; begin r_Game_Draw; + {$IFDEF ENABLE_TOUCH} + r_Touch_Draw; + {$ENDIF} end; procedure r_Render_Resize (w, h: Integer); @@ -221,6 +232,13 @@ implementation r_GFX_OnceAnim(AnimType, X, Y) end; +{$IFDEF ENABLE_TOUCH} + procedure r_Render_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean); + begin + r_Touch_GetKeyRect (key, x, y, w, h, founded) + end; +{$ENDIF} + procedure r_Render_DrawLoading (force: Boolean); begin r_Window_DrawLoading(force) diff --git a/src/game/sdl2/g_touch.pas b/src/game/opengl/r_touch.pas similarity index 55% rename from src/game/sdl2/g_touch.pas rename to src/game/opengl/r_touch.pas index 6451760..812ce9e 100644 --- a/src/game/sdl2/g_touch.pas +++ b/src/game/opengl/r_touch.pas @@ -12,41 +12,71 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . *) -{$INCLUDE ../shared/a_modes.inc} -unit g_touch; +{$INCLUDE ../../shared/a_modes.inc} +unit r_touch; interface - uses - SDL2; - - var - g_touch_enabled: Boolean = False; - g_touch_size: Single = 1.0; - g_touch_offset: Single = 50.0; - g_touch_fire: Boolean = True; - g_touch_alt: Boolean = False; - - procedure g_Touch_Init; - procedure g_Touch_ShowKeyboard(yes: Boolean); - procedure g_Touch_HandleEvent(const ev: TSDL_TouchFingerEvent); - procedure g_Touch_Draw; + procedure r_Touch_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean); + procedure r_Touch_Draw; implementation uses - {$IFDEF ENABLE_MENU} - g_gui, + {$IFDEF USE_SDL2} + SDL2, {$ENDIF} SysUtils, - e_log, r_graphics, r_game, e_input, g_options, g_game, g_weapons, g_console + e_input, g_options, g_system, + r_game, r_graphics ; - var - angleFire: Boolean; - keyFinger: array [VK_FIRSTKEY..VK_LASTKEY] of Integer; + function GetKeyName (key: Integer): String; + begin + case key of + VK_SHOWKBD: result := 'KBD'; + VK_HIDEKBD: result := 'KBD'; + VK_LEFT: result := 'LEFT'; + VK_RIGHT: result := 'RIGHT'; + VK_UP: result := 'UP'; + VK_DOWN: result := 'DOWN'; + VK_FIRE: result := 'FIRE'; + VK_OPEN: result := 'OPEN'; + VK_JUMP: result := 'JUMP'; + VK_CHAT: result := 'CHAT'; + VK_ESCAPE: result := 'ESC'; + VK_0: result := '0'; + VK_1: result := '1'; + VK_2: result := '2'; + VK_3: result := '3'; + VK_4: result := '4'; + VK_5: result := '5'; + VK_6: result := '6'; + VK_7: result := '7'; + VK_8: result := '8'; + VK_9: result := '9'; + VK_A: result := '10'; + VK_B: result := '11'; + VK_C: result := '12'; + VK_D: result := '13'; + VK_E: result := '14'; + VK_F: result := '15'; + VK_CONSOLE: result := 'CON'; + VK_STATUS: result := 'STAT'; + VK_TEAM: result := 'TEAM'; + VK_PREV: result := ' 0) and (key < e_MaxInputKeys) then + result := e_KeyNames[key] + else + result := '<' + IntToStr(key) + '>' + end + end; - procedure GetKeyRect(key: Integer; out x, y, w, h: Integer; out founded: Boolean); + procedure r_Touch_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean); var sw, sh, sz: Integer; dpi: Single; @@ -62,7 +92,7 @@ implementation begin founded := false; - {$IFNDEF SDL2_NODPI} + {$IF DEFINED(USE_SDL2) AND NOT DEFINED(SDL2_NODPI)} if SDL_GetDisplayDPI(0, @dpi, nil, nil) <> 0 then dpi := 96; {$ELSE} @@ -73,7 +103,7 @@ implementation x := 0; y := Round(sh * g_touch_offset / 100); w := sz; h := sz; - if SDL_IsTextInputActive() = SDL_True then + if sys_IsTextInputActive() then case key of VK_HIDEKBD: S(sw - (sz/2), 0, sz / 2, sz / 2); end @@ -134,205 +164,21 @@ implementation end end; - function GetKeyName(key: Integer): String; - begin - case key of - VK_SHOWKBD: result := 'KBD'; - VK_HIDEKBD: result := 'KBD'; - VK_LEFT: result := 'LEFT'; - VK_RIGHT: result := 'RIGHT'; - VK_UP: result := 'UP'; - VK_DOWN: result := 'DOWN'; - VK_FIRE: result := 'FIRE'; - VK_OPEN: result := 'OPEN'; - VK_JUMP: result := 'JUMP'; - VK_CHAT: result := 'CHAT'; - VK_ESCAPE: result := 'ESC'; - VK_0: result := '0'; - VK_1: result := '1'; - VK_2: result := '2'; - VK_3: result := '3'; - VK_4: result := '4'; - VK_5: result := '5'; - VK_6: result := '6'; - VK_7: result := '7'; - VK_8: result := '8'; - VK_9: result := '9'; - VK_A: result := '10'; - VK_B: result := '11'; - VK_C: result := '12'; - VK_D: result := '13'; - VK_E: result := '14'; - VK_F: result := '15'; - VK_CONSOLE: result := 'CON'; - VK_STATUS: result := 'STAT'; - VK_TEAM: result := 'TEAM'; - VK_PREV: result := ' 0) and (key < e_MaxInputKeys) then - result := e_KeyNames[key] - else - result := '<' + IntToStr(key) + '>' - end - end; - - function IntersectControl(ctl, xx, yy: Integer): Boolean; - var - x, y, w, h: Integer; - founded: Boolean; - begin - GetKeyRect(ctl, x, y, w, h, founded); - result := founded and (xx >= x) and (yy >= y) and (xx <= x + w) and (yy <= y + h); - end; - - procedure g_Touch_Init; - begin -{$IFNDEF HEADLESS} - g_Touch_ShowKeyboard(FALSE); - g_touch_enabled := SDL_GetNumTouchDevices() > 0 -{$ENDIF} - end; - - procedure g_Touch_ShowKeyboard(yes: Boolean); + procedure r_Touch_Draw; + var i, x, y, w, h: Integer; founded: Boolean; begin -{$IFNDEF HEADLESS} - if g_dbg_input then - e_LogWritefln('g_Touch_ShowKeyboard(%s)', [yes]); - (* on desktop we always receive text (needed for cheats) *) - if yes or (SDL_HasScreenKeyboardSupport() = SDL_FALSE) then - SDL_StartTextInput - else - SDL_StopTextInput -{$ENDIF} - end; - - procedure g_Touch_HandleEvent(const ev: TSDL_TouchFingerEvent); - var - x, y, i, finger: Integer; - - procedure KeyUp (finger, i: Integer); - begin - if g_dbg_input then - e_LogWritefln('Input Debug: g_touch.KeyUp, finger=%s, key=%s', [finger, i]); - - keyFinger[i] := 0; - e_KeyUpDown(i, False); - g_Console_ProcessBind(i, False); - - (* up/down + fire hack *) -{$IFDEF ENABLE_MENU} - if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) and angleFire then -{$ELSE} - if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and angleFire then -{$ENDIF} - begin - if (i = VK_UP) or (i = VK_DOWN) then - begin - angleFire := False; - keyFinger[VK_FIRE] := 0; - e_KeyUpDown(VK_FIRE, False); - g_Console_ProcessBind(VK_FIRE, False) - end - end - end; - - procedure KeyDown (finger, i: Integer); + if g_touch_enabled then begin - if g_dbg_input then - e_LogWritefln('Input Debug: g_touch.KeyDown, finger=%s, key=%s', [finger, i]); - - keyFinger[i] := finger; - e_KeyUpDown(i, True); - g_Console_ProcessBind(i, True); - - (* up/down + fire hack *) -{$IFDEF ENABLE_MENU} - if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) then -{$ELSE} - if g_touch_fire and (gGameSettings.GameType <> GT_NONE) then -{$ENDIF} + for i := VK_FIRSTKEY to VK_LASTKEY do begin - if i = VK_UP then + r_Touch_GetKeyRect(i, x, y, w, h, founded); + if founded then begin - angleFire := True; - keyFinger[VK_FIRE] := -1; - e_KeyUpDown(VK_FIRE, True); - g_Console_ProcessBind(VK_FIRE, True) + e_DrawQuad(x, y, x + w, y + h, 0, 255, 0, 31); + e_TextureFontPrintEx(x, y, GetKeyName(i), gStdFont, 255, 255, 255, 1, True) end - else if i = VK_DOWN then - begin - angleFire := True; - keyFinger[VK_FIRE] := -1; - e_KeyUpDown(VK_FIRE, True); - g_Console_ProcessBind(VK_FIRE, True) - end - end - end; - - procedure KeyMotion (finger, i: Integer); - begin - if keyFinger[i] <> finger then - begin - KeyUp(finger, i); - KeyDown(finger, i) - end - end; - - begin - if not g_touch_enabled then - Exit; - - finger := ev.fingerId + 2; - x := Trunc(ev.x * gWinSizeX); - y := Trunc(ev.y * gWinSizeY); - - for i := VK_FIRSTKEY to VK_LASTKEY do - begin - if IntersectControl(i, x, y) then - begin - if ev.type_ = SDL_FINGERUP then - KeyUp(finger, i) - else if ev.type_ = SDL_FINGERMOTION then - KeyMotion(finger, i) - else if ev.type_ = SDL_FINGERDOWN then - keyDown(finger, i) - end - else if keyFinger[i] = finger then - begin - if ev.type_ = SDL_FINGERUP then - KeyUp(finger, i) - else if ev.type_ = SDL_FINGERMOTION then - KeyUp(finger, i) - end - end - end; - - procedure g_Touch_Draw; - var i, x, y, w, h: Integer; founded: Boolean; - begin -{$IFNDEF HEADLESS} - if not g_touch_enabled then - Exit; - - for i := VK_FIRSTKEY to VK_LASTKEY do - begin - GetKeyRect(i, x, y, w, h, founded); - if founded then - begin - e_DrawQuad(x, y, x + w, y + h, 0, 255, 0, 31); - e_TextureFontPrintEx(x, y, GetKeyName(i), gStdFont, 255, 255, 255, 1, True) end end -{$ENDIF} end; -initialization - conRegVar('touch_enable', @g_touch_enabled, 'enable/disable virtual buttons', 'draw buttons'); - conRegVar('touch_fire', @g_touch_fire, 'enable/disable fire when press virtual up/down', 'fire when press up/down'); - conRegVar('touch_size', @g_touch_size, 0.1, 10, 'size of virtual buttons', 'button size'); - conRegVar('touch_offset', @g_touch_offset, 0, 100, '', ''); - conRegVar('touch_alt', @g_touch_alt, 'althernative virtual buttons layout', 'althernative layout'); end. diff --git a/src/game/sdl/g_system.pas b/src/game/sdl/g_system.pas index 0067bbc..8fdb42c 100644 --- a/src/game/sdl/g_system.pas +++ b/src/game/sdl/g_system.pas @@ -29,6 +29,11 @@ interface function sys_HandleInput (): Boolean; procedure sys_RequestQuit; +{$IFDEF ENABLE_TOUCH} + function sys_IsTextInputActive (): Boolean; + procedure sys_ShowKeyboard (yes: Boolean); +{$ENDIF} + (* --- Init --- *) procedure sys_Init; procedure sys_Final; @@ -448,6 +453,18 @@ implementation end; {$ENDIF} +{$IFDEF ENABLE_TOUCH} + procedure sys_ShowKeyboard (yes: Boolean); + begin + // stub + end; + + function sys_IsTextInputActive (): Boolean; + begin + Result := false + end; +{$ENDIF} + function Key2Stub (key: Integer): Integer; var x: Integer; begin diff --git a/src/game/sdl/g_touch.pas b/src/game/sdl/g_touch.pas deleted file mode 100644 index d07de41..0000000 --- a/src/game/sdl/g_touch.pas +++ /dev/null @@ -1,53 +0,0 @@ -(* Copyright (C) Doom 2D: Forever Developers - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 3 of the License ONLY. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - *) -{$INCLUDE ../shared/a_modes.inc} -unit g_touch; - -interface - - var - g_touch_enabled: Boolean = False; - g_touch_size: Single = 1.0; - g_touch_offset: Single = 50.0; - g_touch_fire: Boolean = True; - g_touch_alt: Boolean = False; - - procedure g_Touch_Init; - procedure g_Touch_ShowKeyboard(yes: Boolean); - procedure g_Touch_Draw; - -implementation - - uses g_console; - - procedure g_Touch_Init; - begin - end; - - procedure g_Touch_ShowKeyboard(yes: Boolean); - begin - end; - - procedure g_Touch_Draw; - begin - end; - -initialization - conRegVar('touch_enable', @g_touch_enabled, 'enable/disable virtual buttons', 'draw buttons'); - conRegVar('touch_fire', @g_touch_fire, 'enable/disable fire when press virtual up/down', 'fire when press up/down'); - conRegVar('touch_size', @g_touch_size, 0.1, 10, 'size of virtual buttons', 'button size'); - conRegVar('touch_offset', @g_touch_offset, 0, 100, '', ''); - conRegVar('touch_alt', @g_touch_alt, 'althernative virtual buttons layout', 'althernative layout'); -end. diff --git a/src/game/sdl2/g_system.pas b/src/game/sdl2/g_system.pas index 89127ad..bc60586 100644 --- a/src/game/sdl2/g_system.pas +++ b/src/game/sdl2/g_system.pas @@ -29,6 +29,11 @@ interface function sys_HandleInput (): Boolean; procedure sys_RequestQuit; +{$IFDEF ENABLE_TOUCH} + function sys_IsTextInputActive (): Boolean; + procedure sys_ShowKeyboard (yes: Boolean); +{$ENDIF} + (* --- Init --- *) procedure sys_Init; procedure sys_Final; @@ -40,12 +45,18 @@ interface implementation uses - SysUtils, SDL2, Math, ctypes, - e_log, e_input, e_sound, {$IFDEF ENABLE_HOLMES} sdlcarcass, {$ENDIF} - g_touch, g_options, g_console, g_game, g_basic + {$IFNDEF HEADLESS} + r_render, + {$ENDIF} + {$IFDEF ENABLE_MENU} + g_gui, + {$ENDIF} + SysUtils, SDL2, Math, ctypes, + e_log, e_input, e_sound, + g_options, g_console, g_game, g_basic ; const @@ -60,6 +71,10 @@ implementation JoystickHatState: array [0..e_MaxJoys - 1, 0..e_MaxJoyHats - 1, HAT_LEFT..HAT_DOWN] of Boolean; JoystickZeroAxes: array [0..e_MaxJoys - 1, 0..e_MaxJoyAxes - 1] of Integer; + var (* touch *) + angleFire: Boolean; + keyFinger: array [VK_FIRSTKEY..VK_LASTKEY] of Integer; + (* --------- Graphics --------- *) function GetTitle (): AnsiString; @@ -365,6 +380,136 @@ implementation end end; + (* --------- Touch --------- *) + +{$IFDEF ENABLE_TOUCH} + function sys_IsTextInputActive (): Boolean; + begin + Result := SDL_IsTextInputActive() = SDL_True + end; + + procedure sys_ShowKeyboard (yes: Boolean); + begin + {$IFNDEF HEADLESS} + if g_dbg_input then + e_LogWritefln('g_Touch_ShowKeyboard(%s)', [yes]); + (* on desktop we always receive text (needed for cheats) *) + if yes or (SDL_HasScreenKeyboardSupport() = SDL_FALSE) then + SDL_StartTextInput + else + SDL_StopTextInput + {$ENDIF} + end; + + procedure HandleTouch (const ev: TSDL_TouchFingerEvent); + var + x, y, i, finger: Integer; + + function IntersectControl(ctl, xx, yy: Integer): Boolean; + var x, y, w, h: Integer; founded: Boolean; + begin + r_Render_GetKeyRect(ctl, x, y, w, h, founded); + result := founded and (xx >= x) and (yy >= y) and (xx <= x + w) and (yy <= y + h); + end; + + procedure KeyUp (finger, i: Integer); + begin + if g_dbg_input then + e_LogWritefln('Input Debug: g_touch.KeyUp, finger=%s, key=%s', [finger, i]); + + keyFinger[i] := 0; + e_KeyUpDown(i, False); + g_Console_ProcessBind(i, False); + + (* up/down + fire hack *) +{$IFDEF ENABLE_MENU} + if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) and angleFire then +{$ELSE} + if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and angleFire then +{$ENDIF} + begin + if (i = VK_UP) or (i = VK_DOWN) then + begin + angleFire := False; + keyFinger[VK_FIRE] := 0; + e_KeyUpDown(VK_FIRE, False); + g_Console_ProcessBind(VK_FIRE, False) + end + end + end; + + procedure KeyDown (finger, i: Integer); + begin + if g_dbg_input then + e_LogWritefln('Input Debug: g_touch.KeyDown, finger=%s, key=%s', [finger, i]); + + keyFinger[i] := finger; + e_KeyUpDown(i, True); + g_Console_ProcessBind(i, True); + + (* up/down + fire hack *) +{$IFDEF ENABLE_MENU} + if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) then +{$ELSE} + if g_touch_fire and (gGameSettings.GameType <> GT_NONE) then +{$ENDIF} + begin + if i = VK_UP then + begin + angleFire := True; + keyFinger[VK_FIRE] := -1; + e_KeyUpDown(VK_FIRE, True); + g_Console_ProcessBind(VK_FIRE, True) + end + else if i = VK_DOWN then + begin + angleFire := True; + keyFinger[VK_FIRE] := -1; + e_KeyUpDown(VK_FIRE, True); + g_Console_ProcessBind(VK_FIRE, True) + end + end + end; + + procedure KeyMotion (finger, i: Integer); + begin + if keyFinger[i] <> finger then + begin + KeyUp(finger, i); + KeyDown(finger, i) + end + end; + + begin + if not g_touch_enabled then + Exit; + + finger := ev.fingerId + 2; + x := Trunc(ev.x * gWinSizeX); + y := Trunc(ev.y * gWinSizeY); + + for i := VK_FIRSTKEY to VK_LASTKEY do + begin + if IntersectControl(i, x, y) then + begin + if ev.type_ = SDL_FINGERUP then + KeyUp(finger, i) + else if ev.type_ = SDL_FINGERMOTION then + KeyMotion(finger, i) + else if ev.type_ = SDL_FINGERDOWN then + keyDown(finger, i) + end + else if keyFinger[i] = finger then + begin + if ev.type_ = SDL_FINGERUP then + KeyUp(finger, i) + else if ev.type_ = SDL_FINGERMOTION then + KeyUp(finger, i) + end + end + end; +{$ENDIF} // TOUCH + (* --------- Input --------- *) function HandleWindow (var ev: TSDL_WindowEvent): Boolean; @@ -471,7 +616,9 @@ implementation SDL_JOYDEVICEADDED: HandleJoyAdd(ev.jdevice); SDL_JOYDEVICEREMOVED: HandleJoyRemove(ev.jdevice); SDL_TEXTINPUT: HandleTextInput(ev.text); - SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP: g_Touch_HandleEvent(ev.tfinger); + {$IFDEF ENABLE_TOUCH} + SDL_FINGERMOTION, SDL_FINGERDOWN, SDL_FINGERUP: HandleTouch(ev.tfinger); + {$ENDIF} {$IFDEF ENABLE_HOLMES} SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_MOUSEMOTION: fuiOnSDLEvent(ev); {$ENDIF} @@ -509,6 +656,12 @@ implementation e_LogWritefln('SDL: Init subsystem failed: %s', [SDL_GetError()]); {$ENDIF} SDL_ShowCursor(SDL_DISABLE); + {$IFDEF ENABLE_TOUCH} + sys_ShowKeyboard(FALSE); + g_touch_enabled := SDL_GetNumTouchDevices() > 0; + {$ELSE} + g_touch_enabled := False; + {$ENDIF} end; procedure sys_Final; diff --git a/src/game/stub/g_system.pas b/src/game/stub/g_system.pas index a1e3473..6202cb4 100644 --- a/src/game/stub/g_system.pas +++ b/src/game/stub/g_system.pas @@ -29,6 +29,11 @@ interface function sys_HandleInput (): Boolean; procedure sys_RequestQuit; +{$IFDEF ENABLE_TOUCH} + function sys_IsTextInputActive (): Boolean; + procedure sys_ShowKeyboard (yes: Boolean); +{$ENDIF} + (* --- Init --- *) procedure sys_Init; procedure sys_Final; @@ -63,6 +68,17 @@ implementation (* --------- Input --------- *) +{$IFDEF ENABLE_TOUCH} + function sys_IsTextInputActive (): Boolean; + begin + Result := false + end; + + procedure sys_ShowKeyboard (yes: Boolean); + begin + end; +{$ENDIF} + function sys_HandleInput (): Boolean; begin result := false diff --git a/src/game/stub/g_touch.pas b/src/game/stub/g_touch.pas deleted file mode 100644 index d07de41..0000000 --- a/src/game/stub/g_touch.pas +++ /dev/null @@ -1,53 +0,0 @@ -(* Copyright (C) Doom 2D: Forever Developers - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 3 of the License ONLY. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - *) -{$INCLUDE ../shared/a_modes.inc} -unit g_touch; - -interface - - var - g_touch_enabled: Boolean = False; - g_touch_size: Single = 1.0; - g_touch_offset: Single = 50.0; - g_touch_fire: Boolean = True; - g_touch_alt: Boolean = False; - - procedure g_Touch_Init; - procedure g_Touch_ShowKeyboard(yes: Boolean); - procedure g_Touch_Draw; - -implementation - - uses g_console; - - procedure g_Touch_Init; - begin - end; - - procedure g_Touch_ShowKeyboard(yes: Boolean); - begin - end; - - procedure g_Touch_Draw; - begin - end; - -initialization - conRegVar('touch_enable', @g_touch_enabled, 'enable/disable virtual buttons', 'draw buttons'); - conRegVar('touch_fire', @g_touch_fire, 'enable/disable fire when press virtual up/down', 'fire when press up/down'); - conRegVar('touch_size', @g_touch_size, 0.1, 10, 'size of virtual buttons', 'button size'); - conRegVar('touch_offset', @g_touch_offset, 0, 100, '', ''); - conRegVar('touch_alt', @g_touch_alt, 'althernative virtual buttons layout', 'althernative layout'); -end. diff --git a/src/shared/a_modes.inc b/src/shared/a_modes.inc index 43b773b..909a813 100644 --- a/src/shared/a_modes.inc +++ b/src/shared/a_modes.inc @@ -110,6 +110,11 @@ {$UNDEF ENABLE_MENU} {$DEFINE DISABLE_MENU} {$ENDIF} + {$IFDEF ENABLE_TOUCH} + {$WARNING Touch screen in headless mode has no sense. Disabled.} + {$UNDEF ENABLE_TOUCH} + {$DEFINE DISABLE_TOUCH} + {$ENDIF} {$ENDIF} {$IF DEFINED(ENABLE_MENU) AND DEFINED(DISABLE_MENU)} @@ -123,6 +128,17 @@ {$ENDIF} {$ENDIF} +{$IF DEFINED(ENABLE_TOUCH) AND DEFINED(DISABLE_TOUCH)} + {$ERROR Select ENABLE_TOUCH or DISABLE_TOUCH} +{$ELSEIF NOT DEFINED(ENABLE_TOUCH) AND NOT DEFINED(DISABLE_TOUCH)} + // defaut ENABLE/DISABLE touch + {$IFDEF HEADLESS} + {$DEFINE DISABLE_TOUCH} + {$ELSE} + {$DEFINE ENABLE_TOUCH} + {$ENDIF} +{$ENDIF} + {$IF DEFINED(USE_SYSSTUB)} {$IF DEFINED(USE_SDL) OR DEFINED(USE_SDL2)} {$ERROR Only one system driver must be selected!} -- 2.29.2