2d4f30ea2c300b4a0b67eea1c9d03ea764de1bc7
[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_replay_play,
29 k_srbind_replay_freecam,
30 k_srbind_replay_resume,
31 k_srbind_world_left,
32 k_srbind_world_right,
33 k_srbind_home,
34 k_srbind_lobby,
35 k_srbind_chat,
36 k_srbind_run,
37
38 k_srbind_miniworld_teleport,
39 k_srbind_miniworld_resume,
40 k_srbind_devbutton,
41 k_srbind_max,
42 };
43
44 enum sr_joystick{
45 k_srjoystick_steer = 0,
46 k_srjoystick_grab,
47 k_srjoystick_look,
48 k_srjoystick_max
49 };
50
51 enum sr_axis{
52 k_sraxis_grab = 0,
53 k_sraxis_mbrowse_h,
54 k_sraxis_mbrowse_v,
55 k_sraxis_replay_h,
56 k_sraxis_skid,
57 k_sraxis_max
58 };
59
60
61 #define INPUT_BASIC( KB, JS ) \
62 (vg_input_op[]){vg_keyboard, KB, vg_joy_button, JS, vg_end}
63
64 static vg_input_op *input_button_list[] = {
65 [k_srbind_jump] = INPUT_BASIC( SDLK_SPACE, SDL_CONTROLLER_BUTTON_A ),
66 [k_srbind_push] = INPUT_BASIC( SDLK_w, SDL_CONTROLLER_BUTTON_B ),
67 [k_srbind_trick0] = (vg_input_op[]){
68 vg_mouse, SDL_BUTTON_LEFT,
69 vg_joy_button, SDL_CONTROLLER_BUTTON_A, vg_end
70 },
71 [k_srbind_trick1] = (vg_input_op[]){
72 vg_mouse, SDL_BUTTON_RIGHT,
73 vg_joy_button, SDL_CONTROLLER_BUTTON_B, vg_end
74 },
75 [k_srbind_trick2] = (vg_input_op[]){
76 vg_mouse, SDL_BUTTON_LEFT, vg_mode_mul, vg_mouse, SDL_BUTTON_RIGHT,
77 vg_mode_absmax, vg_joy_button, SDL_CONTROLLER_BUTTON_X, vg_end
78 },
79 [k_srbind_use] = INPUT_BASIC( SDLK_e, SDL_CONTROLLER_BUTTON_Y ),
80 [k_srbind_reset] = INPUT_BASIC( SDLK_r, SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
81 [k_srbind_dead_respawn] =
82 INPUT_BASIC( SDLK_q, SDL_CONTROLLER_BUTTON_DPAD_UP ),
83 [k_srbind_camera]= INPUT_BASIC( SDLK_c, SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
84 [k_srbind_mleft] = INPUT_BASIC( SDLK_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
85 [k_srbind_mright]= INPUT_BASIC( SDLK_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
86 [k_srbind_world_left] =
87 INPUT_BASIC( SDLK_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
88 [k_srbind_world_right] =
89 INPUT_BASIC( SDLK_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
90 [k_srbind_home] = INPUT_BASIC( SDLK_h, SDL_CONTROLLER_BUTTON_Y ),
91 [k_srbind_mup] = INPUT_BASIC( SDLK_UP, SDL_CONTROLLER_BUTTON_DPAD_UP ),
92 [k_srbind_mdown] = INPUT_BASIC( SDLK_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN ),
93 [k_srbind_mback] = INPUT_BASIC( SDLK_ESCAPE, SDL_CONTROLLER_BUTTON_B ),
94 [k_srbind_mopen] = INPUT_BASIC( SDLK_ESCAPE, SDL_CONTROLLER_BUTTON_START ),
95 [k_srbind_maccept] = (vg_input_op[]){
96 vg_keyboard, SDLK_e, vg_gui_visible, 0,
97 vg_keyboard, SDLK_RETURN, vg_keyboard, SDLK_RETURN2,
98 vg_gui_visible, 1,
99 vg_joy_button, SDL_CONTROLLER_BUTTON_A, vg_end
100 },
101 [k_srbind_replay_play] = INPUT_BASIC( SDLK_g, SDL_CONTROLLER_BUTTON_X ),
102 [k_srbind_replay_resume] = INPUT_BASIC( SDLK_SPACE, SDL_CONTROLLER_BUTTON_A ),
103 [k_srbind_replay_freecam] = INPUT_BASIC( SDLK_f, SDL_CONTROLLER_BUTTON_Y ),
104 [k_srbind_sit] = INPUT_BASIC( SDLK_z, SDL_CONTROLLER_BUTTON_B ),
105 [k_srbind_lobby] = INPUT_BASIC( SDLK_TAB, SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
106 [k_srbind_chat ] = (vg_input_op[]){ vg_keyboard, SDLK_y, vg_end },
107 [k_srbind_run ] = (vg_input_op[]){ vg_keyboard, SDLK_LSHIFT,
108 vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERLEFT, vg_end },
109
110 [k_srbind_miniworld_resume] = (vg_input_op[]){
111 vg_keyboard, SDLK_RETURN, vg_gui_visible, 0,
112 vg_keyboard, SDLK_RETURN2,
113 vg_gui_visible, 1,
114 vg_joy_button, SDL_CONTROLLER_BUTTON_X, vg_end
115 },
116 [k_srbind_miniworld_teleport]= INPUT_BASIC( SDLK_q,
117 SDL_CONTROLLER_BUTTON_LEFTSHOULDER ),
118 [k_srbind_skid] = (vg_input_op[]){ vg_keyboard, SDLK_LCTRL, vg_end },
119 [k_srbind_devbutton] = (vg_input_op[]){ vg_keyboard, SDLK_3, vg_end },
120 [k_srbind_max]=NULL
121 };
122
123 static vg_input_op *input_axis_list[] = {
124 [k_sraxis_grab] = (vg_input_op[]){
125 vg_keyboard, SDLK_LSHIFT,
126 vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, vg_end
127 },
128 [k_sraxis_mbrowse_h] = (vg_input_op[]){
129 vg_mode_sub, vg_keyboard, SDLK_LEFT,
130 vg_mode_add, vg_keyboard, SDLK_RIGHT,
131 vg_mode_add, vg_joy_axis, SDL_CONTROLLER_AXIS_LEFTX,
132 vg_end
133 },
134 [k_sraxis_mbrowse_v] = (vg_input_op[]){
135 vg_mode_sub, vg_keyboard, SDLK_DOWN,
136 vg_mode_add, vg_keyboard, SDLK_UP,
137 vg_mode_sub, vg_joy_axis, SDL_CONTROLLER_AXIS_LEFTY,
138 vg_end
139 },
140 [k_sraxis_replay_h] = (vg_input_op[]){
141 vg_mode_sub, vg_keyboard, SDLK_q,
142 vg_mode_add, vg_keyboard, SDLK_e,
143 vg_mode_sub, vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERLEFT,
144 vg_mode_add, vg_joy_axis, SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
145 vg_end
146 },
147 [k_sraxis_skid] = (vg_input_op[]){
148 vg_mode_sub, vg_joy_button, SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
149 vg_mode_add, vg_joy_button, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
150 vg_end
151 },
152 [k_sraxis_max]=NULL
153 };
154
155 static vg_input_op *input_joy_list[] = {
156 [k_srjoystick_steer] = (vg_input_op[]){
157 vg_index, 0, vg_mode_sub, vg_keyboard, SDLK_a,
158 vg_mode_add, vg_keyboard, SDLK_d,
159 vg_index, 1, vg_mode_sub, vg_keyboard, SDLK_w,
160 vg_mode_add, vg_keyboard, SDLK_s,
161 vg_mode_absmax, vg_joy_ls,
162 vg_end
163 },
164 [k_srjoystick_grab] = (vg_input_op[]){
165 vg_joy_rs, vg_end
166 },
167 [k_srjoystick_look] = (vg_input_op[]){
168 vg_joy_rs, vg_end
169 },
170 [k_srjoystick_max]=NULL
171 };
172
173 struct {
174 float axis_states[ k_sraxis_max ][2];
175 v2f joystick_states[ k_srjoystick_max ][2];
176 u8 button_states[ k_srbind_max ][2];
177
178 enum input_state {
179 k_input_state_enabled,
180 k_input_state_resume,
181 k_input_state_resuming,
182 k_input_state_pause
183 }
184 state;
185 }
186 static srinput;
187
188 static int input_filter_generic(void){
189 if( srinput.state != k_input_state_enabled || vg_console.enabled )
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 }