1 #include <msp/gl/meshbuilder.h>
11 Image::Image(const GL::Texture2D *i):
18 void Image::autosize()
25 if(const Part *image_part = style->get_part("image"))
27 const Sides &margin = image_part->get_margin();
30 geom.w = max(geom.w, image->get_width()+margin.left+margin.right);
31 geom.h = max(geom.h, image->get_height()+margin.top+margin.bottom);
35 geom.w = max(geom.w, margin.left+margin.right);
36 geom.h = max(geom.h, margin.top+margin.bottom);
43 void Image::set_image(const GL::Texture2D *i)
46 signal_autosize_changed.emit();
50 void Image::set_keep_aspect(bool ka)
56 void Image::rebuild_special(const Part &part, CachedPart &cache)
58 if(part.get_name()=="image")
66 const Alignment &align = part.get_alignment();
67 Geometry rgeom = part.get_geometry();
68 align.apply(rgeom, geom, part.get_margin());
72 float aspect = static_cast<float>(image->get_width())/image->get_height();
73 if(rgeom.w<rgeom.h*aspect)
75 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
76 rgeom.y += (rgeom.h-h)*align.y;
81 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
82 rgeom.x += (rgeom.w-w)*align.x;
87 cache.texture = image;
90 GL::MeshBuilder bld(*cache.mesh);
91 bld.color(1.0f, 1.0f, 1.0f);
93 bld.texcoord(0.0, 0.0);
94 bld.vertex(rgeom.x, rgeom.y);
95 bld.texcoord(1.0, 0.0);
96 bld.vertex(rgeom.x+rgeom.w, rgeom.y);
97 bld.texcoord(1.0, 1.0);
98 bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
99 bld.texcoord(0.0, 1.0);
100 bld.vertex(rgeom.x, rgeom.y+rgeom.h);