Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2010-10-26 15:32:42 +0000
committerStijn Buys <ingar@osirion.org>2010-10-26 15:32:42 +0000
commit23c7d2c11170ee8736673e82a88e87a3d2e538f7 (patch)
treecd88e317a4f42391bc3567ed1881c160227cd3c6 /src/ui/slider.cc
parent5189168482f32d8aac630cde27fc0999e70ed57b (diff)
enables ui::Slider mouse dragging, corrected a bug in ui::Widget coordinate mapping
Diffstat (limited to 'src/ui/slider.cc')
-rw-r--r--src/ui/slider.cc55
1 files changed, 54 insertions, 1 deletions
diff --git a/src/ui/slider.cc b/src/ui/slider.cc
index b5a38f1..0abd109 100644
--- a/src/ui/slider.cc
+++ b/src/ui/slider.cc
@@ -12,6 +12,8 @@ namespace ui {
Slider::Slider(Widget *parent, const float minimum, const float maximum) : Widget(parent)
{
+ slider_dragging = false;
+
slider_minimum = minimum;
slider_maximum = maximum;
slider_value = slider_minimum;
@@ -121,11 +123,58 @@ bool Slider::on_keypress(const int key, const unsigned int modifier)
}
return true;
break;
+
+ case 512 + SDL_BUTTON_LEFT:
+ if (slider_maximum > slider_minimum) {
+ // TODO position hit test
+ slider_dragging = true;
+ }
+ return true;
+ break;
+
+ default:
+ break;
+ }
+
+ return false;
+}
+
+bool Slider::on_keyrelease(const int key, const unsigned int modifier)
+{
+ if (key == 512 + SDL_BUTTON_LEFT) {
+ slider_dragging = false;
+ return true;
}
return false;
}
+void Slider::on_mousemove(const math::Vector2f &cursor)
+{
+ if ((width() <= 0) || (height() <= 0)) {
+ return;
+ }
+
+ if (slider_dragging && (slider_maximum > slider_minimum)) {
+ // total width of dragable area
+ if ((cursor.x() >= 2.0f * height()) && (cursor.x() <= width() - 2.0f * height())) {
+ const float w = (width() - 4.0f * height());
+ const float s = w / (slider_maximum - slider_minimum);
+ const float p = cursor.x() - 2.0f * height();
+ const float newvalue = slider_minimum + round(p /s);
+ if (slider_value != newvalue) {
+ slider_value = newvalue;
+ emit(EventSliderChanged, this);
+ }
+ }
+ }
+}
+
+void Slider::on_mouseover(const math::Vector2f &cursor)
+{
+ slider_dragging = false;
+}
+
void Slider::resize()
{
// note: slider expects width > height
@@ -147,7 +196,11 @@ void Slider::draw()
if (slider_maximum > slider_minimum) {
const float range = (slider_value - slider_minimum) / (slider_maximum - slider_minimum);
const float x = (width() - 5.0f * height()) * range;
- Paint::set_color(palette()->foreground());
+ if (slider_dragging) {
+ Paint::set_color(palette()->highlight());
+ } else {
+ Paint::set_color(palette()->foreground());
+ }
Paint::draw_rectangle(math::Vector2f(global_location().x() + 2.0f * height() + x + 1, global_location().y() +1 ), math::Vector2f(height() - 2, height() - 2));
}