void Controls_WrapGameControllerAxis(int axis, Sint16 value) { // Since left/right triggers on X360-like controllers are actually axes, // and we still need them as buttons, we remap these axes to button events. // Button event is invoked only if trigger is pressed more than 1/3 of its range. // Triggers are coded as native SDL2 enum number + JOY_TRIGGER_MASK (1200). if( (axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT) || (axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) ) { if(value >= JOY_TRIGGER_DEADZONE) { Controls_Key((axis + JOY_TRIGGER_MASK), SDL_PRESSED); } else { Controls_Key((axis + JOY_TRIGGER_MASK), SDL_RELEASED); } } else { Controls_JoyAxis(axis, value); } }
void Controls_PollSDLInput() { SDL_Event event; static int mouse_setup = 0; while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_MOUSEMOTION: if(!ConsoleInfo::instance().isVisible() && control_states.mouse_look && ((event.motion.x != (screen_info.w / 2)) || (event.motion.y != (screen_info.h / 2)))) { if(mouse_setup) // it is not perfect way, but cursor { // every engine start is in one place control_states.look_axis_x = event.motion.xrel * control_mapper.mouse_sensitivity * 0.01f; control_states.look_axis_y = event.motion.yrel * control_mapper.mouse_sensitivity * 0.01f; } if((event.motion.x < ((screen_info.w / 2) - (screen_info.w / 4))) || (event.motion.x >((screen_info.w / 2) + (screen_info.w / 4))) || (event.motion.y < ((screen_info.h / 2) - (screen_info.h / 4))) || (event.motion.y >((screen_info.h / 2) + (screen_info.h / 4)))) { SDL_WarpMouseInWindow(sdl_window, screen_info.w / 2, screen_info.h / 2); } } mouse_setup = 1; break; case SDL_MOUSEBUTTONDOWN: if(event.button.button == 1) //LM = 1, MM = 2, RM = 3 { Controls_PrimaryMouseDown(); } else if(event.button.button == 3) { Controls_SecondaryMouseDown(); } break; // Controller events are only invoked when joystick is initialized as // game controller, otherwise, generic joystick event will be used. case SDL_CONTROLLERAXISMOTION: Controls_WrapGameControllerAxis(event.caxis.axis, event.caxis.value); break; case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONUP: Controls_WrapGameControllerKey(event.cbutton.button, event.cbutton.state == SDL_PRESSED); break; // Joystick events are still invoked, even if joystick is initialized as game // controller - that's why we need sdl_joystick checking - to filter out // duplicate event calls. case SDL_JOYAXISMOTION: if(sdl_joystick) Controls_JoyAxis(event.jaxis.axis, event.jaxis.value); break; case SDL_JOYHATMOTION: if(sdl_joystick) Controls_JoyHat(event.jhat.value); break; case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: // NOTE: Joystick button numbers are passed with added JOY_BUTTON_MASK (1000). if(sdl_joystick) Controls_Key((event.jbutton.button + JOY_BUTTON_MASK), event.jbutton.state == SDL_PRESSED); break; case SDL_TEXTINPUT: case SDL_TEXTEDITING: if(ConsoleInfo::instance().isVisible() && event.key.state) { ConsoleInfo::instance().filter(event.text.text); return; } break; case SDL_KEYUP: case SDL_KEYDOWN: if((event.key.keysym.sym == SDLK_F4) && (event.key.state == SDL_PRESSED) && (event.key.keysym.mod & KMOD_ALT)) { done = true; break; } if(ConsoleInfo::instance().isVisible() && event.key.state) { switch(event.key.keysym.sym) { case SDLK_RETURN: case SDLK_UP: case SDLK_DOWN: case SDLK_LEFT: case SDLK_RIGHT: case SDLK_HOME: case SDLK_END: case SDLK_BACKSPACE: case SDLK_DELETE: ConsoleInfo::instance().edit(event.key.keysym.sym); break; default: break; } return; } else { Controls_Key(event.key.keysym.sym, event.key.state == SDL_PRESSED); // DEBUG KEYBOARD COMMANDS Controls_DebugKeys(event.key.keysym.sym, event.key.state); } break; case SDL_QUIT: done = true; break; case SDL_WINDOWEVENT: if(event.window.event == SDL_WINDOWEVENT_RESIZED) { Engine_Resize(event.window.data1, event.window.data2, event.window.data1, event.window.data2); } break; default: break; } } }