void JoystickKeyboardController::process_axis_event(const SDL_JoyAxisEvent& jaxis) { if (wait_for_joystick >= 0) { if (abs(jaxis.value) > dead_zone) { if (jaxis.value < 0) bind_joyaxis(jaxis.which, -(jaxis.axis + 1), Control(wait_for_joystick)); else bind_joyaxis(jaxis.which, jaxis.axis + 1, Control(wait_for_joystick)); MenuStorage::get_joystick_options_menu()->update(); wait_for_joystick = -1; } } else { // Split the axis into left and right, so that both can be // mapped separately (needed for jump/down vs up/down) int axis = jaxis.axis + 1; AxisMap::iterator left = joy_axis_map.find(std::make_pair(jaxis.which, -axis)); AxisMap::iterator right = joy_axis_map.find(std::make_pair(jaxis.which, axis)); if(left == joy_axis_map.end()) { // std::cout << "Unmapped joyaxis " << (int)jaxis.axis << " moved" << std::endl; } else { if (jaxis.value < -dead_zone) set_joy_controls(left->second, true); else if (jaxis.value > dead_zone) set_joy_controls(left->second, false); else set_joy_controls(left->second, false); } if(right == joy_axis_map.end()) { // std::cout << "Unmapped joyaxis " << (int)jaxis.axis << " moved" << std::endl; } else { if (jaxis.value < -dead_zone) set_joy_controls(right->second, false); else if (jaxis.value > dead_zone) set_joy_controls(right->second, true); else set_joy_controls(right->second, false); } } }
void JoystickConfig::read(const lisp::Lisp& joystick_lisp) { joystick_lisp.get("dead-zone", dead_zone); joystick_lisp.get("jump-with-up", jump_with_up_joy); lisp::ListIterator iter(&joystick_lisp); while(iter.next()) { if (iter.item() == "map") { int button = -1; int axis = 0; int hat = -1; std::string control; const lisp::Lisp* map = iter.lisp(); map->get("control", control); int i = 0; for(i = 0; Controller::controlNames[i] != 0; ++i) { if (control == Controller::controlNames[i]) break; } if (Controller::controlNames[i] == 0) { log_info << "Invalid control '" << control << "' in buttonmap" << std::endl; } else { if (map->get("button", button)) { bind_joybutton(0, button, Controller::Control(i)); } else if (map->get("axis", axis)) { bind_joyaxis(0, axis, Controller::Control(i)); } else if (map->get("hat", hat)) { if (hat != SDL_HAT_UP && hat != SDL_HAT_DOWN && hat != SDL_HAT_LEFT && hat != SDL_HAT_RIGHT) { log_info << "Invalid axis '" << axis << "' in axismap" << std::endl; } else { bind_joyhat(0, hat, Controller::Control(i)); } } } } } }
void JoystickConfig::read(const ReaderMapping& joystick_lisp) { joystick_lisp.get("dead-zone", dead_zone); joystick_lisp.get("jump-with-up", jump_with_up_joy); joystick_lisp.get("use-game-controller", use_game_controller); auto iter = joystick_lisp.get_iter(); while(iter.next()) { if (iter.get_key() == "map") { int button = -1; int axis = 0; int hat = -1; std::string control; auto map = iter.as_mapping(); map.get("control", control); int i = 0; for(i = 0; Controller::controlNames[i] != 0; ++i) { if (control == Controller::controlNames[i]) break; } if (Controller::controlNames[i] == 0) { log_info << "Invalid control '" << control << "' in buttonmap" << std::endl; } else { if (map.get("button", button)) { bind_joybutton(0, button, Controller::Control(i)); } else if (map.get("axis", axis)) { bind_joyaxis(0, axis, Controller::Control(i)); } else if (map.get("hat", hat)) { if (hat != SDL_HAT_UP && hat != SDL_HAT_DOWN && hat != SDL_HAT_LEFT && hat != SDL_HAT_RIGHT) { log_info << "Invalid axis '" << axis << "' in axismap" << std::endl; } else { bind_joyhat(0, hat, Controller::Control(i)); } } } } } }
JoystickConfig::JoystickConfig() : dead_zone(8000), jump_with_up_joy(false), joy_button_map(), joy_axis_map(), joy_hat_map() { // Default joystick button configuration bind_joybutton(0, 0, Controller::JUMP); bind_joybutton(0, 0, Controller::MENU_SELECT); bind_joybutton(0, 1, Controller::ACTION); bind_joybutton(0, 4, Controller::PEEK_LEFT); bind_joybutton(0, 5, Controller::PEEK_RIGHT); bind_joybutton(0, 6, Controller::START); // Default joystick axis configuration bind_joyaxis(0, -1, Controller::LEFT); bind_joyaxis(0, 1, Controller::RIGHT); bind_joyaxis(0, -2, Controller::UP); bind_joyaxis(0, 2, Controller::DOWN); }
JoystickKeyboardController::JoystickKeyboardController() : controller(), keymap(), joy_button_map(), joy_axis_map(), joy_hat_map(), joysticks(), name(), dead_zone(), min_joybuttons(), max_joybuttons(), max_joyaxis(), max_joyhats(), hat_state(0), jump_with_up_joy(), jump_with_up_kbd(), wait_for_key(-1), wait_for_joystick(-1) { controller = new Controller; // initialize default keyboard map keymap[SDLK_LEFT] = Controller::LEFT; keymap[SDLK_RIGHT] = Controller::RIGHT; keymap[SDLK_UP] = Controller::UP; keymap[SDLK_DOWN] = Controller::DOWN; keymap[SDLK_SPACE] = Controller::JUMP; keymap[SDLK_LCTRL] = Controller::ACTION; keymap[SDLK_LALT] = Controller::ACTION; keymap[SDLK_ESCAPE] = Controller::PAUSE_MENU; keymap[SDLK_p] = Controller::PAUSE_MENU; keymap[SDLK_PAUSE] = Controller::PAUSE_MENU; keymap[SDLK_RETURN] = Controller::MENU_SELECT; keymap[SDLK_KP_ENTER] = Controller::MENU_SELECT; keymap[SDLK_CARET] = Controller::CONSOLE; keymap[SDLK_DELETE] = Controller::PEEK_LEFT; keymap[SDLK_PAGEDOWN] = Controller::PEEK_RIGHT; keymap[SDLK_HOME] = Controller::PEEK_UP; keymap[SDLK_END] = Controller::PEEK_DOWN; jump_with_up_joy = false; jump_with_up_kbd = false; updateAvailableJoysticks(); dead_zone = 1000; // Default joystick button configuration bind_joybutton(0, 0, Controller::JUMP); bind_joybutton(0, 1, Controller::ACTION); // 6 or more Buttons if( min_joybuttons > 5 ){ bind_joybutton(0, 4, Controller::PEEK_LEFT); bind_joybutton(0, 5, Controller::PEEK_RIGHT); // 8 or more if(min_joybuttons > 7) bind_joybutton(0, min_joybuttons-1, Controller::PAUSE_MENU); } else { // map the last 2 buttons to menu and pause if(min_joybuttons > 2) bind_joybutton(0, min_joybuttons-1, Controller::PAUSE_MENU); // map all remaining joystick buttons to MENU_SELECT for(int i = 2; i < max_joybuttons; ++i) { if(i != min_joybuttons-1) bind_joybutton(0, i, Controller::MENU_SELECT); } } // Default joystick axis configuration bind_joyaxis(0, -1, Controller::LEFT); bind_joyaxis(0, 1, Controller::RIGHT); bind_joyaxis(0, -2, Controller::UP); bind_joyaxis(0, 2, Controller::DOWN); }
void JoystickKeyboardController::read(const Reader& lisp) { const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap"); if(keymap_lisp) { keymap.clear(); keymap_lisp->get("jump-with-up", jump_with_up_kbd); lisp::ListIterator iter(keymap_lisp); while(iter.next()) { if(iter.item() == "map") { int key = -1; std::string control; const lisp::Lisp* map = iter.lisp(); map->get("key", key); map->get("control", control); if(key < SDLK_FIRST || key >= SDLK_LAST) { log_info << "Invalid key '" << key << "' in keymap" << std::endl; continue; } int i = 0; for(i = 0; Controller::controlNames[i] != 0; ++i) { if(control == Controller::controlNames[i]) break; } if(Controller::controlNames[i] == 0) { log_info << "Invalid control '" << control << "' in keymap" << std::endl; continue; } keymap[SDLKey(key)] = Control(i); } } } const lisp::Lisp* joystick_lisp = lisp.get_lisp("joystick"); if(joystick_lisp) { joystick_lisp->get("dead-zone", dead_zone); joystick_lisp->get("jump-with-up", jump_with_up_joy); lisp::ListIterator iter(joystick_lisp); while(iter.next()) { if(iter.item() == "map") { int button = -1; int axis = 0; int hat = -1; std::string control; const lisp::Lisp* map = iter.lisp(); map->get("control", control); int i = 0; for(i = 0; Controller::controlNames[i] != 0; ++i) { if(control == Controller::controlNames[i]) break; } if(Controller::controlNames[i] == 0) { log_info << "Invalid control '" << control << "' in buttonmap" << std::endl; continue; } if (map->get("button", button)) { if(button < 0 || button >= max_joybuttons) { log_info << "Invalid button '" << button << "' in buttonmap" << std::endl; continue; } bind_joybutton(0, button, Control(i)); } if (map->get("axis", axis)) { if (axis == 0 || abs(axis) > max_joyaxis) { log_info << "Invalid axis '" << axis << "' in axismap" << std::endl; continue; } bind_joyaxis(0, axis, Control(i)); } if (map->get("hat", hat)) { if (hat != SDL_HAT_UP && hat != SDL_HAT_DOWN && hat != SDL_HAT_LEFT && hat != SDL_HAT_RIGHT) { log_info << "Invalid axis '" << axis << "' in axismap" << std::endl; continue; } else { bind_joyhat(0, hat, Control(i)); } } } } } }