2014-06-25 16:29:58 +00:00
|
|
|
#include "components/RatingComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
|
|
|
#include "resources/TextureResource.h"
|
|
|
|
#include "ThemeData.h"
|
2014-06-25 16:29:58 +00:00
|
|
|
|
2017-06-08 23:18:27 +00:00
|
|
|
RatingComponent::RatingComponent(Window* window) : GuiComponent(window), mColorShift(0xFFFFFFFF)
|
2014-06-25 16:29:58 +00:00
|
|
|
{
|
|
|
|
mFilledTexture = TextureResource::get(":/star_filled.svg", true);
|
|
|
|
mUnfilledTexture = TextureResource::get(":/star_unfilled.svg", true);
|
|
|
|
mValue = 0.5f;
|
2017-10-28 20:24:35 +00:00
|
|
|
mSize = Vector2f(64 * NUM_RATING_STARS, 64);
|
2014-06-25 16:29:58 +00:00
|
|
|
updateVertices();
|
2017-06-14 01:01:08 +00:00
|
|
|
updateColors();
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RatingComponent::setValue(const std::string& value)
|
|
|
|
{
|
|
|
|
if(value.empty())
|
|
|
|
{
|
|
|
|
mValue = 0.0f;
|
|
|
|
}else{
|
|
|
|
mValue = stof(value);
|
|
|
|
if(mValue > 1.0f)
|
|
|
|
mValue = 1.0f;
|
|
|
|
else if(mValue < 0.0f)
|
|
|
|
mValue = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateVertices();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RatingComponent::getValue() const
|
|
|
|
{
|
2015-01-25 17:26:16 +00:00
|
|
|
// do not use std::to_string here as it will use the current locale
|
|
|
|
// and that sometimes encodes decimals as commas
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << mValue;
|
|
|
|
return ss.str();
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 23:18:27 +00:00
|
|
|
void RatingComponent::setOpacity(unsigned char opacity)
|
|
|
|
{
|
|
|
|
mOpacity = opacity;
|
|
|
|
updateColors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RatingComponent::setColorShift(unsigned int color)
|
|
|
|
{
|
|
|
|
mColorShift = color;
|
|
|
|
updateColors();
|
|
|
|
}
|
|
|
|
|
2014-06-25 16:29:58 +00:00
|
|
|
void RatingComponent::onSizeChanged()
|
|
|
|
{
|
|
|
|
if(mSize.y() == 0)
|
|
|
|
mSize[1] = mSize.x() / NUM_RATING_STARS;
|
|
|
|
else if(mSize.x() == 0)
|
|
|
|
mSize[0] = mSize.y() * NUM_RATING_STARS;
|
|
|
|
|
|
|
|
if(mSize.y() > 0)
|
|
|
|
{
|
2017-11-13 22:16:38 +00:00
|
|
|
size_t heightPx = (size_t)Math::round(mSize.y());
|
2017-01-22 23:28:06 +00:00
|
|
|
if (mFilledTexture)
|
|
|
|
mFilledTexture->rasterizeAt(heightPx, heightPx);
|
|
|
|
if(mUnfilledTexture)
|
|
|
|
mUnfilledTexture->rasterizeAt(heightPx, heightPx);
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateVertices();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RatingComponent::updateVertices()
|
|
|
|
{
|
2019-08-31 13:06:23 +00:00
|
|
|
const float numStars = NUM_RATING_STARS;
|
|
|
|
const float h = getSize().y(); // is the same as a single star's width
|
|
|
|
const float w = getSize().y() * mValue * numStars;
|
|
|
|
const float fw = getSize().y() * numStars;
|
|
|
|
|
|
|
|
mVertices[0] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, 0 };
|
|
|
|
mVertices[1] = { { 0.0f, h }, { 0.0f, 0.0f }, 0 };
|
|
|
|
mVertices[2] = { { w, 0.0f }, { mValue * numStars, 1.0f }, 0 };
|
|
|
|
mVertices[3] = { { w, h }, { mValue * numStars, 0.0f }, 0 };
|
|
|
|
|
|
|
|
mVertices[4] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, 0 };
|
|
|
|
mVertices[5] = { { 0.0f, h }, { 0.0f, 0.0f }, 0 };
|
|
|
|
mVertices[6] = { { fw, 0.0f }, { numStars, 1.0f }, 0 };
|
|
|
|
mVertices[7] = { { fw, h }, { numStars, 0.0f }, 0 };
|
|
|
|
|
|
|
|
updateColors();
|
2019-08-31 16:19:43 +00:00
|
|
|
|
|
|
|
// round vertices
|
|
|
|
for(int i = 0; i < 8; ++i)
|
|
|
|
mVertices[i].pos.round();
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 23:18:27 +00:00
|
|
|
void RatingComponent::updateColors()
|
|
|
|
{
|
2019-08-31 13:06:23 +00:00
|
|
|
const float opacity = mOpacity / 255.0;
|
|
|
|
const unsigned int color = Renderer::convertColor(mColorShift & 0xFFFFFF00 | (unsigned char)((mColorShift & 0xFF) * opacity));
|
2019-08-08 20:16:11 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < 8; ++i)
|
|
|
|
mVertices[i].col = color;
|
2017-06-08 23:18:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void RatingComponent::render(const Transform4x4f& parentTrans)
|
2014-06-25 16:29:58 +00:00
|
|
|
{
|
2019-07-22 03:13:48 +00:00
|
|
|
if (!isVisible())
|
|
|
|
return;
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
2014-06-25 16:29:58 +00:00
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
|
|
|
|
mFilledTexture->bind();
|
2019-08-08 20:16:11 +00:00
|
|
|
Renderer::drawTriangleStrips(&mVertices[0], 4);
|
2014-06-25 16:29:58 +00:00
|
|
|
|
|
|
|
mUnfilledTexture->bind();
|
2019-08-08 20:16:11 +00:00
|
|
|
Renderer::drawTriangleStrips(&mVertices[4], 4);
|
2014-06-25 16:29:58 +00:00
|
|
|
|
|
|
|
renderChildren(trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RatingComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
|
|
|
if(config->isMappedTo("a", input) && input.value != 0)
|
|
|
|
{
|
|
|
|
mValue += 1.f / NUM_RATING_STARS;
|
|
|
|
if(mValue > 1.0f)
|
|
|
|
mValue = 0.0f;
|
|
|
|
|
|
|
|
updateVertices();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RatingComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties)
|
|
|
|
{
|
|
|
|
GuiComponent::applyTheme(theme, view, element, properties);
|
|
|
|
|
|
|
|
using namespace ThemeFlags;
|
|
|
|
|
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "rating");
|
|
|
|
if(!elem)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool imgChanged = false;
|
|
|
|
if(properties & PATH && elem->has("filledPath"))
|
|
|
|
{
|
|
|
|
mFilledTexture = TextureResource::get(elem->get<std::string>("filledPath"), true);
|
|
|
|
imgChanged = true;
|
|
|
|
}
|
|
|
|
if(properties & PATH && elem->has("unfilledPath"))
|
|
|
|
{
|
|
|
|
mUnfilledTexture = TextureResource::get(elem->get<std::string>("unfilledPath"), true);
|
|
|
|
imgChanged = true;
|
|
|
|
}
|
|
|
|
|
2017-06-08 23:18:27 +00:00
|
|
|
|
|
|
|
if(properties & COLOR && elem->has("color"))
|
|
|
|
setColorShift(elem->get<unsigned int>("color"));
|
|
|
|
|
2014-06-25 16:29:58 +00:00
|
|
|
if(imgChanged)
|
|
|
|
onSizeChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HelpPrompt> RatingComponent::getHelpPrompts()
|
|
|
|
{
|
|
|
|
std::vector<HelpPrompt> prompts;
|
|
|
|
prompts.push_back(HelpPrompt("a", "add star"));
|
|
|
|
return prompts;
|
|
|
|
}
|