move from constructing keypress abstract with handler into setting the handler so that it can be set by the UI.

This commit is contained in:
MatthewColvin 2023-09-29 15:21:56 -05:00
parent 5ed9fcc3d0
commit 160114f54c
5 changed files with 19 additions and 15 deletions

View file

@ -1,5 +1,8 @@
#include "KeyPressAbstract.hpp" #include "KeyPressAbstract.hpp"
KeyPressAbstract::KeyPressAbstract( KeyPressAbstract::KeyPressAbstract() {}
std::function<bool(KeyPressAbstract::KeyEvent)> aKeyEventHandler)
: mKeyEventHandler(std::move(aKeyEventHandler)) {} void KeyPressAbstract::RegisterKeyPressHandler(
std::function<bool(KeyPressAbstract::KeyEvent)> aKeyEventHandler) {
mKeyEventHandler = std::move(aKeyEventHandler);
}

View file

@ -47,10 +47,12 @@ public:
const KeyId mId; const KeyId mId;
const Type mType; const Type mType;
}; };
/// @brief When Constructing pass a callable to be ran for Handling
/// KeyEvents KeyPressAbstract();
/// @param aKeyEventHandler
KeyPressAbstract(std::function<bool(KeyEvent)> aKeyEventHandler); /// @brief Register a SINGLE handler to be used for proccessing keys
/// @param aKeyEventHandler - Callable the Handles KeyEvent
void RegisterKeyPressHandler(std::function<bool(KeyEvent)> aKeyEventHandler);
protected: protected:
/// @brief Function ment to be called regularly to allow /// @brief Function ment to be called regularly to allow

View file

@ -13,7 +13,7 @@ HardwareSimulator::HardwareSimulator()
mBattery(std::make_shared<BatterySimulator>()), mBattery(std::make_shared<BatterySimulator>()),
mDisplay(SDLDisplay::getInstance()), mDisplay(SDLDisplay::getInstance()),
mWifiHandler(std::make_shared<wifiHandlerSim>()), mWifiHandler(std::make_shared<wifiHandlerSim>()),
mKeys(std::make_shared<KeyPressSim>([](auto keypress) { return true; })) { mKeys(std::make_shared<KeyPressSim>()) {
mHardwareStatusTitleUpdate = std::thread([this] { mHardwareStatusTitleUpdate = std::thread([this] {
int dataToShow = 0; int dataToShow = 0;
while (true) { while (true) {

View file

@ -3,9 +3,8 @@
#include <memory> #include <memory>
KeyPressSim::KeyPressSim( KeyPressSim::KeyPressSim()
std::function<bool(KeyPressAbstract::KeyEvent)> aKeyHandler) : mKeyGrabberThread([this] {
: KeyPressAbstract(std::move(aKeyHandler)), mKeyGrabberThread([this] {
while (true) { while (true) {
HandleKeyPresses(); HandleKeyPresses();
std::this_thread::sleep_for(std::chrono::milliseconds(50)); std::this_thread::sleep_for(std::chrono::milliseconds(50));
@ -35,9 +34,9 @@ void KeyPressSim::GrabKeys() {
void KeyPressSim::HandleKeyPresses() { void KeyPressSim::HandleKeyPresses() {
std::lock_guard lock(mQueueGaurd); std::lock_guard lock(mQueueGaurd);
while (!mKeyEventQueue.empty()) { while (!mKeyEventQueue.empty()) {
printf(mKeyEventQueue.front().mType == KeyEvent::Type::Release ? "release" if (mKeyEventHandler) {
: "press"); mKeyEventHandler(mKeyEventQueue.front());
mKeyEventHandler(mKeyEventQueue.front()); }
mKeyEventQueue.pop(); mKeyEventQueue.pop();
} }
}; };

View file

@ -10,7 +10,7 @@ class KeyPressSim : public KeyPressAbstract {
public: public:
static constexpr auto MaxQueueableKeyEvents = 3; static constexpr auto MaxQueueableKeyEvents = 3;
KeyPressSim(std::function<bool(KeyPressAbstract::KeyEvent)> aKeyHandler); KeyPressSim();
void GrabKeys(); void GrabKeys();
void HandleKeyPresses() override; void HandleKeyPresses() override;