6 #include "vg/vg_platform.h"
7 #include "vg/vg_console.h"
8 #include "vg/vg_input.h"
22 k_srbind_dead_respawn
,
32 k_srbind_replay_freecam
,
33 k_srbind_replay_resume
,
40 k_srbind_miniworld_teleport
,
41 k_srbind_miniworld_resume
,
47 k_srjoystick_steer
= 0,
63 #define INPUT_BASIC( KB, JS ) \
64 (vg_input_op[]){vg_keyboard, KB, vg_joy_button, JS, vg_end}
66 static vg_input_op
*input_button_list
[] = {
67 [k_srbind_jump
] = INPUT_BASIC( SDLK_SPACE
, SDL_CONTROLLER_BUTTON_A
),
68 [k_srbind_push
] = INPUT_BASIC( SDLK_w
, SDL_CONTROLLER_BUTTON_B
),
69 [k_srbind_trick0
] = (vg_input_op
[]){
70 vg_mouse
, SDL_BUTTON_LEFT
,
71 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
73 [k_srbind_trick1
] = (vg_input_op
[]){
74 vg_mouse
, SDL_BUTTON_RIGHT
,
75 vg_joy_button
, SDL_CONTROLLER_BUTTON_B
, vg_end
77 [k_srbind_trick2
] = (vg_input_op
[]){
78 vg_mouse
, SDL_BUTTON_LEFT
, vg_mode_mul
, vg_mouse
, SDL_BUTTON_RIGHT
,
79 vg_mode_absmax
, vg_joy_button
, SDL_CONTROLLER_BUTTON_X
, vg_end
81 [k_srbind_use
] = INPUT_BASIC( SDLK_e
, SDL_CONTROLLER_BUTTON_Y
),
82 [k_srbind_reset
] = INPUT_BASIC( SDLK_r
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
83 [k_srbind_dead_respawn
] =
84 INPUT_BASIC( SDLK_q
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
85 [k_srbind_camera
]= INPUT_BASIC( SDLK_c
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
86 [k_srbind_mleft
] = INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
87 [k_srbind_mright
]= INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
88 [k_srbind_world_left
] =
89 INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
90 [k_srbind_world_right
] =
91 INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
92 [k_srbind_home
] = INPUT_BASIC( SDLK_h
, SDL_CONTROLLER_BUTTON_Y
),
93 [k_srbind_mup
] = INPUT_BASIC( SDLK_UP
, SDL_CONTROLLER_BUTTON_DPAD_UP
),
94 [k_srbind_mdown
] = INPUT_BASIC( SDLK_DOWN
, SDL_CONTROLLER_BUTTON_DPAD_DOWN
),
95 [k_srbind_mback
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_B
),
96 [k_srbind_mopen
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_START
),
97 [k_srbind_maccept
] = (vg_input_op
[]){
98 vg_keyboard
, SDLK_e
, vg_gui_visible
, 0,
99 vg_keyboard
, SDLK_RETURN
, vg_keyboard
, SDLK_RETURN2
,
101 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
103 [k_srbind_replay_play
] = INPUT_BASIC( SDLK_g
, SDL_CONTROLLER_BUTTON_X
),
104 [k_srbind_replay_resume
] = INPUT_BASIC( SDLK_SPACE
, SDL_CONTROLLER_BUTTON_A
),
105 [k_srbind_replay_freecam
] = INPUT_BASIC( SDLK_f
, SDL_CONTROLLER_BUTTON_Y
),
106 [k_srbind_sit
] = INPUT_BASIC( SDLK_z
, SDL_CONTROLLER_BUTTON_B
),
107 [k_srbind_lobby
] = INPUT_BASIC( SDLK_TAB
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
108 [k_srbind_chat
] = (vg_input_op
[]){ vg_keyboard
, SDLK_y
, 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
},
122 static vg_input_op
*input_axis_list
[] = {
123 [k_sraxis_grab
] = (vg_input_op
[]){
124 vg_keyboard
, SDLK_LSHIFT
,
125 vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
, vg_end
127 [k_sraxis_mbrowse_h
] = (vg_input_op
[]){
128 vg_mode_sub
, vg_keyboard
, SDLK_LEFT
,
129 vg_mode_add
, vg_keyboard
, SDLK_RIGHT
,
130 vg_mode_add
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTX
,
133 [k_sraxis_mbrowse_v
] = (vg_input_op
[]){
134 vg_mode_add
, vg_keyboard
, SDLK_UP
,
135 vg_mode_sub
, vg_keyboard
, SDLK_DOWN
,
136 vg_mode_sub
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTY
,
139 [k_sraxis_replay_h
] = (vg_input_op
[]){
140 vg_mode_add
, vg_keyboard
, SDLK_e
,
141 vg_mode_sub
, vg_keyboard
, SDLK_q
,
142 vg_mode_add
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
,
143 vg_mode_sub
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERLEFT
,
146 [k_sraxis_skid
] = (vg_input_op
[]){
147 vg_mode_add
, vg_joy_button
, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
,
148 vg_mode_sub
, vg_joy_button
, SDL_CONTROLLER_BUTTON_LEFTSHOULDER
,
154 static vg_input_op
*input_joy_list
[] = {
155 [k_srjoystick_steer
] = (vg_input_op
[]){
156 vg_index
, 0, vg_mode_add
, vg_keyboard
, SDLK_d
,
157 vg_mode_sub
, vg_keyboard
, SDLK_a
,
158 vg_index
, 1, vg_mode_add
, vg_keyboard
, SDLK_s
,
159 vg_mode_sub
, vg_keyboard
, SDLK_w
,
160 vg_mode_absmax
, vg_joy_ls
,
163 [k_srjoystick_grab
] = (vg_input_op
[]){
166 [k_srjoystick_look
] = (vg_input_op
[]){
169 [k_srjoystick_max
]=NULL
173 float axis_states
[ k_sraxis_max
][2];
174 v2f joystick_states
[ k_srjoystick_max
][2];
175 u8 button_states
[ k_srbind_max
][2];
178 k_input_state_enabled
,
179 k_input_state_resume
,
180 k_input_state_resuming
,
187 static int input_filter_generic(void){
188 if( srinput
.state
!= k_input_state_enabled
||
189 vg_ui
.wants_mouse
|| 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;