2 #include "vg/vg_platform.h"
3 #include "vg/vg_console.h"
4 #include "vg/vg_input.h"
19 k_srbind_dead_respawn
,
29 k_srbind_replay_freecam
,
30 k_srbind_replay_resume
,
38 k_srbind_miniworld_teleport
,
39 k_srbind_miniworld_resume
,
45 k_srjoystick_steer
= 0,
61 #define INPUT_BASIC( KB, JS ) \
62 (vg_input_op[]){vg_keyboard, KB, vg_joy_button, JS, vg_end}
64 static vg_input_op
*input_button_list
[] = {
65 [k_srbind_jump
] = INPUT_BASIC( SDLK_SPACE
, SDL_CONTROLLER_BUTTON_A
),
66 [k_srbind_push
] = INPUT_BASIC( SDLK_w
, SDL_CONTROLLER_BUTTON_B
),
67 [k_srbind_trick0
] = (vg_input_op
[]){
68 vg_mouse
, SDL_BUTTON_LEFT
,
69 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
71 [k_srbind_trick1
] = (vg_input_op
[]){
72 vg_mouse
, SDL_BUTTON_RIGHT
,
73 vg_joy_button
, SDL_CONTROLLER_BUTTON_B
, vg_end
75 [k_srbind_trick2
] = (vg_input_op
[]){
76 vg_mouse
, SDL_BUTTON_LEFT
, vg_mode_mul
, vg_mouse
, SDL_BUTTON_RIGHT
,
77 vg_mode_absmax
, vg_joy_button
, SDL_CONTROLLER_BUTTON_X
, vg_end
79 [k_srbind_use
] = INPUT_BASIC( SDLK_e
, SDL_CONTROLLER_BUTTON_Y
),
80 [k_srbind_reset
] = INPUT_BASIC( SDLK_r
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
81 [k_srbind_dead_respawn
] =
82 INPUT_BASIC( SDLK_q
, SDL_CONTROLLER_BUTTON_DPAD_UP
),
83 [k_srbind_camera
]= INPUT_BASIC( SDLK_c
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
84 [k_srbind_mleft
] = INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
85 [k_srbind_mright
]= INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
86 [k_srbind_world_left
] =
87 INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
88 [k_srbind_world_right
] =
89 INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
90 [k_srbind_home
] = INPUT_BASIC( SDLK_h
, SDL_CONTROLLER_BUTTON_Y
),
91 [k_srbind_mup
] = INPUT_BASIC( SDLK_UP
, SDL_CONTROLLER_BUTTON_DPAD_UP
),
92 [k_srbind_mdown
] = INPUT_BASIC( SDLK_DOWN
, SDL_CONTROLLER_BUTTON_DPAD_DOWN
),
93 [k_srbind_mback
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_B
),
94 [k_srbind_mopen
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_START
),
95 [k_srbind_maccept
] = (vg_input_op
[]){
96 vg_keyboard
, SDLK_e
, vg_gui_visible
, 0,
97 vg_keyboard
, SDLK_RETURN
, vg_keyboard
, SDLK_RETURN2
,
99 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
101 [k_srbind_replay_play
] = INPUT_BASIC( SDLK_g
, SDL_CONTROLLER_BUTTON_X
),
102 [k_srbind_replay_resume
] = INPUT_BASIC( SDLK_SPACE
, SDL_CONTROLLER_BUTTON_A
),
103 [k_srbind_replay_freecam
] = INPUT_BASIC( SDLK_f
, SDL_CONTROLLER_BUTTON_Y
),
104 [k_srbind_sit
] = INPUT_BASIC( SDLK_z
, SDL_CONTROLLER_BUTTON_B
),
105 [k_srbind_lobby
] = INPUT_BASIC( SDLK_TAB
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
106 [k_srbind_chat
] = (vg_input_op
[]){ vg_keyboard
, SDLK_y
, vg_end
},
107 [k_srbind_run
] = (vg_input_op
[]){ vg_keyboard
, SDLK_LSHIFT
,
108 vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERLEFT
, vg_end
},
110 [k_srbind_miniworld_resume
] = (vg_input_op
[]){
111 vg_keyboard
, SDLK_RETURN
, vg_gui_visible
, 0,
112 vg_keyboard
, SDLK_RETURN2
,
114 vg_joy_button
, SDL_CONTROLLER_BUTTON_X
, vg_end
116 [k_srbind_miniworld_teleport
]= INPUT_BASIC( SDLK_q
,
117 SDL_CONTROLLER_BUTTON_LEFTSHOULDER
),
118 [k_srbind_skid
] = (vg_input_op
[]){ vg_keyboard
, SDLK_LCTRL
, vg_end
},
119 [k_srbind_devbutton
] = (vg_input_op
[]){ vg_keyboard
, SDLK_3
, vg_end
},
123 static vg_input_op
*input_axis_list
[] = {
124 [k_sraxis_grab
] = (vg_input_op
[]){
125 vg_keyboard
, SDLK_LSHIFT
,
126 vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
, vg_end
128 [k_sraxis_mbrowse_h
] = (vg_input_op
[]){
129 vg_mode_sub
, vg_keyboard
, SDLK_LEFT
,
130 vg_mode_add
, vg_keyboard
, SDLK_RIGHT
,
131 vg_mode_add
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTX
,
134 [k_sraxis_mbrowse_v
] = (vg_input_op
[]){
135 vg_mode_sub
, vg_keyboard
, SDLK_DOWN
,
136 vg_mode_add
, vg_keyboard
, SDLK_UP
,
137 vg_mode_sub
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTY
,
140 [k_sraxis_replay_h
] = (vg_input_op
[]){
141 vg_mode_sub
, vg_keyboard
, SDLK_q
,
142 vg_mode_add
, vg_keyboard
, SDLK_e
,
143 vg_mode_sub
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERLEFT
,
144 vg_mode_add
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
,
147 [k_sraxis_skid
] = (vg_input_op
[]){
148 vg_mode_sub
, vg_joy_button
, SDL_CONTROLLER_BUTTON_LEFTSHOULDER
,
149 vg_mode_add
, vg_joy_button
, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
,
155 static vg_input_op
*input_joy_list
[] = {
156 [k_srjoystick_steer
] = (vg_input_op
[]){
157 vg_index
, 0, vg_mode_sub
, vg_keyboard
, SDLK_a
,
158 vg_mode_add
, vg_keyboard
, SDLK_d
,
159 vg_index
, 1, vg_mode_sub
, vg_keyboard
, SDLK_w
,
160 vg_mode_add
, vg_keyboard
, SDLK_s
,
161 vg_mode_absmax
, vg_joy_ls
,
164 [k_srjoystick_grab
] = (vg_input_op
[]){
167 [k_srjoystick_look
] = (vg_input_op
[]){
170 [k_srjoystick_max
]=NULL
174 float axis_states
[ k_sraxis_max
][2];
175 v2f joystick_states
[ k_srjoystick_max
][2];
176 u8 button_states
[ k_srbind_max
][2];
179 k_input_state_enabled
,
180 k_input_state_resume
,
181 k_input_state_resuming
,
188 static int input_filter_generic(void){
189 if( srinput
.state
!= k_input_state_enabled
|| vg_console
.enabled
)
195 static int buttons_filter_fixed(void){
196 if( input_filter_generic() )
199 if( vg
.engine_stage
== k_engine_stage_update_fixed
)
200 if( vg
.fixed_iterations
> 0 )
206 /* Rising edge of button */
207 static int button_down( enum sr_bind button
){
208 if( buttons_filter_fixed() ) return 0;
210 if( srinput
.button_states
[ button
][0] &&
211 !srinput
.button_states
[ button
][1] )
217 /* Falling edge of button */
218 static int button_up( enum sr_bind button
){
219 if( buttons_filter_fixed() ) return 0;
221 if( !srinput
.button_states
[ button
][0] &&
222 srinput
.button_states
[ button
][1] )
228 /* State of button */
229 static int button_press( enum sr_bind button
){
230 if( input_filter_generic() )
233 srinput
.button_states
[ button
][0];
236 static void joystick_state( enum sr_joystick joystick
, v2f state
){
237 if( input_filter_generic() )
240 v2_copy( srinput
.joystick_states
[ joystick
][0], state
);
243 static float axis_state( enum sr_axis axis
){
244 if( input_filter_generic() )
247 return srinput
.axis_states
[axis
][0];
250 static void skaterift_preupdate_inputs(void){
251 if( srinput
.state
== k_input_state_resuming
)
252 srinput
.state
= k_input_state_enabled
;
254 if( srinput
.state
== k_input_state_resume
)
255 srinput
.state
= k_input_state_resuming
;
257 for( u32 i
=0; i
<k_srbind_max
; i
++ ){
258 srinput
.button_states
[i
][1] = srinput
.button_states
[i
][0];
259 srinput
.button_states
[i
][0] = 0;
262 for( u32 i
=0; i
<k_srjoystick_max
; i
++ ){
263 v2_copy( srinput
.joystick_states
[i
][0], srinput
.joystick_states
[i
][1] );
264 v2_zero( srinput
.joystick_states
[i
][0] );
267 for( u32 i
=0; i
<k_sraxis_max
; i
++ ){
268 srinput
.axis_states
[i
][1] = srinput
.axis_states
[i
][0];
269 srinput
.axis_states
[i
][0] = 0.0f
;
272 for( int i
=0; i
<k_srbind_max
; i
++ ){
273 vg_input_op
*prog
= input_button_list
[i
];
275 vg_exec_input_program( k_vg_input_type_button_u8
, prog
,
276 &srinput
.button_states
[i
][0] );
280 for( int i
=0; i
<k_sraxis_max
; i
++ ){
281 vg_input_op
*prog
= input_axis_list
[i
];
283 vg_exec_input_program( k_vg_input_type_axis_f32
, prog
,
284 &srinput
.axis_states
[i
][0] );
288 for( int i
=0; i
<k_srjoystick_max
; i
++ ){
289 vg_input_op
*prog
= input_joy_list
[i
];
291 vg_exec_input_program( k_vg_input_type_joy_v2f
, prog
,
292 srinput
.joystick_states
[i
][0] );
296 f32 x
= srinput
.axis_states
[k_sraxis_mbrowse_h
][0],
297 y
= srinput
.axis_states
[k_sraxis_mbrowse_v
][0],
300 if( fabsf(x
) > sensitivity
){
301 if( x
> 0.0f
) srinput
.button_states
[k_srbind_mright
][0] = 1;
302 else srinput
.button_states
[k_srbind_mleft
][0] = 1;
305 if( fabsf(y
) > sensitivity
){
306 if( y
> 0.0f
) srinput
.button_states
[k_srbind_mup
][0] = 1;
307 else srinput
.button_states
[k_srbind_mdown
][0] = 1;