add strip for hub switch to map
[carveJwlIkooP6JGAAIwe30JlM.git] / input.h
1 #pragma once
2 #include "vg/vg_platform.h"
3 #include "vg/vg_console.h"
4 #include "vg/vg_input.h"
5 #include "vg/vg_m.h"
6 #include "font.h"
7
8 enum sr_bind
9 {
10 k_srbind_jump = 0,
11 k_srbind_push,
12 k_srbind_skid,
13 k_srbind_trick0,
14 k_srbind_trick1,
15 k_srbind_trick2,
16 k_srbind_sit,
17 k_srbind_use,
18 k_srbind_reset,
19 k_srbind_dead_respawn,
20 k_srbind_camera,
21 k_srbind_mleft,
22 k_srbind_mright,
23 k_srbind_mup,
24 k_srbind_mdown,
25 k_srbind_mback,
26 k_srbind_maccept,
27 k_srbind_mopen,
28 k_srbind_mhub,
29 k_srbind_replay_play,
30 k_srbind_replay_freecam,
31 k_srbind_replay_resume,
32 k_srbind_world_left,
33 k_srbind_world_right,
34 k_srbind_home,
35 k_srbind_lobby,
36 k_srbind_chat,
37 k_srbind_run,
38
39 k_srbind_miniworld_teleport,
40 k_srbind_miniworld_resume,
41 k_srbind_devbutton,
42 k_srbind_max,
43 };
44
45 enum sr_joystick{
46 k_srjoystick_steer = 0,
47 k_srjoystick_grab,
48 k_srjoystick_look,
49 k_srjoystick_max
50 };
51
52 enum sr_axis{
53 k_sraxis_grab = 0,
54 k_sraxis_mbrowse_h,
55 k_sraxis_mbrowse_v,
56 k_sraxis_replay_h,
57 k_sraxis_skid,
58 k_sraxis_max
59 };
60
61
62 #define INPUT_BASIC( KB, JS ) \
63 (vg_input_op[]){vg_keyboard, KB, vg_joy_button, JS, vg_end}
64
65 static vg_input_op *input_button_list[] = {
66 [k_srbind_jump] = INPUT_BASIC( SDLK_SPACE, SDL_CONTROLLER_BUTTON_A ),
67 [k_srbind_push] = INPUT_BASIC( SDLK_w, SDL_CONTROLLER_BUTTON_B ),
68 [k_srbind_trick0] = (vg_input_op[]){
69 vg_mouse, SDL_BUTTON_LEFT,
70 vg_joy_button, SDL_CONTROLLER_BUTTON_A, vg_end
71 },
72 [k_srbind_trick1] = (vg_input_op[]){
73 vg_mouse, SDL_BUTTON_RIGHT,
74 vg_joy_button, SDL_CONTROLLER_BUTTON_B, vg_end
75 },
76 [k_srbind_trick2] = (vg_input_op[]){
77 vg_mouse, SDL_BUTTON_LEFT, vg_mode_mul, vg_mouse, SDL_BUTTON_RIGHT,
78 vg_mode_absmax, vg_joy_button, SDL_CONTROLLER_BUTTON_X, vg_end
79 },
80 [k_srbind_use] = INPUT_BASIC( SDLK_e, SDL_CONTROLLER_BUTTON_Y ),
81 [k_srbind_reset] = INPUT_BASIC( SDLK_r, SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
82 [k_srbind_dead_respawn] =
83 INPUT_BASIC( SDLK_q, SDL_CONTROLLER_BUTTON_DPAD_UP ),
84 [k_srbind_camera]= INPUT_BASIC( SDLK_c, SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
85 [k_srbind_mleft] = INPUT_BASIC( SDLK_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
86 [k_srbind_mright]= INPUT_BASIC( SDLK_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
87 [k_srbind_world_left] =
88 INPUT_BASIC( SDLK_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
89 [k_srbind_world_right] =
90 INPUT_BASIC( SDLK_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
91 [k_srbind_home] = INPUT_BASIC( SDLK_h, SDL_CONTROLLER_BUTTON_Y ),
92 [k_srbind_mup] = INPUT_BASIC( SDLK_UP, SDL_CONTROLLER_BUTTON_DPAD_UP ),
93 [k_srbind_mdown] = INPUT_BASIC( SDLK_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN ),
94 [k_srbind_mback] = INPUT_BASIC( SDLK_ESCAPE, SDL_CONTROLLER_BUTTON_B ),
95 [k_srbind_mopen] = INPUT_BASIC( SDLK_ESCAPE, SDL_CONTROLLER_BUTTON_START ),
96 [k_srbind_mhub] = (vg_input_op[]){ vg_joy_button, SDL_CONTROLLER_BUTTON_Y,
97 vg_end },
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,
101 vg_gui_visible, 1,
102 vg_joy_button, SDL_CONTROLLER_BUTTON_A, vg_end
103 },
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 ] = (vg_input_op[]){ vg_keyboard, SDLK_LSHIFT,
111 vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERLEFT, vg_end },
112
113 [k_srbind_miniworld_resume] = (vg_input_op[]){
114 vg_keyboard, SDLK_RETURN, vg_gui_visible, 0,
115 vg_keyboard, SDLK_RETURN2,
116 vg_gui_visible, 1,
117 vg_joy_button, SDL_CONTROLLER_BUTTON_X, vg_end
118 },
119 [k_srbind_miniworld_teleport]= INPUT_BASIC( SDLK_q,
120 SDL_CONTROLLER_BUTTON_LEFTSHOULDER ),
121 [k_srbind_skid] = (vg_input_op[]){ vg_keyboard, SDLK_LCTRL, vg_end },
122 [k_srbind_devbutton] = (vg_input_op[]){ vg_keyboard, SDLK_3, vg_end },
123 [k_srbind_max]=NULL
124 };
125
126 static vg_input_op *input_axis_list[] = {
127 [k_sraxis_grab] = (vg_input_op[]){
128 vg_keyboard, SDLK_LSHIFT,
129 vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, vg_end
130 },
131 [k_sraxis_mbrowse_h] = (vg_input_op[]){
132 vg_mode_sub, vg_keyboard, SDLK_LEFT,
133 vg_mode_add, vg_keyboard, SDLK_RIGHT,
134 vg_mode_add, vg_joy_axis, SDL_CONTROLLER_AXIS_LEFTX,
135 vg_end
136 },
137 [k_sraxis_mbrowse_v] = (vg_input_op[]){
138 vg_mode_sub, vg_keyboard, SDLK_DOWN,
139 vg_mode_add, vg_keyboard, SDLK_UP,
140 vg_mode_sub, vg_joy_axis, SDL_CONTROLLER_AXIS_LEFTY,
141 vg_end
142 },
143 [k_sraxis_replay_h] = (vg_input_op[]){
144 vg_mode_sub, vg_keyboard, SDLK_q,
145 vg_mode_add, vg_keyboard, SDLK_e,
146 vg_mode_sub, vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERLEFT,
147 vg_mode_add, vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
148 vg_end
149 },
150 [k_sraxis_skid] = (vg_input_op[]){
151 vg_mode_sub, vg_joy_button, SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
152 vg_mode_add, vg_joy_button, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
153 vg_end
154 },
155 [k_sraxis_max]=NULL
156 };
157
158 static vg_input_op *input_joy_list[] = {
159 [k_srjoystick_steer] = (vg_input_op[]){
160 vg_index, 0, vg_mode_sub, vg_keyboard, SDLK_a,
161 vg_mode_add, vg_keyboard, SDLK_d,
162 vg_index, 1, vg_mode_sub, vg_keyboard, SDLK_w,
163 vg_mode_add, vg_keyboard, SDLK_s,
164 vg_mode_absmax, vg_joy_ls,
165 vg_end
166 },
167 [k_srjoystick_grab] = (vg_input_op[]){
168 vg_joy_rs, vg_end
169 },
170 [k_srjoystick_look] = (vg_input_op[]){
171 vg_joy_rs, vg_end
172 },
173 [k_srjoystick_max]=NULL
174 };
175
176 struct {
177 float axis_states[ k_sraxis_max ][2];
178 v2f joystick_states[ k_srjoystick_max ][2];
179 u8 button_states[ k_srbind_max ][2];
180
181 enum input_state {
182 k_input_state_enabled,
183 k_input_state_resume,
184 k_input_state_resuming,
185 k_input_state_pause
186 }
187 state;
188 }
189 static srinput;
190
191 static int input_filter_generic(void){
192 if( srinput.state != k_input_state_enabled || vg_console.enabled )
193 return 1;
194 else
195 return 0;
196 }
197
198 static int buttons_filter_fixed(void){
199 if( input_filter_generic() )
200 return 1;
201
202 if( vg.engine_stage == k_engine_stage_update_fixed )
203 if( vg.fixed_iterations > 0 )
204 return 1;
205
206 return 0;
207 }
208
209 /* Rising edge of button */
210 static int button_down( enum sr_bind button ){
211 if( buttons_filter_fixed() ) return 0;
212
213 if( srinput.button_states[ button ][0] &&
214 !srinput.button_states[ button ][1] )
215 return 1;
216 else
217 return 0;
218 }
219
220 /* Falling edge of button */
221 static int button_up( enum sr_bind button ){
222 if( buttons_filter_fixed() ) return 0;
223
224 if( !srinput.button_states[ button ][0] &&
225 srinput.button_states[ button ][1] )
226 return 1;
227 else
228 return 0;
229 }
230
231 /* State of button */
232 static int button_press( enum sr_bind button ){
233 if( input_filter_generic() )
234 return 0;
235 return
236 srinput.button_states[ button ][0];
237 }
238
239 static void joystick_state( enum sr_joystick joystick, v2f state ){
240 if( input_filter_generic() )
241 v2_zero( state );
242 else
243 v2_copy( srinput.joystick_states[ joystick ][0], state );
244 }
245
246 static float axis_state( enum sr_axis axis ){
247 if( input_filter_generic() )
248 return 0.0f;
249 else
250 return srinput.axis_states[axis][0];
251 }
252
253 static void skaterift_preupdate_inputs(void){
254 if( srinput.state == k_input_state_resuming )
255 srinput.state = k_input_state_enabled;
256
257 if( srinput.state == k_input_state_resume )
258 srinput.state = k_input_state_resuming;
259
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;
263 }
264
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] );
268 }
269
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;
273 }
274
275 for( int i=0; i<k_srbind_max; i++ ){
276 vg_input_op *prog = input_button_list[i];
277 if( prog ){
278 vg_exec_input_program( k_vg_input_type_button_u8, prog,
279 &srinput.button_states[i][0] );
280 }
281 }
282
283 for( int i=0; i<k_sraxis_max; i++ ){
284 vg_input_op *prog = input_axis_list[i];
285 if( prog ){
286 vg_exec_input_program( k_vg_input_type_axis_f32, prog,
287 &srinput.axis_states[i][0] );
288 }
289 }
290
291 for( int i=0; i<k_srjoystick_max; i++ ){
292 vg_input_op *prog = input_joy_list[i];
293 if( prog ){
294 vg_exec_input_program( k_vg_input_type_joy_v2f, prog,
295 srinput.joystick_states[i][0] );
296 }
297 }
298
299 f32 x = srinput.axis_states[k_sraxis_mbrowse_h][0],
300 y = srinput.axis_states[k_sraxis_mbrowse_v][0],
301 sensitivity = 0.35f;
302
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;
306 }
307
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;
311 }
312 }