#include "Slider.h"

#include "Image.h"
#include "Globals.h"

using namespace Framework;

Slider::Slider()
    : DrawableBackground()
{
    value = 50;
}

Slider::~Slider() {}

void Slider::setValue(float value)
{
    this->value = value;
    rend |= 1;
}

float Slider::getValue() const
{
    return value;
}

// Processes a mouse event. Called automatically by the framework.
//  me: the event
void Slider::doMouseEvent(MouseEvent& me, bool userRet)
{
    if (gr.x
        && (me.id == ME_PLeft
            || (me.id == ME_Move && getMouseState(M_Left))))
    {
        value = (1 - ((gr.x - 10) - me.mx) / (gr.x - 20.0f)) * 100;
        if (value < 0) value = 0;
        if (value > 100) value = 100;
        rend |= 1;
    }
    DrawableBackground::doMouseEvent(me, userRet);
}

// renders the drawing into a specific image
//  zRObj: The image to be drawn into
void Slider::render(Image& zRObj)
{
    if (hasStyle(Style::Visible))
    {
        DrawableBackground::render(zRObj);
        if (zRObj.setDrawOptions(pos, gr))
        {
            zRObj.drawLineH(10, gr.y / 2, gr.x - 20, 0xFFFFFFFF);
            zRObj.fillRegion(
                (int)(gr.x / 100 * value - 10), 0, 20, gr.y, 0xFFFFFFFF);
            zRObj.releaseDrawOptions();
        }
    }
}