6 #include "vg/vg_platform.h"
7 #include "vg/vg_console.h"
8 #include "vg/vg_input.h"
30 k_srbind_replay_freecam
,
31 k_srbind_replay_resume
,
41 k_srjoystick_steer
= 0,
56 #define INPUT_BASIC( KB, JS ) \
57 (vg_input_op[]){vg_keyboard, KB, vg_joy_button, JS, vg_end}
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
66 [k_srbind_trick1
] = (vg_input_op
[]){
67 vg_mouse
, SDL_BUTTON_RIGHT
,
68 vg_joy_button
, SDL_CONTROLLER_BUTTON_B
, vg_end
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
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
,
92 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
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
},
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
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
,
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
,
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
,
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
,
139 [k_srjoystick_grab
] = (vg_input_op
[]){
142 [k_srjoystick_look
] = (vg_input_op
[]){
145 [k_srjoystick_max
]=NULL
149 float axis_states
[ k_sraxis_max
][2];
150 v2f joystick_states
[ k_srjoystick_max
][2];
151 u8 button_states
[ k_srbind_max
][2];
154 k_input_state_enabled
,
155 k_input_state_resume
,
156 k_input_state_resuming
,
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" },
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
)
186 return controller_glyphs
[ sdl_index
][ 1 ];
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
)
200 return controller_glyphs
[ sdl_index
][ 0 ];
203 #define KEYBOARD_GLYPH( X ) "\x1f" X " "
205 static const char *button_display_string( enum sr_bind button
)
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
),
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" )
261 if( vg_input
.display_input_method
== k_input_method_controller
)
262 return controller_table
[ button
];
264 return keyboard_table
[ button
];
267 static const char *axis_display_string( enum sr_axis axis
)
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",
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"),
283 if( vg_input
.display_input_method
== k_input_method_controller
)
284 return controller_table
[ axis
];
286 return keyboard_table
[ axis
];
289 static const char *joystick_display_string( enum sr_joystick joystick
,
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"}
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*/
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"}
308 if( vg_input
.display_input_method
== k_input_method_controller
)
309 return controller_table
[ joystick
][axis
];
310 else return keyboard_table
[ joystick
][axis
];
314 static int input_filter_generic(void){
315 if( srinput
.state
!= k_input_state_enabled
||
316 vg_ui
.wants_mouse
|| vg_console
.enabled
)
322 static int buttons_filter_fixed(void){
323 if( input_filter_generic() )
326 if( vg
.engine_stage
== k_engine_stage_update_fixed
)
327 if( vg
.fixed_iterations
> 0 )
333 /* Rising edge of button */
334 static int button_down( enum sr_bind button
){
335 if( buttons_filter_fixed() ) return 0;
337 if( srinput
.button_states
[ button
][0] &&
338 !srinput
.button_states
[ button
][1] )
344 /* Falling edge of button */
345 static int button_up( enum sr_bind button
){
346 if( buttons_filter_fixed() ) return 0;
348 if( !srinput
.button_states
[ button
][0] &&
349 srinput
.button_states
[ button
][1] )
355 /* State of button */
356 static int button_press( enum sr_bind button
){
357 if( input_filter_generic() )
360 srinput
.button_states
[ button
][0];
363 static void joystick_state( enum sr_joystick joystick
, v2f state
){
364 if( input_filter_generic() )
367 v2_copy( srinput
.joystick_states
[ joystick
][0], state
);
370 static float axis_state( enum sr_axis axis
){
371 if( input_filter_generic() )
374 return srinput
.axis_states
[axis
][0];
377 static void skaterift_preupdate_inputs(void){
378 if( srinput
.state
== k_input_state_resuming
)
379 srinput
.state
= k_input_state_enabled
;
381 if( srinput
.state
== k_input_state_resume
)
382 srinput
.state
= k_input_state_resuming
;
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;
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] );
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
;
399 for( int i
=0; i
<k_srbind_max
; i
++ ){
400 vg_input_op
*prog
= input_button_list
[i
];
402 vg_exec_input_program( k_vg_input_type_button_u8
, prog
,
403 &srinput
.button_states
[i
][0] );
407 for( int i
=0; i
<k_sraxis_max
; i
++ ){
408 vg_input_op
*prog
= input_axis_list
[i
];
410 vg_exec_input_program( k_vg_input_type_axis_f32
, prog
,
411 &srinput
.axis_states
[i
][0] );
415 for( int i
=0; i
<k_srjoystick_max
; i
++ ){
416 vg_input_op
*prog
= input_joy_list
[i
];
418 vg_exec_input_program( k_vg_input_type_joy_v2f
, prog
,
419 srinput
.joystick_states
[i
][0] );
423 f32 x
= srinput
.axis_states
[k_sraxis_mbrowse_h
][0],
424 y
= srinput
.axis_states
[k_sraxis_mbrowse_v
][0],
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;
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;