Input.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * Triangles
  3. * Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3 of the License.
  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. */
  17. #include "Input.hpp"
  18. Input::Input(const Input& other): _type(other._type), _positive(other._positive)
  19. {
  20. std::memcpy(&_event, &other._event, sizeof(sf::Event));
  21. }
  22. Input& Input::operator=(const Input& other)
  23. {
  24. std::memcpy(&_event, &other._event, sizeof(sf::Event));
  25. _type = other._type;
  26. return *this;
  27. }
  28. Input::Input(const sf::Keyboard::Key& key, int type): _type(type), _positive(true)
  29. {
  30. _event.type = sf::Event::EventType::KeyPressed;
  31. _event.key.code = key;
  32. }
  33. Input::Input(const sf::Mouse::Button& button, int type): _type(type), _positive(true)
  34. {
  35. _event.type = sf::Event::EventType::MouseButtonPressed;
  36. _event.mouseButton.button = button;
  37. }
  38. Input::Input(const int joystickButton, int type): _type(type), _positive(true)
  39. {
  40. _event.type = sf::Event::EventType::JoystickButtonPressed;
  41. _event.joystickButton.button = joystickButton;
  42. }
  43. Input::Input(const sf::Joystick::Axis& axis, bool positive): _type(Type::RealTime|Type::Pressed), _positive(positive)
  44. {
  45. _event.type = sf::Event::EventType::JoystickMoved;
  46. _event.joystickMove.joystickId = 0;
  47. _event.joystickMove.axis = axis;
  48. }
  49. float Input::test() const
  50. {
  51. switch(_event.type)
  52. {
  53. case sf::Event::EventType::KeyPressed:
  54. if(_type & Type::Pressed)
  55. return sf::Keyboard::isKeyPressed(_event.key.code) ? 1.f : 0.f;
  56. break;
  57. case sf::Event::EventType::MouseButtonPressed:
  58. if(_type & Type::Pressed)
  59. return sf::Mouse::isButtonPressed(_event.mouseButton.button) ? 1.f : 0.f;
  60. break;
  61. case sf::Event::EventType::JoystickButtonPressed:
  62. if(_type & Type::Pressed)
  63. return sf::Joystick::isButtonPressed(0, _event.joystickButton.button) ? 1.f : 0.f;
  64. break;
  65. case sf::Event::EventType::JoystickMoved:
  66. if(_type & Type::Pressed)
  67. {
  68. float f = sf::Joystick::getAxisPosition(_event.joystickMove.joystickId, _event.joystickMove.axis) / 100.f;
  69. if(!_positive)
  70. f = -f;
  71. return f > 0.f ? f : 0.f;
  72. }
  73. break;
  74. default:
  75. break;
  76. }
  77. return 0.f;
  78. }
  79. bool Input::operator==(const sf::Event& event) const
  80. {
  81. switch(event.type)
  82. {
  83. case sf::Event::EventType::KeyPressed:
  84. if(_type & Type::Pressed && _event.type == sf::Event::EventType::KeyPressed)
  85. return event.key.code == _event.key.code;
  86. break;
  87. case sf::Event::EventType::KeyReleased:
  88. if(_type & Type::Released && _event.type == sf::Event::EventType::KeyPressed)
  89. return event.key.code == _event.key.code;
  90. break;
  91. case sf::Event::EventType::MouseButtonPressed:
  92. if(_type & Type::Pressed && _event.type == sf::Event::EventType::MouseButtonPressed)
  93. return event.mouseButton.button == _event.mouseButton.button;
  94. break;
  95. case sf::Event::EventType::MouseButtonReleased:
  96. if(_type & Type::Released && _event.type == sf::Event::EventType::MouseButtonPressed)
  97. return event.mouseButton.button == _event.mouseButton.button;
  98. break;
  99. case sf::Event::EventType::JoystickButtonPressed:
  100. if(_type & Type::Pressed && _event.type == sf::Event::EventType::JoystickButtonPressed)
  101. return event.joystickButton.button == _event.joystickButton.button;
  102. break;
  103. case sf::Event::EventType::JoystickMoved:
  104. if(_type & Type::Pressed && _event.type == sf::Event::EventType::JoystickMoved)
  105. return event.joystickMove.position == _event.joystickMove.position;
  106. break;
  107. default:
  108. break;
  109. }
  110. return false;
  111. }
  112. bool Input::operator==(const Input& other) const
  113. {
  114. return _type == other._type and other == _event;
  115. }