DEADSOFTWARE

Added shadow to vkbd text
[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_enabled: Boolean;
26 g_touch_size: Single;
27 g_touch_offset: Single;
28 g_touch_fire: Boolean;
29 g_touch_alt: Boolean;
31 procedure g_Touch_Init;
32 procedure g_Touch_ShowKeyboard(yes: Boolean);
33 procedure g_Touch_HandleEvent(const ev: TSDL_TouchFingerEvent);
34 procedure g_Touch_Draw;
36 implementation
38 uses
39 SysUtils,
40 e_log, e_graphics, e_input, g_options, g_game, g_main, g_weapons, g_console;
42 var
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 g_touch_alt then
58 begin
59 w := sz div 2; h := sz div 2;
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 div 2 - w; y := 0 end;
64 VK_STATUS: begin x := sw div 2 - w div 2 + 0; y := 0 end;
65 VK_TEAM: begin x := sw div 2 - w div 2 + w; y := 0 end;
66 VK_PREV: begin x := 0; y := sh - 4*sz - 1; w := sz end;
67 VK_NEXT: begin x := sw - sz - 1; y := sh - 4*sz - 1; w := sz end;
68 else
69 w := sz; h := sz * 3;
70 case key of
71 VK_LEFT: begin x := 0; y := sh - h - 1 end;
72 VK_RIGHT: begin x := w; y := sh - h - 1 end;
73 else
74 w := sz; h := sz;
75 case key of
76 VK_UP: begin x := sw - 2*w - 1; y := sh - 3*h - 1 end;
77 VK_FIRE: begin x := sw - 2*w - 1; y := sh - 2*h - 1 end;
78 VK_DOWN: begin x := sw - 2*w - 1; y := sh - 1*h - 1 end;
79 VK_OPEN: begin x := sw - 1*w - 1; y := sh - 1*h - h div 2 - 1 end;
80 VK_JUMP: begin x := sw - 1*w - 1; y := sh - 2*h - h div 2 - 1 end;
81 else
82 founded := false
83 end
84 end
85 end
86 end
87 else
88 begin
89 x := 0; y := Round(sh * g_touch_offset / 100); w := sz; h := sz;
90 case key of
91 VK_LSTRAFE: begin x := 0; y := y - h div 2; w := w div 2 end;
92 VK_LEFT: begin x := w div 2; y := y - h div 2 end;
93 VK_RIGHT: begin x := w div 2 + 1*w; y := y - h div 2 end;
94 VK_RSTRAFE: begin x := w div 2 + 2*w; y := y - h div 2; w := w div 2 end;
95 VK_UP: begin x := sw - w - 1; y := y - h div 2 - h end;
96 VK_FIRE: begin x := sw - 1*w - 1; y := y - h div 2 end;
97 VK_DOWN: begin x := sw - w - 1; y := y - h div 2 + h end;
98 VK_NEXT: begin x := sw - 2*w - 1; y := y - h div 2 - h end;
99 VK_JUMP: begin x := sw - 2*w - 1; y := y - h div 2 end;
100 VK_PREV: begin x := sw - 3*w - 1; y := y - h div 2 - h end;
101 VK_OPEN: begin x := sw - 3*w - 1; y := y - h div 2 end;
102 else
103 x := 0; y := 0; w := sz div 2; h := sz div 2;
104 case key of
105 VK_0: begin x := sw div 2 - w div 2 - 5*w - 1; y := sh - 1*h - 1 end;
106 VK_1: begin x := sw div 2 - w div 2 - 4*w - 1; y := sh - 1*h - 1 end;
107 VK_2: begin x := sw div 2 - w div 2 - 3*w - 1; y := sh - 1*h - 1 end;
108 VK_3: begin x := sw div 2 - w div 2 - 2*w - 1; y := sh - 1*h - 1 end;
109 VK_4: begin x := sw div 2 - w div 2 - 1*w - 1; y := sh - 1*h - 1 end;
110 VK_5: begin x := sw div 2 - w div 2 + 0*w - 1; y := sh - 1*h - 1 end;
111 VK_6: begin x := sw div 2 - w div 2 + 1*w - 1; y := sh - 1*h - 1 end;
112 VK_7: begin x := sw div 2 - w div 2 + 2*w - 1; y := sh - 1*h - 1 end;
113 VK_8: begin x := sw div 2 - w div 2 + 3*w - 1; y := sh - 1*h - 1 end;
114 VK_9: begin x := sw div 2 - w div 2 + 4*w - 1; y := sh - 1*h - 1 end;
115 VK_A: begin x := sw div 2 - w div 2 + 5*w - 1; y := sh - 1*h - 1 end;
116 VK_CHAT: begin x := sw div 2 - w div 2 - 2*w - 1; y := sh - 2*h - 1 end;
117 VK_ESCAPE: begin x := sw div 2 - w div 2 - 1*w - 1; y := sh - 2*h - 1 end;
118 VK_CONSOLE: begin x := sw div 2 - w div 2 + 0*w - 1; y := sh - 2*h - 1 end;
119 VK_STATUS: begin x := sw div 2 - w div 2 + 1*w - 1; y := sh - 2*h - 1 end;
120 VK_TEAM: begin x := sw div 2 - w div 2 + 2*w - 1; y := sh - 2*h - 1 end;
121 else
122 founded := false
123 end
124 end
125 end
126 end;
128 function GetKeyName(key: Word): String;
129 begin
130 case key of
131 VK_LEFT: result := 'LEFT';
132 VK_RIGHT: result := 'RIGHT';
133 VK_UP: result := 'UP';
134 VK_DOWN: result := 'DOWN';
135 VK_FIRE: result := 'FIRE';
136 VK_OPEN: result := 'OPEN';
137 VK_JUMP: result := 'JUMP';
138 VK_CHAT: result := 'CHAT';
139 VK_ESCAPE: result := 'ESC';
140 VK_0: result := '0';
141 VK_1: result := '1';
142 VK_2: result := '2';
143 VK_3: result := '3';
144 VK_4: result := '4';
145 VK_5: result := '5';
146 VK_6: result := '6';
147 VK_7: result := '7';
148 VK_8: result := '8';
149 VK_9: result := '9';
150 VK_A: result := '10';
151 VK_B: result := '11';
152 VK_C: result := '12';
153 VK_D: result := '13';
154 VK_E: result := '14';
155 VK_F: result := '15';
156 VK_CONSOLE: result := 'CON';
157 VK_STATUS: result := 'STAT';
158 VK_TEAM: result := 'TEAM';
159 VK_PREV: result := '<PREW';
160 VK_NEXT: result := 'NEXT>';
161 VK_LSTRAFE: result := '<';
162 VK_RSTRAFE: result := '>';
163 else
164 if (key > 0) and (key < e_MaxInputKeys) then
165 result := e_KeyNames[key]
166 else
167 result := '<' + IntToStr(key) + '>'
168 end
169 end;
171 function IntersectControl(ctl, xx, yy: Integer): Boolean;
172 var
173 x, y, w, h: Integer;
174 founded: Boolean;
175 begin
176 GetKeyRect(ctl, x, y, w, h, founded);
177 result := founded and (xx >= x) and (yy >= y) and (xx <= x + w) and (yy <= y + h);
178 end;
180 procedure g_Touch_Init;
181 begin
182 {$IFNDEF HEADLESS}
183 g_touch_enabled := SDL_GetNumTouchDevices() > 0
184 {$ENDIF}
185 end;
187 procedure g_Touch_ShowKeyboard(yes: Boolean);
188 begin
189 {$IFNDEF HEADLESS}
190 if not g_touch_enabled then
191 Exit;
193 if yes then
194 SDL_StartTextInput
195 else
196 SDL_StopTextInput
197 {$ENDIF}
198 end;
200 procedure g_Touch_HandleEvent(const ev: TSDL_TouchFingerEvent);
201 var
202 x, y, i, finger: Integer;
203 begin
204 if not g_touch_enabled then
205 Exit;
206 if SDL_IsTextInputActive() = SDL_True then
207 Exit;
209 finger := ev.fingerId + 2;
210 x := Trunc(ev.x * gScreenWidth);
211 y := Trunc(ev.y * gScreenHeight);
213 for i := VK_FIRSTKEY to VK_LASTKEY do
214 begin
215 if IntersectControl(i, x, y) then
216 begin
217 if ev.type_ = SDL_FINGERUP then
218 keyFinger[i] := 0
219 else if ev.type_ = SDL_FINGERMOTION then
220 keyFinger[i] := finger
221 else if ev.type_ = SDL_FINGERDOWN then
222 begin
223 KeyPress(i); // Menu events
224 keyFinger[i] := finger;
225 end
226 end
227 else if keyFinger[i] = finger then
228 begin
229 if ev.type_ = SDL_FINGERUP then
230 keyFinger[i] := 0
231 else if ev.type_ = SDL_FINGERMOTION then
232 keyFinger[i] := 0
233 end;
235 e_KeyUpDown(i, keyFinger[i] <> 0);
236 end;
238 (* emulate up+fire / donw+fire *)
239 if g_touch_fire and (gGameSettings.GameType <> GT_NONE) then
240 begin
241 if keyFinger[VK_UP] <> 0 then
242 begin
243 angleFire := true;
244 keyFinger[VK_FIRE] := keyFinger[VK_UP];
245 e_KeyUpDown(VK_FIRE, true);
246 end
247 else if keyFinger[VK_DOWN] <> 0 then
248 begin
249 angleFire := true;
250 keyFinger[VK_FIRE] := keyFinger[VK_DOWN];
251 e_KeyUpDown(VK_FIRE, true);
252 end
253 else if angleFire then
254 begin
255 angleFire := false;
256 keyFinger[VK_FIRE] := 0;
257 e_KeyUpDown(VK_FIRE, false);
258 end
259 end;
261 (* left/right strafe *)
262 if gGameSettings.GameType <> GT_NONE then
263 begin
264 if keyFinger[VK_LSTRAFE] <> 0 then
265 begin
266 keyFinger[VK_LEFT] := finger;
267 keyFinger[VK_RIGHT] := 0;
268 keyFinger[VK_STRAFE] := finger;
269 e_KeyUpDown(VK_LEFT, true);
270 e_KeyUpDown(VK_RIGHT, false);
271 e_KeyUpDown(VK_STRAFE, true);
272 end
273 else if keyFinger[VK_RSTRAFE] <> 0 then
274 begin
275 keyFinger[VK_LEFT] := 0;
276 keyFinger[VK_RIGHT] := finger;
277 keyFinger[VK_STRAFE] := finger;
278 e_KeyUpDown(VK_LEFT, false);
279 e_KeyUpDown(VK_RIGHT, true);
280 e_KeyUpDown(VK_STRAFE, true);
281 end
282 else
283 begin
284 keyFinger[VK_STRAFE] := 0;
285 e_KeyUpDown(VK_STRAFE, false);
286 end
287 end;
288 end;
290 procedure g_Touch_Draw;
291 var
292 i, x, y, w, h: Integer;
293 founded: Boolean;
294 begin
295 {$IFNDEF HEADLESS}
296 if not g_touch_enabled then
297 Exit;
298 if SDL_IsTextInputActive() = SDL_True then
299 Exit;
301 for i := VK_FIRSTKEY to VK_LASTKEY do
302 begin
303 GetKeyRect(i, x, y, w, h, founded);
304 if founded then
305 begin
306 e_DrawQuad(x, y, x + w, y + h, 0, 255, 0, 31);
307 e_TextureFontPrintEx(x, y, GetKeyName(i), gStdFont, 255, 255, 255, 1, True)
308 end;
309 end;
310 {$ENDIF}
311 end;
313 initialization
314 conRegVar('touch_enable', @g_touch_enabled, 'enable/disable virtual buttons', 'draw buttons');
315 conRegVar('touch_fire', @g_touch_fire, 'enable/disable fire when press virtual up/down', 'fire when press up/down');
316 conRegVar('touch_size', @g_touch_size, 0.1, 10, 'size of virtual buttons', 'button size');
317 conRegVar('touch_offset', @g_touch_offset, 0, 100, '', '');
318 conRegVar('touch_alt', @g_touch_alt, 'althernative virtual buttons layout', 'althernative layout');
319 end.