DEADSOFTWARE

added optional framebuffer and resolution scaling
[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 {$IFNDEF SDL2_NODPI}
62 if SDL_GetDisplayDPI(0, @dpi, nil, nil) <> 0 then
63 dpi := 96;
64 {$ELSE}
65 dpi := 96;
66 {$ENDIF}
68 sz := Trunc(g_touch_size * dpi); sw := gWinSizeX; sh := gWinSizeY;
69 x := 0; y := Round(sh * g_touch_offset / 100);
70 w := sz; h := sz;
72 if SDL_IsTextInputActive() = SDL_True then
73 case key of
74 VK_HIDEKBD: S(sw - (sz/2), 0, sz / 2, sz / 2);
75 end
76 else if g_touch_alt then
77 case key of
78 (* top ------- x ------------------------------- y w ----- h -- *)
79 VK_CONSOLE: S(0, 0, sz / 2, sz / 2);
80 VK_ESCAPE: S(sw - 1*(sz/2) - 1, 0, sz / 2, sz / 2);
81 VK_SHOWKBD: S(sw - 2*(sz/2) - 1, 0, sz / 2, sz / 2);
82 VK_CHAT: S(sw / 2 - (sz/2) / 2 - (sz/2) - 1, 0, sz / 2, sz / 2);
83 VK_STATUS: S(sw / 2 - (sz/2) / 2 - 1, 0, sz / 2, sz / 2);
84 VK_TEAM: S(sw / 2 - (sz/2) / 2 + (sz/2) - 1, 0, sz / 2, sz / 2);
85 (* left --- x - y -------------- w - h --- *)
86 VK_PREV: S(0, sh - 3.0*sz - 1, sz, sz / 2);
87 VK_LEFT: S(0, sh - 2.0*sz - 1, sz, sz * 2);
88 VK_RIGHT: S(sz, sh - 2.0*sz - 1, sz, sz * 2);
89 (* right - x ------------ y -------------- w - h -- *)
90 VK_NEXT: S(sw - 1*sz - 1, sh - 3.0*sz - 1, sz, sz / 2);
91 VK_UP: S(sw - 2*sz - 1, sh - 2.0*sz - 1, sz, sz / 2);
92 VK_FIRE: S(sw - 2*sz - 1, sh - 1.5*sz - 1, sz, sz);
93 VK_DOWN: S(sw - 2*sz - 1, sh - 0.5*sz - 1, sz, sz / 2);
94 VK_JUMP: S(sw - 1*sz - 1, sh - 2.0*sz - 1, sz, sz);
95 VK_OPEN: S(sw - 1*sz - 1, sh - 1.0*sz - 1, sz, sz);
96 end
97 else
98 case key of
99 (* left ----- x ----- y -------------- w ----- h -- *)
100 VK_ESCAPE: S(0.0*sz, y - 1*sz - sz/2, sz, sz / 2);
101 VK_LSTRAFE: S(0.0*sz, y - 0*sz - sz/2, sz / 2, sz);
102 VK_LEFT: S(0.5*sz, y - 0*sz - sz/2, sz, sz);
103 VK_RIGHT: S(1.5*sz, y - 0*sz - sz/2, sz, sz);
104 VK_RSTRAFE: S(2.5*sz, y - 0*sz - sz/2, sz / 2, sz);
105 (* right - x ------------ y --------------- w - h *)
106 VK_UP: S(sw - 1*sz - 1, y - 1*sz - sz/2, sz, sz);
107 VK_FIRE: S(sw - 1*sz - 1, y - 0*sz - sz/2, sz, sz);
108 VK_DOWN: S(sw - 1*sz - 1, y - -1*sz - sz/2, sz, sz);
109 VK_NEXT: S(sw - 2*sz - 1, y - 1*sz - sz/2, sz, sz);
110 VK_JUMP: S(sw - 2*sz - 1, y - 0*sz - sz/2, sz, sz);
111 VK_PREV: S(sw - 3*sz - 1, y - 1*sz - sz/2, sz, sz);
112 VK_OPEN: S(sw - 3*sz - 1, y - 0*sz - sz/2, sz, sz);
113 (* bottom ---- x -------------------------- y ---------------- w ----- h -- *)
114 VK_CHAT: S(sw/2 - sz/4 - 2*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
115 VK_CONSOLE: S(sw/2 - sz/4 - 1*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
116 VK_STATUS: S(sw/2 - sz/4 - 0*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
117 VK_TEAM: S(sw/2 - sz/4 - -1*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
118 VK_SHOWKBD: S(sw/2 - sz/4 - -2*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
119 VK_0: S(sw/2 - sz/4 - 5*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
120 VK_1: S(sw/2 - sz/4 - 4*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
121 VK_2: S(sw/2 - sz/4 - 3*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
122 VK_3: S(sw/2 - sz/4 - 2*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
123 VK_4: S(sw/2 - sz/4 - 1*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
124 VK_5: S(sw/2 - sz/4 - 0*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
125 VK_6: S(sw/2 - sz/4 - -1*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
126 VK_7: S(sw/2 - sz/4 - -2*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
127 VK_8: S(sw/2 - sz/4 - -3*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
128 VK_9: S(sw/2 - sz/4 - -4*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
129 VK_A: S(sw/2 - sz/4 - -5*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
130 end
131 end;
133 function GetKeyName(key: Integer): String;
134 begin
135 case key of
136 VK_SHOWKBD: result := 'KBD';
137 VK_HIDEKBD: result := 'KBD';
138 VK_LEFT: result := 'LEFT';
139 VK_RIGHT: result := 'RIGHT';
140 VK_UP: result := 'UP';
141 VK_DOWN: result := 'DOWN';
142 VK_FIRE: result := 'FIRE';
143 VK_OPEN: result := 'OPEN';
144 VK_JUMP: result := 'JUMP';
145 VK_CHAT: result := 'CHAT';
146 VK_ESCAPE: result := 'ESC';
147 VK_0: result := '0';
148 VK_1: result := '1';
149 VK_2: result := '2';
150 VK_3: result := '3';
151 VK_4: result := '4';
152 VK_5: result := '5';
153 VK_6: result := '6';
154 VK_7: result := '7';
155 VK_8: result := '8';
156 VK_9: result := '9';
157 VK_A: result := '10';
158 VK_B: result := '11';
159 VK_C: result := '12';
160 VK_D: result := '13';
161 VK_E: result := '14';
162 VK_F: result := '15';
163 VK_CONSOLE: result := 'CON';
164 VK_STATUS: result := 'STAT';
165 VK_TEAM: result := 'TEAM';
166 VK_PREV: result := '<PREW';
167 VK_NEXT: result := 'NEXT>';
168 VK_LSTRAFE: result := '<';
169 VK_RSTRAFE: result := '>';
170 else
171 if (key > 0) and (key < e_MaxInputKeys) then
172 result := e_KeyNames[key]
173 else
174 result := '<' + IntToStr(key) + '>'
175 end
176 end;
178 function IntersectControl(ctl, xx, yy: Integer): Boolean;
179 var
180 x, y, w, h: Integer;
181 founded: Boolean;
182 begin
183 GetKeyRect(ctl, x, y, w, h, founded);
184 result := founded and (xx >= x) and (yy >= y) and (xx <= x + w) and (yy <= y + h);
185 end;
187 procedure g_Touch_Init;
188 begin
189 {$IFNDEF HEADLESS}
190 g_Touch_ShowKeyboard(FALSE);
191 g_touch_enabled := SDL_GetNumTouchDevices() > 0
192 {$ENDIF}
193 end;
195 procedure g_Touch_ShowKeyboard(yes: Boolean);
196 begin
197 {$IFNDEF HEADLESS}
198 if g_dbg_input then
199 e_LogWritefln('g_Touch_ShowKeyboard(%s)', [yes]);
200 (* on desktop we always receive text (needed for cheats) *)
201 if yes or (SDL_HasScreenKeyboardSupport() = SDL_FALSE) then
202 SDL_StartTextInput
203 else
204 SDL_StopTextInput
205 {$ENDIF}
206 end;
208 procedure g_Touch_HandleEvent(const ev: TSDL_TouchFingerEvent);
209 var
210 x, y, i, finger: Integer;
212 procedure KeyUp (finger, i: Integer);
213 begin
214 if g_dbg_input then
215 e_LogWritefln('Input Debug: g_touch.KeyUp, finger=%s, key=%s', [finger, i]);
217 keyFinger[i] := 0;
218 e_KeyUpDown(i, False);
219 g_Console_ProcessBind(i, False);
221 (* up/down + fire hack *)
222 if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) and angleFire then
223 begin
224 if (i = VK_UP) or (i = VK_DOWN) then
225 begin
226 angleFire := False;
227 keyFinger[VK_FIRE] := 0;
228 e_KeyUpDown(VK_FIRE, False);
229 g_Console_ProcessBind(VK_FIRE, False)
230 end
231 end
232 end;
234 procedure KeyDown (finger, i: Integer);
235 begin
236 if g_dbg_input then
237 e_LogWritefln('Input Debug: g_touch.KeyDown, finger=%s, key=%s', [finger, i]);
239 keyFinger[i] := finger;
240 e_KeyUpDown(i, True);
241 g_Console_ProcessBind(i, True);
243 (* up/down + fire hack *)
244 if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) then
245 begin
246 if i = VK_UP then
247 begin
248 angleFire := True;
249 keyFinger[VK_FIRE] := -1;
250 e_KeyUpDown(VK_FIRE, True);
251 g_Console_ProcessBind(VK_FIRE, True)
252 end
253 else if i = VK_DOWN then
254 begin
255 angleFire := True;
256 keyFinger[VK_FIRE] := -1;
257 e_KeyUpDown(VK_FIRE, True);
258 g_Console_ProcessBind(VK_FIRE, True)
259 end
260 end
261 end;
263 procedure KeyMotion (finger, i: Integer);
264 begin
265 if keyFinger[i] <> finger then
266 begin
267 KeyUp(finger, i);
268 KeyDown(finger, i)
269 end
270 end;
272 begin
273 if not g_touch_enabled then
274 Exit;
276 finger := ev.fingerId + 2;
277 x := Trunc(ev.x * gWinSizeX);
278 y := Trunc(ev.y * gWinSizeY);
280 for i := VK_FIRSTKEY to VK_LASTKEY do
281 begin
282 if IntersectControl(i, x, y) then
283 begin
284 if ev.type_ = SDL_FINGERUP then
285 KeyUp(finger, i)
286 else if ev.type_ = SDL_FINGERMOTION then
287 KeyMotion(finger, i)
288 else if ev.type_ = SDL_FINGERDOWN then
289 keyDown(finger, i)
290 end
291 else if keyFinger[i] = finger then
292 begin
293 if ev.type_ = SDL_FINGERUP then
294 KeyUp(finger, i)
295 else if ev.type_ = SDL_FINGERMOTION then
296 KeyUp(finger, i)
297 end
298 end
299 end;
301 procedure g_Touch_Draw;
302 var i, x, y, w, h: Integer; founded: Boolean;
303 begin
304 {$IFNDEF HEADLESS}
305 if not g_touch_enabled then
306 Exit;
308 for i := VK_FIRSTKEY to VK_LASTKEY do
309 begin
310 GetKeyRect(i, x, y, w, h, founded);
311 if founded then
312 begin
313 e_DrawQuad(x, y, x + w, y + h, 0, 255, 0, 31);
314 e_TextureFontPrintEx(x, y, GetKeyName(i), gStdFont, 255, 255, 255, 1, True)
315 end
316 end
317 {$ENDIF}
318 end;
320 initialization
321 conRegVar('touch_enable', @g_touch_enabled, 'enable/disable virtual buttons', 'draw buttons');
322 conRegVar('touch_fire', @g_touch_fire, 'enable/disable fire when press virtual up/down', 'fire when press up/down');
323 conRegVar('touch_size', @g_touch_size, 0.1, 10, 'size of virtual buttons', 'button size');
324 conRegVar('touch_offset', @g_touch_offset, 0, 100, '', '');
325 conRegVar('touch_alt', @g_touch_alt, 'althernative virtual buttons layout', 'althernative layout');
326 end.