



#include <allegro_flare/allegro_flare.h>

#include <flare_gui/slider.h>








FGUIVerticalSlider::FGUIVerticalSlider(FGUIParent *parent, float x, float y, float w, float h)
	: FGUIWidget(parent, new FGUICollisionBox(x, y, w, h))
	, place(*gimmie_placement())
	, val(0.6)
	, mouse_y(0)
	, notches(0)
{}



void FGUIVerticalSlider::on_draw()
{
	al_draw_filled_rectangle(0, 0, place.w, place.h, color::color(color::black, 0.2));
	al_draw_filled_rectangle(0, place.h*(1.0-val), place.w, place.h, color::color(color::dodgerblue, 1.0));
	FontBin &fonts = *gimmie_fonts();
	//al_draw_text(fonts["consola.ttf 18"], color::white, place.w/2, place.h + 9, ALLEGRO_ALIGN_CENTRE, tostring((int)(val*100)).c_str());
}



void FGUIVerticalSlider::set_notches(int notches)
{
	this->notches = notches;
}



void FGUIVerticalSlider::set_val_by_y(float y)
{
	set_val((place.h-y)/place.h);
}



void FGUIVerticalSlider::set_val(float val)
{
	val = limit<float>(0.0, 1.0, val);
	if (notches >= 3) val = (round(val * notches)) / (float)notches;
	this->val = limit<float>(0.0, 1.0, val);
	on_change();
}



void FGUIVerticalSlider::on_mouse_move(float x, float y, float dx, float dy)
{
	place.transform_coordinates(&x, &y);
	mouse_y = y;
}



void FGUIVerticalSlider::on_mouse_down()
{
	//if (mouse_over && af::current_event->mouse.button == 1) set_val_by_y(mouse_y);
	if (mouse_over) set_val_by_y(mouse_y);
}



void FGUIVerticalSlider::on_drag(float x, float y, float dx, float dy)
{
	place.transform_coordinates(&x, &y);
	set_val_by_y(y);
}



void FGUIVerticalSlider::on_key_char()
{
	if (!focused) return;
	float increment_dist = 0.1;
	if (notches >= 3) increment_dist = 1.0 / notches;
	switch (af::current_event->keyboard.keycode)
	{
		case ALLEGRO_KEY_UP:
			set_val(val + increment_dist);
			break;
		case ALLEGRO_KEY_DOWN:
			set_val(val - increment_dist);
			break;
	}
}



void FGUIVerticalSlider::on_change() {}



float FGUIVerticalSlider::get_val()
{
	return val;
}



float FGUIVerticalSlider::get_val(float min, float max)
{
	return (max-min) * val + min;
}

