2014-06-20 01:30:09 +00:00
|
|
|
#include "components/SliderComponent.h"
|
2013-06-19 01:12:30 +00:00
|
|
|
#include <assert.h>
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Renderer.h"
|
|
|
|
#include "resources/Font.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "Util.h"
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-22 21:55:18 +00:00
|
|
|
#define MOVE_REPEAT_DELAY 500
|
|
|
|
#define MOVE_REPEAT_RATE 40
|
|
|
|
|
2014-01-10 22:01:28 +00:00
|
|
|
SliderComponent::SliderComponent(Window* window, float min, float max, float increment, const std::string& suffix) : GuiComponent(window),
|
2014-03-22 21:55:18 +00:00
|
|
|
mMin(min), mMax(max), mSingleIncrement(increment), mMoveRate(0), mKnob(window), mSuffix(suffix)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
assert((min - max) != 0);
|
|
|
|
|
2014-03-22 21:55:18 +00:00
|
|
|
// some sane default value
|
2013-06-19 01:12:30 +00:00
|
|
|
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());
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SliderComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
|
|
|
if(config->isMappedTo("left", input))
|
|
|
|
{
|
|
|
|
if(input.value)
|
2014-03-22 21:55:18 +00:00
|
|
|
setValue(mValue - mSingleIncrement);
|
2013-06-19 21:02:42 +00:00
|
|
|
|
2014-03-22 21:55:18 +00:00
|
|
|
mMoveRate = input.value ? -mSingleIncrement : 0;
|
|
|
|
mMoveAccumulator = -MOVE_REPEAT_DELAY;
|
2013-06-19 01:12:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(config->isMappedTo("right", input))
|
|
|
|
{
|
|
|
|
if(input.value)
|
2014-03-22 21:55:18 +00:00
|
|
|
setValue(mValue + mSingleIncrement);
|
2013-06-19 21:02:42 +00:00
|
|
|
|
2014-03-22 21:55:18 +00:00
|
|
|
mMoveRate = input.value ? mSingleIncrement : 0;
|
|
|
|
mMoveAccumulator = -MOVE_REPEAT_DELAY;
|
2013-06-19 01:12:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SliderComponent::update(int deltaTime)
|
|
|
|
{
|
2013-06-19 21:02:42 +00:00
|
|
|
if(mMoveRate != 0)
|
|
|
|
{
|
2014-03-22 21:55:18 +00:00
|
|
|
mMoveAccumulator += deltaTime;
|
|
|
|
while(mMoveAccumulator >= MOVE_REPEAT_RATE)
|
|
|
|
{
|
|
|
|
setValue(mValue + mMoveRate);
|
|
|
|
mMoveAccumulator -= MOVE_REPEAT_RATE;
|
|
|
|
}
|
2013-06-19 21:02:42 +00:00
|
|
|
}
|
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
GuiComponent::update(deltaTime);
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void SliderComponent::render(const Transform4x4f& parentTrans)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
|
|
|
trans.round();
|
2013-07-17 04:18:30 +00:00
|
|
|
Renderer::setMatrix(trans);
|
2013-07-10 11:29:43 +00:00
|
|
|
|
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);
|
2014-01-10 22:01:28 +00:00
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
//render line
|
2014-03-19 20:03:23 +00:00
|
|
|
const float lineWidth = 2;
|
|
|
|
Renderer::drawRect(mKnob.getSize().x() / 2, mSize.y() / 2 - lineWidth / 2, width, lineWidth, 0x777777FF);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-08 00:16:08 +00:00
|
|
|
//render knob
|
|
|
|
mKnob.render(trans);
|
2014-01-10 22:01:28 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
GuiComponent::renderChildren(trans);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
2013-06-19 21:02:42 +00:00
|
|
|
|
|
|
|
void SliderComponent::setValue(float value)
|
|
|
|
{
|
|
|
|
mValue = value;
|
2014-03-22 23:17:14 +00:00
|
|
|
if(mValue < mMin)
|
|
|
|
mValue = mMin;
|
|
|
|
else if(mValue > mMax)
|
|
|
|
mValue = mMax;
|
2014-03-22 21:55:18 +00:00
|
|
|
|
2014-01-10 22:01:28 +00:00
|
|
|
onValueChanged();
|
2013-06-19 21:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float SliderComponent::getValue()
|
|
|
|
{
|
|
|
|
return mValue;
|
|
|
|
}
|
2014-01-10 22:01:28 +00:00
|
|
|
|
|
|
|
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();
|
2014-01-10 22:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SliderComponent::onValueChanged()
|
|
|
|
{
|
2014-03-08 00:16:08 +00:00
|
|
|
// update suffix textcache
|
2014-01-10 22:01:28 +00:00
|
|
|
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();
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f textSize = mFont->sizeText(max);
|
2014-03-17 00:52:15 +00:00
|
|
|
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-01-10 22:01:28 +00:00
|
|
|
}
|
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);
|
2014-01-10 22:01:28 +00:00
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
|
|
|
|
std::vector<HelpPrompt> SliderComponent::getHelpPrompts()
|
|
|
|
{
|
|
|
|
std::vector<HelpPrompt> prompts;
|
2014-03-24 01:33:27 +00:00
|
|
|
prompts.push_back(HelpPrompt("left/right", "change"));
|
2014-01-25 23:34:29 +00:00
|
|
|
return prompts;
|
|
|
|
}
|