]> git.tdb.fi Git - libs/gltk.git/blob - source/hslider.cpp
Properly account for margins in sliders
[libs/gltk.git] / source / hslider.cpp
1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
3 #include "graphic.h"
4 #include "hslider.h"
5 #include "part.h"
6 #include "style.h"
7
8 namespace Msp {
9 namespace GLtk {
10
11 HSlider::HSlider():
12         total_margin(0)
13 { }
14
15 void HSlider::autosize_special(const Part &part, Geometry &ageom) const
16 {
17         if(part.get_name()=="slider")
18         {
19                 const Sides &margin = part.get_margin();
20                 const Geometry &pgeom = part.get_geometry();
21                 ageom.w = std::max(ageom.w, pgeom.w*3/2+margin.left+margin.right);
22                 ageom.h = std::max(ageom.h, pgeom.h+margin.top+margin.bottom);
23         }
24 }
25
26 void HSlider::rebuild_special(const Part &part)
27 {
28         if(part.get_name()=="slider")
29         {
30                 const Graphic *graphic = part.get_graphic(state);
31                 if(!graphic || !graphic->get_texture())
32                         return;
33
34                 Alignment align = part.get_alignment();
35                 if(max>min)
36                         align.x = (value-min)/(max-min);
37
38                 Geometry pgeom = part.get_geometry();
39                 align.apply(pgeom, geom, part.get_margin());
40
41                 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
42                 bld.matrix() *= GL::Matrix::translation(pgeom.x, pgeom.y, 0);
43                 graphic->build(pgeom.w, pgeom.h, bld);
44         }
45 }
46
47 void HSlider::button_press(int x, int, unsigned btn)
48 {
49         if(btn==1 && max>min)
50                 click(x);
51 }
52
53 void HSlider::button_release(int, int, unsigned btn)
54 {
55         if(btn==1 && dragging)
56                 end_drag();
57 }
58
59 void HSlider::pointer_motion(int x, int)
60 {
61         if(dragging)
62                 drag(x);
63 }
64
65 void HSlider::on_geometry_change()
66 {
67         drag_area_size = geom.w-total_margin;
68 }
69
70 void HSlider::on_style_change()
71 {
72         if(!style)
73                 return;
74
75         if(const Part *slider_part = style->get_part("slider"))
76         {
77                 slider_size = slider_part->get_geometry().w;
78                 const Sides &margin = slider_part->get_margin();
79                 drag_area_offset = margin.left;
80                 total_margin = margin.left+margin.right;
81         }
82
83         on_geometry_change();
84 }
85
86 } // namespace GLtk
87 } // namespace Msp