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
,
38 k_srbind_miniworld_teleport
,
39 k_srbind_miniworld_resume
,
45 k_srjoystick_steer
= 0,
60 #define INPUT_BASIC( KB, JS ) \
61 (vg_input_op[]){vg_keyboard, KB, vg_joy_button, JS, vg_end}
63 static vg_input_op
*input_button_list
[] = {
64 [k_srbind_jump
] = INPUT_BASIC( SDLK_SPACE
, SDL_CONTROLLER_BUTTON_A
),
65 [k_srbind_push
] = INPUT_BASIC( SDLK_w
, SDL_CONTROLLER_BUTTON_B
),
66 [k_srbind_trick0
] = (vg_input_op
[]){
67 vg_mouse
, SDL_BUTTON_LEFT
,
68 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
70 [k_srbind_trick1
] = (vg_input_op
[]){
71 vg_mouse
, SDL_BUTTON_RIGHT
,
72 vg_joy_button
, SDL_CONTROLLER_BUTTON_B
, vg_end
74 [k_srbind_trick2
] = (vg_input_op
[]){
75 vg_mouse
, SDL_BUTTON_LEFT
, vg_mode_mul
, vg_mouse
, SDL_BUTTON_RIGHT
,
76 vg_mode_absmax
, vg_joy_button
, SDL_CONTROLLER_BUTTON_X
, vg_end
78 [k_srbind_use
] = INPUT_BASIC( SDLK_e
, SDL_CONTROLLER_BUTTON_Y
),
79 [k_srbind_reset
] = INPUT_BASIC( SDLK_r
, SDL_CONTROLLER_BUTTON_LEFTSHOULDER
),
80 [k_srbind_camera
]= INPUT_BASIC( SDLK_c
, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
),
81 [k_srbind_mleft
] = INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
82 [k_srbind_mright
]= INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
83 [k_srbind_world_left
] =
84 INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
85 [k_srbind_world_right
] =
86 INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
87 [k_srbind_home
] = INPUT_BASIC( SDLK_h
, SDL_CONTROLLER_BUTTON_Y
),
88 [k_srbind_mup
] = INPUT_BASIC( SDLK_UP
, SDL_CONTROLLER_BUTTON_DPAD_UP
),
89 [k_srbind_mdown
] = INPUT_BASIC( SDLK_DOWN
, SDL_CONTROLLER_BUTTON_DPAD_DOWN
),
90 [k_srbind_mback
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_B
),
91 [k_srbind_mopen
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_START
),
92 [k_srbind_maccept
] = (vg_input_op
[]){
93 vg_keyboard
, SDLK_e
, vg_gui_visible
, 0,
94 vg_keyboard
, SDLK_RETURN
, vg_keyboard
, SDLK_RETURN2
,
96 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
98 [k_srbind_replay_play
] = INPUT_BASIC( SDLK_g
, SDL_CONTROLLER_BUTTON_X
),
99 [k_srbind_replay_resume
] = INPUT_BASIC( SDLK_SPACE
, SDL_CONTROLLER_BUTTON_A
),
100 [k_srbind_replay_freecam
] = INPUT_BASIC( SDLK_f
, SDL_CONTROLLER_BUTTON_Y
),
101 [k_srbind_sit
] = INPUT_BASIC( SDLK_z
, SDL_CONTROLLER_BUTTON_B
),
102 [k_srbind_lobby
] = INPUT_BASIC( SDLK_TAB
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
103 [k_srbind_chat
] = (vg_input_op
[]){ vg_keyboard
, SDLK_y
, vg_end
},
105 [k_srbind_miniworld_resume
] = (vg_input_op
[]){
106 vg_keyboard
, SDLK_RETURN
, vg_gui_visible
, 0,
107 vg_keyboard
, SDLK_RETURN2
,
109 vg_joy_button
, SDL_CONTROLLER_BUTTON_X
, vg_end
111 [k_srbind_miniworld_teleport
]= INPUT_BASIC( SDLK_r
, SDL_CONTROLLER_BUTTON_X
),
116 static vg_input_op
*input_axis_list
[] = {
117 [k_sraxis_grab
] = (vg_input_op
[]){
118 vg_keyboard
, SDLK_LSHIFT
,
119 vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
, vg_end
121 [k_sraxis_mbrowse_h
] = (vg_input_op
[]){
122 vg_mode_sub
, vg_keyboard
, SDLK_LEFT
,
123 vg_mode_add
, vg_keyboard
, SDLK_RIGHT
,
124 vg_mode_absmax
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTX
,
127 [k_sraxis_mbrowse_v
] = (vg_input_op
[]){
128 vg_mode_add
, vg_keyboard
, SDLK_UP
,
129 vg_mode_sub
, vg_keyboard
, SDLK_DOWN
,
130 vg_mode_absmax
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTY
,
133 [k_sraxis_replay_h
] = (vg_input_op
[]){
134 vg_mode_add
, vg_keyboard
, SDLK_e
,
135 vg_mode_sub
, vg_keyboard
, SDLK_q
,
136 vg_mode_add
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
,
137 vg_mode_sub
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERLEFT
,
143 static vg_input_op
*input_joy_list
[] = {
144 [k_srjoystick_steer
] = (vg_input_op
[]){
145 vg_index
, 0, vg_mode_add
, vg_keyboard
, SDLK_d
,
146 vg_mode_sub
, vg_keyboard
, SDLK_a
,
147 vg_index
, 1, vg_mode_add
, vg_keyboard
, SDLK_s
,
148 vg_mode_sub
, vg_keyboard
, SDLK_w
,
149 vg_mode_absmax
, vg_joy_ls
,
152 [k_srjoystick_grab
] = (vg_input_op
[]){
155 [k_srjoystick_look
] = (vg_input_op
[]){
158 [k_srjoystick_max
]=NULL
162 float axis_states
[ k_sraxis_max
][2];
163 v2f joystick_states
[ k_srjoystick_max
][2];
164 u8 button_states
[ k_srbind_max
][2];
167 k_input_state_enabled
,
168 k_input_state_resume
,
169 k_input_state_resuming
,
176 static int input_filter_generic(void){
177 if( srinput
.state
!= k_input_state_enabled
||
178 vg_ui
.wants_mouse
|| vg_console
.enabled
)
184 static int buttons_filter_fixed(void){
185 if( input_filter_generic() )
188 if( vg
.engine_stage
== k_engine_stage_update_fixed
)
189 if( vg
.fixed_iterations
> 0 )
195 /* Rising edge of button */
196 static int button_down( enum sr_bind button
){
197 if( buttons_filter_fixed() ) return 0;
199 if( srinput
.button_states
[ button
][0] &&
200 !srinput
.button_states
[ button
][1] )
206 /* Falling edge of button */
207 static int button_up( 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 /* State of button */
218 static int button_press( enum sr_bind button
){
219 if( input_filter_generic() )
222 srinput
.button_states
[ button
][0];
225 static void joystick_state( enum sr_joystick joystick
, v2f state
){
226 if( input_filter_generic() )
229 v2_copy( srinput
.joystick_states
[ joystick
][0], state
);
232 static float axis_state( enum sr_axis axis
){
233 if( input_filter_generic() )
236 return srinput
.axis_states
[axis
][0];
239 static void skaterift_preupdate_inputs(void){
240 if( srinput
.state
== k_input_state_resuming
)
241 srinput
.state
= k_input_state_enabled
;
243 if( srinput
.state
== k_input_state_resume
)
244 srinput
.state
= k_input_state_resuming
;
246 for( u32 i
=0; i
<k_srbind_max
; i
++ ){
247 srinput
.button_states
[i
][1] = srinput
.button_states
[i
][0];
248 srinput
.button_states
[i
][0] = 0;
251 for( u32 i
=0; i
<k_srjoystick_max
; i
++ ){
252 v2_copy( srinput
.joystick_states
[i
][0], srinput
.joystick_states
[i
][1] );
253 v2_zero( srinput
.joystick_states
[i
][0] );
256 for( u32 i
=0; i
<k_sraxis_max
; i
++ ){
257 srinput
.axis_states
[i
][1] = srinput
.axis_states
[i
][0];
258 srinput
.axis_states
[i
][0] = 0.0f
;
261 for( int i
=0; i
<k_srbind_max
; i
++ ){
262 vg_input_op
*prog
= input_button_list
[i
];
264 vg_exec_input_program( k_vg_input_type_button_u8
, prog
,
265 &srinput
.button_states
[i
][0] );
269 for( int i
=0; i
<k_sraxis_max
; i
++ ){
270 vg_input_op
*prog
= input_axis_list
[i
];
272 vg_exec_input_program( k_vg_input_type_axis_f32
, prog
,
273 &srinput
.axis_states
[i
][0] );
277 for( int i
=0; i
<k_srjoystick_max
; i
++ ){
278 vg_input_op
*prog
= input_joy_list
[i
];
280 vg_exec_input_program( k_vg_input_type_joy_v2f
, prog
,
281 srinput
.joystick_states
[i
][0] );
285 f32 x
= srinput
.axis_states
[k_sraxis_mbrowse_h
][0],
286 y
= srinput
.axis_states
[k_sraxis_mbrowse_v
][0],
289 if( fabsf(x
) > sensitivity
){
290 if( x
> 0.0f
) srinput
.button_states
[k_srbind_mright
][0] = 1;
291 else srinput
.button_states
[k_srbind_mleft
][0] = 1;
294 if( fabsf(y
) > sensitivity
){
295 if( y
> 0.0f
) srinput
.button_states
[k_srbind_mup
][0] = 1;
296 else srinput
.button_states
[k_srbind_mdown
][0] = 1;