-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttonChanged.hpp
More file actions
64 lines (49 loc) · 1.54 KB
/
buttonChanged.hpp
File metadata and controls
64 lines (49 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef DYNAMIC_BUTTON_CHANGED_HPP
#define DYNAMIC_BUTTON_CHANGED_HPP
// ----------------------------------------------------------------------------------------------------
#include "buttonCached.hpp"
// ----------------------------------------------------------------------------------------------------
/**
* @brief The ButtonChanged class provides convenience methods with additional info about a ButtonCached.
* It tracks the previous state and can thus provide information as to the change that
* occured between the second-to-last and last update() call.
*/
template <typename Button_, typename... Args>
class ButtonChanged : public ButtonCached<Button_, Args...>
{
typedef ButtonCached<Button_, Args...> Button;
public:
ButtonChanged(Args... args)
: Button(args...)
, wasDown_(Button::isDown())
{
}
// if this is not called often enough, this will miss intermediate states
void update()
{
wasDown_ = Button::isDown();
Button::update();
}
/**
* @brief pressed - button was pressed.
*/
bool pressed() const
{
return (Button::isDown() && !wasDown_);
}
/**
* @brief released - button was released.
*/
bool released() const
{
return (!Button::isDown() && wasDown_);
}
bool toggled() const
{
return (Button::isDown() != wasDown_);
}
private:
bool wasDown_;
};
// ----------------------------------------------------------------------------------------------------
#endif // DYNAMIC_BUTTON_CHANGED_HPP