]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add a progress bar widget
authorMikko Rasa <tdb@tdb.fi>
Fri, 27 Sep 2019 11:27:49 +0000 (14:27 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 27 Sep 2019 12:19:40 +0000 (15:19 +0300)
source/panel.cpp
source/progressbar.cpp [new file with mode: 0644]
source/progressbar.h [new file with mode: 0644]

index b4f4f5f54190b165c8fad0b826dabd2688b19ac2..59c363ce02b658bfbf0de73afc0770d8901b16e7 100644 (file)
@@ -13,6 +13,7 @@
 #include "list.h"
 #include "panel.h"
 #include "part.h"
+#include "progressbar.h"
 #include "row.h"
 #include "slider.h"
 #include "stack.h"
@@ -225,6 +226,7 @@ Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
                register_child_type<Label>("label");
                register_child_type<List>("list");
                register_child_type<Panel>("panel");
+               register_child_type<ProgressBar>("progressbar");
                register_child_type<Toggle>("toggle");
                register_child_type<VSlider>("vslider");
        }
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
diff --git a/source/progressbar.h b/source/progressbar.h
new file mode 100644 (file)
index 0000000..c1e17b5
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef MSP_GLTK_PROGRESSBAR_H_
+#define MSP_GLTK_PROGRESSBAR_H_
+
+#include "widget.h"
+
+namespace Msp {
+namespace GLtk {
+
+class ProgressBar: public Widget
+{
+private:
+       float range;
+       float fraction;
+
+public:
+       ProgressBar();
+
+       virtual const char *get_class() const { return "progressbar"; }
+
+       void set_range(float);
+       void set_value(float);
+
+private:
+       virtual void autosize_special(const Part &, Geometry &) const;
+       virtual void rebuild_special(const Part &);
+};
+
+} // namespace GLtk
+} // namespace Msp
+
+#endif