ES-DE/es-core/src/components/SliderComponent.cpp

144 lines
3.3 KiB
C++
Raw Normal View History

#include "components/SliderComponent.h"
2017-11-01 22:21:10 +00:00
#include "resources/Font.h"
#define MOVE_REPEAT_DELAY 500
#define MOVE_REPEAT_RATE 40
SliderComponent::SliderComponent(Window* window, float min, float max, float increment, const std::string& suffix) : GuiComponent(window),
mMin(min), mMax(max), mSingleIncrement(increment), mMoveRate(0), mKnob(window), mSuffix(suffix)
{
assert((min - max) != 0);
// some sane default value
mValue = (max + min) / 2;
2014-03-08 00:16:08 +00:00
mKnob.setOrigin(0.5f, 0.5f);
2014-03-22 21:02:25 +00:00
mKnob.setImage(":/slider_knob.svg");
2014-03-08 00:16:08 +00:00
2014-03-22 21:02:25 +00:00
setSize(Renderer::getScreenWidth() * 0.15f, Font::get(FONT_SIZE_MEDIUM)->getLetterHeight());
}
bool SliderComponent::input(InputConfig* config, Input input)
{
2018-11-15 16:38:20 +00:00
if(config->isMappedLike("left", input))
{
if(input.value)
setValue(mValue - mSingleIncrement);
mMoveRate = input.value ? -mSingleIncrement : 0;
mMoveAccumulator = -MOVE_REPEAT_DELAY;
return true;
}
2018-11-15 16:38:20 +00:00
if(config->isMappedLike("right", input))
{
if(input.value)
setValue(mValue + mSingleIncrement);
mMoveRate = input.value ? mSingleIncrement : 0;
mMoveAccumulator = -MOVE_REPEAT_DELAY;
return true;
}
return GuiComponent::input(config, input);
}
void SliderComponent::update(int deltaTime)
{
if(mMoveRate != 0)
{
mMoveAccumulator += deltaTime;
while(mMoveAccumulator >= MOVE_REPEAT_RATE)
{
setValue(mValue + mMoveRate);
mMoveAccumulator -= MOVE_REPEAT_RATE;
}
}
GuiComponent::update(deltaTime);
}
void SliderComponent::render(const Transform4x4f& parentTrans)
{
Transform4x4f trans = parentTrans * getTransform();
trans.round();
Renderer::setMatrix(trans);
2014-03-08 00:16:08 +00:00
// render suffix
if(mValueCache)
mFont->renderTextCache(mValueCache.get());
float width = mSize.x() - mKnob.getSize().x() - (mValueCache ? mValueCache->metrics.size.x() + 4 : 0);
//render line
const float lineWidth = 2;
Renderer::drawRect(mKnob.getSize().x() / 2, mSize.y() / 2 - lineWidth / 2, width, lineWidth, 0x777777FF);
2014-03-08 00:16:08 +00:00
//render knob
mKnob.render(trans);
GuiComponent::renderChildren(trans);
}
void SliderComponent::setValue(float value)
{
mValue = value;
if(mValue < mMin)
mValue = mMin;
else if(mValue > mMax)
mValue = mMax;
onValueChanged();
}
float SliderComponent::getValue()
{
return mValue;
}
void SliderComponent::onSizeChanged()
{
if(!mSuffix.empty())
2014-03-22 21:02:25 +00:00
mFont = Font::get((int)(mSize.y()), FONT_PATH_LIGHT);
2014-03-08 00:16:08 +00:00
onValueChanged();
}
void SliderComponent::onValueChanged()
{
2014-03-08 00:16:08 +00:00
// update suffix textcache
if(mFont)
{
std::stringstream ss;
ss << std::fixed;
ss.precision(0);
ss << mValue;
ss << mSuffix;
const std::string val = ss.str();
ss.str("");
ss.clear();
ss << std::fixed;
ss.precision(0);
ss << mMax;
ss << mSuffix;
const std::string max = ss.str();
Vector2f textSize = mFont->sizeText(max);
mValueCache = std::shared_ptr<TextCache>(mFont->buildTextCache(val, mSize.x() - textSize.x(), (mSize.y() - textSize.y()) / 2, 0x777777FF));
mValueCache->metrics.size[0] = textSize.x(); // fudge the width
}
2014-03-08 00:16:08 +00:00
// update knob position/size
2014-03-22 21:02:25 +00:00
mKnob.setResize(0, mSize.y() * 0.7f);
2014-03-08 00:16:08 +00:00
float lineLength = mSize.x() - mKnob.getSize().x() - (mValueCache ? mValueCache->metrics.size.x() + 4 : 0);
mKnob.setPosition(((mValue + mMin) / mMax) * lineLength + mKnob.getSize().x()/2, mSize.y() / 2);
}
std::vector<HelpPrompt> SliderComponent::getHelpPrompts()
{
std::vector<HelpPrompt> prompts;
prompts.push_back(HelpPrompt("left/right", "change"));
return prompts;
}