controller.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "control/controller.hpp"
  17. #include <ostream>
  18. namespace {
  19. const char* g_control_names[] = {
  20. "left",
  21. "right",
  22. "up",
  23. "down",
  24. "jump",
  25. "action",
  26. "item",
  27. "start",
  28. "escape",
  29. "menu-select",
  30. "menu-select-space",
  31. "menu-back",
  32. "remove",
  33. "cheat-menu",
  34. "debug-menu",
  35. "console",
  36. "peek-left",
  37. "peek-right",
  38. "peek-up",
  39. "peek-down",
  40. nullptr
  41. };
  42. } // namespace
  43. std::ostream& operator<<(std::ostream& os, Control control)
  44. {
  45. return os << g_control_names[static_cast<int>(control)];
  46. }
  47. std::string Control_to_string(Control control)
  48. {
  49. return g_control_names[static_cast<int>(control)];
  50. }
  51. std::optional<Control> Control_from_string(const std::string& text)
  52. {
  53. for(int i = 0; g_control_names[i] != nullptr; ++i) {
  54. if (text == g_control_names[i]) {
  55. return static_cast<Control>(i);
  56. }
  57. }
  58. return std::nullopt;
  59. }
  60. Controller::Controller():
  61. m_touchscreen(false),
  62. m_jump_key_pressed(false)
  63. {
  64. reset();
  65. }
  66. Controller::~Controller()
  67. {}
  68. void
  69. Controller::reset()
  70. {
  71. for (int i = 0; i < static_cast<int>(Control::CONTROLCOUNT); ++i) {
  72. m_controls[i] = false;
  73. m_old_controls[i] = false;
  74. }
  75. m_touchscreen = false;
  76. m_jump_key_pressed = false;
  77. }
  78. void
  79. Controller::set_control(Control control, bool value)
  80. {
  81. if (control == Control::JUMP) {
  82. m_jump_key_pressed = value;
  83. }
  84. m_controls[static_cast<int>(control)] = value;
  85. }
  86. void
  87. Controller::set_jump_key_with_up(bool value)
  88. {
  89. // Do not release the jump key if the jump key is still pressed.
  90. if (!m_jump_key_pressed) {
  91. m_controls[static_cast<int>(Control::JUMP)] = value;
  92. }
  93. }
  94. bool
  95. Controller::hold(Control control) const
  96. {
  97. return m_controls[static_cast<int>(control)];
  98. }
  99. bool
  100. Controller::pressed(Control control) const
  101. {
  102. return !m_old_controls[static_cast<int>(control)] && m_controls[static_cast<int>(control)];
  103. }
  104. bool
  105. Controller::released(Control control) const
  106. {
  107. return m_old_controls[static_cast<int>(control)] && !m_controls[static_cast<int>(control)];
  108. }
  109. void
  110. Controller::update()
  111. {
  112. for (int i = 0; i < static_cast<int>(Control::CONTROLCOUNT); ++i) {
  113. m_old_controls[i] = m_controls[i];
  114. }
  115. }
  116. /* EOF */