Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
path: root/src/ui
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
parent5189168482f32d8aac630cde27fc0999e70ed57b (diff)
enables ui::Slider mouse dragging, corrected a bug in ui::Widget coordinate mapping
Diffstat (limited to 'src/ui')
-rwxr-xr-xsrc/ui/modelview.cc17
-rwxr-xr-xsrc/ui/modelview.h3
-rw-r--r--src/ui/slider.cc55
-rw-r--r--src/ui/slider.h12
-rw-r--r--src/ui/widget.cc4
-rw-r--r--src/ui/widget.h31
6 files changed, 91 insertions, 31 deletions
diff --git a/src/ui/modelview.cc b/src/ui/modelview.cc
index 9542b5f..e756be0 100755
--- a/src/ui/modelview.cc
+++ b/src/ui/modelview.cc
@@ -25,7 +25,7 @@ ModelView::ModelView(Widget *parent, const char *modelname) : Widget(parent)
set_modelname(modelname);
modelview_zoom = 1.0f;
- modelview_manip = false;
+ modelview_dragging = false;
modelview_axis.clear();
modelview_axis.change_direction(180);
@@ -79,8 +79,7 @@ bool ModelView::on_keypress(const int key, const unsigned int modifier)
modelview_zoom = 10.0f;
return true;
} else if (key == 512 + SDL_BUTTON_LEFT) {
- //modelview_manipaxis.clear();
- modelview_manip = true;
+ modelview_dragging = true;
return true;
}
@@ -90,8 +89,7 @@ bool ModelView::on_keypress(const int key, const unsigned int modifier)
bool ModelView::on_keyrelease(const int key, const unsigned int modifier)
{
if (key == 512 + SDL_BUTTON_LEFT) {
- //modelview_manipaxis.clear();
- modelview_manip = false;
+ modelview_dragging = false;
return true;
}
@@ -104,18 +102,16 @@ void ModelView::on_mousemove(const math::Vector2f &cursor)
return;
}
- if (modelview_manip) {
+ if (modelview_dragging) {
const math::Vector2f pos(cursor - modelview_cursor);
const math::Vector3f up(0.0f, 0.0f, 1.0f);
const float zrot = 2.0f * M_PI * pos.x() / width();
modelview_axis.rotate(up, -zrot);
- //modelview_manipaxis.rotate(up, -zrot);
const math::Vector3f left(0.0f, 1.0f, 0.0f);
const float yrot = 2.0f * M_PI * pos.y() / height();
modelview_axis.rotate(left, yrot);
- //modelview_manipaxis.rotate(left, yrot);
modelview_cursor.assign(cursor);
}
@@ -125,8 +121,7 @@ void ModelView::on_mousemove(const math::Vector2f &cursor)
void ModelView::on_mouseover(const math::Vector2f &cursor)
{
modelview_cursor.assign(cursor);
- //modelview_manipaxis.clear();
- modelview_manip = false;
+ modelview_dragging = false;
}
void ModelView::draw_background()
@@ -190,7 +185,7 @@ void ModelView::draw()
/*
// draw manipulation marker
- if (has_mouse_focus() && modelview_manip) {
+ if (has_mouse_focus() && modelview_dragging) {
gl::push();
gl::multmatrix(modelview_manipaxis);
diff --git a/src/ui/modelview.h b/src/ui/modelview.h
index cb01dcb..7cbf76d 100755
--- a/src/ui/modelview.h
+++ b/src/ui/modelview.h
@@ -74,8 +74,7 @@ private:
math::Axis modelview_axis;
math::Vector2f modelview_cursor;
- bool modelview_manip;
- //math::Axis modelview_manipaxis;
+ bool modelview_dragging;
};
}
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));
}
diff --git a/src/ui/slider.h b/src/ui/slider.h
index 46dfd50..bafd848 100644
--- a/src/ui/slider.h
+++ b/src/ui/slider.h
@@ -81,10 +81,19 @@ protected:
/// draw event handler
virtual void draw();
+ /// emit event handler
virtual bool on_emit(Widget *sender, const Event event, void *data=0);
+ /// keypress event handler
virtual bool on_keypress(const int key, const unsigned int modifier);
-
+
+ /// keyrelease event handler
+ virtual bool on_keyrelease(const int key, const unsigned int modifier);
+
+ virtual void on_mouseover(const math::Vector2f &cursor);
+
+ /// mouse movement handler
+ virtual void on_mousemove(const math::Vector2f &cursor);
private:
/// validate slider value
void validate();
@@ -92,6 +101,7 @@ private:
float slider_minimum;
float slider_maximum;
float slider_value;
+ bool slider_dragging;
Button *slider_minbutton;
Button *slider_decbutton;
diff --git a/src/ui/widget.cc b/src/ui/widget.cc
index f663cc6..37bde99 100644
--- a/src/ui/widget.cc
+++ b/src/ui/widget.cc
@@ -402,7 +402,7 @@ void Widget::event_draw()
if (widget_border)
draw_border();
if (debug())
- draw_debug_border();
+ draw_debug();
draw();
for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) {
@@ -449,7 +449,7 @@ bool Widget::on_emit(Widget *sender, const Event event, void *data)
/* -- draw functions ----------------------------------------------- */
-void Widget::draw_debug_border()
+void Widget::draw_debug()
{
Paint::set_color(1.0f, 0.0f, 1.0f, 0.5f);
Paint::draw_border(global_location(), size());
diff --git a/src/ui/widget.h b/src/ui/widget.h
index 530af05..cb42ad0 100644
--- a/src/ui/widget.h
+++ b/src/ui/widget.h
@@ -293,12 +293,23 @@ protected:
/* -- coordinate mapping ----------------------------------- */
+ /// map local widget location to global location
+ inline math::Vector2f global_location() {
+ math::Vector2f v(widget_location);
+ Widget *parent = widget_parent;
+ while (parent) {
+ v += parent->location();
+ parent = parent->parent();
+ }
+ return v;
+ }
+
/// map local coordinates to global coordinates
inline math::Vector2f to_global_coords(const math::Vector2f &local) {
math::Vector2f v(local);
Widget *parent = widget_parent;
do {
- v -= parent->location();
+ v += parent->location();
parent = parent->parent();
} while (parent);
return v;
@@ -309,18 +320,7 @@ protected:
math::Vector2f v(global);
Widget *parent = this;
while (parent) {
- v += parent->location();
- parent = parent->parent();
- }
- return v;
- }
-
- /// map local widget location to global location
- inline math::Vector2f global_location() {
- math::Vector2f v(widget_location);
- Widget *parent = widget_parent;
- while (parent) {
- v += parent->location();
+ v -= parent->location();
parent = parent->parent();
}
return v;
@@ -356,6 +356,9 @@ protected:
/// draw the widget border
virtual void draw_border();
+
+ /// draw denug state
+ virtual void draw_debug();
/// add a child widget
virtual void add_child(Widget *child);
@@ -366,8 +369,8 @@ protected:
/// remove all child widgets
virtual void remove_children();
+
private:
- void draw_debug_border();
bool widget_visible;
bool widget_background;