Skip to content

Commit

Permalink
refactor: 🥅 Handle listener registration failure properly in bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Sep 10, 2024
1 parent df66934 commit 4547d97
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
11 changes: 10 additions & 1 deletion include/gamepad/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ class Button {
* @endcode
*/
bool addListener(EventType event, std::string listenerName, std::function<void(void)> 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.
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions include/gamepad/event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ template <typename Key, typename... Args> 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
*
Expand Down
20 changes: 14 additions & 6 deletions src/gamepad/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<states>(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<bool>(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
31 changes: 26 additions & 5 deletions src/gamepad/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 4547d97

Please sign in to comment.