From e40f70a3af1142e6c0c89c6ea2ee47b996495661 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 20 Sep 2010 16:30:45 +0000 Subject: corrected trading inconsistencies, improved trade window, initial slider widget --- src/ui/slider.cc | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/ui/slider.cc (limited to 'src/ui/slider.cc') diff --git a/src/ui/slider.cc b/src/ui/slider.cc new file mode 100644 index 0000000..5de82b2 --- /dev/null +++ b/src/ui/slider.cc @@ -0,0 +1,111 @@ +/* + ui/slider.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "math/functions.h" +#include "ui/paint.h" +#include "ui/slider.h" + +namespace ui { + +Slider::Slider(Widget *parent, const float minimum, const float maximum) : Widget(parent) +{ + slider_minimum = minimum; + slider_maximum = maximum; + slider_value = slider_minimum; + + slider_minbutton = new ui::Button(this, "<"); + slider_minbutton->set_border(false); + + slider_maxbutton = new ui::Button(this, ">>"); + slider_maxbutton->set_border(false); + + validate(); +} + +Slider::~Slider() { +} + +void Slider::set_value(const float value) +{ + slider_value = value; + validate(); +} + +void Slider::set_minimum(const float minimum) +{ + slider_minimum = minimum; + validate(); +} + +void Slider::set_maximum(const float maximum) +{ + slider_maximum = maximum; + validate(); +} + +void Slider::set_range(const float minimum, const float maximum) +{ + slider_minimum = minimum; + slider_maximum = maximum; + validate(); +} + +void Slider::validate() +{ + if (slider_minimum > slider_maximum) { + math::swap(slider_minimum, slider_maximum); + } + + math::clamp(slider_value, slider_minimum, slider_maximum); +} + +void Slider::print(const size_t indent) const +{ + std::string marker(""); + con_print << aux::pad_left(marker, indent*2) << label() << " \"" << minimum() << "-" << maximum() << "\"" << std::endl; +} + +bool Slider::on_emit(Widget *sender, const Event event, void *data) +{ + if (event == EventButtonClicked) { + if (sender == slider_minbutton) { + slider_value = slider_minimum; + emit(EventSliderChanged, this); + return true; + } else if (sender == slider_maxbutton) { + slider_value = slider_maximum; + emit(EventSliderChanged, this); + return true; + } + } + + return false; +} + + +void Slider::resize() +{ + // note: slider expects width > height + slider_minbutton->set_size(height(), height()); + slider_minbutton->set_location(0, 0); + + slider_maxbutton->set_size(height(), height()); + slider_maxbutton->set_location(width() - slider_maxbutton->width(), 0); +} + +void Slider::draw() +{ + if (slider_maximum > slider_minimum) { + const float range = (slider_value - slider_minimum) / (slider_maximum - slider_minimum); + const float x = (width() - 3.0f * height()) * range; + Paint::set_color(palette()->foreground()); + Paint::draw_rectangle(math::Vector2f(global_location().x() + height() + x, global_location().y()), math::Vector2f(height(), height())); + } + + Widget::draw(); +} + +} // namespace ui -- cgit v1.2.3