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
,
41 k_srbind_miniworld_teleport
,
42 k_srbind_miniworld_resume
,
48 k_srjoystick_steer
= 0,
64 #define INPUT_BASIC( KB, JS ) \
65 (vg_input_op[]){vg_keyboard, KB, vg_joy_button, JS, vg_end}
67 static vg_input_op
*input_button_list
[] = {
68 [k_srbind_jump
] = INPUT_BASIC( SDLK_SPACE
, SDL_CONTROLLER_BUTTON_A
),
69 [k_srbind_push
] = INPUT_BASIC( SDLK_w
, SDL_CONTROLLER_BUTTON_B
),
70 [k_srbind_trick0
] = (vg_input_op
[]){
71 vg_mouse
, SDL_BUTTON_LEFT
,
72 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
74 [k_srbind_trick1
] = (vg_input_op
[]){
75 vg_mouse
, SDL_BUTTON_RIGHT
,
76 vg_joy_button
, SDL_CONTROLLER_BUTTON_B
, vg_end
78 [k_srbind_trick2
] = (vg_input_op
[]){
79 vg_mouse
, SDL_BUTTON_LEFT
, vg_mode_mul
, vg_mouse
, SDL_BUTTON_RIGHT
,
80 vg_mode_absmax
, vg_joy_button
, SDL_CONTROLLER_BUTTON_X
, vg_end
82 [k_srbind_use
] = INPUT_BASIC( SDLK_e
, SDL_CONTROLLER_BUTTON_Y
),
83 [k_srbind_reset
] = INPUT_BASIC( SDLK_r
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
84 [k_srbind_dead_respawn
] =
85 INPUT_BASIC( SDLK_q
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
86 [k_srbind_camera
]= INPUT_BASIC( SDLK_c
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
87 [k_srbind_mleft
] = INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
88 [k_srbind_mright
]= INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
89 [k_srbind_world_left
] =
90 INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
91 [k_srbind_world_right
] =
92 INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
93 [k_srbind_home
] = INPUT_BASIC( SDLK_h
, SDL_CONTROLLER_BUTTON_Y
),
94 [k_srbind_mup
] = INPUT_BASIC( SDLK_UP
, SDL_CONTROLLER_BUTTON_DPAD_UP
),
95 [k_srbind_mdown
] = INPUT_BASIC( SDLK_DOWN
, SDL_CONTROLLER_BUTTON_DPAD_DOWN
),
96 [k_srbind_mback
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_B
),
97 [k_srbind_mopen
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_START
),
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
] = INPUT_BASIC( SDLK_LSHIFT
, SDL_CONTROLLER_BUTTON_X
),
112 [k_srbind_miniworld_resume
] = (vg_input_op
[]){
113 vg_keyboard
, SDLK_RETURN
, vg_gui_visible
, 0,
114 vg_keyboard
, SDLK_RETURN2
,
116 vg_joy_button
, SDL_CONTROLLER_BUTTON_X
, vg_end
118 [k_srbind_miniworld_teleport
]= INPUT_BASIC( SDLK_q
,
119 SDL_CONTROLLER_BUTTON_LEFTSHOULDER
),
120 [k_srbind_skid
] = (vg_input_op
[]){ vg_keyboard
, SDLK_LCTRL
, vg_end
},
121 [k_srbind_devbutton
] = (vg_input_op
[]){ vg_keyboard
, SDLK_3
, vg_end
},
125 static vg_input_op
*input_axis_list
[] = {
126 [k_sraxis_grab
] = (vg_input_op
[]){
127 vg_keyboard
, SDLK_LSHIFT
,
128 vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
, vg_end
130 [k_sraxis_mbrowse_h
] = (vg_input_op
[]){
131 vg_mode_sub
, vg_keyboard
, SDLK_LEFT
,
132 vg_mode_add
, vg_keyboard
, SDLK_RIGHT
,
133 vg_mode_add
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTX
,
136 [k_sraxis_mbrowse_v
] = (vg_input_op
[]){
137 vg_mode_add
, vg_keyboard
, SDLK_UP
,
138 vg_mode_sub
, vg_keyboard
, SDLK_DOWN
,
139 vg_mode_sub
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTY
,
142 [k_sraxis_replay_h
] = (vg_input_op
[]){
143 vg_mode_add
, vg_keyboard
, SDLK_e
,
144 vg_mode_sub
, vg_keyboard
, SDLK_q
,
145 vg_mode_add
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
,
146 vg_mode_sub
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERLEFT
,
149 [k_sraxis_skid
] = (vg_input_op
[]){
150 vg_mode_add
, vg_joy_button
, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
,
151 vg_mode_sub
, vg_joy_button
, SDL_CONTROLLER_BUTTON_LEFTSHOULDER
,
157 static vg_input_op
*input_joy_list
[] = {
158 [k_srjoystick_steer
] = (vg_input_op
[]){
159 vg_index
, 0, vg_mode_add
, vg_keyboard
, SDLK_d
,
160 vg_mode_sub
, vg_keyboard
, SDLK_a
,
161 vg_index
, 1, vg_mode_add
, vg_keyboard
, SDLK_s
,
162 vg_mode_sub
, vg_keyboard
, SDLK_w
,
163 vg_mode_absmax
, vg_joy_ls
,
166 [k_srjoystick_grab
] = (vg_input_op
[]){
169 [k_srjoystick_look
] = (vg_input_op
[]){
172 [k_srjoystick_max
]=NULL
176 float axis_states
[ k_sraxis_max
][2];
177 v2f joystick_states
[ k_srjoystick_max
][2];
178 u8 button_states
[ k_srbind_max
][2];
181 k_input_state_enabled
,
182 k_input_state_resume
,
183 k_input_state_resuming
,
190 static int input_filter_generic(void){
191 if( srinput
.state
!= k_input_state_enabled
||
192 vg_ui
.wants_mouse
|| 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;