diff options
Diffstat (limited to 'src/input.h')
| -rw-r--r-- | src/input.h | 45 |
1 files changed, 43 insertions, 2 deletions
| diff --git a/src/input.h b/src/input.h index ad8b761..9f6ff47 100644 --- a/src/input.h +++ b/src/input.h | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | #include <vector> | 5 | #include <vector> |
| 6 | 6 | ||
| 7 | /** | 7 | /** |
| 8 | * Helper class that detects when a key is newly pressed. | 8 | * Helper class that detects when a key or mouse button is newly pressed. |
| 9 | */ | 9 | */ |
| 10 | class Input { | 10 | class Input { |
| 11 | public: | 11 | public: |
| @@ -16,12 +16,22 @@ public: | |||
| 16 | 16 | ||
| 17 | curstate_.assign(s, s + length_); | 17 | curstate_.assign(s, s + length_); |
| 18 | prevstate_ = curstate_; | 18 | prevstate_ = curstate_; |
| 19 | |||
| 20 | prevmouse_ = SDL_GetMouseState(&mousePos_.x(), &mousePos_.y()); | ||
| 21 | prevMousePos_ = mousePos_; | ||
| 22 | curmouse_ = prevmouse_; | ||
| 19 | } | 23 | } |
| 20 | 24 | ||
| 21 | void tick(const Uint8* keystate) | 25 | void tick() |
| 22 | { | 26 | { |
| 27 | const Uint8* keystate = SDL_GetKeyboardState(nullptr); | ||
| 28 | |||
| 23 | prevstate_ = std::move(curstate_); | 29 | prevstate_ = std::move(curstate_); |
| 24 | curstate_.assign(keystate, keystate + length_); | 30 | curstate_.assign(keystate, keystate + length_); |
| 31 | |||
| 32 | prevmouse_ = curmouse_; | ||
| 33 | prevMousePos_ = mousePos_; | ||
| 34 | curmouse_ = SDL_GetMouseState(&mousePos_.x(), &mousePos_.y()); | ||
| 25 | } | 35 | } |
| 26 | 36 | ||
| 27 | bool wasPressed(int scancode) const | 37 | bool wasPressed(int scancode) const |
| @@ -29,11 +39,42 @@ public: | |||
| 29 | return curstate_.at(scancode) && !prevstate_.at(scancode); | 39 | return curstate_.at(scancode) && !prevstate_.at(scancode); |
| 30 | } | 40 | } |
| 31 | 41 | ||
| 42 | bool wasClicked(Uint32 mask) const | ||
| 43 | { | ||
| 44 | return (curmouse_ & SDL_BUTTON(mask)) && !(prevmouse_ & SDL_BUTTON(mask)); | ||
| 45 | } | ||
| 46 | |||
| 47 | bool wasReleased(Uint32 mask) const | ||
| 48 | { | ||
| 49 | return !(curmouse_ & SDL_BUTTON(mask)) && (prevmouse_ & SDL_BUTTON(mask)); | ||
| 50 | } | ||
| 51 | |||
| 52 | bool isClicked(Uint32 mask) const | ||
| 53 | { | ||
| 54 | return curmouse_ & SDL_BUTTON(mask); | ||
| 55 | } | ||
| 56 | |||
| 57 | const vec2i& getMousePos() const | ||
| 58 | { | ||
| 59 | return mousePos_; | ||
| 60 | } | ||
| 61 | |||
| 62 | bool hasMouseMoved() const | ||
| 63 | { | ||
| 64 | return mousePos_ != prevMousePos_; | ||
| 65 | } | ||
| 66 | |||
| 32 | private: | 67 | private: |
| 33 | 68 | ||
| 34 | int length_; | 69 | int length_; |
| 35 | std::vector<Uint8> curstate_; | 70 | std::vector<Uint8> curstate_; |
| 36 | std::vector<Uint8> prevstate_; | 71 | std::vector<Uint8> prevstate_; |
| 72 | |||
| 73 | Uint32 curmouse_; | ||
| 74 | Uint32 prevmouse_; | ||
| 75 | |||
| 76 | vec2i mousePos_; | ||
| 77 | vec2i prevMousePos_; | ||
| 37 | }; | 78 | }; |
| 38 | 79 | ||
| 39 | #endif /* end of include guard: INPUT_H_0FB34C42 */ | 80 | #endif /* end of include guard: INPUT_H_0FB34C42 */ |
