]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/image.cpp
Add support for displaying named icons in an Image widget
[libs/gltk.git] / source / image.cpp
index a2d44269b86a394115d696796cbc8fbb6fbb5f7c..0102a47e487ea8bf4a9efdf29cf1696172820b8f 100644 (file)
@@ -1,6 +1,8 @@
 #include <msp/gl/meshbuilder.h>
 #include "image.h"
 #include "part.h"
+#include "resources.h"
+#include "root.h"
 #include "style.h"
 
 using namespace std;
@@ -12,56 +14,72 @@ Image::Image(const GL::Texture2D *i):
        image(i),
        keep_aspect(true)
 {
-       focusable = false;
 }
 
-void Image::autosize()
+void Image::autosize_special(const Part &part, Geometry &ageom) const
 {
-       if(!style)
-               return;
-
-       Widget::autosize();
-
-       if(const Part *image_part = style->get_part("image"))
+       if(part.get_name()=="image")
        {
-               const Sides &margin = image_part->get_margin();
+               const Sides &margin = part.get_margin();
                if(image)
                {
-                       geom.w = max(geom.w, image->get_width()+margin.left+margin.right);
-                       geom.h = max(geom.h, image->get_height()+margin.top+margin.bottom);
+                       ageom.w = max(ageom.w, image->get_width()+margin.left+margin.right);
+                       ageom.h = max(ageom.h, image->get_height()+margin.top+margin.bottom);
                }
                else
                {
-                       geom.w = max(geom.w, margin.left+margin.right);
-                       geom.h = max(geom.h, margin.top+margin.bottom);
+                       ageom.w = max(ageom.w, margin.left+margin.right);
+                       ageom.h = max(ageom.h, margin.top+margin.bottom);
                }
        }
-
-       rebuild();
 }
 
 void Image::set_image(const GL::Texture2D *i)
 {
        image = i;
+       icon_name = string();
        signal_autosize_changed.emit();
        rebuild();
 }
 
+void Image::set_icon(const string &n)
+{
+       icon_name = n;
+       update_icon();
+}
+
 void Image::set_keep_aspect(bool ka)
 {
        keep_aspect = ka;
        rebuild();
 }
 
-void Image::rebuild_special(const Part &part, CachedPart &cache)
+void Image::update_icon()
 {
-       if(part.get_name()=="image")
+       if(style)
        {
-               if(!image)
+               Root *root = find_ancestor<Root>();
+               if(root)
                {
-                       cache.texture = 0;
+                       if(icon_name.empty())
+                               image = 0;
+                       else
+                               image = &root->get_resources().get<GL::Texture2D>(icon_name);
+                       signal_autosize_changed.emit();
+                       rebuild();
                        return;
                }
+       }
+
+       image = 0;
+}
+
+void Image::rebuild_special(const Part &part)
+{
+       if(part.get_name()=="image")
+       {
+               if(!image)
+                       return;
 
                const Alignment &align = part.get_alignment();
                Geometry rgeom = part.get_geometry();
@@ -84,23 +102,32 @@ void Image::rebuild_special(const Part &part, CachedPart &cache)
                        }
                }
 
-               cache.texture = image;
-               cache.clear_mesh();
-
-               GL::MeshBuilder bld(*cache.mesh);
+               GL::MeshBuilder bld(part_cache.create_mesh(part, *image));
                bld.color(1.0f, 1.0f, 1.0f);
-               bld.begin(GL::QUADS);
+               bld.begin(GL::TRIANGLE_STRIP);
+               bld.texcoord(0.0, 1.0);
+               bld.vertex(rgeom.x, rgeom.y+rgeom.h);
                bld.texcoord(0.0, 0.0);
                bld.vertex(rgeom.x, rgeom.y);
-               bld.texcoord(1.0, 0.0);
-               bld.vertex(rgeom.x+rgeom.w, rgeom.y);
                bld.texcoord(1.0, 1.0);
                bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
-               bld.texcoord(0.0, 1.0);
-               bld.vertex(rgeom.x, rgeom.y+rgeom.h);
+               bld.texcoord(1.0, 0.0);
+               bld.vertex(rgeom.x+rgeom.w, rgeom.y);
                bld.end();
        }
 }
 
+void Image::on_style_change()
+{
+       if(!icon_name.empty())
+               update_icon();
+}
+
+void Image::on_reparent()
+{
+       if(!icon_name.empty())
+               update_icon();
+}
+
 } // namespace GLtk
 } // namespace Msp