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