2013-06-19 01:12:30 +00:00
|
|
|
#include "SliderComponent.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include "../Renderer.h"
|
|
|
|
|
2013-06-19 21:02:42 +00:00
|
|
|
SliderComponent::SliderComponent(Window* window, float min, float max, float increment) : GuiComponent(window),
|
|
|
|
mMin(min), mMax(max), mIncrement(increment), mMoveRate(0), mRepeatWaitTimer(0)
|
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
|
|
|
|
|
|
|
setSize(Vector2u(128, 32));
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if(mRepeatWaitTimer < 450)
|
|
|
|
mRepeatWaitTimer += deltaTime;
|
|
|
|
}
|
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
GuiComponent::update(deltaTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SliderComponent::onRender()
|
|
|
|
{
|
|
|
|
//render line
|
|
|
|
const int lineWidth = 2;
|
|
|
|
Renderer::drawRect(0, mSize.y / 2 - lineWidth / 2, mSize.x, lineWidth, 0x000000CC);
|
|
|
|
|
|
|
|
//render left end
|
|
|
|
const int capWidth = (int)(mSize.x * 0.03f);
|
|
|
|
Renderer::drawRect(0, 0, capWidth, mSize.y, 0x000000CC);
|
|
|
|
|
|
|
|
//render right end
|
|
|
|
Renderer::drawRect(mSize.x - capWidth, 0, capWidth, mSize.y, 0x000000CC);
|
|
|
|
|
|
|
|
//render our value
|
|
|
|
const int lineLength = mSize.x - capWidth;
|
|
|
|
Renderer::drawRect((int)(((mValue + mMin) / mMax) * lineLength), 0, capWidth, mSize.y, 0x0000FFFF);
|
|
|
|
|
|
|
|
GuiComponent::onRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SliderComponent::setSize(Vector2u size)
|
|
|
|
{
|
|
|
|
mSize = size;
|
|
|
|
}
|
2013-06-19 21:02:42 +00:00
|
|
|
|
|
|
|
void SliderComponent::setValue(float value)
|
|
|
|
{
|
|
|
|
mValue = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
float SliderComponent::getValue()
|
|
|
|
{
|
|
|
|
return mValue;
|
|
|
|
}
|