mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2025-10-17 11:59:28 +00:00
Preparations for multiple ActionSets support
This commit is contained in:
parent
95fd453a99
commit
2fd181913e
22 changed files with 481 additions and 304 deletions
|
@ -84,15 +84,15 @@ static std::string inputcode2string(int k)
|
|||
return std::string();
|
||||
}
|
||||
|
||||
static const char *jaxisname(int joysickID, int axis)
|
||||
static const char *jaxisname(int joystickID, int axis)
|
||||
{
|
||||
Joystick *j = joysickID < core->joysticks.size() ? core->joysticks[joysickID] : NULL;
|
||||
Joystick *j = core->getJoystick(joystickID);
|
||||
return j ? j->getAxisName(axis) : NULL;
|
||||
}
|
||||
|
||||
static const char *jbtnname(int joysickID, int btn)
|
||||
static const char *jbtnname(int joystickID, int btn)
|
||||
{
|
||||
Joystick *j = joysickID < core->joysticks.size() ? core->joysticks[joysickID] : NULL;
|
||||
Joystick *j = core->getJoystick(joystickID);
|
||||
return j ? j->getButtonName(btn) : NULL;
|
||||
}
|
||||
|
||||
|
@ -171,7 +171,7 @@ ActionInput::ActionInput()
|
|||
for (int i = 0; i < INP_JOYSIZE; i++) joy[i] = 0;
|
||||
}
|
||||
|
||||
std::string ActionInput::toString()
|
||||
std::string ActionInput::toString() const
|
||||
{
|
||||
std::ostringstream os;
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ public:
|
|||
int key[INP_KEYSIZE];
|
||||
int joy[INP_JOYSIZE];
|
||||
|
||||
std::string toString();
|
||||
std::string toString() const;
|
||||
void fromString(const std::string &read);
|
||||
};
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ bool ActionMapper::isActing(int actionID)
|
|||
return false;
|
||||
}
|
||||
|
||||
void ActionMapper::addAction (int actionID, int k)
|
||||
void ActionMapper::addAction(int actionID, int k, int source)
|
||||
{
|
||||
ActionData *ad = getActionDataByID(actionID);
|
||||
|
||||
|
@ -203,8 +203,8 @@ bool ActionMapper::getKeyState(int k)
|
|||
{
|
||||
int v = k - JOY_BUTTON_0;
|
||||
|
||||
for(size_t i = 0; i < core->joysticks.size(); ++i)
|
||||
if(Joystick *j = core->joysticks[i])
|
||||
for(size_t i = 0; i < core->getNumJoysticks(); ++i)
|
||||
if(Joystick *j = core->getJoystick(i))
|
||||
if(j->isEnabled())
|
||||
if( ((keyState = j->getButton(v))) )
|
||||
break;
|
||||
|
@ -213,8 +213,8 @@ bool ActionMapper::getKeyState(int k)
|
|||
{
|
||||
int v = k - JOY_AXIS_0_POS;
|
||||
|
||||
for(size_t i = 0; i < core->joysticks.size(); ++i)
|
||||
if(Joystick *j = core->joysticks[i])
|
||||
for(size_t i = 0; i < core->getNumJoysticks(); ++i)
|
||||
if(Joystick *j = core->getJoystick(i))
|
||||
if(j->isEnabled())
|
||||
{
|
||||
float ax = j->getAxisUncalibrated(v);
|
||||
|
@ -227,8 +227,8 @@ bool ActionMapper::getKeyState(int k)
|
|||
{
|
||||
int v = k - JOY_AXIS_END_NEG;
|
||||
|
||||
for(size_t i = 0; i < core->joysticks.size(); ++i)
|
||||
if(Joystick *j = core->joysticks[i])
|
||||
for(size_t i = 0; i < core->getNumJoysticks(); ++i)
|
||||
if(Joystick *j = core->getJoystick(i))
|
||||
if(j->isEnabled())
|
||||
{
|
||||
float ax = j->getAxisUncalibrated(v);
|
||||
|
|
|
@ -68,7 +68,7 @@ public:
|
|||
virtual ~ActionMapper();
|
||||
|
||||
void addAction(Event *event, int k, int state=-1);
|
||||
void addAction(int actionID, int k);
|
||||
void addAction(int actionID, int k, int source);
|
||||
|
||||
void removeAction(int actionID);
|
||||
void removeAllActions();
|
||||
|
|
|
@ -21,11 +21,71 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
#include "ActionSet.h"
|
||||
#include "Core.h"
|
||||
|
||||
JoystickConfig::JoystickConfig()
|
||||
{
|
||||
s1ax = 0;
|
||||
s1ay = 0;
|
||||
s2ax = 0;
|
||||
s2ay = 0;
|
||||
s1dead = 0.3f;
|
||||
s2dead = 0.3f;
|
||||
}
|
||||
|
||||
ActionSet::ActionSet()
|
||||
{
|
||||
enabled = true;
|
||||
joystickID = 0;
|
||||
}
|
||||
|
||||
void ActionSet::clearActions()
|
||||
{
|
||||
inputSet.clear();
|
||||
}
|
||||
|
||||
int ActionSet::assignJoystickByName()
|
||||
{
|
||||
int idx = _whichJoystickForName();
|
||||
if(idx >= 0)
|
||||
assignJoystickIdx(idx);
|
||||
return idx;
|
||||
}
|
||||
|
||||
void ActionSet::assignJoystickIdx(int idx)
|
||||
{
|
||||
if(idx < 0)
|
||||
{
|
||||
joystickID = -1;
|
||||
joystickName.clear();
|
||||
joystickGUID.clear();
|
||||
}
|
||||
else if(idx < (int)core->getNumJoysticks())
|
||||
{
|
||||
if(Joystick *j = core->getJoystick(idx))
|
||||
{
|
||||
joystickGUID = j->getGUID();
|
||||
joystickName = j->getName();
|
||||
joystickID = idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ActionSet::_whichJoystickForName()
|
||||
{
|
||||
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);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
ActionInput *ActionSet::getActionInputByName(const std::string &name)
|
||||
{
|
||||
for (ActionInputSet::iterator i = inputSet.begin(); i != inputSet.end(); i++)
|
||||
|
@ -40,37 +100,39 @@ ActionInput *ActionSet::getActionInputByName(const std::string &name)
|
|||
|
||||
|
||||
|
||||
void ActionSet::importAction(ActionMapper *mapper, const std::string &name, int actionID)
|
||||
void ActionSet::importAction(ActionMapper *mapper, const std::string &name, int actionID, int sourceID) const
|
||||
{
|
||||
if (!enabled) return;
|
||||
if (!mapper) return;
|
||||
|
||||
for (int i = 0; i < inputSet.size(); i++)
|
||||
{
|
||||
ActionInput *actionInput = &inputSet[i];
|
||||
const ActionInput *actionInput = &inputSet[i];
|
||||
if (actionInput->name == name)
|
||||
{
|
||||
for (int i = 0; i < INP_MSESIZE; i++)
|
||||
if (actionInput->mse[i])
|
||||
mapper->addAction(actionID, actionInput->mse[i]);
|
||||
mapper->addAction(actionID, actionInput->mse[i], sourceID);
|
||||
for (int i = 0; i < INP_KEYSIZE; i++)
|
||||
if (actionInput->key[i])
|
||||
mapper->addAction(actionID, actionInput->key[i]);
|
||||
mapper->addAction(actionID, actionInput->key[i], sourceID);
|
||||
for (int i = 0; i < INP_JOYSIZE; i++)
|
||||
if (actionInput->joy[i])
|
||||
mapper->addAction(actionID, actionInput->joy[i]);
|
||||
mapper->addAction(actionID, actionInput->joy[i], sourceID);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ActionSet::importAction(ActionMapper *mapper, const std::string &name, Event *event, int state)
|
||||
void ActionSet::importAction(ActionMapper *mapper, const std::string &name, Event *event, int state) const
|
||||
{
|
||||
if (!enabled) return;
|
||||
if (!mapper) return;
|
||||
|
||||
for (int i = 0; i < inputSet.size(); i++)
|
||||
{
|
||||
ActionInput *actionInput = &inputSet[i];
|
||||
const ActionInput *actionInput = &inputSet[i];
|
||||
if (actionInput->name == name)
|
||||
{
|
||||
for (int i = 0; i < INP_MSESIZE; i++)
|
||||
|
@ -91,15 +153,13 @@ void ActionSet::importAction(ActionMapper *mapper, const std::string &name, Even
|
|||
ActionInput *ActionSet::addActionInput(const std::string &name)
|
||||
{
|
||||
ActionInput *a = getActionInputByName(name);
|
||||
if (!a)
|
||||
{
|
||||
ActionInput newa;
|
||||
newa.name = name;
|
||||
inputSet.push_back(newa);
|
||||
a = getActionInputByName(name);
|
||||
}
|
||||
if(a)
|
||||
return a;
|
||||
|
||||
return a;
|
||||
ActionInput newa;
|
||||
newa.name = name;
|
||||
inputSet.push_back(newa);
|
||||
return &inputSet.back();
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -32,22 +32,42 @@ typedef std::vector<ActionInput> ActionInputSet;
|
|||
class ActionMapper;
|
||||
class Event;
|
||||
|
||||
struct JoystickConfig
|
||||
{
|
||||
JoystickConfig();
|
||||
int s1ax, s1ay, s2ax, s2ay;
|
||||
float s1dead, s2dead;
|
||||
};
|
||||
|
||||
class ActionSet
|
||||
{
|
||||
public:
|
||||
void importAction(ActionMapper *mapper, const std::string &name, Event *event, int state);
|
||||
void importAction(ActionMapper *mapper, const std::string &name, int actionID);
|
||||
ActionSet();
|
||||
|
||||
// import this ActionSet into ActionMapper
|
||||
void importAction(ActionMapper *mapper, const std::string &name, Event *event, int state) const;
|
||||
void importAction(ActionMapper *mapper, const std::string &name, int actionID, int sourceID) const;
|
||||
void clearActions();
|
||||
int assignJoystickByName(); // -1 if no such joystick found
|
||||
void assignJoystickIdx(int idx);
|
||||
|
||||
ActionInput *addActionInput(const std::string &name);
|
||||
|
||||
|
||||
|
||||
ActionInput *getActionInputByName(const std::string &name);
|
||||
|
||||
int joystickID;
|
||||
|
||||
// --- Saved in config ---
|
||||
ActionInputSet inputSet;
|
||||
JoystickConfig joycfg;
|
||||
std::string joystickName;
|
||||
std::string joystickGUID;
|
||||
std::string name;
|
||||
bool enabled;
|
||||
// -----------------------
|
||||
|
||||
//std::string insertInputIntoString(const std::string &string);
|
||||
private:
|
||||
int _whichJoystickForName(); // -1 if no souch joystick found
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -3034,3 +3034,9 @@ void Core::onJoystickRemoved(int instanceID)
|
|||
}
|
||||
// TODO: fixup ActionMapper?
|
||||
}
|
||||
|
||||
Joystick *Core::getJoystick(int idx)
|
||||
{
|
||||
size_t i = idx;
|
||||
return i < joysticks.size() ? joysticks[i] : NULL;
|
||||
}
|
||||
|
|
|
@ -558,6 +558,10 @@ protected:
|
|||
std::string _extraDataDir;
|
||||
|
||||
public:
|
||||
Joystick *getJoystick(int idx); // warning: may return NULL/contain holes
|
||||
// not the actual number of joysticks!
|
||||
size_t getNumJoysticks() const { return joysticks.size(); }
|
||||
private:
|
||||
std::vector<Joystick*> joysticks;
|
||||
};
|
||||
|
||||
|
|
|
@ -55,6 +55,16 @@ Joystick::Joystick()
|
|||
enabled = true;
|
||||
}
|
||||
|
||||
const char *Joystick::getName() const
|
||||
{
|
||||
return name.c_str();
|
||||
}
|
||||
|
||||
const char *Joystick::getGUID() const
|
||||
{
|
||||
return guid.c_str();
|
||||
}
|
||||
|
||||
bool Joystick::init(int stick)
|
||||
{
|
||||
stickIndex = stick;
|
||||
|
@ -89,7 +99,13 @@ bool Joystick::init(int stick)
|
|||
if (sdl_joy)
|
||||
{
|
||||
#ifdef BBGE_BUILD_SDL2
|
||||
debugLog(std::string("Initialized Joystick [") + SDL_JoystickName(sdl_joy) + "]");
|
||||
const char *n = SDL_JoystickName(sdl_joy);
|
||||
name = n ? n : "<?>";
|
||||
SDL_JoystickGUID jg = SDL_JoystickGetGUID(sdl_joy);
|
||||
char guidbuf[40];
|
||||
guid = &guidbuf[0];
|
||||
SDL_JoystickGetGUIDString(jg, &guidbuf[0], sizeof(guidbuf));
|
||||
debugLog(std::string("Initialized Joystick [") + name + "], GUID [" + guid + "]");
|
||||
if (sdl_controller)
|
||||
{
|
||||
debugLog("Joystick is a Game Controller");
|
||||
|
@ -99,7 +115,9 @@ bool Joystick::init(int stick)
|
|||
debugLog("Joystick has force feedback support");
|
||||
instanceID = SDL_JoystickInstanceID(sdl_joy);
|
||||
#else
|
||||
debugLog(std::string("Initialized Joystick [") + SDL_JoystickName(stick)) + std::string("]"));
|
||||
const char *n = SDL_JoystickName(stick);
|
||||
name = n ? n : "<?>";
|
||||
debugLog(std::string("Initialized Joystick [") + name + "]");
|
||||
instanceID = SDL_JoystickIndex(sdl_joy);
|
||||
#endif
|
||||
|
||||
|
@ -108,6 +126,9 @@ bool Joystick::init(int stick)
|
|||
if(numJoyAxes > MAX_JOYSTICK_AXIS)
|
||||
numJoyAxes = MAX_JOYSTICK_AXIS;
|
||||
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,8 @@ public:
|
|||
|
||||
const char *getAxisName(int axis) const;
|
||||
const char *getButtonName(int btn) const;
|
||||
const char *getName() const;
|
||||
const char *getGUID() const;
|
||||
|
||||
Vector rightStick;
|
||||
|
||||
|
@ -56,6 +58,8 @@ private:
|
|||
int numJoyAxes;
|
||||
SDL_Joystick *sdl_joy;
|
||||
float axisRaw[MAX_JOYSTICK_AXIS];
|
||||
std::string name;
|
||||
std::string guid;
|
||||
|
||||
# ifdef BBGE_BUILD_SDL2
|
||||
SDL_GameController *sdl_controller;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue