]> git.tdb.fi Git - libs/gltk.git/blob - source/hslider.cpp
35d0a411d36ce2eb26936e66597627a0a3c6bea2
[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         slider_size(1)
13 { }
14
15 void HSlider::autosize_special(const Part &part, Geometry &ageom)
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 y, unsigned btn)
48 {
49         if(btn==1 && geom.is_inside_relative(x, y) && max>min)
50         {
51                 int sx = static_cast<int>((geom.w-slider_size)*(value-min)/(max-min));
52                 if(x<sx)
53                         set_value(value-step*10);
54                 else if(x>=static_cast<int>(sx+slider_size))
55                         set_value(value+step*10);
56                 else
57                         start_drag(x);
58         }
59 }
60
61 void HSlider::button_release(int, int, unsigned btn)
62 {
63         if(btn==1 && dragging)
64                 end_drag();
65 }
66
67 void HSlider::pointer_motion(int x, int)
68 {
69         if(dragging)
70                 drag(x);
71 }
72
73 void HSlider::on_geometry_change()
74 {
75         drag_area_size = geom.w-slider_size;
76 }
77
78 void HSlider::on_style_change()
79 {
80         if(!style)
81                 return;
82
83         if(const Part *slider_part = style->get_part("slider"))
84                 slider_size = slider_part->get_geometry().w;
85
86         on_geometry_change();
87 }
88
89 } // namespace GLtk
90 } // namespace Msp