0a02e577bf040487632ffbcbf09fd1e52540b38f
1 (* Copyright (C) Doom 2D: Forever Developers
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.
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.
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/>.
16 {$INCLUDE ../shared/a_modes.inc}
24 procedure g_Touch_ShowKeyboard(yes
: Boolean);
25 procedure g_Touch_HandleEvent(const ev
: TSDL_TouchFingerEvent
);
26 procedure g_Touch_Draw
;
32 e_log
, e_graphics
, e_input
, g_options
, g_game
, g_main
, g_weapons
, g_console
;
64 keyFinger
: array [1..CTL_LAST
] of Integer;
66 procedure GetControlRect(control
: Integer; out x
, y
, w
, h
: Integer; out founded
: Boolean);
71 if SDL_GetDisplayDPI(0, @dpi
, nil, nil) <> 0 then
75 sz
:= Trunc(size
* dpi
);
76 x
:= 0; y
:= 0; w
:= sz
; h
:= sz
;
77 sw
:= gScreenWidth
; sh
:= gScreenHeight
;
79 CTL_LEFT
: begin x
:= 0; y
:= sh
div 2 - h
div 2; end;
80 CTL_RIGHT
: begin x
:= w
; y
:= sh
div 2 - h
div 2; end;
81 CTL_UP
: begin x
:= sw
- w
- 1; y
:= sh
div 2 - h
div 2 - h
; end;
82 CTL_DOWN
: begin x
:= sw
- w
- 1; y
:= sh
div 2 - h
div 2 + h
; end;
83 CTL_FIRE
: begin x
:= sw
- 1*w
- 1; y
:= sh
div 2 - h
div 2; end;
84 CTL_OPEN
: begin x
:= sw
- 3*w
- 1; y
:= sh
div 2 - h
div 2; end;
85 CTL_JUMP
: begin x
:= sw
- 2*w
- 1; y
:= sh
div 2 - h
div 2; end;
87 w
:= sz
div 2; h
:= sz
div 2;
89 CTL_W0
: begin x
:= sw
div 2 - w
div 2 - 5*w
- 1; y
:= sh
- 1*h
- 1; end;
90 CTL_W1
: begin x
:= sw
div 2 - w
div 2 - 4*w
- 1; y
:= sh
- 1*h
- 1; end;
91 CTL_W2
: begin x
:= sw
div 2 - w
div 2 - 3*w
- 1; y
:= sh
- 1*h
- 1; end;
92 CTL_W3
: begin x
:= sw
div 2 - w
div 2 - 2*w
- 1; y
:= sh
- 1*h
- 1; end;
93 CTL_W4
: begin x
:= sw
div 2 - w
div 2 - 1*w
- 1; y
:= sh
- 1*h
- 1; end;
94 CTL_W5
: begin x
:= sw
div 2 - w
div 2 + 0*w
- 1; y
:= sh
- 1*h
- 1; end;
95 CTL_W6
: begin x
:= sw
div 2 - w
div 2 + 1*w
- 1; y
:= sh
- 1*h
- 1; end;
96 CTL_W7
: begin x
:= sw
div 2 - w
div 2 + 2*w
- 1; y
:= sh
- 1*h
- 1; end;
97 CTL_W8
: begin x
:= sw
div 2 - w
div 2 + 3*w
- 1; y
:= sh
- 1*h
- 1; end;
98 CTL_W9
: begin x
:= sw
div 2 - w
div 2 + 4*w
- 1; y
:= sh
- 1*h
- 1; end;
99 CTL_W10
: begin x
:= sw
div 2 - w
div 2 + 5*w
- 1; y
:= sh
- 1*h
- 1; end;
100 CTL_CHAT
: begin x
:= sw
div 2 - w
div 2 - 2*w
- 1; y
:= sh
- 2*h
- 1; end;
101 CTL_ESC
: begin x
:= sw
div 2 - w
div 2 - 1*w
- 1; y
:= sh
- 2*h
- 1; end;
102 CTL_CON
: begin x
:= sw
div 2 - w
div 2 + 0*w
- 1; y
:= sh
- 2*h
- 1; end;
103 CTL_STAT
: begin x
:= sw
div 2 - w
div 2 + 1*w
- 1; y
:= sh
- 2*h
- 1; end;
104 CTL_TCHAT
: begin x
:= sw
div 2 - w
div 2 + 2*w
- 1; y
:= sh
- 2*h
- 1; end;
111 function GetMenuKey(control
: Integer): Word;
114 CTL_LEFT
: result
:= IK_LEFT
;
115 CTL_RIGHT
: result
:= IK_RIGHT
;
116 CTL_UP
: result
:= IK_UP
;
117 CTL_DOWN
: result
:= IK_DOWN
;
118 CTL_OPEN
: result
:= IK_ENTER
;
119 CTL_FIRE
: result
:= IK_ENTER
;
120 CTL_JUMP
: result
:= IK_SPACE
;
121 CTL_ESC
: result
:= IK_ESCAPE
;
122 CTL_W0
: result
:= SDL_SCANCODE_0
;
123 CTL_W1
: result
:= SDL_SCANCODE_1
;
124 CTL_W2
: result
:= SDL_SCANCODE_2
;
125 CTL_W3
: result
:= SDL_SCANCODE_3
;
126 CTL_W4
: result
:= SDL_SCANCODE_4
;
127 CTL_W5
: result
:= SDL_SCANCODE_5
;
128 CTL_W6
: result
:= SDL_SCANCODE_6
;
129 CTL_W7
: result
:= SDL_SCANCODE_7
;
130 CTL_W8
: result
:= SDL_SCANCODE_8
;
131 CTL_W9
: result
:= SDL_SCANCODE_9
;
132 CTL_CON
: result
:= IK_GRAVE
;
134 result
:= IK_INVALID
;
138 function GetPlayerKey(control
: Integer): Word;
141 CTL_LEFT
: result
:= gGameControls
.P1Control
.KeyLeft
;
142 CTL_RIGHT
: result
:= gGameControls
.P1Control
.KeyRight
;
143 CTL_UP
: result
:= gGameControls
.P1Control
.KeyUp
;
144 CTL_DOWN
: result
:= gGameControls
.P1Control
.KeyDown
;
145 CTL_OPEN
: result
:= gGameControls
.P1Control
.KeyOpen
;
146 CTL_FIRE
: result
:= gGameControls
.P1Control
.KeyFire
;
147 CTL_JUMP
: result
:= gGameControls
.P1Control
.KeyJump
;
148 CTL_CHAT
: result
:= gGameControls
.GameControls
.Chat
;
149 CTL_ESC
: result
:= IK_ESCAPE
;
150 CTL_W0
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_KASTET
];
151 CTL_W1
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_SAW
];
152 CTL_W2
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_PISTOL
];
153 CTL_W3
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_SHOTGUN1
];
154 CTL_W4
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_SHOTGUN2
];
155 CTL_W5
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_CHAINGUN
];
156 CTL_W6
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_ROCKETLAUNCHER
];
157 CTL_W7
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_PLASMA
];
158 CTL_W8
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_BFG
];
159 CTL_W9
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_SUPERPULEMET
];
160 CTL_W10
: result
:= gGameControls
.P1Control
.KeyWeapon
[WEAPON_FLAMETHROWER
];
161 CTL_CON
: result
:= IK_GRAVE
;
162 CTL_STAT
: result
:= gGameControls
.GameControls
.Stat
;
163 CTL_TCHAT
: result
:= gGameControls
.GameControls
.TeamChat
;
169 function GetControlName(control
: Integer): String;
172 CTL_LEFT
: result
:= 'LEFT';
173 CTL_RIGHT
: result
:= 'RIGHT';
174 CTL_UP
: result
:= 'UP';
175 CTL_DOWN
: result
:= 'DOWN';
176 CTL_OPEN
: result
:= 'OPEN';
177 CTL_FIRE
: result
:= 'FIRE';
178 CTL_JUMP
: result
:= 'JUMP';
179 CTL_CHAT
: result
:= 'CHAT';
180 CTL_ESC
: result
:= 'ESC';
181 CTL_W0
: result
:= '0';
182 CTL_W1
: result
:= '1';
183 CTL_W2
: result
:= '2';
184 CTL_W3
: result
:= '3';
185 CTL_W4
: result
:= '4';
186 CTL_W5
: result
:= '5';
187 CTL_W6
: result
:= '6';
188 CTL_W7
: result
:= '7';
189 CTL_W8
: result
:= '8';
190 CTL_W9
: result
:= '9';
191 CTL_W10
: result
:= '10';
192 CTL_CON
: result
:= 'CON';
193 CTL_STAT
: result
:= 'STAT';
194 CTL_TCHAT
: result
:= 'TEAM';
200 procedure DrawRect(x
, y
, w
, h
: Integer);
202 e_DrawQuad(x
, y
, x
+ w
, y
+ h
, 0, 255, 0, 127);
205 function IntersectControl(ctl
, xx
, yy
: Integer): Boolean;
210 GetControlRect(ctl
, x
, y
, w
, h
, founded
);
211 result
:= founded
and (xx
>= x
) and (yy
>= y
) and (xx
<= x
+ w
) and (yy
<= y
+ h
);
214 procedure g_Touch_ShowKeyboard(yes
: Boolean);
227 procedure g_Touch_HandleEvent(const ev
: TSDL_TouchFingerEvent
);
229 x
, y
, i
, finger
: Integer;
233 if SDL_IsTextInputActive() = SDL_True
then
236 finger
:= ev
.fingerId
+ 2;
237 x
:= Trunc(ev
.x
* gScreenWidth
);
238 y
:= Trunc(ev
.y
* gScreenHeight
);
240 for i
:= 1 to CTL_LAST
do
242 if IntersectControl(i
, x
, y
) then
244 if ev
.type_
= SDL_FINGERUP
then
246 else if ev
.type_
= SDL_FINGERMOTION
then
247 keyFinger
[i
] := finger
248 else if ev
.type_
= SDL_FINGERDOWN
then
250 KeyPress(GetMenuKey(i
));
251 keyFinger
[i
] := finger
;
254 else if keyFinger
[i
] = finger
then
256 if ev
.type_
= SDL_FINGERUP
then
258 else if ev
.type_
= SDL_FINGERMOTION
then
262 e_KeyUpDown(GetPlayerKey(i
), keyFinger
[i
] <> 0);
263 e_KeyUpDown(GetMenuKey(i
), keyFinger
[i
] <> 0);
267 procedure g_Touch_Draw
;
269 i
, x
, y
, w
, h
: Integer;
275 if SDL_IsTextInputActive() = SDL_True
then
278 for i
:= 1 to CTL_LAST
do
280 GetControlRect(i
, x
, y
, w
, h
, founded
);
283 DrawRect(x
, y
, w
, h
);
284 e_TextureFontPrint(x
, y
, GetControlName(i
), gStdFont
)
295 conRegVar('touch_enable', @enabled
, 'enable/disable virtual buttons', 'draw buttons');
296 conRegVar('touch_size', @size
, 0.1, 10, 'size of virtual buttons', 'button size');