2 #include "vg/vg_platform.h"
3 #include "vg/vg_console.h"
4 #include "vg/vg_input.h"
18 k_srbind_dead_respawn
,
28 k_srbind_replay_freecam
,
29 k_srbind_replay_resume
,
37 k_srbind_miniworld_teleport
,
38 k_srbind_miniworld_resume
,
44 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_DPAD_LEFT
),
80 [k_srbind_dead_respawn
] =
81 INPUT_BASIC( SDLK_q
, SDL_CONTROLLER_BUTTON_DPAD_UP
),
82 [k_srbind_camera
]= INPUT_BASIC( SDLK_c
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
83 [k_srbind_mleft
] = INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
84 [k_srbind_mright
]= INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
85 [k_srbind_world_left
] =
86 INPUT_BASIC( SDLK_LEFT
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
87 [k_srbind_world_right
] =
88 INPUT_BASIC( SDLK_RIGHT
, SDL_CONTROLLER_BUTTON_DPAD_RIGHT
),
89 [k_srbind_home
] = INPUT_BASIC( SDLK_h
, SDL_CONTROLLER_BUTTON_Y
),
90 [k_srbind_mup
] = INPUT_BASIC( SDLK_UP
, SDL_CONTROLLER_BUTTON_DPAD_UP
),
91 [k_srbind_mdown
] = INPUT_BASIC( SDLK_DOWN
, SDL_CONTROLLER_BUTTON_DPAD_DOWN
),
92 [k_srbind_mback
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_B
),
93 [k_srbind_mopen
] = INPUT_BASIC( SDLK_ESCAPE
, SDL_CONTROLLER_BUTTON_START
),
94 [k_srbind_maccept
] = (vg_input_op
[]){
95 vg_keyboard
, SDLK_e
, vg_gui_visible
, 0,
96 vg_keyboard
, SDLK_RETURN
, vg_keyboard
, SDLK_RETURN2
,
98 vg_joy_button
, SDL_CONTROLLER_BUTTON_A
, vg_end
100 [k_srbind_replay_play
] = INPUT_BASIC( SDLK_g
, SDL_CONTROLLER_BUTTON_X
),
101 [k_srbind_replay_resume
] = INPUT_BASIC( SDLK_SPACE
, SDL_CONTROLLER_BUTTON_A
),
102 [k_srbind_replay_freecam
] = INPUT_BASIC( SDLK_f
, SDL_CONTROLLER_BUTTON_Y
),
103 [k_srbind_sit
] = INPUT_BASIC( SDLK_z
, SDL_CONTROLLER_BUTTON_B
),
104 [k_srbind_lobby
] = INPUT_BASIC( SDLK_TAB
, SDL_CONTROLLER_BUTTON_DPAD_LEFT
),
105 [k_srbind_chat
] = (vg_input_op
[]){ vg_keyboard
, SDLK_y
, vg_end
},
106 [k_srbind_run
] = (vg_input_op
[]){ vg_keyboard
, SDLK_LSHIFT
,
107 vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERLEFT
, vg_end
},
109 [k_srbind_miniworld_resume
] = (vg_input_op
[]){
110 vg_keyboard
, SDLK_RETURN
, vg_gui_visible
, 0,
111 vg_keyboard
, SDLK_RETURN2
,
113 vg_joy_button
, SDL_CONTROLLER_BUTTON_X
, vg_end
115 [k_srbind_miniworld_teleport
]= INPUT_BASIC( SDLK_q
,
116 SDL_CONTROLLER_BUTTON_LEFTSHOULDER
),
117 [k_srbind_skid
] = (vg_input_op
[]){ vg_keyboard
, SDLK_LCTRL
, vg_end
},
118 [k_srbind_devbutton
] = (vg_input_op
[]){ vg_keyboard
, SDLK_3
, 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
|| vg
.settings_open
)
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;