]> git.tdb.fi Git - libs/gltk.git/blob - hslider.cpp
fbd9b943a8c4703dc8fe77acc542df843777173f
[libs/gltk.git] / 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         slider_size(1)
13 { }
14
15 void HSlider::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*3/2+margin.left+margin.right);
27                 geom.h = std::max(geom.h, pgeom.h+margin.top+margin.bottom);
28         }
29 }
30
31 void HSlider::rebuild_special(const Part &part, CachedPart &cache)
32 {
33         if(part.get_name()=="slider")
34         {
35                 Alignment align = part.get_alignment();
36                 if(max>min)
37                         align.x = (value-min)/(max-min);
38
39                 Geometry pgeom = part.get_geometry();
40                 align.apply(pgeom, geom, part.get_margin());
41
42                 cache.texture = part.get_graphic(state)->get_texture();
43                 cache.clear_mesh();
44
45                 GL::MeshBuilder bld(*cache.mesh);
46                 bld.matrix() *= GL::Matrix::translation(pgeom.x, pgeom.y, 0);
47                 part.get_graphic(state)->build(pgeom.w, pgeom.h, bld);
48         }
49 }
50
51 void HSlider::button_press(int x, int y, unsigned btn)
52 {
53         if(btn==1 && geom.is_inside_relative(x, y) && max>min)
54         {
55                 int sx = static_cast<int>((geom.w-slider_size)*(value-min)/(max-min));
56                 if(x<sx)
57                         set_value(value-step*10);
58                 else if(x>=static_cast<int>(sx+slider_size))
59                         set_value(value+step*10);
60                 else
61                         start_drag(x);
62         }
63 }
64
65 void HSlider::button_release(int, int, unsigned btn)
66 {
67         if(btn==1 && dragging)
68                 end_drag();
69 }
70
71 void HSlider::pointer_motion(int x, int)
72 {
73         if(dragging)
74                 drag(x);
75 }
76
77 void HSlider::on_geometry_change()
78 {
79         drag_area_size = geom.w-slider_size;
80 }
81
82 void HSlider::on_style_change()
83 {
84         if(!style)
85                 return;
86
87         if(const Part *slider_part = style->get_part("slider"))
88                 slider_size = slider_part->get_geometry().w;
89
90         on_geometry_change();
91 }
92
93 } // namespace GLtk
94 } // namespace Msp