small refactor of model loading
[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 k_srbind_jump = 0,
10 k_srbind_push,
11 k_srbind_skid,
12 k_srbind_trick0,
13 k_srbind_trick1,
14 k_srbind_trick2,
15 k_srbind_sit,
16 k_srbind_use,
17 k_srbind_reset,
18 k_srbind_dead_respawn,
19 k_srbind_camera,
20 k_srbind_mleft,
21 k_srbind_mright,
22 k_srbind_mup,
23 k_srbind_mdown,
24 k_srbind_mback,
25 k_srbind_maccept,
26 k_srbind_mopen,
27 k_srbind_replay_play,
28 k_srbind_replay_freecam,
29 k_srbind_replay_resume,
30 k_srbind_world_left,
31 k_srbind_world_right,
32 k_srbind_home,
33 k_srbind_lobby,
34 k_srbind_chat,
35 k_srbind_run,
36
37 k_srbind_miniworld_teleport,
38 k_srbind_miniworld_resume,
39 k_srbind_devbutton,
40 k_srbind_max,
41 };
42
43 enum sr_joystick{
44 k_srjoystick_steer = 0,
45 k_srjoystick_grab,
46 k_srjoystick_look,
47 k_srjoystick_max
48 };
49
50 enum sr_axis{
51 k_sraxis_grab = 0,
52 k_sraxis_mbrowse_h,
53 k_sraxis_mbrowse_v,
54 k_sraxis_replay_h,
55 k_sraxis_skid,
56 k_sraxis_max
57 };
58
59
60 #define INPUT_BASIC( KB, JS ) \
61 (vg_input_op[]){vg_keyboard, KB, vg_joy_button, JS, vg_end}
62
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
69 },
70 [k_srbind_trick1] = (vg_input_op[]){
71 vg_mouse, SDL_BUTTON_RIGHT,
72 vg_joy_button, SDL_CONTROLLER_BUTTON_B, vg_end
73 },
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
77 },
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_RIGHT ),
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,
97 vg_gui_visible, 1,
98 vg_joy_button, SDL_CONTROLLER_BUTTON_A, vg_end
99 },
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 },
108
109 [k_srbind_miniworld_resume] = (vg_input_op[]){
110 vg_keyboard, SDLK_RETURN, vg_gui_visible, 0,
111 vg_keyboard, SDLK_RETURN2,
112 vg_gui_visible, 1,
113 vg_joy_button, SDL_CONTROLLER_BUTTON_X, vg_end
114 },
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 },
119 [k_srbind_max]=NULL
120 };
121
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
126 },
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,
131 vg_end
132 },
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,
137 vg_end
138 },
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,
144 vg_end
145 },
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,
149 vg_end
150 },
151 [k_sraxis_max]=NULL
152 };
153
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,
161 vg_end
162 },
163 [k_srjoystick_grab] = (vg_input_op[]){
164 vg_joy_rs, vg_end
165 },
166 [k_srjoystick_look] = (vg_input_op[]){
167 vg_joy_rs, vg_end
168 },
169 [k_srjoystick_max]=NULL
170 };
171
172 struct {
173 float axis_states[ k_sraxis_max ][2];
174 v2f joystick_states[ k_srjoystick_max ][2];
175 u8 button_states[ k_srbind_max ][2];
176
177 enum input_state {
178 k_input_state_enabled,
179 k_input_state_resume,
180 k_input_state_resuming,
181 k_input_state_pause
182 }
183 state;
184 }
185 static srinput;
186
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 )
190 return 1;
191 else
192 return 0;
193 }
194
195 static int buttons_filter_fixed(void){
196 if( input_filter_generic() )
197 return 1;
198
199 if( vg.engine_stage == k_engine_stage_update_fixed )
200 if( vg.fixed_iterations > 0 )
201 return 1;
202
203 return 0;
204 }
205
206 /* Rising edge of button */
207 static int button_down( enum sr_bind button ){
208 if( buttons_filter_fixed() ) return 0;
209
210 if( srinput.button_states[ button ][0] &&
211 !srinput.button_states[ button ][1] )
212 return 1;
213 else
214 return 0;
215 }
216
217 /* Falling edge of button */
218 static int button_up( enum sr_bind button ){
219 if( buttons_filter_fixed() ) return 0;
220
221 if( !srinput.button_states[ button ][0] &&
222 srinput.button_states[ button ][1] )
223 return 1;
224 else
225 return 0;
226 }
227
228 /* State of button */
229 static int button_press( enum sr_bind button ){
230 if( input_filter_generic() )
231 return 0;
232 return
233 srinput.button_states[ button ][0];
234 }
235
236 static void joystick_state( enum sr_joystick joystick, v2f state ){
237 if( input_filter_generic() )
238 v2_zero( state );
239 else
240 v2_copy( srinput.joystick_states[ joystick ][0], state );
241 }
242
243 static float axis_state( enum sr_axis axis ){
244 if( input_filter_generic() )
245 return 0.0f;
246 else
247 return srinput.axis_states[axis][0];
248 }
249
250 static void skaterift_preupdate_inputs(void){
251 if( srinput.state == k_input_state_resuming )
252 srinput.state = k_input_state_enabled;
253
254 if( srinput.state == k_input_state_resume )
255 srinput.state = k_input_state_resuming;
256
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;
260 }
261
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] );
265 }
266
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;
270 }
271
272 for( int i=0; i<k_srbind_max; i++ ){
273 vg_input_op *prog = input_button_list[i];
274 if( prog ){
275 vg_exec_input_program( k_vg_input_type_button_u8, prog,
276 &srinput.button_states[i][0] );
277 }
278 }
279
280 for( int i=0; i<k_sraxis_max; i++ ){
281 vg_input_op *prog = input_axis_list[i];
282 if( prog ){
283 vg_exec_input_program( k_vg_input_type_axis_f32, prog,
284 &srinput.axis_states[i][0] );
285 }
286 }
287
288 for( int i=0; i<k_srjoystick_max; i++ ){
289 vg_input_op *prog = input_joy_list[i];
290 if( prog ){
291 vg_exec_input_program( k_vg_input_type_joy_v2f, prog,
292 srinput.joystick_states[i][0] );
293 }
294 }
295
296 f32 x = srinput.axis_states[k_sraxis_mbrowse_h][0],
297 y = srinput.axis_states[k_sraxis_mbrowse_v][0],
298 sensitivity = 0.35f;
299
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;
303 }
304
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;
308 }
309 }