From fa538067722a9e250f5fd5acb86d085e324b89ed Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Mon, 13 Feb 2023 17:06:11 +0300 Subject: [PATCH] gl: implement touch screen controls --- src/game/Doom2DF.lpr | 7 +- src/game/renders/opengl/r_render.pas | 10 +- src/game/renders/opengl/r_touch.pas | 185 +++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 src/game/renders/opengl/r_touch.pas diff --git a/src/game/Doom2DF.lpr b/src/game/Doom2DF.lpr index f6d74ef..f0c3a90 100644 --- a/src/game/Doom2DF.lpr +++ b/src/game/Doom2DF.lpr @@ -205,8 +205,13 @@ uses r_fonts in 'renders/opengl/r_fonts.pas', r_common in 'renders/opengl/r_common.pas', r_console in 'renders/opengl/r_console.pas', - r_gui in 'renders/opengl/r_gui.pas', + {$IFDEF ENABLE_MENU} + r_gui in 'renders/opengl/r_gui.pas', + {$ENDIF} r_loadscreen in 'renders/opengl/r_loadscreen.pas', + {$IFDEF ENABLE_TOUCH} + r_touch in 'renders/opengl/r_touch.pas', + {$ENDIF} {$IFDEF ENABLE_HOLMES} r_fui_gfx_gl in 'renders/opengl/r_fui_gfx_gl.pas', r_holmes in 'renders/opengl/r_holmes.pas', diff --git a/src/game/renders/opengl/r_render.pas b/src/game/renders/opengl/r_render.pas index 202500d..80783b6 100644 --- a/src/game/renders/opengl/r_render.pas +++ b/src/game/renders/opengl/r_render.pas @@ -93,6 +93,9 @@ implementation {$IFDEF ENABLE_SYSTEM} g_system, {$ENDIF} + {$IFDEF ENABLE_TOUCH} + r_touch, + {$ENDIF} {$IFDEF ENABLE_HOLMES} r_holmes, {$ENDIF} @@ -1240,7 +1243,9 @@ implementation r_Holmes_DrawUI; {$ENDIF} - // TODO draw touch screen controls + {$IFDEF ENABLE_TOUCH} + r_Touch_Draw; + {$ENDIF} sys_Repaint; end; @@ -1289,8 +1294,7 @@ implementation {$IFDEF ENABLE_TOUCH} procedure r_Render_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean); begin - // TODO implement touchscreen - founded := False; + r_Touch_GetKeyRect(key, x, y, w, h, founded) end; {$ENDIF} diff --git a/src/game/renders/opengl/r_touch.pas b/src/game/renders/opengl/r_touch.pas new file mode 100644 index 0000000..98ddf3c --- /dev/null +++ b/src/game/renders/opengl/r_touch.pas @@ -0,0 +1,185 @@ +(* 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 r_touch; + +interface + + procedure r_Touch_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean); + procedure r_Touch_Draw; + +implementation + + uses + {$IFDEF USE_SDL2} + SDL2, + {$ENDIF} + SysUtils, + e_input, g_options, g_system, g_game, + r_common, r_draw + ; + + + 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 r_Touch_GetKeyRect (key: Integer; out x, y, w, h: Integer; out founded: Boolean); + var + sw, sh, sz: Integer; + dpi: Single; + + procedure S (xx, yy, ww, hh: Single); + begin + x := Trunc(xx); + y := Trunc(yy); + w := Trunc(ww); + h := Trunc(hh); + founded := true; + end; + + begin + founded := false; + {$IF DEFINED(USE_SDL2) AND NOT DEFINED(SDL2_NODPI)} + if SDL_GetDisplayDPI(0, @dpi, nil, nil) <> 0 then + dpi := 96; + {$ELSE} + dpi := 96; + {$ENDIF} + + sz := Trunc(g_touch_size * dpi); sw := gScreenWidth; sh := gScreenHeight; + x := 0; y := Round(sh * g_touch_offset / 100); + w := sz; h := sz; + + if sys_IsTextInputActive() then + case key of + VK_HIDEKBD: S(sw - (sz/2), 0, sz / 2, sz / 2); + end + else if g_touch_alt then + case key of + (* top ------- x ------------------------------- y w ----- h -- *) + VK_CONSOLE: S(0, 0, sz / 2, sz / 2); + VK_ESCAPE: S(sw - 1*(sz/2) - 1, 0, sz / 2, sz / 2); + VK_SHOWKBD: S(sw - 2*(sz/2) - 1, 0, sz / 2, sz / 2); + VK_CHAT: S(sw / 2 - (sz/2) / 2 - (sz/2) - 1, 0, sz / 2, sz / 2); + VK_STATUS: S(sw / 2 - (sz/2) / 2 - 1, 0, sz / 2, sz / 2); + VK_TEAM: S(sw / 2 - (sz/2) / 2 + (sz/2) - 1, 0, sz / 2, sz / 2); + (* left --- x - y -------------- w - h --- *) + VK_PREV: S(0, sh - 3.0*sz - 1, sz, sz / 2); + VK_LEFT: S(0, sh - 2.0*sz - 1, sz, sz * 2); + VK_RIGHT: S(sz, sh - 2.0*sz - 1, sz, sz * 2); + (* right - x ------------ y -------------- w - h -- *) + VK_NEXT: S(sw - 1*sz - 1, sh - 3.0*sz - 1, sz, sz / 2); + VK_UP: S(sw - 2*sz - 1, sh - 2.0*sz - 1, sz, sz / 2); + VK_FIRE: S(sw - 2*sz - 1, sh - 1.5*sz - 1, sz, sz); + VK_DOWN: S(sw - 2*sz - 1, sh - 0.5*sz - 1, sz, sz / 2); + VK_JUMP: S(sw - 1*sz - 1, sh - 2.0*sz - 1, sz, sz); + VK_OPEN: S(sw - 1*sz - 1, sh - 1.0*sz - 1, sz, sz); + end + else + case key of + (* left ----- x ----- y -------------- w ----- h -- *) + VK_ESCAPE: S(0.0*sz, y - 1*sz - sz/2, sz, sz / 2); + VK_LSTRAFE: S(0.0*sz, y - 0*sz - sz/2, sz / 2, sz); + VK_LEFT: S(0.5*sz, y - 0*sz - sz/2, sz, sz); + VK_RIGHT: S(1.5*sz, y - 0*sz - sz/2, sz, sz); + VK_RSTRAFE: S(2.5*sz, y - 0*sz - sz/2, sz / 2, sz); + (* right - x ------------ y --------------- w - h *) + VK_UP: S(sw - 1*sz - 1, y - 1*sz - sz/2, sz, sz); + VK_FIRE: S(sw - 1*sz - 1, y - 0*sz - sz/2, sz, sz); + VK_DOWN: S(sw - 1*sz - 1, y - -1*sz - sz/2, sz, sz); + VK_NEXT: S(sw - 2*sz - 1, y - 1*sz - sz/2, sz, sz); + VK_JUMP: S(sw - 2*sz - 1, y - 0*sz - sz/2, sz, sz); + VK_PREV: S(sw - 3*sz - 1, y - 1*sz - sz/2, sz, sz); + VK_OPEN: S(sw - 3*sz - 1, y - 0*sz - sz/2, sz, sz); + (* bottom ---- x -------------------------- y ---------------- w ----- h -- *) + VK_CHAT: S(sw/2 - sz/4 - 2*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2); + VK_CONSOLE: S(sw/2 - sz/4 - 1*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2); + VK_STATUS: S(sw/2 - sz/4 - 0*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2); + VK_TEAM: S(sw/2 - sz/4 - -1*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2); + VK_SHOWKBD: S(sw/2 - sz/4 - -2*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2); + VK_0: S(sw/2 - sz/4 - 5*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + VK_1: S(sw/2 - sz/4 - 4*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + VK_2: S(sw/2 - sz/4 - 3*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + VK_3: S(sw/2 - sz/4 - 2*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + VK_4: S(sw/2 - sz/4 - 1*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + VK_5: S(sw/2 - sz/4 - 0*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + VK_6: S(sw/2 - sz/4 - -1*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + VK_7: S(sw/2 - sz/4 - -2*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + VK_8: S(sw/2 - sz/4 - -3*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + VK_9: S(sw/2 - sz/4 - -4*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + VK_A: S(sw/2 - sz/4 - -5*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2); + end + end; + + procedure r_Touch_Draw; + var i, x, y, w, h: Integer; founded: Boolean; + begin + if g_touch_enabled then + begin + for i := VK_FIRSTKEY to VK_LASTKEY do + begin + r_Touch_GetKeyRect(i, x, y, w, h, founded); + if founded then + begin + r_Draw_Rect(x, y, x + w, y + h, 0, 255, 0, 225); + r_Draw_Text(GetKeyName(i), x, y, 255, 255, 255, 225, stdfont); + end + end + end + end; + +end. -- 2.29.2