diff --git a/include/gamepad/controller.hpp b/include/gamepad/controller.hpp index 5d5e275..8b92af9 100644 --- a/include/gamepad/controller.hpp +++ b/include/gamepad/controller.hpp @@ -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; diff --git a/src/gamepad/controller.cpp b/src/gamepad/controller.cpp index e2c3438..e7a0b49 100644 --- a/src/gamepad/controller.cpp +++ b/src/gamepad/controller.cpp @@ -1,22 +1,23 @@ #include "gamepad/controller.hpp" #include "gamepad/todo.hpp" +#include namespace Gamepad { bool Button::onPress(std::string listenerName, std::function 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 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 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 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 func) const { @@ -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) { @@ -97,6 +98,11 @@ float Controller::operator[](pros::controller_analog_e_t axis) { } } +std::string Controller::unique_name() { + static std::atomic 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;