]> git.tdb.fi Git - libs/gltk.git/blob - source/vslider.cpp
Add a function to find the index of an item in ListDataStore
[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_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+margin.left+margin.right);
22                 ageom.h = std::max(ageom.h, pgeom.h*3/2+margin.top+margin.bottom);
23         }
24 }
25
26 void VSlider::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.y = (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 VSlider::button_press(int x, int y, unsigned btn)
48 {
49         if(btn==1 && geom.is_inside_relative(x, y) && max>min)
50         {
51                 int sy = static_cast<int>((geom.h-slider_size)*(value-min)/(max-min));
52                 if(y<sy)
53                         set_value(value-step*10);
54                 else if(y>=static_cast<int>(sy+slider_size))
55                         set_value(value+step*10);
56                 else
57                         start_drag(y);
58         }
59 }
60
61 void VSlider::button_release(int, int, unsigned btn)
62 {
63         if(btn==1 && dragging)
64                 end_drag();
65 }
66
67 void VSlider::pointer_motion(int, int y)
68 {
69         if(dragging)
70                 drag(y);
71 }
72
73 void VSlider::on_geometry_change()
74 {
75         drag_area_size = geom.h-slider_size;
76 }
77
78 void VSlider::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().h;
85
86         on_geometry_change();
87 }
88
89 } // namespace GLtk
90 } // namespace Msp
91