controller.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "start",
  27. "escape",
  28. "menu-select",
  29. "menu-select-space",
  30. "menu-back",
  31. "remove",
  32. "cheat-menu",
  33. "debug-menu",
  34. "console",
  35. "peek-left",
  36. "peek-right",
  37. "peek-up",
  38. "peek-down",
  39. nullptr
  40. };
  41. } // namespace
  42. std::ostream& operator<<(std::ostream& os, Control control)
  43. {
  44. return os << g_control_names[static_cast<int>(control)];
  45. }
  46. std::string Control_to_string(Control control)
  47. {
  48. return g_control_names[static_cast<int>(control)];
  49. }
  50. std::optional<Control> Control_from_string(const std::string& text)
  51. {
  52. for(int i = 0; g_control_names[i] != nullptr; ++i) {
  53. if (text == g_control_names[i]) {
  54. return static_cast<Control>(i);
  55. }
  56. }
  57. return std::nullopt;
  58. }
  59. Controller::Controller():
  60. m_touchscreen(false),
  61. m_jump_key_pressed(false)
  62. {
  63. reset();
  64. }
  65. Controller::~Controller()
  66. {}
  67. void
  68. Controller::reset()
  69. {
  70. for (int i = 0; i < static_cast<int>(Control::CONTROLCOUNT); ++i) {
  71. m_controls[i] = false;
  72. m_old_controls[i] = false;
  73. }
  74. m_touchscreen = false;
  75. m_jump_key_pressed = false;
  76. }
  77. void
  78. Controller::set_control(Control control, bool value)
  79. {
  80. if (control == Control::JUMP) {
  81. m_jump_key_pressed = value;
  82. }
  83. m_controls[static_cast<int>(control)] = value;
  84. }
  85. void
  86. Controller::set_jump_key_with_up(bool value)
  87. {
  88. // Do not release the jump key if the jump key is still pressed.
  89. if (!m_jump_key_pressed) {
  90. m_controls[static_cast<int>(Control::JUMP)] = value;
  91. }
  92. }
  93. void
  94. Controller::set_touchscreen(bool value)
  95. {
  96. m_touchscreen = value;
  97. }
  98. bool
  99. Controller::hold(Control control) const
  100. {
  101. return m_controls[static_cast<int>(control)];
  102. }
  103. bool
  104. Controller::pressed(Control control) const
  105. {
  106. return !m_old_controls[static_cast<int>(control)] && m_controls[static_cast<int>(control)];
  107. }
  108. bool
  109. Controller::released(Control control) const
  110. {
  111. return m_old_controls[static_cast<int>(control)] && !m_controls[static_cast<int>(control)];
  112. }
  113. bool
  114. Controller::is_touchscreen() const
  115. {
  116. return m_touchscreen;
  117. }
  118. void
  119. Controller::update()
  120. {
  121. for (int i = 0; i < static_cast<int>(Control::CONTROLCOUNT); ++i) {
  122. m_old_controls[i] = m_controls[i];
  123. }
  124. }
  125. /* EOF */