Skip to content

Commit 44aa6b8

Browse files
committed
HelpComponent: Display different help icons depending on controller
Before this commit, all buttons shown in the help component would use the SNES layout, which wouldn't match when having plugged in an Xbox or Sony Playstation controller. If there are different kinds of controllers plugged in, the input mapping shown will be of the last used or inserted. The intention here is to help solving #442, or at least most of it.
1 parent 1b23914 commit 44aa6b8

File tree

4 files changed

+84
-13
lines changed

4 files changed

+84
-13
lines changed

es-core/src/InputManager.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int SDL_USER_CECBUTTONUP = -1;
3131

3232
InputManager* InputManager::mInstance = NULL;
3333

34-
InputManager::InputManager() : mKeyboardInputConfig(NULL)
34+
InputManager::InputManager() : mKeyboardInputConfig(NULL), mLastUsedKeyboardOrController(DEVICE_KEYBOARD)
3535
{
3636
}
3737

@@ -110,6 +110,8 @@ void InputManager::addJoystickByDeviceIndex(int id)
110110
int numAxes = SDL_JoystickNumAxes(joy);
111111
mPrevAxisValues[joyId] = new int[numAxes];
112112
std::fill(mPrevAxisValues[joyId], mPrevAxisValues[joyId] + numAxes, 0); //initialize array to 0
113+
114+
mLastUsedKeyboardOrController = joyId;
113115
}
114116

115117
void InputManager::removeJoystickByJoystickID(SDL_JoystickID joyId)
@@ -201,6 +203,9 @@ int InputManager::getButtonCountByDevice(SDL_JoystickID id)
201203

202204
InputConfig* InputManager::getInputConfigByDevice(int device)
203205
{
206+
if (device != DEVICE_CEC)
207+
mLastUsedKeyboardOrController = device;
208+
204209
if(device == DEVICE_KEYBOARD)
205210
return mKeyboardInputConfig;
206211
else if(device == DEVICE_CEC)
@@ -209,6 +214,18 @@ InputConfig* InputManager::getInputConfigByDevice(int device)
209214
return mInputConfigs[device];
210215
}
211216

217+
InputConfig* InputManager::getInputConfigForLastUsedDevice() const
218+
{
219+
if(mLastUsedKeyboardOrController == DEVICE_KEYBOARD)
220+
return mKeyboardInputConfig;
221+
222+
const auto it = mInputConfigs.find(mLastUsedKeyboardOrController);
223+
if(it != mInputConfigs.end())
224+
return it->second;
225+
226+
return nullptr; // Could happen if last used controller was unplugged
227+
}
228+
212229
bool InputManager::parseEvent(const SDL_Event& ev, Window* window)
213230
{
214231
bool causedEvent = false;

es-core/src/InputManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class InputManager
2626
std::map<SDL_JoystickID, InputConfig*> mInputConfigs;
2727
InputConfig* mKeyboardInputConfig;
2828
InputConfig* mCECInputConfig;
29+
int mLastUsedKeyboardOrController;
2930

3031
std::map<SDL_JoystickID, int*> mPrevAxisValues;
3132

@@ -56,6 +57,7 @@ class InputManager
5657
std::string getDeviceGUIDString(int deviceId);
5758

5859
InputConfig* getInputConfigByDevice(int deviceId);
60+
InputConfig* getInputConfigForLastUsedDevice() const;
5961

6062
bool parseEvent(const SDL_Event& ev, Window* window);
6163
};

es-core/src/components/HelpComponent.cpp

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
#include "components/ComponentGrid.h"
44
#include "components/ImageComponent.h"
55
#include "components/TextComponent.h"
6-
#include "resources/TextureResource.h"
76
#include "utils/StringUtil.h"
87
#include "Log.h"
98
#include "Settings.h"
9+
#include "InputManager.h"
1010

1111
#define OFFSET_X 12 // move the entire thing right by this amount (px)
1212
#define OFFSET_Y 12 // move the entire thing up by this amount (px)
1313

1414
#define ICON_TEXT_SPACING 8 // space between [icon] and [text] (px)
1515
#define ENTRY_SPACING 16 // space between [text] and next [icon] (px)
1616

