mirror of
https://github.com/love2d/love.git
synced 2026-08-18 03:34:23 +02:00
Add Joystick:getGamepadMappingString and love.joystick.getGamepadMappingString(guid). Resolves issue #1371.
This commit is contained in:
@@ -301,6 +301,33 @@ Joystick::JoystickInput Joystick::getGamepadMapping(const GamepadInput &input) c
|
||||
return jinput;
|
||||
}
|
||||
|
||||
std::string Joystick::getGamepadMappingString() const
|
||||
{
|
||||
char *sdlmapping = nullptr;
|
||||
|
||||
if (controller != nullptr)
|
||||
sdlmapping = SDL_GameControllerMapping(controller);
|
||||
|
||||
if (sdlmapping == nullptr)
|
||||
{
|
||||
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(pguid.c_str());
|
||||
sdlmapping = SDL_GameControllerMappingForGUID(sdlguid);
|
||||
}
|
||||
|
||||
if (sdlmapping == nullptr)
|
||||
return "";
|
||||
|
||||
std::string mappingstr(sdlmapping);
|
||||
SDL_free(sdlmapping);
|
||||
|
||||
// Matches SDL_GameControllerAddMappingsFromRW.
|
||||
if (mappingstr.find_last_of(',') != mappingstr.length() - 1)
|
||||
mappingstr += ",";
|
||||
mappingstr += "platform:" + std::string(SDL_GetPlatform());
|
||||
|
||||
return mappingstr;
|
||||
}
|
||||
|
||||
void *Joystick::getHandle() const
|
||||
{
|
||||
return joyhandle;
|
||||
|
||||
@@ -68,6 +68,7 @@ public:
|
||||
bool isGamepadDown(const std::vector<GamepadButton> &blist) const override;
|
||||
|
||||
JoystickInput getGamepadMapping(const GamepadInput &input) const override;
|
||||
std::string getGamepadMappingString() const override;
|
||||
|
||||
void *getHandle() const override;
|
||||
|
||||
|
||||
@@ -451,6 +451,25 @@ void JoystickModule::loadGamepadMappings(const std::string &mappings)
|
||||
throw love::Exception("Invalid gamepad mappings.");
|
||||
}
|
||||
|
||||
std::string JoystickModule::getGamepadMappingString(const std::string &guid) const
|
||||
{
|
||||
SDL_JoystickGUID sdlguid = SDL_JoystickGetGUIDFromString(guid.c_str());
|
||||
|
||||
char *sdlmapping = SDL_GameControllerMappingForGUID(sdlguid);
|
||||
if (sdlmapping == nullptr)
|
||||
return "";
|
||||
|
||||
std::string mapping(sdlmapping);
|
||||
SDL_free(sdlmapping);
|
||||
|
||||
// Matches SDL_GameControllerAddMappingsFromRW.
|
||||
if (mapping.find_last_of(',') != mapping.length() - 1)
|
||||
mapping += ",";
|
||||
mapping += "platform:" + std::string(SDL_GetPlatform());
|
||||
|
||||
return mapping;
|
||||
}
|
||||
|
||||
std::string JoystickModule::saveGamepadMappings()
|
||||
{
|
||||
std::string mappings;
|
||||
|
||||
@@ -45,19 +45,21 @@ public:
|
||||
virtual ~JoystickModule();
|
||||
|
||||
// Implements Module.
|
||||
const char *getName() const;
|
||||
const char *getName() const override;
|
||||
|
||||
// Implements JoystickModule.
|
||||
love::joystick::Joystick *addJoystick(int deviceindex);
|
||||
void removeJoystick(love::joystick::Joystick *joystick);
|
||||
love::joystick::Joystick *getJoystickFromID(int instanceid);
|
||||
love::joystick::Joystick *getJoystick(int joyindex);
|
||||
int getIndex(const love::joystick::Joystick *joystick);
|
||||
int getJoystickCount() const;
|
||||
love::joystick::Joystick *addJoystick(int deviceindex) override;
|
||||
void removeJoystick(love::joystick::Joystick *joystick) override;
|
||||
love::joystick::Joystick *getJoystickFromID(int instanceid) override;
|
||||
love::joystick::Joystick *getJoystick(int joyindex) override;
|
||||
int getIndex(const love::joystick::Joystick *joystick) override;
|
||||
int getJoystickCount() const override;
|
||||
|
||||
bool setGamepadMapping(const std::string &guid, Joystick::GamepadInput gpinput, Joystick::JoystickInput joyinput);
|
||||
void loadGamepadMappings(const std::string &mappings);
|
||||
std::string saveGamepadMappings();
|
||||
bool setGamepadMapping(const std::string &guid, Joystick::GamepadInput gpinput, Joystick::JoystickInput joyinput) override;
|
||||
void loadGamepadMappings(const std::string &mappings) override;
|
||||
|
||||
std::string getGamepadMappingString(const std::string &guid) const override;
|
||||
std::string saveGamepadMappings() override;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user