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