17-
static const std::map<std::string, const char*> ICON_PATH_MAP {
17+
static const std::map<std::string, const char*> DEFAULT_ICON_PATH_MAP {
1818
{ "up/down", ":/help/dpad_updown.svg" },
1919
{ "left/right", ":/help/dpad_leftright.svg" },
2020
{ "up/down/left/right", ":/help/dpad_all.svg" },
@@ -29,6 +29,36 @@ static const std::map<std::string, const char*> ICON_PATH_MAP {
2929
{ "select", ":/help/button_select.svg" }
3030
};
3131

32+
static const std::map<std::string, const char*> XBOX_ICON_PATH_MAP {
33+
{ "up/down", ":/help/dpad_updown.svg" },
34+
{ "left/right", ":/help/dpad_leftright.svg" },
35+
{ "up/down/left/right", ":/help/dpad_all.svg" },
36+
{ "a", ":/help/button_b.svg" },
37+
{ "b", ":/help/button_a.svg" },
38+
{ "x", ":/help/button_y.svg" },
39+
{ "y", ":/help/button_x.svg" },
40+
{ "l", ":/help/button_l.svg" },
41+
{ "r", ":/help/button_r.svg" },
42+
{ "lr", ":/help/button_lr.svg" },
43+
{ "start", ":/help/button_start.svg" },
44+
{ "select", ":/help/button_select.svg" }
45+
};
46+
47+
static const std::map<std::string, const char*> PLAYSTATION_ICON_PATH_MAP {
48+
{ "up/down", ":/help/dpad_updown.svg" },
49+
{ "left/right", ":/help/dpad_leftright.svg" },
50+
{ "up/down/left/right", ":/help/dpad_all.svg" },
51+
{ "a", ":/help/button_circle.svg" },
52+
{ "b", ":/help/button_x.svg" },
53+
{ "x", ":/help/button_triangle.svg" },
54+
{ "y", ":/help/button_square.svg" },
55+
{ "l", ":/help/button_l.svg" },
56+
{ "r", ":/help/button_r.svg" },
57+
{ "lr", ":/help/button_lr.svg" },
58+
{ "start", ":/help/button_start.svg" },
59+
{ "select", ":/help/button_select.svg" }
60+
};
61+
3262
HelpComponent::HelpComponent(Window* window) : GuiComponent(window)
3363
{
3464
}
@@ -67,12 +97,14 @@ void HelpComponent::updateGrid()
6797
std::vector< std::shared_ptr<ImageComponent> > icons;
6898
std::vector< std::shared_ptr<TextComponent> > labels;
6999

100+
const auto& iconPathMap = getIconPathMap(InputManager::getInstance()->getInputConfigForLastUsedDevice());
101+
70102
float width = 0;
71103
const float height = Math::round(font->getLetterHeight() * 1.25f);
72104
for(auto it = mPrompts.cbegin(); it != mPrompts.cend(); it++)
73105
{
74106
auto icon = std::make_shared<ImageComponent>(mWindow);
75-
icon->setImage(getIconTexture(it->first.c_str()));
107+
icon->setImage(getIconTexture(it->first.c_str(), iconPathMap));
76108
icon->setColorShift(mStyle.iconColor);
77109
icon->setResize(0, height);
78110
icons.push_back(icon);
@@ -100,26 +132,42 @@ void HelpComponent::updateGrid()
100132
mGrid->setOrigin(mStyle.origin);
101133
}
102134

103-
std::shared_ptr<TextureResource> HelpComponent::getIconTexture(const char* name)
135+
const std::map<std::string, const char*>& HelpComponent::getIconPathMap(InputConfig* inputConfig)
104136
{
105-
auto it = mIconCache.find(name);
106-
if(it != mIconCache.cend())
107-
return it->second;
137+
if(!inputConfig)
138+
return DEFAULT_ICON_PATH_MAP;
139+
140+
const auto& deviceName = inputConfig->getDeviceName();
141+
if(strcasestr(deviceName.c_str(), "xbox"))
142+
return XBOX_ICON_PATH_MAP;
108143

109-
auto pathLookup = ICON_PATH_MAP.find(name);
110-
if(pathLookup == ICON_PATH_MAP.cend())
144+
if(strcasestr(deviceName.c_str(), "sony") || strcasestr(deviceName.c_str(), "playstation"))
145+
return PLAYSTATION_ICON_PATH_MAP;
146+
147+
return DEFAULT_ICON_PATH_MAP;
148+
}
149+
150+
std::shared_ptr<TextureResource> HelpComponent::getIconTexture(const char* name, const std::map<std::string, const char*>& iconPathMap)
151+
{
152+
auto pathLookup = iconPathMap.find(name);
153+
if(pathLookup == iconPathMap.cend())
111154
{
112155
LOG(LogError) << "Unknown help icon \"" << name << "\"!";
113156
return nullptr;
114157
}
158+
159+
auto it = mIconCache.find(pathLookup->second);
160+
if(it != mIconCache.cend())
161+
return it->second;
162+
115163
if(!ResourceManager::getInstance()->fileExists(pathLookup->second))
116164
{
117-
LOG(LogError) << "Help icon \"" << name << "\" - corresponding image file \"" << pathLookup->second << "\" misisng!";
165+
LOG(LogError) << "Help icon \"" << name << "\" - corresponding image file \"" << pathLookup->second << "\" missing!";
118166
return nullptr;
119167
}
120168

121169
std::shared_ptr<TextureResource> tex = TextureResource::get(pathLookup->second);
122-
mIconCache[std::string(name)] = tex;
170+
mIconCache[std::string(pathLookup->second)] = tex;
123171
return tex;
124172
}
125173

es-core/src/components/HelpComponent.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
#include "GuiComponent.h"
66
#include "HelpStyle.h"
7+
#include "resources/TextureResource.h"
8+
9+
#include <string>
710

811
class ComponentGrid;
912
class ImageComponent;
@@ -23,7 +26,8 @@ class HelpComponent : public GuiComponent
2326
void setStyle(const HelpStyle& style);
2427

2528
private:
26-
std::shared_ptr<TextureResource> getIconTexture(const char* name);
29+
const std::map<std::string, const char*>& getIconPathMap(InputConfig* inputConfig);
30+
std::shared_ptr<TextureResource> getIconTexture(const char* name, const std::map<std::string, const char*>& iconPathMap);
2731
std::map< std::string, std::shared_ptr<TextureResource> > mIconCache;
2832

2933
std::shared_ptr<ComponentGrid> mGrid;

0 commit comments

Comments
 (0)