DEADSOFTWARE

697187971a198bea8a001a8badf3f9703206fd64
[d2df-sdl.git] / src / game / sdl2 / g_touch.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_touch;
18 interface
20 uses
21 SDL2;
23 var
24 g_touch_enabled: Boolean = False;
25 g_touch_size: Single = 1.0;
26 g_touch_offset: Single = 50.0;
27 g_touch_fire: Boolean = True;
28 g_touch_alt: Boolean = False;
30 procedure g_Touch_Init;
31 procedure g_Touch_ShowKeyboard(yes: Boolean);
32 procedure g_Touch_HandleEvent(const ev: TSDL_TouchFingerEvent);
33 procedure g_Touch_Draw;
35 implementation
37 uses
38 SysUtils,
39 e_log, e_graphics, e_input, g_options, g_game, g_main, g_gui, g_weapons, g_console, g_window;
41 var
42 angleFire: Boolean;
43 keyFinger: array [VK_FIRSTKEY..VK_LASTKEY] of Integer;
45 procedure GetKeyRect(key: Integer; out x, y, w, h: Integer; out founded: Boolean);
46 var
47 sw, sh, sz: Integer;
48 dpi: Single;
50 procedure S (xx, yy, ww, hh: Single);
51 begin
52 x := Trunc(xx);
53 y := Trunc(yy);
54 w := Trunc(ww);
55 h := Trunc(hh);
56 founded := true;
57 end;
59 begin
60 founded := false;
61 if SDL_GetDisplayDPI(0, @dpi, nil, nil) <> 0 then
62 dpi := 96;
64 sz := Trunc(g_touch_size * dpi); sw := gScreenWidth; sh := gScreenHeight;
65 x := 0; y := Round(sh * g_touch_offset / 100);
66 w := sz; h := sz;
68 if SDL_IsTextInputActive() = SDL_True then
69 case key of
70 VK_HIDEKBD: S(sw - (sz/2), 0, sz / 2, sz / 2);
71 end
72 else if g_touch_alt then
73 case key of
74 (* top ------- x ------------------------------- y w ----- h -- *)
75 VK_CONSOLE: S(0, 0, sz / 2, sz / 2);
76 VK_ESCAPE: S(sw - 1*(sz/2) - 1, 0, sz / 2, sz / 2);
77 VK_SHOWKBD: S(sw - 2*(sz/2) - 1, 0, sz / 2, sz / 2);
78 VK_CHAT: S(sw / 2 - (sz/2) / 2 - (sz/2) - 1, 0, sz / 2, sz / 2);
79 VK_STATUS: S(sw / 2 - (sz/2) / 2 - 1, 0, sz / 2, sz / 2);
80 VK_TEAM: S(sw / 2 - (sz/2) / 2 + (sz/2) - 1, 0, sz / 2, sz / 2);
81 (* left --- x - y -------------- w - h --- *)
82 VK_PREV: S(0, sh - 3.0*sz - 1, sz, sz / 2);
83 VK_LEFT: S(0, sh - 2.0*sz - 1, sz, sz * 2);
84 VK_RIGHT: S(sz, sh - 2.0*sz - 1, sz, sz * 2);
85 (* right - x ------------ y -------------- w - h -- *)
86 VK_NEXT: S(sw - 1*sz - 1, sh - 3.0*sz - 1, sz, sz / 2);
87 VK_UP: S(sw - 2*sz - 1, sh - 2.0*sz - 1, sz, sz / 2);
88 VK_FIRE: S(sw - 2*sz - 1, sh - 1.5*sz - 1, sz, sz);
89 VK_DOWN: S(sw - 2*sz - 1, sh - 0.5*sz - 1, sz, sz / 2);
90 VK_JUMP: S(sw - 1*sz - 1, sh - 2.0*sz - 1, sz, sz);
91 VK_OPEN: S(sw - 1*sz - 1, sh - 1.0*sz - 1, sz, sz);
92 end
93 else
94 case key of
95 (* left ----- x ----- y -------------- w ----- h -- *)
96 VK_ESCAPE: S(0.0*sz, y - 1*sz - sz/2, sz, sz / 2);
97 VK_LSTRAFE: S(0.0*sz, y - 0*sz - sz/2, sz / 2, sz);
98 VK_LEFT: S(0.5*sz, y - 0*sz - sz/2, sz, sz);
99 VK_RIGHT: S(1.5*sz, y - 0*sz - sz/2, sz, sz);
100 VK_RSTRAFE: S(2.5*sz, y - 0*sz - sz/2, sz / 2, sz);
101 (* right - x ------------ y --------------- w - h *)
102 VK_UP: S(sw - 1*sz - 1, y - 1*sz - sz/2, sz, sz);
103 VK_FIRE: S(sw - 1*sz - 1, y - 0*sz - sz/2, sz, sz);
104 VK_DOWN: S(sw - 1*sz - 1, y - -1*sz - sz/2, sz, sz);
105 VK_NEXT: S(sw - 2*sz - 1, y - 1*sz - sz/2, sz, sz);
106 VK_JUMP: S(sw - 2*sz - 1, y - 0*sz - sz/2, sz, sz);
107 VK_PREV: S(sw - 3*sz - 1, y - 1*sz - sz/2, sz, sz);
108 VK_OPEN: S(sw - 3*sz - 1, y - 0*sz - sz/2, sz, sz);
109 (* bottom ---- x -------------------------- y ---------------- w ----- h -- *)
110 VK_CHAT: S(sw/2 - sz/4 - 2*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
111 VK_CONSOLE: S(sw/2 - sz/4 - 1*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
112 VK_STATUS: S(sw/2 - sz/4 - 0*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
113 VK_TEAM: S(sw/2 - sz/4 - -1*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
114 VK_SHOWKBD: S(sw/2 - sz/4 - -2*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
115 VK_0: S(sw/2 - sz/4 - 5*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
116 VK_1: S(sw/2 - sz/4 - 4*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
117 VK_2: S(sw/2 - sz/4 - 3*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
118 VK_3: S(sw/2 - sz/4 - 2*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
119 VK_4: S(sw/2 - sz/4 - 1*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
120 VK_5: S(sw/2 - sz/4 - 0*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
121 VK_6: S(sw/2 - sz/4 - -1*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
122 VK_7: S(sw/2 - sz/4 - -2*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
123 VK_8: S(sw/2 - sz/4 - -3*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
124 VK_9: S(sw/2 - sz/4 - -4*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
125 VK_A: S(sw/2 - sz/4 - -5*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
126 end
127 end;
129 function GetKeyName(key: Integer): String;
130 begin
131 case key of
132 VK_SHOWKBD: result := 'KBD';
133 VK_HIDEKBD: result := 'KBD';
134 VK_LEFT: result := 'LEFT';
135 VK_RIGHT: result := 'RIGHT';
136 VK_UP: result := 'UP';
137 VK_DOWN: result := 'DOWN';
138 VK_FIRE: result := 'FIRE';
139 VK_OPEN: result := 'OPEN';
140 VK_JUMP: result := 'JUMP';
141 VK_CHAT: result := 'CHAT';
142 VK_ESCAPE: result := 'ESC';
143 VK_0: result := '0';
144 VK_1: result := '1';
145 VK_2: result := '2';
146 VK_3: result := '3';
147 VK_4: result := '4';
148 VK_5: result := '5';
149 VK_6: result := '6';
150 VK_7: result := '7';
151 VK_8: result := '8';
152 VK_9: result := '9';
153 VK_A: result := '10';
154 VK_B: result := '11';
155 VK_C: result := '12';
156 VK_D: result := '13';
157 VK_E: result := '14';
158 VK_F: result := '15';
159 VK_CONSOLE: result := 'CON';
160 VK_STATUS: result := 'STAT';
161 VK_TEAM: result := 'TEAM';
162 VK_PREV: result := '<PREW';
163 VK_NEXT: result := 'NEXT>';
164 VK_LSTRAFE: result := '<';
165 VK_RSTRAFE: result := '>';
166 else
167 if (key > 0) and (key < e_MaxInputKeys) then
168 result := e_KeyNames[key]
169 else
170 result := '<' + IntToStr(key) + '>'
171 end
172 end;
174 function IntersectControl(ctl, xx, yy: Integer): Boolean;
175 var
176 x, y, w, h: Integer;
177 founded: Boolean;
178 begin
179 GetKeyRect(ctl, x, y, w, h, founded);
180 result := founded and (xx >= x) and (yy >= y) and (xx <= x + w) and (yy <= y + h);
181 end;
183 procedure g_Touch_Init;
184 begin
185 {$IFNDEF HEADLESS}
186 g_Touch_ShowKeyboard(FALSE);
187 g_touch_enabled := SDL_GetNumTouchDevices() > 0
188 {$ENDIF}
189 end;
191 procedure g_Touch_ShowKeyboard(yes: Boolean);
192 begin
193 {$IFNDEF HEADLESS}
194 if g_dbg_input then
195 e_LogWritefln('g_Touch_ShowKeyboard(%s)', [yes]);
196 (* on desktop we always receive text (needed for cheats) *)
197 if yes or (SDL_HasScreenKeyboardSupport() = SDL_FALSE) then
198 SDL_StartTextInput
199 else
200 SDL_StopTextInput
201 {$ENDIF}
202 end;
204 procedure g_Touch_HandleEvent(const ev: TSDL_TouchFingerEvent);
205 var
206 x, y, i, finger: Integer;
208 procedure KeyUp (finger, i: Integer);
209 begin
210 if g_dbg_input then
211 e_LogWritefln('Input Debug: g_touch.KeyUp, finger=%s, key=%s', [finger, i]);
213 keyFinger[i] := 0;
214 e_KeyUpDown(i, False);
215 g_Console_ProcessBind(i, False);
217 (* up/down + fire hack *)
218 if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) and angleFire then
219 begin
220 if (i = VK_UP) or (i = VK_DOWN) then
221 begin
222 angleFire := False;
223 keyFinger[VK_FIRE] := 0;
224 e_KeyUpDown(VK_FIRE, False);
225 g_Console_ProcessBind(VK_FIRE, False)
226 end
227 end
228 end;
230 procedure KeyDown (finger, i: Integer);
231 begin
232 if g_dbg_input then
233 e_LogWritefln('Input Debug: g_touch.KeyDown, finger=%s, key=%s', [finger, i]);
235 keyFinger[i] := finger;
236 e_KeyUpDown(i, True);
237 g_Console_ProcessBind(i, True);
239 (* up/down + fire hack *)
240 if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) then
241 begin
242 if i = VK_UP then
243 begin
244 angleFire := True;
245 keyFinger[VK_FIRE] := -1;
246 e_KeyUpDown(VK_FIRE, True);
247 g_Console_ProcessBind(VK_FIRE, True)
248 end
249 else if i = VK_DOWN then
250 begin
251 angleFire := True;
252 keyFinger[VK_FIRE] := -1;
253 e_KeyUpDown(VK_FIRE, True);
254 g_Console_ProcessBind(VK_FIRE, True)
255 end
256 end
257 end;
259 procedure KeyMotion (finger, i: Integer);
260 begin
261 if keyFinger[i] <> finger then
262 begin
263 KeyUp(finger, i);
264 KeyDown(finger, i)
265 end
266 end;
268 begin
269 if not g_touch_enabled then
270 Exit;
272 finger := ev.fingerId + 2;
273 x := Trunc(ev.x * gScreenWidth);
274 y := Trunc(ev.y * gScreenHeight);
276 for i := VK_FIRSTKEY to VK_LASTKEY do
277 begin
278 if IntersectControl(i, x, y) then
279 begin
280 if ev.type_ = SDL_FINGERUP then
281 KeyUp(finger, i)
282 else if ev.type_ = SDL_FINGERMOTION then
283 KeyMotion(finger, i)
284 else if ev.type_ = SDL_FINGERDOWN then
285 keyDown(finger, i)
286 end
287 else if keyFinger[i] = finger then
288 begin
289 if ev.type_ = SDL_FINGERUP then
290 KeyUp(finger, i)
291 else if ev.type_ = SDL_FINGERMOTION then
292 KeyUp(finger, i)
293 end
294 end
295 end;
297 procedure g_Touch_Draw;
298 var i, x, y, w, h: Integer; founded: Boolean;
299 begin
300 {$IFNDEF HEADLESS}
301 if not g_touch_enabled then
302 Exit;
304 for i := VK_FIRSTKEY to VK_LASTKEY do
305 begin
306 GetKeyRect(i, x, y, w, h, founded);
307 if founded then
308 begin
309 e_DrawQuad(x, y, x + w, y + h, 0, 255, 0, 31);
310 e_TextureFontPrintEx(x, y, GetKeyName(i), gStdFont, 255, 255, 255, 1, True)
311 end
312 end
313 {$ENDIF}
314 end;
316 initialization
317 conRegVar('touch_enable', @g_touch_enabled, 'enable/disable virtual buttons', 'draw buttons');
318 conRegVar('touch_fire', @g_touch_fire, 'enable/disable fire when press virtual up/down', 'fire when press up/down');
319 conRegVar('touch_size', @g_touch_size, 0.1, 10, 'size of virtual buttons', 'button size');
320 conRegVar('touch_offset', @g_touch_offset, 0, 100, '', '');
321 conRegVar('touch_alt', @g_touch_alt, 'althernative virtual buttons layout', 'althernative layout');
322 end.