2013-04-08 14:27:38 +00:00
|
|
|
#ifndef _INPUTCONFIG_H_
|
|
|
|
#define _INPUTCONFIG_H_
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <sstream>
|
2017-04-03 18:45:28 +00:00
|
|
|
#include "pugixml/src/pugixml.hpp"
|
2013-04-08 14:27:38 +00:00
|
|
|
|
|
|
|
#define DEVICE_KEYBOARD -1
|
|
|
|
|
|
|
|
enum InputType
|
|
|
|
{
|
|
|
|
TYPE_AXIS,
|
|
|
|
TYPE_BUTTON,
|
|
|
|
TYPE_HAT,
|
|
|
|
TYPE_KEY,
|
|
|
|
TYPE_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Input
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int device;
|
|
|
|
InputType type;
|
|
|
|
int id;
|
|
|
|
int value;
|
|
|
|
bool configured;
|
|
|
|
|
|
|
|
Input()
|
|
|
|
{
|
|
|
|
device = DEVICE_KEYBOARD;
|
|
|
|
configured = false;
|
|
|
|
id = -1;
|
|
|
|
value = -999;
|
|
|
|
type = TYPE_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
Input(int dev, InputType t, int i, int val, bool conf) : device(dev), type(t), id(i), value(val), configured(conf)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getHatDir(int val)
|
|
|
|
{
|
|
|
|
if(val & SDL_HAT_UP)
|
|
|
|
return "up";
|
|
|
|
else if(val & SDL_HAT_DOWN)
|
|
|
|
return "down";
|
|
|
|
else if(val & SDL_HAT_LEFT)
|
|
|
|
return "left";
|
|
|
|
else if(val & SDL_HAT_RIGHT)
|
|
|
|
return "right";
|
|
|
|
return "neutral?";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string string()
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case TYPE_BUTTON:
|
|
|
|
stream << "Button " << id;
|
|
|
|
break;
|
|
|
|
case TYPE_AXIS:
|
|
|
|
stream << "Axis " << id << (value > 0 ? "+" : "-");
|
|
|
|
break;
|
|
|
|
case TYPE_HAT:
|
|
|
|
stream << "Hat " << id << " " << getHatDir(value);
|
|
|
|
break;
|
|
|
|
case TYPE_KEY:
|
2013-08-18 17:17:24 +00:00
|
|
|
stream << "Key " << SDL_GetKeyName((SDL_Keycode)id);
|
2013-04-08 14:27:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
stream << "Input to string error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class InputConfig
|
|
|
|
{
|
|
|
|
public:
|
2014-03-22 01:12:57 +00:00
|
|
|
InputConfig(int deviceId, const std::string& deviceName, const std::string& deviceGUID);
|
2013-04-08 14:27:38 +00:00
|
|
|
|
|
|
|
void clear();
|
|
|
|
void mapInput(const std::string& name, Input input);
|
2014-04-13 02:09:54 +00:00
|
|
|
void unmapInput(const std::string& name); // unmap all Inputs mapped to this name
|
2013-04-08 14:27:38 +00:00
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
inline int getDeviceId() const { return mDeviceId; };
|
|
|
|
inline const std::string& getDeviceName() { return mDeviceName; }
|
|
|
|
inline const std::string& getDeviceGUIDString() { return mDeviceGUID; }
|
2013-04-08 14:27:38 +00:00
|
|
|
|
|
|
|
//Returns true if Input is mapped to this name, false otherwise.
|
|
|
|
bool isMappedTo(const std::string& name, Input input);
|
|
|
|
|
|
|
|
//Returns a list of names this input is mapped to.
|
|
|
|
std::vector<std::string> getMappedTo(Input input);
|
|
|
|
|
2016-12-02 23:00:10 +00:00
|
|
|
// Returns true if there is an Input mapped to this name, false otherwise.
|
|
|
|
// Writes Input mapped to this name to result if true.
|
|
|
|
bool getInputByName(const std::string& name, Input* result);
|
|
|
|
|
2014-03-22 01:12:57 +00:00
|
|
|
void loadFromXML(pugi::xml_node root);
|
2013-04-11 22:27:27 +00:00
|
|
|
void writeToXML(pugi::xml_node parent);
|
2014-03-22 01:12:57 +00:00
|
|
|
|
|
|
|
bool isConfigured();
|
|
|
|
|
2013-04-08 14:27:38 +00:00
|
|
|
private:
|
|
|
|
std::map<std::string, Input> mNameMap;
|
|
|
|
const int mDeviceId;
|
2013-08-18 17:17:24 +00:00
|
|
|
const std::string mDeviceName;
|
2014-03-22 01:12:57 +00:00
|
|
|
const std::string mDeviceGUID;
|
2013-04-08 14:27:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|