No Description
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.

controller.rs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #[derive(Clone, Copy, PartialEq)]
  2. pub enum ControllerAction {
  3. Down,
  4. Up,
  5. }
  6. #[derive(Clone, Copy, PartialEq)]
  7. pub enum ControllerButton {
  8. Left,
  9. Right,
  10. Up,
  11. Down,
  12. A,
  13. B,
  14. Start,
  15. Select,
  16. }
  17. // a virtual controller, used to normalise player input into a standard set of controls
  18. #[derive(Default)]
  19. pub struct Controller {
  20. pub up: bool,
  21. pub down: bool,
  22. pub left: bool,
  23. pub right: bool,
  24. pub a: bool,
  25. pub b: bool,
  26. pub start: bool,
  27. pub select: bool,
  28. up_previous: bool,
  29. down_previous: bool,
  30. left_previous: bool,
  31. right_previous: bool,
  32. a_previous: bool,
  33. b_previous: bool,
  34. start_previous: bool,
  35. select_previous: bool,
  36. }
  37. impl Controller {
  38. pub fn new() -> Controller {
  39. Controller {
  40. up: false,
  41. down: false,
  42. left: false,
  43. right: false,
  44. a: false,
  45. b: false,
  46. start: false,
  47. select: false,
  48. up_previous: false,
  49. down_previous: false,
  50. left_previous: false,
  51. right_previous: false,
  52. a_previous: false,
  53. b_previous: false,
  54. start_previous: false,
  55. select_previous: false,
  56. }
  57. }
  58. pub fn reset_controller(&mut self) {
  59. self.up_previous = self.up;
  60. self.down_previous = self.down;
  61. self.left_previous = self.left;
  62. self.right_previous = self.right;
  63. self.a_previous = self.a;
  64. self.b_previous = self.b;
  65. self.start_previous = self.start;
  66. self.select_previous = self.select;
  67. }
  68. pub fn just_released(&self, button: ControllerButton) -> bool {
  69. match button {
  70. ControllerButton::Left => !self.left && self.left_previous,
  71. ControllerButton::Right => !self.right && self.right_previous,
  72. ControllerButton::Up => !self.up && self.up_previous,
  73. ControllerButton::Down => !self.down && self.down_previous,
  74. ControllerButton::A => !self.a && self.a_previous,
  75. ControllerButton::B => !self.b && self.b_previous,
  76. ControllerButton::Start => !self.start && self.start_previous,
  77. ControllerButton::Select => !self.select && self.select_previous,
  78. }
  79. }
  80. pub fn just_pressed(&self, button: ControllerButton) -> bool {
  81. match button {
  82. ControllerButton::Left => self.left && !self.left_previous,
  83. ControllerButton::Right => self.right && !self.right_previous,
  84. ControllerButton::Up => self.up && !self.up_previous,
  85. ControllerButton::Down => self.down && !self.down_previous,
  86. ControllerButton::A => self.a && !self.a_previous,
  87. ControllerButton::B => self.b && !self.b_previous,
  88. ControllerButton::Start => self.start && !self.start_previous,
  89. ControllerButton::Select => self.select && !self.select_previous,
  90. }
  91. }
  92. pub fn input(&mut self, button: ControllerButton, action: ControllerAction) {
  93. self.up_previous = self.up;
  94. self.down_previous = self.down;
  95. self.left_previous = self.left;
  96. self.right_previous = self.right;
  97. self.a_previous = self.a;
  98. self.b_previous = self.b;
  99. self.start_previous = self.start;
  100. self.select_previous = self.select;
  101. match button {
  102. ControllerButton::Left => {
  103. self.left = self.bool_from_action(action);
  104. }
  105. ControllerButton::Right => {
  106. self.right = self.bool_from_action(action);
  107. }
  108. ControllerButton::Up => {
  109. self.up = self.bool_from_action(action);
  110. }
  111. ControllerButton::Down => {
  112. self.down = self.bool_from_action(action);
  113. }
  114. ControllerButton::A => {
  115. self.a = self.bool_from_action(action);
  116. }
  117. ControllerButton::B => {
  118. self.b = self.bool_from_action(action);
  119. }
  120. ControllerButton::Start => {
  121. self.start = self.bool_from_action(action);
  122. }
  123. ControllerButton::Select => {
  124. self.select = self.bool_from_action(action);
  125. }
  126. };
  127. }
  128. fn bool_from_action(&self, action: ControllerAction) -> bool {
  129. match action {
  130. ControllerAction::Up => false,
  131. ControllerAction::Down => true,
  132. }
  133. }
  134. }