Skip to content

Commit

Permalink
fix: 🐛 Fix broken impl of Button::removeListener
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Feb 2, 2025
1 parent 08e6723 commit 0ac0ebd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
13 changes: 11 additions & 2 deletions include/gamepad/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ class Button {
* @brief Removes a listener from the button
* @warning Usage of this function is discouraged.
*
* @param event the event type of the listener
* @param listenerName The name of the listener to remove
* @return true The specified listener was successfully removed
* @return false The specified listener could not be removed
Expand All @@ -221,10 +222,10 @@ class Button {
* // Add an event listener...
* gamepad::master.L1.addListener(gamepad::ON_PRESS, "do_something", doSomething);
* // ...and now get rid of it
* gamepad::master.L1.removeListener("do_something");
* gamepad::master.L1.removeListener(gamepad::ON_PRESS, "do_something");
* @endcode
*/
int32_t removeListener(std::string listenerName) const;
int32_t removeListener(EventType event, std::string listenerName) const;

/**
* @brief Returns a value indicating whether the button is currently being held.
Expand All @@ -240,6 +241,14 @@ class Button {
* @param is_held Whether or not the button is currently held down
*/
void update(bool is_held);
/**
* @brief Get the handler object for the given event type
*
* @param event The desired event type
* @return nullptr The event value is invalid
* @return _impl::EventHandler<std::string>* A pointer to the given event's handler
*/
_impl::EventHandler<std::string>* get_handler(EventType event) const;
/// How long the threshold should be for the longPress and shortRelease events
mutable uint32_t m_long_press_threshold = 500;
/// How often repeatPress is called
Expand Down
42 changes: 29 additions & 13 deletions src/gamepad/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@
#include "gamepad/todo.hpp"
#include "pros/rtos.hpp"
#include <cstdint>
#include <stdexcept>

namespace gamepad {
_impl::EventHandler<std::string>* Button::get_handler(EventType event) const {
switch (event) {
case gamepad::EventType::ON_PRESS: return &m_on_press_event;
case gamepad::EventType::ON_LONG_PRESS: return &m_on_long_press_event;
case gamepad::EventType::ON_RELEASE: return &m_on_release_event;
case gamepad::EventType::ON_SHORT_RELEASE: return &m_on_short_release_event;
case gamepad::EventType::ON_LONG_RELEASE: return &m_on_long_release_event;
case gamepad::EventType::ON_REPEAT_PRESS: return &m_on_repeat_press_event;
default: return nullptr;
}
}

void Button::setLongPressThreshold(uint32_t threshold) const { m_long_press_threshold = threshold; }

void Button::setRepeatCooldown(uint32_t cooldown) const { m_repeat_cooldown = cooldown; }
Expand Down Expand Up @@ -33,28 +46,31 @@ int32_t Button::onRepeatPress(std::string listenerName, std::function<void(void)
}

int32_t Button::addListener(EventType event, std::string listenerName, std::function<void(void)> func) const {
switch (event) {
case gamepad::EventType::ON_PRESS: return this->onPress(std::move(listenerName), std::move(func));
case gamepad::EventType::ON_LONG_PRESS: return this->onLongPress(std::move(listenerName), std::move(func));
case gamepad::EventType::ON_RELEASE: return this->onRelease(std::move(listenerName), std::move(func));
case gamepad::EventType::ON_SHORT_RELEASE:
return this->onShortRelease(std::move(listenerName), std::move(func));
case gamepad::EventType::ON_LONG_RELEASE: return this->onLongRelease(std::move(listenerName), std::move(func));
case gamepad::EventType::ON_REPEAT_PRESS: return this->onRepeatPress(std::move(listenerName), std::move(func));
default:
TODO("add error logging")
errno = EINVAL;
return UINT32_MAX;
auto handler = this->get_handler(event);
if (handler != nullptr) {
return handler->addListener(listenerName + "_user", func);
} else {
TODO("add error logging")
errno = EINVAL;
return INT32_MAX;
}
}

int32_t Button::removeListener(std::string listenerName) const {
int32_t Button::removeListener(EventType event, std::string listenerName) const {
return m_on_press_event.removeListener(listenerName + "_user") ||
m_on_long_press_event.removeListener(listenerName + "_user") ||
m_on_release_event.removeListener(listenerName + "_user") ||
m_on_short_release_event.removeListener(listenerName + "_user") ||
m_on_long_release_event.removeListener(listenerName + "_user") ||
m_on_repeat_press_event.removeListener(listenerName + "_user");
auto handler = this->get_handler(event);
if (handler != nullptr) {
return handler->removeListener(listenerName + "_user");
} else {
TODO("add error logging")
errno = EINVAL;
return INT32_MAX;
}
}

void Button::update(const bool is_held) {
Expand Down

0 comments on commit 0ac0ebd

Please sign in to comment.