ECSTASY
All in the name
Loading...
Searching...
No Matches
KeySequenceListener.cpp
Go to the documentation of this file.
1
11
14
16{
18 : _sequence(sequence), _callback(callback)
19 {
20 reset();
21 }
22
24 {
25 if (event.pressed) {
26 if (_heldKey == Keyboard::Key::Unknown && event.key == _sequence.at(_validatedKeys.size()))
27 _heldKey = event.key;
28 else if (_heldKey != Keyboard::Key::Unknown || !_validatedKeys.empty()) {
29 reset();
31 return update(event);
32 }
33 } else {
34 if (_heldKey != Keyboard::Key::Unknown && _heldKey == event.key) {
35 _validatedKeys.push_back(_heldKey);
36 _heldKey = Keyboard::Key::Unknown;
37 return isComplete();
38 }
39 }
40 return false;
41 }
42
43 bool KeySequenceListener::isComplete() const noexcept
44 {
45 return _sequence.size() == _validatedKeys.size();
46 }
47
48 void KeySequenceListener::operator()(Registry &registry, Entity e, bool force)
49 {
50 if (force || isComplete()) {
51 _callback(registry, e, *this);
52 reset();
53 }
54 }
55
57 {
58 _sequence = newSequence;
59 reset();
60 }
61
63 {
66 }
67} // namespace ecstasy::integration::event
Encapsulate an index to an entity.
Definition Entity.hpp:35
Base of an ECS architecture.
Definition Registry.hpp:82
KeySequenceListener(const std::vector< Keyboard::Key > &sequence, Callback callback) noexcept
Construct a new Key Sequence Listener object.
void setSequence(const std::vector< Keyboard::Key > &newSequence) noexcept
Change the expected sequence.
bool update(const KeyEvent &event) noexcept
Update the sequence with the given KeyEvent.
bool isComplete() const noexcept
Check whether the sequence is complete or not.
void operator()(Registry &registry, Entity e, bool force=false)
Call the callback and reset() if the sequence is complete or if force.
void reset() noexcept
Reset the sequence completion.
T clear(T... args)
Event integration.
Definition Event.hpp:25
T size(T... args)
Event describing a key pressed or released.
Definition KeyEvent.hpp:25