1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-29 03:33:48 +00:00

Use sentinel that captures most of the input for sourceID -1

This commit is contained in:
fgenesis 2016-07-18 01:27:58 +02:00
parent c943759ce1
commit ab959bc6d6
5 changed files with 57 additions and 20 deletions

View file

@ -4604,6 +4604,17 @@ void DSQ::initActionButtons()
{
clearActionButtons();
std::vector<int> 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);
}

View file

@ -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<ActionButtonStatus*>& 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<ActionButtonStatus*>& 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;
}
}

View file

@ -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;

View file

@ -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<int>& getToQuery() const {return toQuery; }
inline int getJoystickID() const { return joystickID; }
private:

View file

@ -556,7 +556,10 @@ protected:
void clearActionButtons();
public:
inline const std::vector<ActionButtonStatus*>& 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!