2013-06-19 01:12:30 +00:00
|
|
|
#include "SliderComponent.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include "../Renderer.h"
|
2014-01-10 22:01:28 +00:00
|
|
|
#include "../resources/Font.h"
|
|
|
|
#include "../Log.h"
|
2013-06-19 01:12:30 +00:00
|
|
|
|
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-08 00:16:08 +00:00
|
|
|
mMin(min), mMax(max), mIncrement(increment), mMoveRate(0), mRepeatWaitTimer(0), mKnob(window), mSuffix(suffix)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
assert((min - max) != 0);
|
|
|
|
|
|
|
|
mValue = (max + min) / 2;
|
|
|
|
|
|
|
|
//calculate move scale
|
2013-06-19 21:02:42 +00:00
|
|
|
mMoveScale = ((max - min) * 0.0007f) / increment;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-08 00:16:08 +00:00
|
|
|
mKnob.setOrigin(0.5f, 0.5f);
|
|
|
|
mKnob.setImage(":/slider_knob.png");
|
|
|
|
|
2014-01-10 22:01:28 +00:00
|
|
|
setSize(196, 32);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SliderComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
|
|
|
if(config->isMappedTo("left", input))
|
|
|
|
{
|
|
|
|
if(input.value)
|
2013-06-19 21:02:42 +00:00
|
|
|
mMoveRate = -mIncrement;
|
2013-06-19 01:12:30 +00:00
|
|
|
else
|
|
|
|
mMoveRate = 0;
|
|
|
|
|
2013-06-19 21:02:42 +00:00
|
|
|
//setting mRepeatWaitTimer to 0 will trigger an initial move in our update method
|
|
|
|
mRepeatWaitTimer = 0;
|
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(config->isMappedTo("right", input))
|
|
|
|
{
|
|
|
|
if(input.value)
|
2013-06-19 21:02:42 +00:00
|
|
|
mMoveRate = mIncrement;
|
2013-06-19 01:12:30 +00:00
|
|
|
else
|
|
|
|
mMoveRate = 0;
|
|
|
|
|
2013-06-19 21:02:42 +00:00
|
|
|
mRepeatWaitTimer = 0;
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if(mRepeatWaitTimer == 0)
|
|
|
|
mValue += mMoveRate;
|
|
|
|
else if(mRepeatWaitTimer >= 450)
|
|
|
|
mValue += mMoveRate * deltaTime * mMoveScale;
|
|
|
|
|
|
|
|
if(mValue < mMin)
|
|
|
|
mValue = mMin;
|
|
|
|
if(mValue > mMax)
|
|
|
|
mValue = mMax;
|
|
|
|
|
2014-01-10 22:01:28 +00:00
|
|
|
onValueChanged();
|
|
|
|
|
2013-06-19 21:02:42 +00:00
|
|
|
if(mRepeatWaitTimer < 450)
|
|
|
|
mRepeatWaitTimer += deltaTime;
|
|
|
|
}
|
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
GuiComponent::update(deltaTime);
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void SliderComponent::render(const Eigen::Affine3f& parentTrans)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Affine3f trans = parentTrans * getTransform();
|
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
|
|
|
|
const int lineWidth = 2;
|
2014-03-08 00:16:08 +00:00
|
|
|
Renderer::drawRect((int)mKnob.getSize().x() / 2, (int)mSize.y() / 2 - lineWidth / 2, (int)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-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())
|
|
|
|
mFont = Font::get((int)(mSize.y() * 0.7f));
|
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();
|
|
|
|
|
|
|
|
float w = mFont->sizeText(max).x();
|
2014-03-08 00:16:08 +00:00
|
|
|
mValueCache = std::shared_ptr<TextCache>(mFont->buildTextCache(val, mSize.x() - w, 0, 0x777777FF));
|
2014-01-10 22:01:28 +00:00
|
|
|
mValueCache->metrics.size[0] = w; // fudge the width
|
|
|
|
}
|
2014-03-08 00:16:08 +00:00
|
|
|
|
|
|
|
// update knob position/size
|
|
|
|
if(mKnob.getTextureSize().y() > mSize.y()) // only downscale
|
|
|
|
mKnob.setResize(0, mSize.y());
|
|
|
|
else
|
|
|
|
mKnob.setResize(0, 0);
|
|
|
|
|
|
|
|
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;
|
|
|
|
prompts.push_back(HelpPrompt("left/right", "adjust"));
|
|
|
|
return prompts;
|
|
|
|
}
|