]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/progressbar.cpp
Add a progress bar widget
[libs/gltk.git] / source / progressbar.cpp
diff --git a/source/progressbar.cpp b/source/progressbar.cpp
new file mode 100644 (file)
index 0000000..aed83c3
--- /dev/null
@@ -0,0 +1,63 @@
+#include <msp/gl/meshbuilder.h>
+#include "graphic.h"
+#include "part.h"
+#include "progressbar.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GLtk {
+
+ProgressBar::ProgressBar():
+       range(1),
+       fraction(0)
+{ }
+
+void ProgressBar::set_range(float r)
+{
+       if(r<=0)
+               throw invalid_argument("ProgressBar::set_range");
+
+       range = r;
+       mark_rebuild();
+}
+
+void ProgressBar::set_value(float v)
+{
+       if(v<0 || v>range)
+               throw invalid_argument("ProgressBar::set_value");
+
+       fraction = v/range;
+       mark_rebuild();
+}
+
+void ProgressBar::autosize_special(const Part &part, Geometry &ageom) const
+{
+       const Geometry &pgeom = part.get_geometry();
+       const Sides &pmargin = part.get_margin();
+       ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
+       ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
+}
+
+void ProgressBar::rebuild_special(const Part &part)
+{
+       if(part.get_name()=="bar")
+       {
+               const Graphic *graphic = part.get_graphic(state);
+               if(!graphic || !graphic->get_texture())
+                       return;
+
+               Geometry rgeom = part.get_geometry();
+               Alignment align = part.get_alignment();
+               align.w += (1-align.w)*fraction;
+               align.h += (1-align.h)*fraction;
+               align.apply(rgeom, geom, part.get_margin());
+
+               GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
+               bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
+               graphic->build(rgeom.w, rgeom.h, bld);
+       }
+}
+
+} // namespace GLtk
+} // namespace Msp