]> git.tdb.fi Git - libs/gltk.git/blob - source/vslider.cpp
Cache widget parts in meshes
[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                 Alignment align = part.get_alignment();
36                 if(max>min)
37                         align.y = (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 VSlider::button_press(int x, int y, unsigned btn)
52 {
53         if(btn==1 && geom.is_inside_relative(x, y) && max>min)
54         {
55                 int sy = static_cast<int>((geom.h-slider_size)*(value-min)/(max-min));
56                 if(y<sy)
57                         set_value(value-step*10);
58                 else if(y>=static_cast<int>(sy+slider_size))
59                         set_value(value+step*10);
60                 else
61                         start_drag(y);
62         }
63 }
64
65 void VSlider::button_release(int, int, unsigned btn)
66 {
67         if(btn==1 && dragging)
68                 end_drag();
69 }
70
71 void VSlider::pointer_motion(int, int y)
72 {
73         if(dragging)
74                 drag(y);
75 }
76
77 void VSlider::on_geometry_change()
78 {
79         drag_area_size = geom.h-slider_size;
80 }
81
82 void VSlider::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().h;
89
90         on_geometry_change();
91 }
92
93 } // namespace GLtk
94 } // namespace Msp
95