2 #include "vg/vg_platform.h"
3 #include "vg/vg_console.h"
4 #include "vg/vg_input.h"
19 k_srbind_dead_respawn
,
30 k_srbind_replay_freecam
,
31 k_srbind_replay_resume
,
39 k_srbind_miniworld_teleport
,
40 k_srbind_miniworld_resume
,
46 k_srjoystick_steer
= 0,
62 #define INPUT_BASIC( KB, JS ) \
63 (vg_input_op[]){vg_keyboard, KB, vg_joy_button, JS, vg_end}
65 static vg_input_op
*input_button_list
[] = {
66 [k_srbind_jump
] = INPUT_BASIC( SDLK_SPACE
, SDL_CONTROLLER_BUTTON_A
),
67 [k_srbind_push
] = INPUT_BASIC( SDLK_w
, SDL_CONTROLLER_BUTTON_B
),
68 [k_srbind_trick0
] = (vg_input_op
[]){
69 vg_mouse
, SDL_BUTTON_LEFT
,
70 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
72 [k_srbind_trick1
] = (vg_input_op
[]){
73 vg_mouse
, SDL_BUTTON_RIGHT
,
74 vg_joy_button
, SDL_CONTROLLER_BUTTON_B
, vg_end
76 [k_srbind_trick2
] = (vg_input_op
[]){
77 vg_mouse
, SDL_BUTTON_LEFT
, vg_mode_mul
, vg_mouse
, SDL_BUTTON_RIGHT
,
78 vg_mode_absmax
, vg_joy_button
, SDL_CONTROLLER_BUTTON_X
, vg_end
80 [k_srbind_use
] = INPUT_BASIC( SDLK_e
, SDL_CONTROLLER_BUTTON_Y
),
81 [k_srbind_reset
] = INPUT_BASIC( SDLK_r
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
82 [k_srbind_dead_respawn
] =
83 INPUT_BASIC( SDLK_q
, SDL_CONTROLLER_BUTTON_DPAD_UP
),
84 [k_srbind_camera
]= INPUT_BASIC( SDLK_c
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
85 [k_srbind_mleft
] = INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
86 [k_srbind_mright
]= INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
87 [k_srbind_world_left
] =
88 INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
89 [k_srbind_world_right
] =
90 INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
91 [k_srbind_home
] = INPUT_BASIC( SDLK_h
, SDL_CONTROLLER_BUTTON_Y
),
92 [k_srbind_mup
] = INPUT_BASIC( SDLK_UP
, SDL_CONTROLLER_BUTTON_DPAD_UP
),
93 [k_srbind_mdown
] = INPUT_BASIC( SDLK_DOWN
, SDL_CONTROLLER_BUTTON_DPAD_DOWN
),
94 [k_srbind_mback
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_B
),
95 [k_srbind_mopen
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_START
),
96 [k_srbind_mhub
] = (vg_input_op
[]){ vg_joy_button
, SDL_CONTROLLER_BUTTON_Y
,
98 [k_srbind_maccept
] = (vg_input_op
[]){
99 vg_keyboard
, SDLK_e
, vg_gui_visible
, 0,
100 vg_keyboard
, SDLK_RETURN
, vg_keyboard
, SDLK_RETURN2
,
102 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
104 [k_srbind_replay_play
] = INPUT_BASIC( SDLK_g
, SDL_CONTROLLER_BUTTON_X
),
105 [k_srbind_replay_resume
] = INPUT_BASIC( SDLK_SPACE
, SDL_CONTROLLER_BUTTON_A
),
106 [k_srbind_replay_freecam
] = INPUT_BASIC( SDLK_f
, SDL_CONTROLLER_BUTTON_Y
),
107 [k_srbind_sit
] = INPUT_BASIC( SDLK_z
, SDL_CONTROLLER_BUTTON_B
),
108 [k_srbind_lobby
] = INPUT_BASIC( SDLK_TAB
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
109 [k_srbind_chat
] = (vg_input_op
[]){ vg_keyboard
, SDLK_y
, vg_end
},
110 [k_srbind_run
] = (vg_input_op
[]){ vg_keyboard
, SDLK_LSHIFT
,
111 vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERLEFT
, vg_end
},
113 [k_srbind_miniworld_resume
] = (vg_input_op
[]){
114 vg_keyboard
, SDLK_RETURN
, vg_gui_visible
, 0,
115 vg_keyboard
, SDLK_RETURN2
,
117 vg_joy_button
, SDL_CONTROLLER_BUTTON_X
, vg_end
119 [k_srbind_miniworld_teleport
]= INPUT_BASIC( SDLK_q
,
120 SDL_CONTROLLER_BUTTON_LEFTSHOULDER
),
121 [k_srbind_skid
] = (vg_input_op
[]){ vg_keyboard
, SDLK_LCTRL
, vg_end
},
122 [k_srbind_devbutton
] = (vg_input_op
[]){ vg_keyboard
, SDLK_3
, vg_end
},
126 static vg_input_op
*input_axis_list
[] = {
127 [k_sraxis_grab
] = (vg_input_op
[]){
128 vg_keyboard
, SDLK_LSHIFT
,
129 vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
, vg_end
131 [k_sraxis_mbrowse_h
] = (vg_input_op
[]){
132 vg_mode_sub
, vg_keyboard
, SDLK_LEFT
,
133 vg_mode_add
, vg_keyboard
, SDLK_RIGHT
,
134 vg_mode_add
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTX
,
137 [k_sraxis_mbrowse_v
] = (vg_input_op
[]){
138 vg_mode_sub
, vg_keyboard
, SDLK_DOWN
,
139 vg_mode_add
, vg_keyboard
, SDLK_UP
,
140 vg_mode_sub
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTY
,
143 [k_sraxis_replay_h
] = (vg_input_op
[]){
144 vg_mode_sub
, vg_keyboard
, SDLK_q
,
145 vg_mode_add
, vg_keyboard
, SDLK_e
,
146 vg_mode_sub
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERLEFT
,
147 vg_mode_add
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
,
150 [k_sraxis_skid
] = (vg_input_op
[]){
151 vg_mode_sub
, vg_joy_button
, SDL_CONTROLLER_BUTTON_LEFTSHOULDER
,
152 vg_mode_add
, vg_joy_button
, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
,
158 static vg_input_op
*input_joy_list
[] = {
159 [k_srjoystick_steer
] = (vg_input_op
[]){
160 vg_index
, 0, vg_mode_sub
, vg_keyboard
, SDLK_a
,
161 vg_mode_add
, vg_keyboard
, SDLK_d
,
162 vg_index
, 1, vg_mode_sub
, vg_keyboard
, SDLK_w
,
163 vg_mode_add
, vg_keyboard
, SDLK_s
,
164 vg_mode_absmax
, vg_joy_ls
,
167 [k_srjoystick_grab
] = (vg_input_op
[]){
170 [k_srjoystick_look
] = (vg_input_op
[]){
173 [k_srjoystick_max
]=NULL
177 float axis_states
[ k_sraxis_max
][2];
178 v2f joystick_states
[ k_srjoystick_max
][2];
179 u8 button_states
[ k_srbind_max
][2];
182 k_input_state_enabled
,
183 k_input_state_resume
,
184 k_input_state_resuming
,
191 static int input_filter_generic(void){
192 if( srinput
.state
!= k_input_state_enabled
|| vg_console
.enabled
)
198 static int buttons_filter_fixed(void){
199 if( input_filter_generic() )
202 if( vg
.engine_stage
== k_engine_stage_update_fixed
)
203 if( vg
.fixed_iterations
> 0 )
209 /* Rising edge of button */
210 static int button_down( enum sr_bind button
){
211 if( buttons_filter_fixed() ) return 0;
213 if( srinput
.button_states
[ button
][0] &&
214 !srinput
.button_states
[ button
][1] )
220 /* Falling edge of button */
221 static int button_up( enum sr_bind button
){
222 if( buttons_filter_fixed() ) return 0;
224 if( !srinput
.button_states
[ button
][0] &&
225 srinput
.button_states
[ button
][1] )
231 /* State of button */
232 static int button_press( enum sr_bind button
){
233 if( input_filter_generic() )
236 srinput
.button_states
[ button
][0];
239 static void joystick_state( enum sr_joystick joystick
, v2f state
){
240 if( input_filter_generic() )
243 v2_copy( srinput
.joystick_states
[ joystick
][0], state
);
246 static float axis_state( enum sr_axis axis
){
247 if( input_filter_generic() )
250 return srinput
.axis_states
[axis
][0];
253 static void skaterift_preupdate_inputs(void){
254 if( srinput
.state
== k_input_state_resuming
)
255 srinput
.state
= k_input_state_enabled
;
257 if( srinput
.state
== k_input_state_resume
)
258 srinput
.state
= k_input_state_resuming
;
260 for( u32 i
=0; i
<k_srbind_max
; i
++ ){
261 srinput
.button_states
[i
][1] = srinput
.button_states
[i
][0];
262 srinput
.button_states
[i
][0] = 0;
265 for( u32 i
=0; i
<k_srjoystick_max
; i
++ ){
266 v2_copy( srinput
.joystick_states
[i
][0], srinput
.joystick_states
[i
][1] );
267 v2_zero( srinput
.joystick_states
[i
][0] );
270 for( u32 i
=0; i
<k_sraxis_max
; i
++ ){
271 srinput
.axis_states
[i
][1] = srinput
.axis_states
[i
][0];
272 srinput
.axis_states
[i
][0] = 0.0f
;
275 for( int i
=0; i
<k_srbind_max
; i
++ ){
276 vg_input_op
*prog
= input_button_list
[i
];
278 vg_exec_input_program( k_vg_input_type_button_u8
, prog
,
279 &srinput
.button_states
[i
][0] );
283 for( int i
=0; i
<k_sraxis_max
; i
++ ){
284 vg_input_op
*prog
= input_axis_list
[i
];
286 vg_exec_input_program( k_vg_input_type_axis_f32
, prog
,
287 &srinput
.axis_states
[i
][0] );
291 for( int i
=0; i
<k_srjoystick_max
; i
++ ){
292 vg_input_op
*prog
= input_joy_list
[i
];
294 vg_exec_input_program( k_vg_input_type_joy_v2f
, prog
,
295 srinput
.joystick_states
[i
][0] );
299 f32 x
= srinput
.axis_states
[k_sraxis_mbrowse_h
][0],
300 y
= srinput
.axis_states
[k_sraxis_mbrowse_v
][0],
303 if( fabsf(x
) > sensitivity
){
304 if( x
> 0.0f
) srinput
.button_states
[k_srbind_mright
][0] = 1;
305 else srinput
.button_states
[k_srbind_mleft
][0] = 1;
308 if( fabsf(y
) > sensitivity
){
309 if( y
> 0.0f
) srinput
.button_states
[k_srbind_mup
][0] = 1;
310 else srinput
.button_states
[k_srbind_mdown
][0] = 1;