3 This file is part of libmspgltk
4 Copyright © 2010-2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/bindable.h>
9 #include <msp/gl/immediate.h>
10 #include <msp/gl/matrix.h>
20 Image::Image(const GL::Texture2D *i):
27 void Image::autosize()
34 if(const Part *image_part = style->get_part("image"))
36 const Sides &margin = image_part->get_margin();
39 geom.w = max(geom.w, image->get_width()+margin.left+margin.right);
40 geom.h = max(geom.h, image->get_height()+margin.top+margin.bottom);
44 geom.w = max(geom.w, margin.left+margin.right);
45 geom.h = max(geom.h, margin.top+margin.bottom);
50 void Image::set_image(const GL::Texture2D *i)
53 signal_autosize_changed.emit();
56 void Image::set_keep_aspect(bool ka)
61 void Image::render_special(const Part &part) const
63 if(part.get_name()=="image")
68 const Alignment &align = part.get_alignment();
69 Geometry rgeom = part.get_geometry();
70 align.apply(rgeom, geom, part.get_margin());
74 float aspect = static_cast<float>(image->get_width())/image->get_height();
75 if(rgeom.w<rgeom.h*aspect)
77 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
78 rgeom.y += (rgeom.h-h)*align.y;
83 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
84 rgeom.x += (rgeom.w-w)*align.x;
89 GL::Bind _bind_tex(image);
90 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
91 imm.color(1.0f, 1.0f, 1.0f);
93 imm.texcoord(0.0, 0.0);
94 imm.vertex(rgeom.x, rgeom.y);
95 imm.texcoord(1.0, 0.0);
96 imm.vertex(rgeom.x+rgeom.w, rgeom.y);
97 imm.texcoord(1.0, 1.0);
98 imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
99 imm.texcoord(0.0, 1.0);
100 imm.vertex(rgeom.x, rgeom.y+rgeom.h);