Skip to content

Commit

Permalink
feat: ✨ Allow internal code to add listeners that don't conflict with…
Browse files Browse the repository at this point in the history
… user listener names
  • Loading branch information
ion098 committed Jul 8, 2024
1 parent e14160d commit 54b5302
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions include/gamepad/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Controller {
m_A {};
float m_LeftX = 0, m_LeftY = 0, m_RightX = 0, m_RightY = 0;
Button Fake {};
static std::string unique_name();
static Button Controller::*button_to_ptr(pros::controller_digital_e_t button);
void updateButton(pros::controller_digital_e_t button_id);
pros::Controller controller;
Expand Down
20 changes: 13 additions & 7 deletions src/gamepad/controller.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
#include "gamepad/controller.hpp"
#include "gamepad/todo.hpp"
#include <atomic>

namespace Gamepad {

bool Button::onPress(std::string listenerName, std::function<void(void)> func) const {
return this->onPressEvent.add_listener(std::move(listenerName), std::move(func));
return this->onPressEvent.add_listener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onLongPress(std::string listenerName, std::function<void(void)> func) const {
return this->onLongPressEvent.add_listener(std::move(listenerName), std::move(func));
return this->onLongPressEvent.add_listener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onRelease(std::string listenerName, std::function<void(void)> func) const {
return this->onReleaseEvent.add_listener(std::move(listenerName), std::move(func));
return this->onReleaseEvent.add_listener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::onShortRelease(std::string listenerName, std::function<void(void)> func) const {
return this->onShortReleaseEvent.add_listener(std::move(listenerName), std::move(func));
return this->onShortReleaseEvent.add_listener(std::move(listenerName) + "_user", std::move(func));
}

bool Button::addListener(EventType event, std::string listenerName, std::function<void(void)> func) const {
Expand All @@ -34,9 +35,9 @@ bool Button::addListener(EventType event, std::string listenerName, std::functio
}

bool Button::removeListener(std::string listenerName) const {
return this->onPressEvent.remove_listener(listenerName) || this->onLongPressEvent.remove_listener(listenerName) ||
this->onReleaseEvent.remove_listener(listenerName) ||
this->onShortReleaseEvent.remove_listener(listenerName);
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");
}

void Button::update(const bool is_held) {
Expand Down Expand Up @@ -97,6 +98,11 @@ float Controller::operator[](pros::controller_analog_e_t axis) {
}
}

std::string Controller::unique_name() {
static std::atomic<uint32_t> i = 0;
return std::to_string(i++) + "_internal";
}

Button Controller::*Controller::button_to_ptr(pros::controller_digital_e_t button) {
switch (button) {
case DIGITAL_L1: return &Controller::m_L1;
Expand Down

0 comments on commit 54b5302

Please sign in to comment.