DEADSOFTWARE

Added vertical offset for virtual keyboard
[d2df-sdl.git] / src / game / 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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_touch;
19 interface
21 uses
22 SDL2;
24 var
25 g_touch_size: Single = 1.0;
26 g_touch_offset: Single = 50;
27 g_touch_fire: Boolean = True;
29 procedure g_Touch_Init;
30 procedure g_Touch_ShowKeyboard(yes: Boolean);
31 procedure g_Touch_HandleEvent(const ev: TSDL_TouchFingerEvent);
32 procedure g_Touch_Draw;
34 implementation
36 uses
37 SysUtils,
38 e_log, e_graphics, e_input, g_options, g_game, g_main, g_weapons, g_console;
40 var
41 jab: Boolean;
42 enabled: Boolean;
43 angleFire: Boolean;
44 keyFinger: array [VK_FIRSTKEY..VK_LASTKEY] of Integer;
46 procedure GetKeyRect(key: Word; out x, y, w, h: Integer; out founded: Boolean);
47 var
48 sw, sh, sz: Integer;
49 dpi: Single;
50 begin
51 if SDL_GetDisplayDPI(0, @dpi, nil, nil) <> 0 then
52 dpi := 96;
54 founded := true;
55 sz := Trunc(g_touch_size * dpi);
56 sw := gScreenWidth; sh := gScreenHeight;
57 if jab then
58 begin
59 w := sz div 2; h := sz div 3;
60 case key of
61 VK_CONSOLE: begin x := 0; y := 0 end;
62 VK_ESCAPE: begin x := sw - w - 1; y := 0 end;
63 VK_CHAT: begin x := sw div 2 - w - 4; y := 0 end;
64 VK_TEAM: begin x := sw div 2 + 0 + 4; y := 0 end;
65 else
66 w := sz; h := sz * 2;
67 case key of
68 VK_LEFT: begin x := 0; y := sh - h - 1 end;
69 VK_RIGHT: begin x := w; y := sh - h - 1 end;
70 else
71 w := sz; h := sz;
72 case key of
73 VK_OPEN: begin h := sz; x := sw - 1*w - 1; y := sh - 1*h - 1 end;
74 VK_JUMP: begin h := sz; x := sw - 1*w - 1; y := sh - 2*h - 1 end;
75 VK_UP: begin h := sz div 2; x := sw - 2*w - 1; y := sh - 2*sz - 1 end;
76 VK_FIRE: begin h := sz; x := sw - 2*w - 1; y := sh - sz div 2 - sz - 1 end;
77 VK_DOWN: begin h := sz div 2; x := sw - 2*w - 1; y := sh - sz div 2 - 1 end;
78 VK_PREV: begin h := sz div 2; x := 0; y := sh - 3*sz - 1 end;
79 VK_NEXT: begin h := sz div 2; x := sw - w - 1; y := sh - 3*sz - 1 end;
80 else
81 founded := false
82 end
83 end
84 end
85 end
86 else
87 begin
88 x := 0; y := Round(sh * g_touch_offset / 100); w := sz; h := sz;
89 case key of
90 VK_LSTRAFE: begin x := 0; y := y - h div 2; w := w div 2 end;
91 VK_LEFT: begin x := w div 2; y := y - h div 2 end;
92 VK_RIGHT: begin x := w div 2 + 1*w; y := y - h div 2 end;
93 VK_RSTRAFE: begin x := w div 2 + 2*w; y := y - h div 2; w := w div 2 end;
94 VK_UP: begin x := sw - w - 1; y := y - h div 2 - h end;
95 VK_FIRE: begin x := sw - 1*w - 1; y := y - h div 2 end;
96 VK_DOWN: begin x := sw - w - 1; y := y - h div 2 + h end;
97 VK_NEXT: begin x := sw - 2*w - 1; y := y - h div 2 - h end;
98 VK_JUMP: begin x := sw - 2*w - 1; y := y - h div 2 end;
99 VK_PREV: begin x := sw - 3*w - 1; y := y - h div 2 - h end;
100 VK_OPEN: begin x := sw - 3*w - 1; y := y - h div 2 end;
101 else
102 x := 0; y := 0; w := sz div 2; h := sz div 2;
103 case key of
104 VK_0: begin x := sw div 2 - w div 2 - 5*w - 1; y := sh - 1*h - 1 end;
105 VK_1: begin x := sw div 2 - w div 2 - 4*w - 1; y := sh - 1*h - 1 end;
106 VK_2: begin x := sw div 2 - w div 2 - 3*w - 1; y := sh - 1*h - 1 end;
107 VK_3: begin x := sw div 2 - w div 2 - 2*w - 1; y := sh - 1*h - 1 end;
108 VK_4: begin x := sw div 2 - w div 2 - 1*w - 1; y := sh - 1*h - 1 end;
109 VK_5: begin x := sw div 2 - w div 2 + 0*w - 1; y := sh - 1*h - 1 end;
110 VK_6: begin x := sw div 2 - w div 2 + 1*w - 1; y := sh - 1*h - 1 end;
111 VK_7: begin x := sw div 2 - w div 2 + 2*w - 1; y := sh - 1*h - 1 end;
112 VK_8: begin x := sw div 2 - w div 2 + 3*w - 1; y := sh - 1*h - 1 end;
113 VK_9: begin x := sw div 2 - w div 2 + 4*w - 1; y := sh - 1*h - 1 end;
114 VK_A: begin x := sw div 2 - w div 2 + 5*w - 1; y := sh - 1*h - 1 end;
115 VK_CHAT: begin x := sw div 2 - w div 2 - 2*w - 1; y := sh - 2*h - 1 end;
116 VK_ESCAPE: begin x := sw div 2 - w div 2 - 1*w - 1; y := sh - 2*h - 1 end;
117 VK_CONSOLE: begin x := sw div 2 - w div 2 + 0*w - 1; y := sh - 2*h - 1 end;
118 VK_STATUS: begin x := sw div 2 - w div 2 + 1*w - 1; y := sh - 2*h - 1 end;
119 VK_TEAM: begin x := sw div 2 - w div 2 + 2*w - 1; y := sh - 2*h - 1 end;
120 else
121 founded := false
122 end
123 end
124 end
125 end;
127 function GetKeyName(key: Word): String;
128 begin
129 case key of
130 VK_LEFT: result := 'LEFT';
131 VK_RIGHT: result := 'RIGHT';
132 VK_UP: result := 'UP';
133 VK_DOWN: result := 'DOWN';
134 VK_FIRE: result := 'FIRE';
135 VK_OPEN: result := 'OPEN';
136 VK_JUMP: result := 'JUMP';
137 VK_CHAT: result := 'CHAT';
138 VK_ESCAPE: result := 'ESC';
139 VK_0: result := '0';
140 VK_1: result := '1';
141 VK_2: result := '2';
142 VK_3: result := '3';
143 VK_4: result := '4';
144 VK_5: result := '5';
145 VK_6: result := '6';
146 VK_7: result := '7';
147 VK_8: result := '8';
148 VK_9: result := '9';
149 VK_A: result := '10';
150 VK_B: result := '11';
151 VK_C: result := '12';
152 VK_D: result := '13';
153 VK_E: result := '14';
154 VK_F: result := '15';
155 VK_CONSOLE: result := 'CON';
156 VK_STATUS: result := 'STAT';
157 VK_TEAM: result := 'TEAM';
158 VK_PREV: result := '<PREW';
159 VK_NEXT: result := 'NEXT>';
160 VK_LSTRAFE: result := '<';
161 VK_RSTRAFE: result := '>';
162 else
163 if (key > 0) and (key < e_MaxInputKeys) then
164 result := e_KeyNames[key]
165 else
166 result := '<' + IntToStr(key) + '>'
167 end
168 end;
170 procedure DrawRect(x, y, w, h: Integer);
171 begin
172 e_DrawQuad(x, y, x + w, y + h, 0, 255, 0, 31);
173 end;
175 function IntersectControl(ctl, xx, yy: Integer): Boolean;
176 var
177 x, y, w, h: Integer;
178 founded: Boolean;
179 begin
180 GetKeyRect(ctl, x, y, w, h, founded);
181 result := founded and (xx >= x) and (yy >= y) and (xx <= x + w) and (yy <= y + h);
182 end;
184 procedure g_Touch_Init;
185 begin
186 {$IFNDEF HEADLESS}
187 enabled := SDL_GetNumTouchDevices() > 0
188 {$ENDIF}
189 end;
191 procedure g_Touch_ShowKeyboard(yes: Boolean);
192 begin
193 {$IFNDEF HEADLESS}
194 if not enabled then
195 Exit;
197 if yes 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;
207 begin
208 if not enabled then
209 Exit;
210 if SDL_IsTextInputActive() = SDL_True then
211 Exit;
213 finger := ev.fingerId + 2;
214 x := Trunc(ev.x * gScreenWidth);
215 y := Trunc(ev.y * gScreenHeight);
217 for i := VK_FIRSTKEY to VK_LASTKEY do
218 begin
219 if IntersectControl(i, x, y) then
220 begin
221 if ev.type_ = SDL_FINGERUP then
222 keyFinger[i] := 0
223 else if ev.type_ = SDL_FINGERMOTION then
224 keyFinger[i] := finger
225 else if ev.type_ = SDL_FINGERDOWN then
226 begin
227 KeyPress(i); // Menu events
228 keyFinger[i] := finger;
229 end
230 end
231 else if keyFinger[i] = finger then
232 begin
233 if ev.type_ = SDL_FINGERUP then
234 keyFinger[i] := 0
235 else if ev.type_ = SDL_FINGERMOTION then
236 keyFinger[i] := 0
237 end;
239 e_KeyUpDown(i, keyFinger[i] <> 0);
240 end;
242 (* emulate up+fire / donw+fire *)
243 if g_touch_fire and (gGameSettings.GameType <> GT_NONE) then
244 begin
245 if keyFinger[VK_UP] <> 0 then
246 begin
247 angleFire := true;
248 keyFinger[VK_FIRE] := keyFinger[VK_UP];
249 e_KeyUpDown(VK_FIRE, true);
250 end
251 else if keyFinger[VK_DOWN] <> 0 then
252 begin
253 angleFire := true;
254 keyFinger[VK_FIRE] := keyFinger[VK_DOWN];
255 e_KeyUpDown(VK_FIRE, true);
256 end
257 else if angleFire then
258 begin
259 angleFire := false;
260 keyFinger[VK_FIRE] := 0;
261 e_KeyUpDown(VK_FIRE, false);
262 end
263 end;
265 (* left/right strafe *)
266 if gGameSettings.GameType <> GT_NONE then
267 begin
268 if keyFinger[VK_LSTRAFE] <> 0 then
269 begin
270 keyFinger[VK_LEFT] := finger;
271 keyFinger[VK_RIGHT] := 0;
272 keyFinger[VK_STRAFE] := finger;
273 e_KeyUpDown(VK_LEFT, true);
274 e_KeyUpDown(VK_RIGHT, false);
275 e_KeyUpDown(VK_STRAFE, true);
276 end
277 else if keyFinger[VK_RSTRAFE] <> 0 then
278 begin
279 keyFinger[VK_LEFT] := 0;
280 keyFinger[VK_RIGHT] := finger;
281 keyFinger[VK_STRAFE] := finger;
282 e_KeyUpDown(VK_LEFT, false);
283 e_KeyUpDown(VK_RIGHT, true);
284 e_KeyUpDown(VK_STRAFE, true);
285 end
286 else
287 begin
288 keyFinger[VK_STRAFE] := 0;
289 e_KeyUpDown(VK_STRAFE, false);
290 end
291 end;
292 end;
294 procedure g_Touch_Draw;
295 var
296 i, x, y, w, h: Integer;
297 founded: Boolean;
298 begin
299 {$IFNDEF HEADLESS}
300 if not enabled then
301 Exit;
302 if SDL_IsTextInputActive() = SDL_True then
303 Exit;
305 for i := VK_FIRSTKEY to VK_LASTKEY do
306 begin
307 GetKeyRect(i, x, y, w, h, founded);
308 if founded then
309 begin
310 DrawRect(x, y, w, h);
311 e_TextureFontPrint(x, y, GetKeyName(i), gStdFont)
312 end;
313 end;
314 {$ENDIF}
315 end;
317 initialization
318 conRegVar('touch_enable', @enabled, 'enable/disable virtual buttons', 'draw buttons');
319 conRegVar('touch_fire', @g_touch_fire, 'enable/disable fire when press virtual up/down', 'fire when press up/down');
320 conRegVar('touch_size', @g_touch_size, 0.1, 10, 'size of virtual buttons', 'button size');
321 conRegVar('touch_offset', @g_touch_offset, 0, 100, '', '');
322 conRegVar('touch_alt', @jab, 'althernative virtual buttons layout', 'althernative layout');
323 end.