2011-08-03 20:05:33 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2007, 2010 - Bit-Blot
|
|
|
|
|
|
|
|
This file is part of Aquaria.
|
|
|
|
|
|
|
|
Aquaria is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
*/
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
#include "ActionMapper.h"
|
|
|
|
#include "Core.h"
|
|
|
|
|
|
|
|
|
2017-01-14 21:53:20 +00:00
|
|
|
InputDevice getDeviceForActionbutton(int k)
|
|
|
|
{
|
|
|
|
if(k <= KEY_MAXARRAY)
|
|
|
|
return INPUT_KEYBOARD;
|
|
|
|
if(k < MOUSE_BUTTON_EXTRA_END)
|
|
|
|
return INPUT_MOUSE;
|
|
|
|
return INPUT_JOYSTICK;
|
|
|
|
}
|
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
ActionMapper::ActionMapper()
|
|
|
|
{
|
|
|
|
cleared = false;
|
|
|
|
inputEnabled = true;
|
|
|
|
inUpdate = false;
|
2016-07-17 20:25:24 +00:00
|
|
|
//memset(keyStatus, 0, sizeof(keyStatus));
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ActionMapper::~ActionMapper()
|
|
|
|
{
|
|
|
|
clearCreatedEvents();
|
|
|
|
}
|
|
|
|
|
2016-07-16 01:09:44 +00:00
|
|
|
ActionData *ActionMapper::getActionDataByIDAndSource(int actionID, int source)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-16 01:09:44 +00:00
|
|
|
for (ActionDataSet::iterator i = actionData.begin(); i != actionData.end(); ++i)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-16 01:09:44 +00:00
|
|
|
if (i->id == actionID && i->source == source)
|
2011-08-03 20:05:33 +00:00
|
|
|
return &(*i);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-16 01:09:44 +00:00
|
|
|
bool ActionMapper::isActing(int actionID, int source)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-16 01:09:44 +00:00
|
|
|
if(source < 0)
|
|
|
|
{
|
|
|
|
for (ActionDataSet::iterator i = actionData.begin(); i != actionData.end(); ++i)
|
|
|
|
{
|
|
|
|
ActionData& ad = *i;
|
|
|
|
if(ad.id == actionID)
|
|
|
|
for (ButtonList::iterator ii = ad.buttonList.begin(); ii != ad.buttonList.end(); ++ii)
|
2016-07-17 20:25:24 +00:00
|
|
|
if (getKeyState(*ii, source))
|
2016-07-16 01:09:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionData *ad = getActionDataByIDAndSource(actionID, source);
|
2011-08-03 20:05:33 +00:00
|
|
|
if (ad)
|
|
|
|
{
|
|
|
|
ButtonList::iterator i = ad->buttonList.begin();
|
|
|
|
for (; i != ad->buttonList.end(); i++)
|
|
|
|
{
|
2016-07-17 20:25:24 +00:00
|
|
|
if (getKeyState(*i, source))
|
2011-08-03 20:05:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-15 01:22:27 +00:00
|
|
|
void ActionMapper::addAction(int actionID, int k, int source)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-16 01:09:44 +00:00
|
|
|
ActionData *ad = getActionDataByIDAndSource(actionID, source);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2016-06-30 00:58:55 +00:00
|
|
|
if (!ad)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
ActionData data;
|
|
|
|
data.id = actionID;
|
2016-07-16 01:09:44 +00:00
|
|
|
data.source = source;
|
2011-08-03 20:05:33 +00:00
|
|
|
actionData.push_back(data);
|
2016-07-16 01:09:44 +00:00
|
|
|
ad = &actionData.back();
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ad)
|
|
|
|
{
|
2015-08-16 00:24:14 +00:00
|
|
|
if(std::find(ad->buttonList.begin(), ad->buttonList.end(), k) == ad->buttonList.end())
|
|
|
|
ad->buttonList.push_back(k);
|
2016-07-17 20:25:24 +00:00
|
|
|
//keyStatus[k] = core->getKeyState(k);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionMapper::addAction(Event *event, int k, int state)
|
|
|
|
{
|
|
|
|
ActionData data;
|
|
|
|
data.event = event;
|
|
|
|
data.state = state;
|
|
|
|
data.buttonList.push_back(k);
|
|
|
|
actionData.push_back(data);
|
|
|
|
|
2016-07-17 20:25:24 +00:00
|
|
|
//keyStatus[k] = core->getKeyState(k);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Event* ActionMapper::addCreatedEvent(Event *event)
|
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
for (size_t i = 0; i < createdEvents.size(); i++)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
if (createdEvents[i] == event)
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
createdEvents.push_back(event);
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionMapper::clearCreatedEvents()
|
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
for (size_t i = 0; i < createdEvents.size(); i++)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
delete createdEvents[i];
|
|
|
|
}
|
|
|
|
createdEvents.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionMapper::enableInput()
|
|
|
|
{
|
|
|
|
inputEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionMapper::disableInput()
|
|
|
|
{
|
|
|
|
inputEnabled = false;
|
|
|
|
}
|
|
|
|
|
2016-07-17 20:25:24 +00:00
|
|
|
/*
|
2016-07-16 01:09:44 +00:00
|
|
|
bool ActionMapper::pollAction(int actionID, int source)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-16 01:09:44 +00:00
|
|
|
if(source < 0)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-16 01:09:44 +00:00
|
|
|
for (ActionDataSet::iterator i = actionData.begin(); i != actionData.end(); i++)
|
|
|
|
if(i->id == actionID && _pollActionData(*i))
|
|
|
|
return true;
|
|
|
|
return false;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2016-07-16 01:09:44 +00:00
|
|
|
ActionData *ad = getActionDataByIDAndSource(actionID, source);
|
|
|
|
return ad && _pollActionData(*ad);
|
|
|
|
}
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2016-07-16 01:09:44 +00:00
|
|
|
bool ActionMapper::_pollActionData(const ActionData& ad)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-16 01:09:44 +00:00
|
|
|
const ButtonList& blist = ad.buttonList;
|
|
|
|
for (ButtonList::const_iterator j = blist.begin(); j != blist.end(); j++)
|
|
|
|
if (getKeyState((*j)))
|
|
|
|
return true;
|
|
|
|
return false;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
2016-07-17 20:25:24 +00:00
|
|
|
*/
|
2016-07-17 15:50:27 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
void ActionMapper::onUpdate (float dt)
|
2016-05-05 17:40:28 +00:00
|
|
|
{
|
2016-07-17 20:25:24 +00:00
|
|
|
if (inUpdate)
|
|
|
|
return;
|
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
inUpdate = true;
|
2016-07-17 20:25:24 +00:00
|
|
|
cleared = false;
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2016-07-17 20:25:24 +00:00
|
|
|
for (ActionDataSet::iterator i = actionData.begin(); i != actionData.end(); ++i)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2017-01-14 21:53:20 +00:00
|
|
|
for (ButtonList::iterator j = i->buttonList.begin(); j != i->buttonList.end(); j++)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-17 20:25:24 +00:00
|
|
|
const int k = (*j);
|
|
|
|
const ActionData *ad = &(*i);
|
|
|
|
const bool keyChanged = isKeyChanged(k, ad->source);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2016-07-17 20:25:24 +00:00
|
|
|
if (keyChanged)
|
2016-05-05 17:40:28 +00:00
|
|
|
{
|
2016-07-17 20:25:24 +00:00
|
|
|
bool keyState = getKeyState(k, ad->source);
|
2011-08-03 20:05:33 +00:00
|
|
|
if (inputEnabled)
|
|
|
|
{
|
|
|
|
if (ad->event)
|
|
|
|
{
|
2016-07-17 20:25:24 +00:00
|
|
|
if (ad->state==-1 || keyState == !!ad->state)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
ad->event->act();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-14 21:53:20 +00:00
|
|
|
action(ad->id, keyState, ad->source, getDeviceForActionbutton(k));
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
if (core->loopDone) goto out;
|
|
|
|
}
|
2011-08-11 21:43:55 +00:00
|
|
|
if (cleared) { cleared = false; goto out; } // actionData has been cleared, stop iteration
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
inUpdate = false;
|
2016-07-17 20:25:24 +00:00
|
|
|
}
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2016-07-17 20:25:24 +00:00
|
|
|
bool ActionMapper::getKeyState(int k, int sourceID)
|
|
|
|
{
|
|
|
|
if(sourceID < 0)
|
|
|
|
return getKeyState(k);
|
2016-07-17 23:27:58 +00:00
|
|
|
return core->getActionStatus(sourceID)->getKeyState(k);
|
2016-07-17 20:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ActionMapper::getKeyState(int k)
|
|
|
|
{
|
2016-07-17 23:27:58 +00:00
|
|
|
// all including sentinel
|
|
|
|
const int m = core->getMaxActionStatusIndex();
|
|
|
|
for(int i = -1; i <= m; ++i)
|
|
|
|
if(core->getActionStatus(i)->getKeyState(k))
|
2016-07-17 20:25:24 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionMapper::clearActions()
|
|
|
|
{
|
|
|
|
cleared = true;
|
|
|
|
actionData.clear();
|
|
|
|
}
|
2016-07-17 20:25:24 +00:00
|
|
|
|
|
|
|
bool ActionMapper::isKeyChanged(int k, int sourceID)
|
|
|
|
{
|
|
|
|
if(sourceID < 0)
|
|
|
|
return isKeyChanged(k);
|
2016-07-17 23:27:58 +00:00
|
|
|
return core->getActionStatus(sourceID)->isKeyChanged(k);
|
2016-07-17 20:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ActionMapper::isKeyChanged(int k)
|
|
|
|
{
|
2016-07-17 23:27:58 +00:00
|
|
|
// all including sentinel
|
|
|
|
const int m = core->getMaxActionStatusIndex();
|
|
|
|
for(int i = -1; i <= m; ++i)
|
|
|
|
if(core->getActionStatus(i)->isKeyChanged(k))
|
2016-07-17 20:25:24 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
2016-07-17 23:27:58 +00:00
|
|
|
}
|