]> git.tdb.fi Git - libs/gltk.git/commitdiff
Refactor all slider logic into the Slider class
authorMikko Rasa <tdb@tdb.fi>
Mon, 23 Sep 2019 22:10:45 +0000 (01:10 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 23 Sep 2019 22:10:45 +0000 (01:10 +0300)
Having to write the same logic twice is annoying and more prone to
errors.

source/entry.cpp
source/hslider.cpp [deleted file]
source/hslider.h
source/list.cpp
source/list.h
source/panel.cpp
source/slider.cpp
source/slider.h
source/vslider.cpp [deleted file]
source/vslider.h

index 11fd2423c8cffc3e6d25e4760f29bb33203b1eee..83ed563298b793b919d31d1a7bc442b0d3b24e2d 100644 (file)
@@ -5,8 +5,8 @@
 #include "entry.h"
 #include "graphic.h"
 #include "part.h"
+#include "slider.h"
 #include "style.h"
-#include "vslider.h"
 
 using namespace std;
 
diff --git a/source/hslider.cpp b/source/hslider.cpp
deleted file mode 100644 (file)
index 1b6fadf..0000000
+++ /dev/null
@@ -1,87 +0,0 @@
-#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
index 3784a7db7ff57b02f162cd5646dcf0fb58aa2ffe..59222ebc40ac1e3c55796f118902765e125bdd64 100644 (file)
@@ -1,39 +1,2 @@
-#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
index 5493947c566b025df46b6aebbe26f865f3432f3f..13a5619a2e4d96bcbb72d83d46b5874b9ae96309 100644 (file)
@@ -9,7 +9,6 @@
 #include "part.h"
 #include "style.h"
 #include "text.h"
-#include "vslider.h"
 
 using namespace std;
 
index 8b9db4090ecfa7ef9b6ece56ef291366f75313f6..19fdaa5df6f4592f2682b0c80982e62a8c697d62 100644 (file)
@@ -7,7 +7,7 @@
 #include "container.h"
 #include "label.h"
 #include "listdata.h"
-#include "vslider.h"
+#include "slider.h"
 
 namespace Msp {
 namespace GLtk {
index c97cd6d7212da9c648cbed683edf23dad4250701..b4f4f5f54190b165c8fad0b826dabd2688b19ac2 100644 (file)
@@ -7,7 +7,6 @@
 #include "dropdown.h"
 #include "entry.h"
 #include "grid.h"
-#include "hslider.h"
 #include "image.h"
 #include "indicator.h"
 #include "label.h"
@@ -15,9 +14,9 @@
 #include "panel.h"
 #include "part.h"
 #include "row.h"
+#include "slider.h"
 #include "stack.h"
 #include "toggle.h"
-#include "vslider.h"
 
 using namespace std;
 
index 5034b2475d8c46f20e4e09f2fe626c69c196d19a..8ff342d56a81646aa6fe1786c58fcaf39a816068 100644 (file)
@@ -1,9 +1,15 @@
+#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),
@@ -11,7 +17,8 @@ Slider::Slider():
        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)
@@ -48,8 +55,49 @@ void Slider::set_step(double s)
        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;
@@ -60,27 +108,61 @@ void Slider::click(int p)
        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();
 }
 
 
index a95a6a57665fd905577d3e76f54afa76ef5313f1..b6041af96480320c8fa5643a313c6cdb96958b69 100644 (file)
@@ -9,8 +9,8 @@ namespace GLtk {
 
 /**
 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
 {
@@ -21,9 +21,18 @@ public:
                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;
@@ -34,8 +43,9 @@ protected:
        unsigned drag_area_size;
        unsigned drag_area_offset;
        unsigned slider_size;
+       unsigned total_margin;
 
-       Slider();
+       Slider(Direction);
 
 public:
        void set_value(double);
@@ -44,10 +54,34 @@ public:
        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
diff --git a/source/vslider.cpp b/source/vslider.cpp
deleted file mode 100644 (file)
index cb345bb..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-#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
-
index 96fd03fbd40b05f72b6523936c868dd674fc20d1..59222ebc40ac1e3c55796f118902765e125bdd64 100644 (file)
@@ -1,35 +1,2 @@
-#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