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
] = INPUT_BASIC( SDLK_e
, SDL_CONTROLLER_BUTTON_A
),
106 [k_srbind_miniworld_teleport
]= INPUT_BASIC( SDLK_r
, SDL_CONTROLLER_BUTTON_X
),
111 static vg_input_op
*input_axis_list
[] = {
112 [k_sraxis_grab
] = (vg_input_op
[]){
113 vg_keyboard
, SDLK_LSHIFT
,
114 vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
, vg_end
116 [k_sraxis_mbrowse_h
] = (vg_input_op
[]){
117 vg_mode_sub
, vg_keyboard
, SDLK_LEFT
,
118 vg_mode_add
, vg_keyboard
, SDLK_RIGHT
,
119 vg_mode_absmax
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTX
,
122 [k_sraxis_mbrowse_v
] = (vg_input_op
[]){
123 vg_mode_add
, vg_keyboard
, SDLK_UP
,
124 vg_mode_sub
, vg_keyboard
, SDLK_DOWN
,
125 vg_mode_absmax
, vg_joy_axis
, SDL_CONTROLLER_AXIS_LEFTY
,
128 [k_sraxis_replay_h
] = (vg_input_op
[]){
129 vg_mode_add
, vg_keyboard
, SDLK_e
,
130 vg_mode_sub
, vg_keyboard
, SDLK_q
,
131 vg_mode_add
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERRIGHT
,
132 vg_mode_sub
, vg_joy_axis
, SDL_CONTROLLER_AXIS_TRIGGERLEFT
,
138 static vg_input_op
*input_joy_list
[] = {
139 [k_srjoystick_steer
] = (vg_input_op
[]){
140 vg_index
, 0, vg_mode_add
, vg_keyboard
, SDLK_d
,
141 vg_mode_sub
, vg_keyboard
, SDLK_a
,
142 vg_index
, 1, vg_mode_add
, vg_keyboard
, SDLK_s
,
143 vg_mode_sub
, vg_keyboard
, SDLK_w
,
144 vg_mode_absmax
, vg_joy_ls
,
147 [k_srjoystick_grab
] = (vg_input_op
[]){
150 [k_srjoystick_look
] = (vg_input_op
[]){
153 [k_srjoystick_max
]=NULL
157 float axis_states
[ k_sraxis_max
][2];
158 v2f joystick_states
[ k_srjoystick_max
][2];
159 u8 button_states
[ k_srbind_max
][2];
162 k_input_state_enabled
,
163 k_input_state_resume
,
164 k_input_state_resuming
,
171 static int input_filter_generic(void){
172 if( srinput
.state
!= k_input_state_enabled
||
173 vg_ui
.wants_mouse
|| vg_console
.enabled
)
179 static int buttons_filter_fixed(void){
180 if( input_filter_generic() )
183 if( vg
.engine_stage
== k_engine_stage_update_fixed
)
184 if( vg
.fixed_iterations
> 0 )
190 /* Rising edge of button */
191 static int button_down( enum sr_bind button
){
192 if( buttons_filter_fixed() ) return 0;
194 if( srinput
.button_states
[ button
][0] &&
195 !srinput
.button_states
[ button
][1] )
201 /* Falling edge of button */
202 static int button_up( enum sr_bind button
){
203 if( buttons_filter_fixed() ) return 0;
205 if( !srinput
.button_states
[ button
][0] &&
206 srinput
.button_states
[ button
][1] )
212 /* State of button */
213 static int button_press( enum sr_bind button
){
214 if( input_filter_generic() )
217 srinput
.button_states
[ button
][0];
220 static void joystick_state( enum sr_joystick joystick
, v2f state
){
221 if( input_filter_generic() )
224 v2_copy( srinput
.joystick_states
[ joystick
][0], state
);
227 static float axis_state( enum sr_axis axis
){
228 if( input_filter_generic() )
231 return srinput
.axis_states
[axis
][0];
234 static void skaterift_preupdate_inputs(void){
235 if( srinput
.state
== k_input_state_resuming
)
236 srinput
.state
= k_input_state_enabled
;
238 if( srinput
.state
== k_input_state_resume
)
239 srinput
.state
= k_input_state_resuming
;
241 for( u32 i
=0; i
<k_srbind_max
; i
++ ){
242 srinput
.button_states
[i
][1] = srinput
.button_states
[i
][0];
243 srinput
.button_states
[i
][0] = 0;
246 for( u32 i
=0; i
<k_srjoystick_max
; i
++ ){
247 v2_copy( srinput
.joystick_states
[i
][0], srinput
.joystick_states
[i
][1] );
248 v2_zero( srinput
.joystick_states
[i
][0] );
251 for( u32 i
=0; i
<k_sraxis_max
; i
++ ){
252 srinput
.axis_states
[i
][1] = srinput
.axis_states
[i
][0];
253 srinput
.axis_states
[i
][0] = 0.0f
;
256 for( int i
=0; i
<k_srbind_max
; i
++ ){
257 vg_input_op
*prog
= input_button_list
[i
];
259 vg_exec_input_program( k_vg_input_type_button_u8
, prog
,
260 &srinput
.button_states
[i
][0] );
264 for( int i
=0; i
<k_sraxis_max
; i
++ ){
265 vg_input_op
*prog
= input_axis_list
[i
];
267 vg_exec_input_program( k_vg_input_type_axis_f32
, prog
,
268 &srinput
.axis_states
[i
][0] );
272 for( int i
=0; i
<k_srjoystick_max
; i
++ ){
273 vg_input_op
*prog
= input_joy_list
[i
];
275 vg_exec_input_program( k_vg_input_type_joy_v2f
, prog
,
276 srinput
.joystick_states
[i
][0] );
280 f32 x
= srinput
.axis_states
[k_sraxis_mbrowse_h
][0],
281 y
= srinput
.axis_states
[k_sraxis_mbrowse_v
][0],
284 if( fabsf(x
) > sensitivity
){
285 if( x
> 0.0f
) srinput
.button_states
[k_srbind_mright
][0] = 1;
286 else srinput
.button_states
[k_srbind_mleft
][0] = 1;
289 if( fabsf(y
) > sensitivity
){
290 if( y
> 0.0f
) srinput
.button_states
[k_srbind_mup
][0] = 1;
291 else srinput
.button_states
[k_srbind_mdown
][0] = 1;