Пример #1
0
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));
          }
        }
      }
    }
  }
}
Пример #2
0
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));
          }
        }
      }
    }
  }
}
Пример #3
0
void
JoystickKeyboardController::process_button_event(const SDL_JoyButtonEvent& jbutton)
{
  if(wait_for_joystick >= 0) 
  {
    if(jbutton.state == SDL_PRESSED)
    {
      bind_joybutton(jbutton.which, jbutton.button, (Control)wait_for_joystick);
      MenuStorage::get_joystick_options_menu()->update();
      reset();
      wait_for_joystick = -1;
    }
  } 
  else 
  {
    ButtonMap::iterator i = joy_button_map.find(std::make_pair(jbutton.which, jbutton.button));
    if(i == joy_button_map.end()) {
      log_debug << "Unmapped joybutton " << (int)jbutton.button << " pressed" << std::endl;
    } else {
      set_joy_controls(i->second, (jbutton.state == SDL_PRESSED));
    }
  }
}
Пример #4
0
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);
}
Пример #5
0
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);
}
Пример #6
0
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));
          }
        }
      }
    }
  }
}