💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.1 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "input-mapper.hpp"
  20. #include "mouse.hpp"
  21. #include "event/event-dispatcher.hpp"
  22. input_mapper::input_mapper():
  23. event_dispatcher(nullptr),
  24. control(nullptr),
  25. callback(nullptr),
  26. enabled(false)
  27. {}
  28. input_mapper::~input_mapper()
  29. {
  30. set_event_dispatcher(nullptr);
  31. }
  32. void input_mapper::set_event_dispatcher(::event_dispatcher* event_dispatcher)
  33. {
  34. if (this->event_dispatcher)
  35. {
  36. this->event_dispatcher->unsubscribe<key_pressed_event>(this);
  37. this->event_dispatcher->unsubscribe<mouse_moved_event>(this);
  38. this->event_dispatcher->unsubscribe<mouse_wheel_scrolled_event>(this);
  39. this->event_dispatcher->unsubscribe<mouse_button_pressed_event>(this);
  40. this->event_dispatcher->unsubscribe<game_controller_axis_moved_event>(this);
  41. this->event_dispatcher->unsubscribe<game_controller_button_pressed_event>(this);
  42. }
  43. this->event_dispatcher = event_dispatcher;
  44. if (event_dispatcher)
  45. {
  46. event_dispatcher->subscribe<key_pressed_event>(this);
  47. event_dispatcher->subscribe<mouse_moved_event>(this);
  48. event_dispatcher->subscribe<mouse_wheel_scrolled_event>(this);
  49. event_dispatcher->subscribe<mouse_button_pressed_event>(this);
  50. event_dispatcher->subscribe<game_controller_axis_moved_event>(this);
  51. event_dispatcher->subscribe<game_controller_button_pressed_event>(this);
  52. }
  53. }
  54. void input_mapper::set_control(::control* control)
  55. {
  56. this->control = control;
  57. }
  58. void input_mapper::set_callback(std::function<void(const input_mapping&)> callback)
  59. {
  60. this->callback = callback;
  61. }
  62. void input_mapper::set_enabled(bool enabled)
  63. {
  64. this->enabled = enabled;
  65. }
  66. void input_mapper::handle_event(const key_pressed_event& event)
  67. {
  68. if (!is_enabled() || !callback)
  69. {
  70. return;
  71. }
  72. callback(key_mapping(control, event.keyboard, event.scancode));
  73. }
  74. void input_mapper::handle_event(const mouse_moved_event& event)
  75. {
  76. if (!is_enabled() || !callback)
  77. {
  78. return;
  79. }
  80. if (event.dx != 0)
  81. {
  82. mouse_motion_axis axis = (event.dx < 0) ? mouse_motion_axis::negative_x : mouse_motion_axis::positive_x;
  83. callback(mouse_motion_mapping(control, event.mouse, axis));
  84. }
  85. if (event.dy != 0)
  86. {
  87. mouse_motion_axis axis = (event.dy < 0) ? mouse_motion_axis::negative_y : mouse_motion_axis::positive_y;
  88. callback(mouse_motion_mapping(control, event.mouse, axis));
  89. }
  90. }
  91. void input_mapper::handle_event(const mouse_button_pressed_event& event)
  92. {
  93. if (!is_enabled() || !callback)
  94. {
  95. return;
  96. }
  97. callback(mouse_button_mapping(control, event.mouse, event.button));
  98. }
  99. void input_mapper::handle_event(const mouse_wheel_scrolled_event& event)
  100. {
  101. if (!is_enabled() || !callback)
  102. {
  103. return;
  104. }
  105. if (event.x != 0)
  106. {
  107. mouse_wheel_axis axis = (event.x < 0) ? mouse_wheel_axis::negative_x : mouse_wheel_axis::positive_x;
  108. callback(mouse_wheel_mapping(control, event.mouse, axis));
  109. }
  110. if (event.y != 0)
  111. {
  112. mouse_wheel_axis axis = (event.y < 0) ? mouse_wheel_axis::negative_y : mouse_wheel_axis::positive_y;
  113. callback(mouse_wheel_mapping(control, event.mouse, axis));
  114. }
  115. }
  116. void input_mapper::handle_event(const game_controller_button_pressed_event& event)
  117. {
  118. if (!is_enabled() || !callback)
  119. {
  120. return;
  121. }
  122. callback(game_controller_button_mapping(control, event.game_controller, event.button));
  123. }
  124. void input_mapper::handle_event(const game_controller_axis_moved_event& event)
  125. {
  126. if (!is_enabled() || !callback)
  127. {
  128. return;
  129. }
  130. callback(game_controller_axis_mapping(control, event.game_controller, event.axis, (event.value < 0.0f)));
  131. }