]> git.tdb.fi Git - libs/gltk.git/blob - source/progressbar.cpp
7c6def7c37e2c14c9bf8390d3dd6cd0d206e50fb
[libs/gltk.git] / source / progressbar.cpp
1 #include <msp/gl/meshbuilder.h>
2 #include "graphic.h"
3 #include "part.h"
4 #include "progressbar.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GLtk {
10
11 void ProgressBar::set_range(float r)
12 {
13         if(r<=0)
14                 throw invalid_argument("ProgressBar::set_range");
15
16         range = r;
17         mark_rebuild();
18 }
19
20 void ProgressBar::set_value(float v)
21 {
22         if(v<0 || v>range)
23                 throw invalid_argument("ProgressBar::set_value");
24
25         fraction = v/range;
26         mark_rebuild();
27 }
28
29 void ProgressBar::autosize_special(const Part &part, Geometry &ageom) const
30 {
31         const Geometry &pgeom = part.get_geometry();
32         const Sides &pmargin = part.get_margin();
33         ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
34         ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
35 }
36
37 void ProgressBar::rebuild_special(const Part &part)
38 {
39         if(part.get_name()=="bar")
40         {
41                 const Graphic *graphic = part.get_graphic(state);
42                 if(!graphic || !graphic->get_texture())
43                         return;
44
45                 Geometry rgeom = part.get_geometry();
46                 Alignment align = part.get_alignment();
47                 align.w += (1-align.w)*fraction;
48                 align.h += (1-align.h)*fraction;
49                 align.apply(rgeom, geom, part.get_margin());
50
51                 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
52                 bld.transform(GL::Matrix::translation(rgeom.x, rgeom.y, 0));
53                 graphic->build(rgeom.w, rgeom.h, bld);
54         }
55 }
56
57 } // namespace GLtk
58 } // namespace Msp