From ab959bc6d600b2ea254d4c563f2df0af8b2124ad Mon Sep 17 00:00:00 2001 From: fgenesis Date: Mon, 18 Jul 2016 01:27:58 +0200 Subject: [PATCH] Use sentinel that captures most of the input for sourceID -1 --- Aquaria/DSQ.cpp | 18 +++++++++++++++--- BBGE/ActionMapper.cpp | 20 +++++++++++--------- BBGE/ActionStatus.cpp | 33 ++++++++++++++++++++++++++------- BBGE/ActionStatus.h | 1 + BBGE/Core.h | 5 ++++- 5 files changed, 57 insertions(+), 20 deletions(-) diff --git a/Aquaria/DSQ.cpp b/Aquaria/DSQ.cpp index c99f252..c34d147 100644 --- a/Aquaria/DSQ.cpp +++ b/Aquaria/DSQ.cpp @@ -4604,6 +4604,17 @@ void DSQ::initActionButtons() { clearActionButtons(); + std::vector allkeys; + // Don't need joysticks keys for this + for(int i = 0; i < MOUSE_BUTTON_EXTRA_END; ++i) + allkeys.push_back(i); + + // create sentinel + ActionButtonStatus *allbtn = new ActionButtonStatus; + allbtn->importQuery(&allkeys[0], allkeys.size()); + actionStatus.push_back(allbtn); + + // create the rest for(size_t i = 0; i < user.control.actionSets.size(); ++i) actionStatus.push_back(new ActionButtonStatus); @@ -4612,11 +4623,12 @@ void DSQ::initActionButtons() void DSQ::importActionButtons() { - assert(user.control.actionSets.size() == actionStatus.size()); + assert(user.control.actionSets.size()+1 == actionStatus.size()); - for(size_t i = 0; i < actionStatus.size(); ++i) + // ignore sentinel + for(size_t i = 1; i < actionStatus.size(); ++i) { - const ActionSet& as = user.control.actionSets[i]; + const ActionSet& as = user.control.actionSets[i-1]; ActionButtonStatus *abs = actionStatus[i]; abs->import(as); } diff --git a/BBGE/ActionMapper.cpp b/BBGE/ActionMapper.cpp index 42f3c06..1ead200 100644 --- a/BBGE/ActionMapper.cpp +++ b/BBGE/ActionMapper.cpp @@ -206,14 +206,15 @@ bool ActionMapper::getKeyState(int k, int sourceID) { if(sourceID < 0) return getKeyState(k); - return core->getActionStatus()[sourceID]->getKeyState(k); + return core->getActionStatus(sourceID)->getKeyState(k); } bool ActionMapper::getKeyState(int k) { - const std::vector& absv = core->getActionStatus(); - for(size_t i = 0; i < absv.size(); ++i) - if(absv[i]->getKeyState(k)) + // all including sentinel + const int m = core->getMaxActionStatusIndex(); + for(int i = -1; i <= m; ++i) + if(core->getActionStatus(i)->getKeyState(k)) return true; return false; } @@ -228,14 +229,15 @@ bool ActionMapper::isKeyChanged(int k, int sourceID) { if(sourceID < 0) return isKeyChanged(k); - return core->getActionStatus()[sourceID]->isKeyChanged(k); + return core->getActionStatus(sourceID)->isKeyChanged(k); } bool ActionMapper::isKeyChanged(int k) { - const std::vector& absv = core->getActionStatus(); - for(size_t i = 0; i < absv.size(); ++i) - if(absv[i]->isKeyChanged(k)) + // all including sentinel + const int m = core->getMaxActionStatusIndex(); + for(int i = -1; i <= m; ++i) + if(core->getActionStatus(i)->isKeyChanged(k)) return true; return false; -} \ No newline at end of file +} diff --git a/BBGE/ActionStatus.cpp b/BBGE/ActionStatus.cpp index 6472ec7..2e6a0cc 100644 --- a/BBGE/ActionStatus.cpp +++ b/BBGE/ActionStatus.cpp @@ -19,7 +19,8 @@ void ActionButtonStatus::import(const ActionSet& as) { const ActionInput& inp = as.inputSet[i]; for(int j = 0; j < INP_COMBINED_SIZE; ++j) - found[inp.all[j]] = 1; + if(unsigned(inp.all[j]) < ACTION_BUTTON_ENUM_SIZE) + found[inp.all[j]] = 1; } toQuery.clear(); @@ -27,6 +28,28 @@ void ActionButtonStatus::import(const ActionSet& as) if(found[k]) toQuery.push_back(k); + memset(status, 0, sizeof(status)); + memset(changed, 0, sizeof(changed)); + + update(); +} + +void ActionButtonStatus::importQuery(const int *pKeys, size_t num) +{ + unsigned char found[ACTION_BUTTON_ENUM_SIZE]; + memset(found, 0, sizeof(found)); + for(size_t i = 0; i < num; ++i) + if(unsigned(pKeys[i]) < ACTION_BUTTON_ENUM_SIZE) + found[pKeys[i]] = 1; + + toQuery.clear(); + for(int k = 1; k < sizeof(found); ++k) // ignore [0] + if(found[k]) + toQuery.push_back(k); + + memset(status, 0, sizeof(status)); + memset(changed, 0, sizeof(changed)); + update(); } @@ -37,14 +60,10 @@ void ActionButtonStatus::update() void ActionButtonStatus::_queryAllStatus() { - //memset(status, 0, sizeof(status)); - //memset(changed, 0, sizeof(changed)); - // k==0 is always 0 - //for(size_t i = 0; i < toQuery.size(); ++i) - for(int k = 1; k < ACTION_BUTTON_ENUM_SIZE; ++k) + for(size_t i = 0; i < toQuery.size(); ++i) { - //const int k = toQuery[i]; + const int k = toQuery[i]; bool state = _queryStatus(k); changed[k] = !!status[k] != state; status[k] = state; diff --git a/BBGE/ActionStatus.h b/BBGE/ActionStatus.h index 60f8324..3c8d282 100644 --- a/BBGE/ActionStatus.h +++ b/BBGE/ActionStatus.h @@ -37,6 +37,7 @@ public: inline bool getKeyState(int k) const { return !!status[k]; } inline bool isKeyChanged(int k) { return !!changed[k]; } void import(const ActionSet& as); + void importQuery(const int *pKeys, size_t num); inline const std::vector& getToQuery() const {return toQuery; } inline int getJoystickID() const { return joystickID; } private: diff --git a/BBGE/Core.h b/BBGE/Core.h index 593ea7f..3bc0e6e 100644 --- a/BBGE/Core.h +++ b/BBGE/Core.h @@ -556,7 +556,10 @@ protected: void clearActionButtons(); public: - inline const std::vector& getActionStatus() { return actionStatus; } + // inclusive! + inline int getMaxActionStatusIndex() const { return int(actionStatus.size()) - 2; } + // pass -1 for is a sentinel that captures all input + inline ActionButtonStatus *getActionStatus(int idx) { return actionStatus[idx + 1]; } Joystick *getJoystick(int idx); // warning: may return NULL/contain holes // not the actual number of joysticks!