#include "entry.h"
#include "graphic.h"
#include "part.h"
+#include "slider.h"
#include "style.h"
-#include "vslider.h"
using namespace std;
+++ /dev/null
-#include <msp/gl/matrix.h>
-#include <msp/gl/meshbuilder.h>
-#include "graphic.h"
-#include "hslider.h"
-#include "part.h"
-#include "style.h"
-
-namespace Msp {
-namespace GLtk {
-
-HSlider::HSlider():
- total_margin(0)
-{ }
-
-void HSlider::autosize_special(const Part &part, Geometry &ageom) const
-{
- if(part.get_name()=="slider")
- {
- const Sides &margin = part.get_margin();
- const Geometry &pgeom = part.get_geometry();
- ageom.w = std::max(ageom.w, pgeom.w*3/2+margin.left+margin.right);
- ageom.h = std::max(ageom.h, pgeom.h+margin.top+margin.bottom);
- }
-}
-
-void HSlider::rebuild_special(const Part &part)
-{
- if(part.get_name()=="slider")
- {
- const Graphic *graphic = part.get_graphic(state);
- if(!graphic || !graphic->get_texture())
- return;
-
- Alignment align = part.get_alignment();
- if(max>min)
- align.x = (value-min)/(max-min);
-
- Geometry pgeom = part.get_geometry();
- align.apply(pgeom, geom, part.get_margin());
-
- GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
- bld.matrix() *= GL::Matrix::translation(pgeom.x, pgeom.y, 0);
- graphic->build(pgeom.w, pgeom.h, bld);
- }
-}
-
-void HSlider::button_press(int x, int, unsigned btn)
-{
- if(btn==1 && max>min)
- click(x);
-}
-
-void HSlider::button_release(int, int, unsigned btn)
-{
- if(btn==1 && dragging)
- end_drag();
-}
-
-void HSlider::pointer_motion(int x, int)
-{
- if(dragging)
- drag(x);
-}
-
-void HSlider::on_size_change()
-{
- drag_area_size = geom.w-total_margin;
-}
-
-void HSlider::on_style_change()
-{
- if(!style)
- return;
-
- if(const Part *slider_part = style->get_part("slider"))
- {
- slider_size = slider_part->get_geometry().w;
- const Sides &margin = slider_part->get_margin();
- drag_area_offset = margin.left;
- total_margin = margin.left+margin.right;
- }
-
- on_size_change();
-}
-
-} // namespace GLtk
-} // namespace Msp
-#ifndef MSP_GLTK_HSLIDER_H_
-#define MSP_GLTK_HSLIDER_H_
-
+// Deprecated, use slider.h instead
#include "slider.h"
-
-namespace Msp {
-namespace GLtk {
-
-/**
-Horizontal slider widget. A special part named "slider" will be positioned at
-the current value of the widget.
-*/
-class HSlider: public Slider
-{
-private:
- unsigned total_margin;
-
-public:
- HSlider();
-
- virtual const char *get_class() const { return "hslider"; }
-
-private:
- virtual void autosize_special(const Part &, Geometry &) const;
- virtual void rebuild_special(const Part &);
-
-public:
- virtual void button_press(int, int, unsigned);
- virtual void button_release(int, int, unsigned);
- virtual void pointer_motion(int, int);
-private:
- virtual void on_size_change();
- virtual void on_style_change();
-};
-
-} // namespace GLtk
-} // namespace Msp
-
-#endif
#include "part.h"
#include "style.h"
#include "text.h"
-#include "vslider.h"
using namespace std;
#include "container.h"
#include "label.h"
#include "listdata.h"
-#include "vslider.h"
+#include "slider.h"
namespace Msp {
namespace GLtk {
#include "dropdown.h"
#include "entry.h"
#include "grid.h"
-#include "hslider.h"
#include "image.h"
#include "indicator.h"
#include "label.h"
#include "panel.h"
#include "part.h"
#include "row.h"
+#include "slider.h"
#include "stack.h"
#include "toggle.h"
-#include "vslider.h"
using namespace std;
+#include <msp/gl/matrix.h>
+#include <msp/gl/meshbuilder.h>
+#include "graphic.h"
+#include "part.h"
#include "slider.h"
+#include "style.h"
namespace Msp {
namespace GLtk {
-Slider::Slider():
+Slider::Slider(Direction d):
+ dir(d),
min(0),
max(1),
value(0),
dragging(false),
drag_area_size(0),
drag_area_offset(0),
- slider_size(1)
+ slider_size(1),
+ total_margin(0)
{ }
void Slider::set_value(double v)
set_value(value);
}
-void Slider::click(int p)
+void Slider::autosize_special(const Part &part, Geometry &ageom) const
{
+ if(part.get_name()=="slider")
+ {
+ const Sides &margin = part.get_margin();
+ const Geometry &pgeom = part.get_geometry();
+ ageom.w = std::max(ageom.w, pgeom.w*(dir==HORIZONTAL)/2+margin.left+margin.right);
+ ageom.h = std::max(ageom.h, pgeom.h*(dir==VERTICAL)/2+margin.top+margin.bottom);
+ }
+}
+
+void Slider::rebuild_special(const Part &part)
+{
+ if(part.get_name()=="slider")
+ {
+ const Graphic *graphic = part.get_graphic(state);
+ if(!graphic || !graphic->get_texture())
+ return;
+
+ float pos_fraction = ((max>min) ? (value-min)/(max-min) : 0);
+
+ Alignment align = part.get_alignment();
+ if(dir==HORIZONTAL)
+ align.x = pos_fraction;
+ else
+ align.y = pos_fraction;
+
+ Geometry rgeom = part.get_geometry();
+ align.apply(rgeom, geom, part.get_margin());
+
+ GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
+ bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
+ graphic->build(rgeom.w, rgeom.h, bld);
+ }
+}
+
+void Slider::button_press(int x, int y, unsigned btn)
+{
+ if(btn!=1)
+ return;
+
+ int p = (dir==HORIZONTAL ? x : y);
+
p -= drag_area_offset;
if(p<0 || p>=static_cast<int>(drag_area_size))
return;
else if(p>=static_cast<int>(sp+slider_size))
set_value(value+step*10);
else
- start_drag(drag_area_offset+p);
+ {
+ dragging = true;
+ drag_start_pos = p;
+ drag_start_value = value;
+ set_state(ACTIVE);
+ }
}
-void Slider::start_drag(int p)
+void Slider::button_release(int, int, unsigned btn)
{
- dragging = true;
- drag_start_pos = p;
- drag_start_value = value;
- set_state(ACTIVE);
+ if(btn==1 && dragging)
+ {
+ dragging = false;
+ clear_state(ACTIVE);
+ }
}
-void Slider::drag(int p)
+void Slider::pointer_motion(int x, int y)
{
- if(max>min)
+ if(dragging && max>min)
+ {
+ int p = (dir==HORIZONTAL ? x : y);
set_value(drag_start_value+(p-drag_start_pos)*(max-min)/(drag_area_size-slider_size));
+ }
}
-void Slider::end_drag()
+void Slider::on_size_change()
{
- dragging = false;
- clear_state(ACTIVE);
+ drag_area_size = (dir==HORIZONTAL ? geom.w : geom.h)-total_margin;
+}
+
+void Slider::on_style_change()
+{
+ if(!style)
+ return;
+
+ if(const Part *slider_part = style->get_part("slider"))
+ {
+ const Geometry &pgeom = slider_part->get_geometry();
+ const Sides &margin = slider_part->get_margin();
+ if(dir==HORIZONTAL)
+ {
+ slider_size = pgeom.w;
+ drag_area_offset = margin.left;
+ total_margin = margin.left+margin.right;
+ }
+ else
+ {
+ slider_size = pgeom.h;
+ drag_area_offset = margin.bottom;
+ total_margin = margin.bottom+margin.top;
+ }
+ }
+
+ on_size_change();
}
/**
Sliders are used to adjust numeric values visually. This class provides the
-common interface for sliders - see classes HSlider and VSlider for concrete
-variations.
+common interface and logic for sliders but can't be instantiated. Use HSlider
+or VSlider depending on which direction you want the slider to be in.
*/
class Slider: public Widget
{
Loader(Slider &);
};
+protected:
+ enum Direction
+ {
+ HORIZONTAL,
+ VERTICAL
+ };
+
+public:
sigc::signal<void, double> signal_value_changed;
protected:
+ Direction dir;
double min, max;
double value;
double step;
unsigned drag_area_size;
unsigned drag_area_offset;
unsigned slider_size;
+ unsigned total_margin;
- Slider();
+ Slider(Direction);
public:
void set_value(double);
double get_value() const { return value; }
protected:
- void click(int);
- void start_drag(int);
- void drag(int);
- void end_drag();
+ virtual void autosize_special(const Part &, Geometry &) const;
+ virtual void rebuild_special(const Part &);
+
+public:
+ virtual void button_press(int, int, unsigned);
+ virtual void button_release(int, int, unsigned);
+ virtual void pointer_motion(int, int);
+
+protected:
+ virtual void on_size_change();
+ virtual void on_style_change();
+};
+
+
+class VSlider: public Slider
+{
+public:
+ VSlider(): Slider(VERTICAL) { }
+
+ virtual const char *get_class() const { return "vslider"; }
+};
+
+class HSlider: public Slider
+{
+public:
+ HSlider(): Slider(HORIZONTAL) { }
+
+ virtual const char *get_class() const { return "hslider"; }
};
} // namespace GLtk
+++ /dev/null
-#include <msp/gl/matrix.h>
-#include <msp/gl/meshbuilder.h>
-#include "graphic.h"
-#include "part.h"
-#include "style.h"
-#include "vslider.h"
-
-namespace Msp {
-namespace GLtk {
-
-VSlider::VSlider():
- total_margin(0)
-{ }
-
-void VSlider::autosize_special(const Part &part, Geometry &ageom) const
-{
- if(part.get_name()=="slider")
- {
- const Sides &margin = part.get_margin();
- const Geometry &pgeom = part.get_geometry();
- ageom.w = std::max(ageom.w, pgeom.w+margin.left+margin.right);
- ageom.h = std::max(ageom.h, pgeom.h*3/2+margin.top+margin.bottom);
- }
-}
-
-void VSlider::rebuild_special(const Part &part)
-{
- if(part.get_name()=="slider")
- {
- const Graphic *graphic = part.get_graphic(state);
- if(!graphic || !graphic->get_texture())
- return;
-
- Alignment align = part.get_alignment();
- if(max>min)
- align.y = (value-min)/(max-min);
-
- Geometry pgeom = part.get_geometry();
- align.apply(pgeom, geom, part.get_margin());
-
- GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
- bld.matrix() *= GL::Matrix::translation(pgeom.x, pgeom.y, 0);
- graphic->build(pgeom.w, pgeom.h, bld);
- }
-}
-
-void VSlider::button_press(int, int y, unsigned btn)
-{
- if(btn==1 && max>min)
- click(y);
-}
-
-void VSlider::button_release(int, int, unsigned btn)
-{
- if(btn==1 && dragging)
- end_drag();
-}
-
-void VSlider::pointer_motion(int, int y)
-{
- if(dragging)
- drag(y);
-}
-
-void VSlider::on_size_change()
-{
- drag_area_size = geom.h-total_margin;
-}
-
-void VSlider::on_style_change()
-{
- if(!style)
- return;
-
- if(const Part *slider_part = style->get_part("slider"))
- {
- slider_size = slider_part->get_geometry().h;
- const Sides &margin = slider_part->get_margin();
- drag_area_offset = margin.bottom;
- total_margin = margin.bottom+margin.top;
- }
-
- on_size_change();
-}
-
-} // namespace GLtk
-} // namespace Msp
-
-#ifndef MSP_GLTK_VSLIDER_H_
-#define MSP_GLTK_VSLIDER_H_
-
+// Deprecated, use slider.h instead
#include "slider.h"
-
-namespace Msp {
-namespace GLtk {
-
-class VSlider: public Slider
-{
-private:
- unsigned total_margin;
-
-public:
- VSlider();
-
- virtual const char *get_class() const { return "vslider"; }
-
-private:
- virtual void autosize_special(const Part &, Geometry &) const;
- virtual void rebuild_special(const Part &);
-
-public:
- virtual void button_press(int, int, unsigned);
- virtual void button_release(int, int, unsigned);
- virtual void pointer_motion(int, int);
-private:
- virtual void on_size_change();
- virtual void on_style_change();
-};
-
-} // namespace GLtk
-} // namespace Msp
-
-#endif