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"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "Settings.h"
|
|
|
|
#include "Util.h"
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2014-04-07 00:15:02 +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
|
|
|
|
|
|
|
#define ICON_TEXT_SPACING 8 // space between [icon] and [text] (px)
|
|
|
|
#define ENTRY_SPACING 16 // space between [text] and next [icon] (px)
|
|
|
|
|
2017-11-03 00:33:08 +00:00
|
|
|
static const std::map<std::string, const char*> ICON_PATH_MAP {
|
|
|
|
{ "up/down", ":/help/dpad_updown.svg" },
|
|
|
|
{ "left/right", ":/help/dpad_leftright.svg" },
|
|
|
|
{ "up/down/left/right", ":/help/dpad_all.svg" },
|
|
|
|
{ "a", ":/help/button_a.svg" },
|
|
|
|
{ "b", ":/help/button_b.svg" },
|
|
|
|
{ "x", ":/help/button_x.svg" },
|
|
|
|
{ "y", ":/help/button_y.svg" },
|
|
|
|
{ "l", ":/help/button_l.svg" },
|
|
|
|
{ "r", ":/help/button_r.svg" },
|
|
|
|
{ "start", ":/help/button_start.svg" },
|
|
|
|
{ "select", ":/help/button_select.svg" }
|
|
|
|
};
|
2014-01-25 23:34:29 +00:00
|
|
|
|
|
|
|
HelpComponent::HelpComponent(Window* window) : GuiComponent(window)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelpComponent::clearPrompts()
|
|
|
|
{
|
|
|
|
mPrompts.clear();
|
2014-03-24 01:33:27 +00:00
|
|
|
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
|
|
|
{
|
2014-03-24 01:33:27 +00:00
|
|
|
mPrompts = prompts;
|
|
|
|
updateGrid();
|
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2014-05-29 20:41:47 +00:00
|
|
|
void HelpComponent::setStyle(const HelpStyle& style)
|
|
|
|
{
|
|
|
|
mStyle = style;
|
|
|
|
updateGrid();
|
|
|
|
}
|
|
|
|
|
2014-03-24 01:33:27 +00:00
|
|
|
void HelpComponent::updateGrid()
|
|
|
|
{
|
|
|
|
if(!Settings::getInstance()->getBool("ShowHelpPrompts") || mPrompts.empty())
|
|
|
|
{
|
|
|
|
mGrid.reset();
|
|
|
|
return;
|
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2014-05-29 20:41:47 +00:00
|
|
|
std::shared_ptr<Font>& font = mStyle.font;
|
|
|
|
|
2017-11-17 14:58:52 +00:00
|
|
|
mGrid = std::make_shared<ComponentGrid>(mWindow, Vector2i((int)mPrompts.size() * 4, 1));
|
2014-03-24 01:33:27 +00:00
|
|
|
// [icon] [spacer1] [text] [spacer2]
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2014-03-24 01:33:27 +00:00
|
|
|
std::vector< std::shared_ptr<ImageComponent> > icons;
|
|
|
|
std::vector< std::shared_ptr<TextComponent> > labels;
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2014-03-24 01:33:27 +00:00
|
|
|
float width = 0;
|
2017-11-13 22:16:38 +00:00
|
|
|
const float height = Math::round(font->getLetterHeight() * 1.25f);
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mPrompts.cbegin(); it != mPrompts.cend(); it++)
|
2014-03-24 01:33:27 +00:00
|
|
|
{
|
|
|
|
auto icon = std::make_shared<ImageComponent>(mWindow);
|
2017-08-22 23:21:33 +00:00
|
|
|
icon->setImage(getIconTexture(it->first.c_str()));
|
2014-05-29 21:27:18 +00:00
|
|
|
icon->setColorShift(mStyle.iconColor);
|
2014-03-24 21:29:56 +00:00
|
|
|
icon->setResize(0, height);
|
2014-03-24 01:33:27 +00:00
|
|
|
icons.push_back(icon);
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2014-05-29 20:41:47 +00:00
|
|
|
auto lbl = std::make_shared<TextComponent>(mWindow, strToUpper(it->second), font, mStyle.textColor);
|
2014-03-24 01:33:27 +00:00
|
|
|
labels.push_back(lbl);
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2014-03-24 01:33:27 +00:00
|
|
|
width += icon->getSize().x() + lbl->getSize().x() + ICON_TEXT_SPACING + ENTRY_SPACING;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
mGrid->setColWidthPerc(col + 1, ICON_TEXT_SPACING / width);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
mGrid->setPosition(Vector3f(mStyle.position.x(), mStyle.position.y(), 0.0f));
|
2014-05-29 20:41:47 +00:00
|
|
|
//mGrid->setPosition(OFFSET_X, Renderer::getScreenHeight() - mGrid->getSize().y() - OFFSET_Y);
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<TextureResource> HelpComponent::getIconTexture(const char* name)
|
|
|
|
{
|
|
|
|
auto it = mIconCache.find(name);
|
2017-11-11 14:56:22 +00:00
|
|
|
if(it != mIconCache.cend())
|
2014-01-25 23:34:29 +00:00
|
|
|
return it->second;
|
|
|
|
|
|
|
|
auto pathLookup = ICON_PATH_MAP.find(name);
|
2017-11-11 14:56:22 +00:00
|
|
|
if(pathLookup == ICON_PATH_MAP.cend())
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
|
|
|
LOG(LogError) << "Unknown help icon \"" << name << "\"!";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if(!ResourceManager::getInstance()->fileExists(pathLookup->second))
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Help icon \"" << name << "\" - corresponding image file \"" << pathLookup->second << "\" misisng!";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<TextureResource> tex = TextureResource::get(pathLookup->second);
|
|
|
|
mIconCache[std::string(name)] = tex;
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:58:16 +00:00
|
|
|
void HelpComponent::setOpacity(unsigned char opacity)
|
|
|
|
{
|
|
|
|
GuiComponent::setOpacity(opacity);
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < mGrid->getChildCount(); i++)
|
|
|
|
{
|
|
|
|
mGrid->getChild(i)->setOpacity(opacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void HelpComponent::render(const Transform4x4f& parentTrans)
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2014-03-24 01:33:27 +00:00
|
|
|
if(mGrid)
|
|
|
|
mGrid->render(trans);
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|