2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-28 16:39:18 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-28 16:39:18 +00:00
|
|
|
// HelpComponent.cpp
|
|
|
|
//
|
|
|
|
// Help information in icon and text pairs.
|
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/HelpComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
|
|
|
#include "components/ComponentGrid.h"
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/ImageComponent.h"
|
|
|
|
#include "components/TextComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "resources/TextureResource.h"
|
2018-01-27 17:04:28 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "Settings.h"
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
#define OFFSET_X 12 // Move the entire thing right by this amount (px).
|
|
|
|
#define OFFSET_Y 12 // Move the entire thing up by this amount (px).
|
2014-03-24 01:33:27 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
#define ICON_TEXT_SPACING 8 // Space between [icon] and [text] (px).
|
|
|
|
#define ENTRY_SPACING 16 // Space between [text] and next [icon] (px).
|
2014-03-24 01:33:27 +00:00
|
|
|
|
2021-05-23 17:12:31 +00:00
|
|
|
static std::map<std::string, std::string> sIconPathMap {};
|
2014-01-25 23:34:29 +00:00
|
|
|
|
|
|
|
HelpComponent::HelpComponent(Window* window) : GuiComponent(window)
|
|
|
|
{
|
2021-05-23 17:12:31 +00:00
|
|
|
assignIcons();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpComponent::assignIcons()
|
|
|
|
{
|
|
|
|
std::string controllerType = Settings::getInstance()->getString("InputControllerType");
|
|
|
|
|
|
|
|
sIconPathMap.clear();
|
|
|
|
|
|
|
|
// These graphics files are common between all controller types.
|
|
|
|
sIconPathMap["up/down"] = ":/help/dpad_updown.svg";
|
|
|
|
sIconPathMap["left/right"] = ":/help/dpad_leftright.svg";
|
|
|
|
sIconPathMap["up/down/left/right"] = ":/help/dpad_all.svg";
|
|
|
|
sIconPathMap["thumbstickclick"] = ":/help/thumbstick_click.svg";
|
|
|
|
sIconPathMap["l"] = ":/help/button_l.svg";
|
|
|
|
sIconPathMap["r"] = ":/help/button_r.svg";
|
|
|
|
sIconPathMap["lr"] = ":/help/button_lr.svg";
|
|
|
|
|
|
|
|
// These graphics files are custom per controller type.
|
|
|
|
if (controllerType == "snes") {
|
|
|
|
sIconPathMap["a"] = ":/help/button_a_SNES.svg";
|
|
|
|
sIconPathMap["b"] = ":/help/button_b_SNES.svg";
|
|
|
|
sIconPathMap["x"] = ":/help/button_x_SNES.svg";
|
|
|
|
sIconPathMap["y"] = ":/help/button_y_SNES.svg";
|
|
|
|
sIconPathMap["start"] = ":/help/button_start_SNES.svg";
|
|
|
|
sIconPathMap["select"] = ":/help/button_back_SNES.svg";
|
|
|
|
}
|
|
|
|
else if (controllerType == "ps4") {
|
|
|
|
sIconPathMap["a"] = ":/help/button_a_PS.svg";
|
|
|
|
sIconPathMap["b"] = ":/help/button_b_PS.svg";
|
|
|
|
sIconPathMap["x"] = ":/help/button_x_PS.svg";
|
|
|
|
sIconPathMap["y"] = ":/help/button_y_PS.svg";
|
|
|
|
sIconPathMap["start"] = ":/help/button_start_PS4.svg";
|
|
|
|
sIconPathMap["select"] = ":/help/button_back_PS4.svg";
|
|
|
|
}
|
|
|
|
else if (controllerType == "ps5") {
|
|
|
|
sIconPathMap["a"] = ":/help/button_a_PS.svg";
|
|
|
|
sIconPathMap["b"] = ":/help/button_b_PS.svg";
|
|
|
|
sIconPathMap["x"] = ":/help/button_x_PS.svg";
|
|
|
|
sIconPathMap["y"] = ":/help/button_y_PS.svg";
|
|
|
|
sIconPathMap["start"] = ":/help/button_start_PS5.svg";
|
|
|
|
sIconPathMap["select"] = ":/help/button_back_PS5.svg";
|
|
|
|
}
|
|
|
|
else if (controllerType == "xbox360") {
|
|
|
|
sIconPathMap["a"] = ":/help/button_a_XBOX.svg";
|
|
|
|
sIconPathMap["b"] = ":/help/button_b_XBOX.svg";
|
|
|
|
sIconPathMap["x"] = ":/help/button_x_XBOX.svg";
|
|
|
|
sIconPathMap["y"] = ":/help/button_y_XBOX.svg";
|
|
|
|
sIconPathMap["start"] = ":/help/button_start_XBOX360.svg";
|
|
|
|
sIconPathMap["select"] = ":/help/button_back_XBOX360.svg";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Xbox One and later.
|
|
|
|
sIconPathMap["a"] = ":/help/button_a_XBOX.svg";
|
|
|
|
sIconPathMap["b"] = ":/help/button_b_XBOX.svg";
|
|
|
|
sIconPathMap["x"] = ":/help/button_x_XBOX.svg";
|
|
|
|
sIconPathMap["y"] = ":/help/button_y_XBOX.svg";
|
|
|
|
sIconPathMap["start"] = ":/help/button_start_XBOX.svg";
|
|
|
|
sIconPathMap["select"] = ":/help/button_back_XBOX.svg";
|
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HelpComponent::clearPrompts()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mPrompts.clear();
|
|
|
|
updateGrid();
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 01:33:27 +00:00
|
|
|
void HelpComponent::setPrompts(const std::vector<HelpPrompt>& prompts)
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mPrompts = prompts;
|
|
|
|
updateGrid();
|
2014-03-24 01:33:27 +00:00
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2014-05-29 20:41:47 +00:00
|
|
|
void HelpComponent::setStyle(const HelpStyle& style)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mStyle = style;
|
|
|
|
updateGrid();
|
2014-05-29 20:41:47 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 01:33:27 +00:00
|
|
|
void HelpComponent::updateGrid()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (!Settings::getInstance()->getBool("ShowHelpPrompts") || mPrompts.empty()) {
|
|
|
|
mGrid.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Font>& font = mStyle.font;
|
|
|
|
|
2020-11-17 22:06:54 +00:00
|
|
|
mGrid = std::make_shared<ComponentGrid>(mWindow,
|
|
|
|
Vector2i(static_cast<int>(mPrompts.size()) * 4, 1));
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
// [icon] [spacer1] [text] [spacer2]
|
|
|
|
|
2021-03-27 09:26:13 +00:00
|
|
|
std::vector<std::shared_ptr<ImageComponent>> icons;
|
|
|
|
std::vector<std::shared_ptr<TextComponent>> labels;
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
float width = 0;
|
2020-12-28 10:29:32 +00:00
|
|
|
const float height = std::round(font->getLetterHeight() * 1.25f);
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
for (auto it = mPrompts.cbegin(); it != mPrompts.cend(); it++) {
|
|
|
|
auto icon = std::make_shared<ImageComponent>(mWindow);
|
|
|
|
icon->setImage(getIconTexture(it->first.c_str()));
|
|
|
|
icon->setColorShift(mStyle.iconColor);
|
|
|
|
icon->setResize(0, height);
|
|
|
|
icons.push_back(icon);
|
|
|
|
|
|
|
|
auto lbl = std::make_shared<TextComponent>(mWindow,
|
|
|
|
Utils::String::toUpper(it->second), font, mStyle.textColor);
|
|
|
|
labels.push_back(lbl);
|
|
|
|
|
2021-01-13 18:45:56 +00:00
|
|
|
width += icon->getSize().x() + lbl->getSize().x() +
|
|
|
|
((ICON_TEXT_SPACING + ENTRY_SPACING) * Renderer::getScreenWidthModifier());
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mGrid->setSize(width, height);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < icons.size(); i++) {
|
|
|
|
const int col = i*4;
|
|
|
|
mGrid->setColWidthPerc(col, icons.at(i)->getSize().x() / width);
|
2021-01-13 18:45:56 +00:00
|
|
|
mGrid->setColWidthPerc(col + 1, (ICON_TEXT_SPACING *
|
|
|
|
Renderer::getScreenWidthModifier()) / width);
|
2020-06-28 16:39:18 +00:00
|
|
|
mGrid->setColWidthPerc(col + 2, labels.at(i)->getSize().x() / width);
|
|
|
|
|
|
|
|
mGrid->setEntry(icons.at(i), Vector2i(col, 0), false, false);
|
|
|
|
mGrid->setEntry(labels.at(i), Vector2i(col + 2, 0), false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
mGrid->setPosition(Vector3f(mStyle.position.x(), mStyle.position.y(), 0.0f));
|
|
|
|
//mGrid->setPosition(OFFSET_X, Renderer::getScreenHeight() - mGrid->getSize().y() - OFFSET_Y);
|
|
|
|
mGrid->setOrigin(mStyle.origin);
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<TextureResource> HelpComponent::getIconTexture(const char* name)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
auto it = mIconCache.find(name);
|
|
|
|
if (it != mIconCache.cend())
|
|
|
|
return it->second;
|
|
|
|
|
2021-05-23 17:12:31 +00:00
|
|
|
auto pathLookup = sIconPathMap.find(name);
|
|
|
|
if (pathLookup == sIconPathMap.cend()) {
|
2021-04-05 11:26:25 +00:00
|
|
|
LOG(LogError) << "Unknown help icon \"" << name << "\"";
|
2020-06-28 16:39:18 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!ResourceManager::getInstance()->fileExists(pathLookup->second)) {
|
2021-04-05 11:26:25 +00:00
|
|
|
LOG(LogError) << "Couldn't load help icon \"" << name <<
|
|
|
|
"\" as the file \"" << pathLookup->second << "\" is missing";
|
2020-06-28 16:39:18 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:26:25 +00:00
|
|
|
std::shared_ptr<TextureResource> tex =
|
|
|
|
TextureResource::get(pathLookup->second, false, false, false);
|
2020-06-28 16:39:18 +00:00
|
|
|
mIconCache[std::string(name)] = tex;
|
|
|
|
return tex;
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:58:16 +00:00
|
|
|
void HelpComponent::setOpacity(unsigned char opacity)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
GuiComponent::setOpacity(opacity);
|
2014-05-15 01:58:16 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
for (unsigned int i = 0; i < mGrid->getChildCount(); i++)
|
|
|
|
mGrid->getChild(i)->setOpacity(opacity);
|
2014-05-15 01:58:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void HelpComponent::render(const Transform4x4f& parentTrans)
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
if (mGrid)
|
|
|
|
mGrid->render(trans);
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|