Button.cpp 715 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include "input/Button.h"
  2. Button::Button(int key, const char* name)
  3. : key(key), downTime(0), keyboardUp(0), keyboardDown(0),
  4. controllerDown(false), released(false), name(name) {
  5. }
  6. void Button::tick() {
  7. bool down = (keyboardDown > 0) || controllerDown;
  8. bool up = (keyboardUp == keyboardDown) && !controllerDown;
  9. if(released) {
  10. downTime = 0;
  11. }
  12. downTime += down;
  13. released = down && up;
  14. keyboardDown -= keyboardUp;
  15. keyboardUp = 0;
  16. }
  17. bool Button::isDown() const {
  18. return downTime > 0;
  19. }
  20. int Button::getDownTime() const {
  21. return downTime;
  22. }
  23. bool Button::wasReleased() const {
  24. return released;
  25. }
  26. const char* Button::getName() const {
  27. return name;
  28. }