Controller.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "client/input/Controller.h"
  2. #include "client/rendering/Engine.h"
  3. Controller::Controller()
  4. : left(GLFW_KEY_A, "Left"), right(GLFW_KEY_D, "Right"), up(GLFW_KEY_W, "X"),
  5. down(GLFW_KEY_S, "Y"), jump(GLFW_KEY_SPACE, "L"),
  6. sneak(GLFW_KEY_LEFT_SHIFT, "R"), camLeft(GLFW_KEY_LEFT, "Start"),
  7. camRight(GLFW_KEY_RIGHT, "Select"), camUp(GLFW_KEY_UP, "Left"),
  8. camDown(GLFW_KEY_DOWN, "Right"),
  9. leftClick(GLFW_MOUSE_BUTTON_LEFT, "Left Click"),
  10. rightClick(GLFW_MOUSE_BUTTON_RIGHT, "Right Click") {
  11. }
  12. void Controller::init() {
  13. Buttons& b = Engine::getButtons();
  14. b.add(left);
  15. b.add(right);
  16. b.add(up);
  17. b.add(down);
  18. b.add(jump);
  19. b.add(sneak);
  20. b.add(camLeft);
  21. b.add(camRight);
  22. b.add(camUp);
  23. b.add(camDown);
  24. b.addMouse(leftClick);
  25. b.addMouse(rightClick);
  26. b.mapGamepadAxis(left, -0.5f, GLFW_GAMEPAD_AXIS_LEFT_X);
  27. b.mapGamepadAxis(right, 0.5f, GLFW_GAMEPAD_AXIS_LEFT_X);
  28. b.mapGamepadAxis(up, -0.5f, GLFW_GAMEPAD_AXIS_LEFT_Y);
  29. b.mapGamepadAxis(down, 0.5f, GLFW_GAMEPAD_AXIS_LEFT_Y);
  30. b.mapGamepadAxis(camUp, -0.5f, GLFW_GAMEPAD_AXIS_RIGHT_Y);
  31. b.mapGamepadAxis(camDown, 0.5f, GLFW_GAMEPAD_AXIS_RIGHT_Y);
  32. b.mapGamepadAxis(camLeft, -0.5f, GLFW_GAMEPAD_AXIS_RIGHT_X);
  33. b.mapGamepadAxis(camRight, 0.5f, GLFW_GAMEPAD_AXIS_RIGHT_X);
  34. }
  35. Vector2 Controller::getMouse() const {
  36. Buttons& b = Engine::getButtons();
  37. return Vector2(static_cast<float>(b.getMouseX()),
  38. static_cast<float>(b.getMouseY()));
  39. }
  40. Vector2 Controller::getMouseChange() const {
  41. Buttons& b = Engine::getButtons();
  42. return Vector2(static_cast<float>(b.getMouseX() - b.getLastMouseX()),
  43. static_cast<float>(b.getMouseY() - b.getLastMouseY()));
  44. }