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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
#include "ActionSet.h"
|
|
|
|
#include "Core.h"
|
|
|
|
|
2016-07-15 01:22:27 +00:00
|
|
|
JoystickConfig::JoystickConfig()
|
|
|
|
{
|
|
|
|
s1ax = 0;
|
2016-07-16 20:08:39 +00:00
|
|
|
s1ay = 1;
|
|
|
|
s2ax = 2;
|
|
|
|
s2ay = 3;
|
2016-07-15 01:22:27 +00:00
|
|
|
s1dead = 0.3f;
|
|
|
|
s2dead = 0.3f;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionSet::ActionSet()
|
|
|
|
{
|
2018-01-21 11:47:32 +00:00
|
|
|
inputMode = INPUT_NODEVICE;
|
2016-07-15 01:22:27 +00:00
|
|
|
enabled = true;
|
2016-07-17 03:54:09 +00:00
|
|
|
joystickID = ACTIONSET_REASSIGN_JOYSTICK;
|
2016-07-15 01:22:27 +00:00
|
|
|
}
|
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
void ActionSet::clearActions()
|
|
|
|
{
|
|
|
|
inputSet.clear();
|
|
|
|
}
|
|
|
|
|
2016-07-17 03:54:09 +00:00
|
|
|
int ActionSet::assignJoystickByName(bool force)
|
2016-07-15 01:22:27 +00:00
|
|
|
{
|
|
|
|
int idx = _whichJoystickForName();
|
2016-07-17 03:54:09 +00:00
|
|
|
if(idx >= 0 || force)
|
|
|
|
assignJoystickIdx(idx, false);
|
2016-07-15 01:22:27 +00:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2016-07-17 03:54:09 +00:00
|
|
|
void ActionSet::assignJoystickIdx(int idx, bool updateValues)
|
2016-07-15 01:22:27 +00:00
|
|
|
{
|
|
|
|
if(idx < 0)
|
|
|
|
{
|
2016-07-17 03:54:09 +00:00
|
|
|
if(updateValues && idx != ACTIONSET_REASSIGN_JOYSTICK)
|
|
|
|
{
|
|
|
|
joystickName.clear();
|
|
|
|
joystickGUID.clear();
|
|
|
|
}
|
2016-07-15 01:22:27 +00:00
|
|
|
}
|
|
|
|
else if(idx < (int)core->getNumJoysticks())
|
|
|
|
{
|
|
|
|
if(Joystick *j = core->getJoystick(idx))
|
|
|
|
{
|
2016-07-17 03:54:09 +00:00
|
|
|
if(updateValues)
|
|
|
|
{
|
|
|
|
joystickGUID = j->getGUID();
|
|
|
|
joystickName = j->getName();
|
|
|
|
}
|
2016-07-15 01:22:27 +00:00
|
|
|
}
|
2016-07-17 03:54:09 +00:00
|
|
|
else
|
|
|
|
idx = -1;
|
2016-07-15 01:22:27 +00:00
|
|
|
}
|
2016-07-17 03:54:09 +00:00
|
|
|
joystickID = idx;
|
2016-07-15 01:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ActionSet::_whichJoystickForName()
|
|
|
|
{
|
2016-07-17 03:54:09 +00:00
|
|
|
if(joystickName == "NONE")
|
|
|
|
return -1;
|
|
|
|
|
2016-07-16 01:09:44 +00:00
|
|
|
if(joystickGUID.length() && joystickName.length())
|
|
|
|
for(size_t i = 0; i < core->getNumJoysticks(); ++i)
|
|
|
|
if(Joystick *j = core->getJoystick(i))
|
|
|
|
if(j->getGUID()[0] && joystickGUID == j->getGUID() && joystickName == j->getName())
|
|
|
|
return int(i);
|
|
|
|
|
2016-07-15 01:22:27 +00:00
|
|
|
if(joystickGUID.length())
|
|
|
|
for(size_t i = 0; i < core->getNumJoysticks(); ++i)
|
|
|
|
if(Joystick *j = core->getJoystick(i))
|
|
|
|
if(j->getGUID()[0] && joystickGUID == j->getGUID())
|
|
|
|
return int(i);
|
|
|
|
|
|
|
|
if(joystickName.length())
|
|
|
|
for(size_t i = 0; i < core->getNumJoysticks(); ++i)
|
|
|
|
if(Joystick *j = core->getJoystick(i))
|
|
|
|
if(joystickName == j->getName())
|
|
|
|
return int(i);
|
|
|
|
|
2016-07-17 03:54:09 +00:00
|
|
|
// first attached
|
|
|
|
if(!joystickGUID.length() && !joystickName.length())
|
|
|
|
for(size_t i = 0; i < core->getNumJoysticks(); ++i)
|
|
|
|
if(Joystick *j = core->getJoystick(i))
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return ACTIONSET_REASSIGN_JOYSTICK;
|
2016-07-15 01:22:27 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 20:08:39 +00:00
|
|
|
void ActionSet::updateJoystick()
|
|
|
|
{
|
2016-07-17 03:54:09 +00:00
|
|
|
bool reassign = joystickID == ACTIONSET_REASSIGN_JOYSTICK;
|
|
|
|
|
|
|
|
if(joystickID >= 0)
|
|
|
|
{
|
|
|
|
Joystick *j = core->getJoystick(joystickID);
|
|
|
|
if(!j)
|
|
|
|
reassign = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(reassign)
|
|
|
|
assignJoystickByName(true);
|
|
|
|
|
2016-07-19 00:45:56 +00:00
|
|
|
// Enable joystick if used by this ActionSet.
|
|
|
|
// There might be other ActionSets that are also set to this
|
|
|
|
// joystick but disabled, so we can't just do
|
|
|
|
// j->setEnabled(enabled) here.
|
2016-07-16 20:08:39 +00:00
|
|
|
Joystick *j = core->getJoystick(joystickID);
|
2016-07-19 00:45:56 +00:00
|
|
|
if(j && enabled)
|
|
|
|
j->setEnabled(true);
|
2016-07-16 20:08:39 +00:00
|
|
|
}
|
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
ActionInput *ActionSet::getActionInputByName(const std::string &name)
|
|
|
|
{
|
|
|
|
for (ActionInputSet::iterator i = inputSet.begin(); i != inputSet.end(); i++)
|
|
|
|
{
|
|
|
|
if (nocasecmp((*i).name, name) == 0)
|
|
|
|
{
|
|
|
|
return &(*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-15 01:22:27 +00:00
|
|
|
void ActionSet::importAction(ActionMapper *mapper, const std::string &name, int actionID, int sourceID) const
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-15 01:22:27 +00:00
|
|
|
if (!enabled) return;
|
2011-08-03 20:05:33 +00:00
|
|
|
if (!mapper) return;
|
|
|
|
|
2017-01-14 17:10:20 +00:00
|
|
|
for (size_t i = 0; i < inputSet.size(); i++)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-15 01:22:27 +00:00
|
|
|
const ActionInput *actionInput = &inputSet[i];
|
2011-08-03 20:05:33 +00:00
|
|
|
if (actionInput->name == name)
|
|
|
|
{
|
2017-01-14 21:53:20 +00:00
|
|
|
for (int i = 0; i < INP_COMBINED_SIZE; i++)
|
2017-01-17 10:15:47 +00:00
|
|
|
if (actionInput->data.all[i])
|
|
|
|
mapper->addAction(actionID, actionInput->data.all[i], sourceID);
|
2011-08-03 20:05:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 01:22:27 +00:00
|
|
|
void ActionSet::importAction(ActionMapper *mapper, const std::string &name, Event *event, int state) const
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-15 01:22:27 +00:00
|
|
|
if (!enabled) return;
|
2011-08-03 20:05:33 +00:00
|
|
|
if (!mapper) return;
|
|
|
|
|
2017-01-14 17:10:20 +00:00
|
|
|
for (size_t i = 0; i < inputSet.size(); i++)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-07-15 01:22:27 +00:00
|
|
|
const ActionInput *actionInput = &inputSet[i];
|
2011-08-03 20:05:33 +00:00
|
|
|
if (actionInput->name == name)
|
|
|
|
{
|
2017-01-14 21:53:20 +00:00
|
|
|
for (int i = 0; i < INP_COMBINED_SIZE; i++)
|
2017-01-17 10:15:47 +00:00
|
|
|
if (actionInput->data.all[i])
|
|
|
|
mapper->addAction(event, actionInput->data.all[i], state);
|
2011-08-03 20:05:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionInput *ActionSet::addActionInput(const std::string &name)
|
|
|
|
{
|
|
|
|
ActionInput *a = getActionInputByName(name);
|
2016-07-15 01:22:27 +00:00
|
|
|
if(a)
|
|
|
|
return a;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2016-07-15 01:22:27 +00:00
|
|
|
ActionInput newa;
|
|
|
|
newa.name = name;
|
|
|
|
inputSet.push_back(newa);
|
|
|
|
return &inputSet.back();
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
2016-06-30 00:58:55 +00:00
|
|
|
/*
|
2011-08-03 20:05:33 +00:00
|
|
|
std::string ActionSet::insertInputIntoString(const std::string &string)
|
|
|
|
{
|
|
|
|
std::string str = string;
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2017-01-14 17:10:20 +00:00
|
|
|
size_t start = str.find('{');
|
|
|
|
size_t end = str.find('}');
|
2011-08-03 20:05:33 +00:00
|
|
|
if (start == std::string::npos || end == std::string::npos)
|
|
|
|
return string;
|
|
|
|
std::string code = str.substr(start+1, end - start);
|
|
|
|
stringToLower(code);
|
|
|
|
std::string part1 = str.substr(0, start);
|
|
|
|
std::string part3 = str.substr(end+1, str.size());
|
2016-05-05 17:40:28 +00:00
|
|
|
|
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
int thing = code.find(':');
|
|
|
|
std::string input = code.substr(0, thing);
|
|
|
|
std::string button = code.substr(thing+1, code.size());
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
char buttonType;
|
|
|
|
int buttonNum;
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
std::istringstream is(button);
|
|
|
|
is >> buttonType >> buttonNum;
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
ActionInput *actionInput=0;
|
|
|
|
actionInput = getActionInputByName(input);
|
|
|
|
if (!actionInput)
|
|
|
|
{
|
|
|
|
// don't have that input, bail
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
int inputCode=0;
|
|
|
|
switch(buttonType)
|
|
|
|
{
|
|
|
|
case 'k':
|
|
|
|
inputCode = actionInput->key[buttonNum];
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
inputCode = actionInput->joy[buttonNum];
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
inputCode = actionInput->mse[buttonNum];
|
|
|
|
break;
|
|
|
|
}
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
std::string part2 = getInputCodeToUserString(inputCode);
|
|
|
|
return part1 + part2 + part3;
|
|
|
|
}
|
2016-06-30 00:58:55 +00:00
|
|
|
*/
|