update helpers
[carveJwlIkooP6JGAAIwe30JlM.git] / input.h
1 #ifndef INPUT_H
2 #define INPUT_H
3
4 #define VG_GAME
5 #include "vg/vg.h"
6 #include "vg/vg_platform.h"
7 #include "vg/vg_console.h"
8 #include "vg/vg_input.h"
9 #include "vg/vg_m.h"
10 #include "font.h"
11
12 enum sr_bind{
13 k_srbind_jump = 0,
14 k_srbind_push,
15 k_srbind_trick0,
16 k_srbind_trick1,
17 k_srbind_trick2,
18 k_srbind_sit,
19 k_srbind_use,
20 k_srbind_reset,
21 k_srbind_camera,
22 k_srbind_mleft,
23 k_srbind_mright,
24 k_srbind_mup,
25 k_srbind_mdown,
26 k_srbind_mback,
27 k_srbind_maccept,
28 k_srbind_mopen,
29 k_srbind_replay_play,
30 k_srbind_replay_freecam,
31 k_srbind_replay_resume,
32 k_srbind_world_left,
33 k_srbind_world_right,
34 k_srbind_home,
35 k_srbind_lobby,
36 k_srbind_chat,
37 k_srbind_max,
38 };
39
40 enum sr_joystick{
41 k_srjoystick_steer = 0,
42 k_srjoystick_grab,
43 k_srjoystick_look,
44 k_srjoystick_max
45 };
46
47 enum sr_axis{
48 k_sraxis_grab = 0,
49 k_sraxis_mbrowse_h,
50 k_sraxis_mbrowse_v,
51 k_sraxis_replay_h,
52 k_sraxis_max
53 };
54
55
56 #define INPUT_BASIC( KB, JS ) \
57 (vg_input_op[]){vg_keyboard, KB, vg_joy_button, JS, vg_end}
58
59 static vg_input_op *input_button_list[] = {
60 [k_srbind_jump] = INPUT_BASIC( SDLK_SPACE, SDL_CONTROLLER_BUTTON_A ),
61 [k_srbind_push] = INPUT_BASIC( SDLK_w, SDL_CONTROLLER_BUTTON_B ),
62 [k_srbind_trick0] = (vg_input_op[]){
63 vg_mouse, SDL_BUTTON_LEFT,
64 vg_joy_button, SDL_CONTROLLER_BUTTON_A, vg_end
65 },
66 [k_srbind_trick1] = (vg_input_op[]){
67 vg_mouse, SDL_BUTTON_RIGHT,
68 vg_joy_button, SDL_CONTROLLER_BUTTON_B, vg_end
69 },
70 [k_srbind_trick2] = (vg_input_op[]){
71 vg_mouse, SDL_BUTTON_LEFT, vg_mode_mul, vg_mouse, SDL_BUTTON_RIGHT,
72 vg_mode_absmax, vg_joy_button, SDL_CONTROLLER_BUTTON_X, vg_end
73 },
74 [k_srbind_use] = INPUT_BASIC( SDLK_e, SDL_CONTROLLER_BUTTON_Y ),
75 [k_srbind_reset] = INPUT_BASIC( SDLK_r, SDL_CONTROLLER_BUTTON_LEFTSHOULDER ),
76 [k_srbind_camera]= INPUT_BASIC( SDLK_c, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ),
77 [k_srbind_mleft] = INPUT_BASIC( SDLK_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
78 [k_srbind_mright]= INPUT_BASIC( SDLK_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
79 [k_srbind_world_left] =
80 INPUT_BASIC( SDLK_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
81 [k_srbind_world_right] =
82 INPUT_BASIC( SDLK_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
83 [k_srbind_home] = INPUT_BASIC( SDLK_h, SDL_CONTROLLER_BUTTON_Y ),
84 [k_srbind_mup] = INPUT_BASIC( SDLK_UP, SDL_CONTROLLER_BUTTON_DPAD_UP ),
85 [k_srbind_mdown] = INPUT_BASIC( SDLK_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN ),
86 [k_srbind_mback] = INPUT_BASIC( SDLK_ESCAPE, SDL_CONTROLLER_BUTTON_B ),
87 [k_srbind_mopen] = INPUT_BASIC( SDLK_ESCAPE, SDL_CONTROLLER_BUTTON_START ),
88 [k_srbind_maccept] = (vg_input_op[]){
89 vg_keyboard, SDLK_e, vg_gui_visible, 0,
90 vg_keyboard, SDLK_RETURN, vg_keyboard, SDLK_RETURN2,
91 vg_gui_visible, 1,
92 vg_joy_button, SDL_CONTROLLER_BUTTON_A, vg_end
93 },
94 [k_srbind_replay_play] = INPUT_BASIC( SDLK_g, SDL_CONTROLLER_BUTTON_X ),
95 [k_srbind_replay_resume] = INPUT_BASIC( SDLK_SPACE, SDL_CONTROLLER_BUTTON_A ),
96 [k_srbind_replay_freecam] = INPUT_BASIC( SDLK_f, SDL_CONTROLLER_BUTTON_Y ),
97 [k_srbind_sit] = INPUT_BASIC( SDLK_z, SDL_CONTROLLER_BUTTON_B ),
98 [k_srbind_lobby] = INPUT_BASIC( SDLK_TAB, SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
99 [k_srbind_chat ] = (vg_input_op[]){ vg_keyboard, SDLK_y, vg_end },
100 [k_srbind_max]=NULL
101 };
102
103 static vg_input_op *input_axis_list[] = {
104 [k_sraxis_grab] = (vg_input_op[]){
105 vg_keyboard, SDLK_LSHIFT,
106 vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, vg_end
107 },
108 [k_sraxis_mbrowse_h] = (vg_input_op[]){
109 vg_mode_sub, vg_keyboard, SDLK_LEFT,
110 vg_mode_add, vg_keyboard, SDLK_RIGHT,
111 vg_mode_absmax, vg_joy_axis, SDL_CONTROLLER_AXIS_LEFTX,
112 vg_end
113 },
114 [k_sraxis_mbrowse_v] = (vg_input_op[]){
115 vg_mode_add, vg_keyboard, SDLK_UP,
116 vg_mode_sub, vg_keyboard, SDLK_DOWN,
117 vg_mode_absmax, vg_joy_axis, SDL_CONTROLLER_AXIS_LEFTY,
118 vg_end
119 },
120 [k_sraxis_replay_h] = (vg_input_op[]){
121 vg_mode_add, vg_keyboard, SDLK_e,
122 vg_mode_sub, vg_keyboard, SDLK_q,
123 vg_mode_add, vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
124 vg_mode_sub, vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERLEFT,
125 vg_end
126 },
127 [k_sraxis_max]=NULL
128 };
129
130 static vg_input_op *input_joy_list[] = {
131 [k_srjoystick_steer] = (vg_input_op[]){
132 vg_index, 0, vg_mode_add, vg_keyboard, SDLK_d,
133 vg_mode_sub, vg_keyboard, SDLK_a,
134 vg_index, 1, vg_mode_add, vg_keyboard, SDLK_s,
135 vg_mode_sub, vg_keyboard, SDLK_w,
136 vg_mode_absmax, vg_joy_ls,
137 vg_end
138 },
139 [k_srjoystick_grab] = (vg_input_op[]){
140 vg_joy_rs, vg_end
141 },
142 [k_srjoystick_look] = (vg_input_op[]){
143 vg_joy_rs, vg_end
144 },
145 [k_srjoystick_max]=NULL
146 };
147
148 struct {
149 float axis_states[ k_sraxis_max ][2];
150 v2f joystick_states[ k_srjoystick_max ][2];
151 u8 button_states[ k_srbind_max ][2];
152
153 enum input_state {
154 k_input_state_enabled,
155 k_input_state_resume,
156 k_input_state_resuming,
157 k_input_state_pause
158 }
159 state;
160 }
161 static srinput;
162
163 #if 0
164 static const char *controller_glyphs[ SDL_CONTROLLER_BUTTON_MAX ][2] =
165 { /* xbox/generic playstation */
166 [ SDL_CONTROLLER_BUTTON_A ] = { "\x1e\x85","\x1e\x82" },
167 [ SDL_CONTROLLER_BUTTON_B ] = { "\x1e\x86","\x1e\x81" },
168 [ SDL_CONTROLLER_BUTTON_X ] = { "\x1e\x83","\x1e\x7f" },
169 [ SDL_CONTROLLER_BUTTON_Y ] = { "\x1e\x84","\x1e\x80" },
170 [ SDL_CONTROLLER_BUTTON_LEFTSTICK ] = { "\x87", "\x87" },
171 [ SDL_CONTROLLER_BUTTON_RIGHTSTICK ] = { "\x8b", "\x8b" },
172 [ SDL_CONTROLLER_BUTTON_LEFTSHOULDER ] = { "\x91", "\x91" },
173 [ SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ]= { "\x92", "\x92" },
174 [ SDL_CONTROLLER_BUTTON_DPAD_LEFT ] = { "\x1e\x93","\x1e\x93" },
175 [ SDL_CONTROLLER_BUTTON_DPAD_UP ] = { "\x1e\x94","\x1e\x94" },
176 [ SDL_CONTROLLER_BUTTON_DPAD_RIGHT ] = { "\x1e\x95","\x1e\x95" },
177 [ SDL_CONTROLLER_BUTTON_DPAD_DOWN ] = { "\x1e\x96","\x1e\x96" },
178 [ SDL_CONTROLLER_BUTTON_GUIDE ] = { "\x91", "\x91" },
179 };
180
181 static const char *controller_glyph( u32 sdl_index ){
182 if( vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS3 ||
183 vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS4 ||
184 vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS5 )
185 {
186 return controller_glyphs[ sdl_index ][ 1 ];
187 }
188 else if( vg_input.display_input_type ==
189 SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO ||
190 vg_input.display_input_type ==
191 SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_LEFT ||
192 vg_input.display_input_type ==
193 SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_PAIR ||
194 vg_input.display_input_type ==
195 SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT )
196 {
197 return NULL;
198 }
199 else
200 return controller_glyphs[ sdl_index ][ 0 ];
201 }
202
203 #define KEYBOARD_GLYPH( X ) "\x1f" X " "
204
205 static const char *button_display_string( enum sr_bind button )
206 {
207 const char *controller_table[ k_srbind_max ] = {
208 [k_srbind_jump] = controller_glyph( SDL_CONTROLLER_BUTTON_A ),
209 [k_srbind_push] = controller_glyph( SDL_CONTROLLER_BUTTON_B ),
210 [k_srbind_trick0] = controller_glyph( SDL_CONTROLLER_BUTTON_A ),
211 [k_srbind_trick1] = controller_glyph( SDL_CONTROLLER_BUTTON_B ),
212 [k_srbind_trick2] = controller_glyph( SDL_CONTROLLER_BUTTON_X ),
213 [k_srbind_use] = controller_glyph( SDL_CONTROLLER_BUTTON_Y ),
214 [k_srbind_reset] = controller_glyph( SDL_CONTROLLER_BUTTON_LEFTSHOULDER ),
215 [k_srbind_camera] = controller_glyph( SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ),
216 [k_srbind_mleft] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
217 [k_srbind_mright] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
218 [k_srbind_world_left] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
219 [k_srbind_world_right] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
220 [k_srbind_home] = controller_glyph( SDL_CONTROLLER_BUTTON_Y ),
221 [k_srbind_mup] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_UP ),
222 [k_srbind_mdown] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_DOWN ),
223 [k_srbind_mback] = controller_glyph( SDL_CONTROLLER_BUTTON_B ),
224 [k_srbind_maccept]= controller_glyph( SDL_CONTROLLER_BUTTON_A ),
225 [k_srbind_mopen] = controller_glyph( SDL_CONTROLLER_BUTTON_GUIDE ),
226 [k_srbind_replay_play]= controller_glyph( SDL_CONTROLLER_BUTTON_X ),
227 [k_srbind_replay_freecam]=controller_glyph(SDL_CONTROLLER_BUTTON_Y ),
228 [k_srbind_replay_resume]=controller_glyph( SDL_CONTROLLER_BUTTON_A ),
229 [k_srbind_sit] = controller_glyph( SDL_CONTROLLER_BUTTON_B ),
230 [k_srbind_lobby] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
231 [k_srbind_chat] = ""
232 };
233
234 const char *keyboard_table[ k_srbind_max ] = {
235 [k_srbind_jump] = "\xa1",
236 [k_srbind_push] = KEYBOARD_GLYPH( "W" ),
237 [k_srbind_trick0] = "\x99",
238 [k_srbind_trick1] = "\x9a",
239 [k_srbind_trick2] = "\x9b",
240 [k_srbind_use] = KEYBOARD_GLYPH( "E" ),
241 [k_srbind_reset] = KEYBOARD_GLYPH( "R" ),
242 [k_srbind_camera] = KEYBOARD_GLYPH( "C" ),
243 [k_srbind_mleft] = KEYBOARD_GLYPH( "\x93" ),
244 [k_srbind_mright] = KEYBOARD_GLYPH( "\x95" ),
245 [k_srbind_world_left] = KEYBOARD_GLYPH( "\x93" ),
246 [k_srbind_world_right] = KEYBOARD_GLYPH( "\x95" ),
247 [k_srbind_home] = KEYBOARD_GLYPH( "H" ),
248 [k_srbind_mup] = KEYBOARD_GLYPH( "\x94" ),
249 [k_srbind_mdown] = KEYBOARD_GLYPH( "\x96" ),
250 [k_srbind_mback] = "\xa3",
251 [k_srbind_maccept]= "\xa2",
252 [k_srbind_mopen] = "\xa3" ,
253 [k_srbind_replay_play]= KEYBOARD_GLYPH( "G" ),
254 [k_srbind_replay_freecam] = KEYBOARD_GLYPH( "F" ),
255 [k_srbind_replay_resume]= "\xa1",
256 [k_srbind_sit] = KEYBOARD_GLYPH( "Z" ),
257 [k_srbind_lobby] = KEYBOARD_GLYPH( "" ), // FIXME: what is tab?
258 [k_srbind_chat] = KEYBOARD_GLYPH( "Y" )
259 };
260
261 if( vg_input.display_input_method == k_input_method_controller )
262 return controller_table[ button ];
263 else
264 return keyboard_table[ button ];
265 }
266
267 static const char *axis_display_string( enum sr_axis axis )
268 {
269 const char *controller_table[ k_sraxis_max ] = {
270 [k_sraxis_grab] = "\x90",
271 [k_sraxis_mbrowse_h] = "\x88",
272 [k_sraxis_mbrowse_v] = "\x89",
273 [k_sraxis_replay_h] = "\x8f\x90",
274 };
275
276 const char *keyboard_table[ k_sraxis_max ] = {
277 [k_sraxis_grab] = "\x9e",
278 [k_sraxis_mbrowse_h] = KEYBOARD_GLYPH("\x93")KEYBOARD_GLYPH("\x95"),
279 [k_sraxis_mbrowse_v] = KEYBOARD_GLYPH("\x96")KEYBOARD_GLYPH("\x94"),
280 [k_sraxis_replay_h] = KEYBOARD_GLYPH("Q")KEYBOARD_GLYPH("E"),
281 };
282
283 if( vg_input.display_input_method == k_input_method_controller )
284 return controller_table[ axis ];
285 else
286 return keyboard_table[ axis ];
287 }
288
289 static const char *joystick_display_string( enum sr_joystick joystick,
290 u32 axis )
291 {
292 const char *controller_table[ k_srjoystick_max ][3] = {
293 [k_srjoystick_steer] = {"\x88","\x89","\x8a"},
294 [k_srjoystick_grab] = {"\x8c","\x8d","\x8e"},
295 [k_srjoystick_look] = {"\x8c","\x8d","\x8e"}
296 };
297
298 const char *keyboard_table[ k_srjoystick_max ][3] = {
299 [k_srjoystick_steer] = {KEYBOARD_GLYPH("A")KEYBOARD_GLYPH("D"), /*h*/
300 KEYBOARD_GLYPH("W")KEYBOARD_GLYPH("S"), /*v*/
301
302 KEYBOARD_GLYPH("A")KEYBOARD_GLYPH("D") /*hv*/
303 KEYBOARD_GLYPH("W")KEYBOARD_GLYPH("S"), },
304 [k_srjoystick_grab] = {"\xa4","\xa4","\xa4"},
305 [k_srjoystick_look] = {"\xa4","\xa4","\xa4"}
306 };
307
308 if( vg_input.display_input_method == k_input_method_controller )
309 return controller_table[ joystick ][axis];
310 else return keyboard_table[ joystick ][axis];
311 }
312 #endif
313
314 static int input_filter_generic(void){
315 if( srinput.state != k_input_state_enabled ||
316 vg_ui.wants_mouse || vg_console.enabled )
317 return 1;
318 else
319 return 0;
320 }
321
322 static int buttons_filter_fixed(void){
323 if( input_filter_generic() )
324 return 1;
325
326 if( vg.engine_stage == k_engine_stage_update_fixed )
327 if( vg.fixed_iterations > 0 )
328 return 1;
329
330 return 0;
331 }
332
333 /* Rising edge of button */
334 static int button_down( enum sr_bind button ){
335 if( buttons_filter_fixed() ) return 0;
336
337 if( srinput.button_states[ button ][0] &&
338 !srinput.button_states[ button ][1] )
339 return 1;
340 else
341 return 0;
342 }
343
344 /* Falling edge of button */
345 static int button_up( enum sr_bind button ){
346 if( buttons_filter_fixed() ) return 0;
347
348 if( !srinput.button_states[ button ][0] &&
349 srinput.button_states[ button ][1] )
350 return 1;
351 else
352 return 0;
353 }
354
355 /* State of button */
356 static int button_press( enum sr_bind button ){
357 if( input_filter_generic() )
358 return 0;
359 return
360 srinput.button_states[ button ][0];
361 }
362
363 static void joystick_state( enum sr_joystick joystick, v2f state ){
364 if( input_filter_generic() )
365 v2_zero( state );
366 else
367 v2_copy( srinput.joystick_states[ joystick ][0], state );
368 }
369
370 static float axis_state( enum sr_axis axis ){
371 if( input_filter_generic() )
372 return 0.0f;
373 else
374 return srinput.axis_states[axis][0];
375 }
376
377 static void skaterift_preupdate_inputs(void){
378 if( srinput.state == k_input_state_resuming )
379 srinput.state = k_input_state_enabled;
380
381 if( srinput.state == k_input_state_resume )
382 srinput.state = k_input_state_resuming;
383
384 for( u32 i=0; i<k_srbind_max; i++ ){
385 srinput.button_states[i][1] = srinput.button_states[i][0];
386 srinput.button_states[i][0] = 0;
387 }
388
389 for( u32 i=0; i<k_srjoystick_max; i++ ){
390 v2_copy( srinput.joystick_states[i][0], srinput.joystick_states[i][1] );
391 v2_zero( srinput.joystick_states[i][0] );
392 }
393
394 for( u32 i=0; i<k_sraxis_max; i++ ){
395 srinput.axis_states[i][1] = srinput.axis_states[i][0];
396 srinput.axis_states[i][0] = 0.0f;
397 }
398
399 for( int i=0; i<k_srbind_max; i++ ){
400 vg_input_op *prog = input_button_list[i];
401 if( prog ){
402 vg_exec_input_program( k_vg_input_type_button_u8, prog,
403 &srinput.button_states[i][0] );
404 }
405 }
406
407 for( int i=0; i<k_sraxis_max; i++ ){
408 vg_input_op *prog = input_axis_list[i];
409 if( prog ){
410 vg_exec_input_program( k_vg_input_type_axis_f32, prog,
411 &srinput.axis_states[i][0] );
412 }
413 }
414
415 for( int i=0; i<k_srjoystick_max; i++ ){
416 vg_input_op *prog = input_joy_list[i];
417 if( prog ){
418 vg_exec_input_program( k_vg_input_type_joy_v2f, prog,
419 srinput.joystick_states[i][0] );
420 }
421 }
422
423 f32 x = srinput.axis_states[k_sraxis_mbrowse_h][0],
424 y = srinput.axis_states[k_sraxis_mbrowse_v][0],
425 sensitivity = 0.35f;
426
427 if( fabsf(x) > sensitivity ){
428 if( x > 0.0f ) srinput.button_states[k_srbind_mright][0] = 1;
429 else srinput.button_states[k_srbind_mleft][0] = 1;
430 }
431
432 if( fabsf(y) > sensitivity ){
433 if( y > 0.0f ) srinput.button_states[k_srbind_mup][0] = 1;
434 else srinput.button_states[k_srbind_mdown][0] = 1;
435 }
436 }
437
438 #endif /* INPUT_H */