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