diff --git a/include/gamepad/button.hpp b/include/gamepad/button.hpp index 2eb1df6..1f1fb15 100644 --- a/include/gamepad/button.hpp +++ b/include/gamepad/button.hpp @@ -125,6 +125,15 @@ class Button { * @endcode */ bool addListener(EventType event, std::string listenerName, std::function func) const; + /** + * @brief + * + * @param event + * @param listenerName + * @return true + * @return false + */ + bool hasListener(EventType event, std::string listenerName) const; /** * @brief Removes a listener from the button * @warning Usage of this function is discouraged. @@ -141,7 +150,7 @@ class Button { * Gamepad::master.L1.removeListener("do_something"); * @endcode */ - bool removeListener(std::string listenerName) const; + bool removeListener(EventType event, std::string listenerName) const; /** * @brief Returns a value indicating whether the button is currently being held. diff --git a/include/gamepad/event_handler.hpp b/include/gamepad/event_handler.hpp index 76ab6cc..8c72c76 100644 --- a/include/gamepad/event_handler.hpp +++ b/include/gamepad/event_handler.hpp @@ -53,6 +53,11 @@ template class EventHandler { return false; } + bool has_listener(Key key) { + auto i = std::find(keys.begin(), keys.end(), std::move(key)); + return i != keys.end(); + } + /** * @brief Whther or not there are any listeners registered * diff --git a/src/gamepad/bindings.cpp b/src/gamepad/bindings.cpp index 42bebce..17c7c75 100644 --- a/src/gamepad/bindings.cpp +++ b/src/gamepad/bindings.cpp @@ -8,38 +8,46 @@ bool motorTwoButton(std::string name, pros::Motor motor, Button& fwd_button, But enum states { FWD, REV, NONE }; bool ret_val = true; + ret_val &= fwd_button.hasListener(EventType::ON_PRESS, "Bindings" + name); + ret_val &= fwd_button.hasListener(EventType::ON_RELEASE, "Bindings" + name); + ret_val &= rev_button.hasListener(EventType::ON_PRESS, "Bindings" + name); + ret_val &= rev_button.hasListener(EventType::ON_RELEASE, "Bindings" + name); + if (!ret_val) return false; + auto state = std::make_shared(NONE); - ret_val &= fwd_button.onPress("Bindings" + name, [=]() { + fwd_button.onPress("Bindings" + name, [=]() { *state = FWD; motor.move(127); }); - ret_val &= fwd_button.onRelease("Bindings" + name, [=]() { + fwd_button.onRelease("Bindings" + name, [=]() { if (*state == FWD) { *state = NONE; motor.move(0); } }); - ret_val &= rev_button.onPress("Bindings" + name, [=]() { + rev_button.onPress("Bindings" + name, [=]() { *state = REV; motor.move(-127); }); - ret_val &= rev_button.onRelease("Bindings" + name, [=]() { + rev_button.onRelease("Bindings" + name, [=]() { if (*state == REV) { *state = NONE; motor.move(0); } }); - return ret_val; + return true; } bool motorOneButton(std::string name, pros::Motor motor, Button& toggle_button, uint32_t speed) { bool ret_val = true; + ret_val &= toggle_button.hasListener(EventType::ON_PRESS, "Bindings" + name); + if (!ret_val) return false; auto state = std::make_shared(false); ret_val &= toggle_button.onPress("Bindings" + name, [=]() { if (*state) motor.move(speed); else motor.move(0); *state = !*state; }); - return ret_val; + return true; } } // namespace Gamepad::Bindings \ No newline at end of file diff --git a/src/gamepad/button.cpp b/src/gamepad/button.cpp index 14bf96e..3815739 100644 --- a/src/gamepad/button.cpp +++ b/src/gamepad/button.cpp @@ -33,11 +33,32 @@ bool Button::addListener(EventType event, std::string listenerName, std::functio } } -bool Button::removeListener(std::string listenerName) const { - return this->onPressEvent.remove_listener(listenerName + "_user") || - this->onLongPressEvent.remove_listener(listenerName + "_user") || - this->onReleaseEvent.remove_listener(listenerName + "_user") || - this->onShortReleaseEvent.remove_listener(listenerName + "_user"); +bool Button::removeListener(EventType event, std::string listenerName) const { + switch (event) { + case Gamepad::EventType::ON_PRESS: return this->onPressEvent.remove_listener(listenerName + "_user"); + case Gamepad::EventType::ON_LONG_PRESS: return this->onLongPressEvent.remove_listener(listenerName + "_user"); + case Gamepad::EventType::ON_RELEASE: return this->onReleaseEvent.remove_listener(listenerName + "_user"); + case Gamepad::EventType::ON_SHORT_RELEASE: + return this->onShortReleaseEvent.remove_listener(listenerName + "_user"); + default: + TODO("add error logging") + errno = EINVAL; + return false; + } +} + +bool Button::hasListener(EventType event, std::string listenerName) const { + switch (event) { + case Gamepad::EventType::ON_PRESS: return this->onPressEvent.has_listener(listenerName + "_user"); + case Gamepad::EventType::ON_LONG_PRESS: return this->onLongPressEvent.has_listener(listenerName + "_user"); + case Gamepad::EventType::ON_RELEASE: return this->onReleaseEvent.has_listener(listenerName + "_user"); + case Gamepad::EventType::ON_SHORT_RELEASE: + return this->onShortReleaseEvent.has_listener(listenerName + "_user"); + default: + TODO("add error logging") + errno = EINVAL; + return false; + } } void Button::update(const bool is_held) {