1 #include <msp/gl/meshbuilder.h>
13 Image::Image(const GL::Texture2D *i):
18 void Image::autosize_special(const Part &part, Geometry &ageom) const
20 if(part.get_name()=="image")
22 const Sides &margin = part.get_margin();
25 ageom.w = max(ageom.w, image->get_width()+margin.left+margin.right);
26 ageom.h = max(ageom.h, image->get_height()+margin.top+margin.bottom);
30 ageom.w = max(ageom.w, margin.left+margin.right);
31 ageom.h = max(ageom.h, margin.top+margin.bottom);
36 void Image::set_image(const GL::Texture2D *i)
40 signal_autosize_changed.emit();
44 void Image::set_icon(const string &n)
50 void Image::set_keep_aspect(bool ka)
56 void Image::update_icon()
62 Root *root = find_ancestor<Root>();
66 if(!icon_name.empty())
67 image = &root->get_resources().get<GL::Texture2D>(icon_name);
68 signal_autosize_changed.emit();
72 void Image::rebuild_special(const Part &part)
74 if(part.get_name()=="image")
79 const Alignment &align = part.get_alignment();
80 Geometry rgeom = part.get_geometry();
81 align.apply(rgeom, geom, part.get_margin());
85 float aspect = static_cast<float>(image->get_width())/image->get_height();
86 if(rgeom.w<rgeom.h*aspect)
88 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
89 rgeom.y += (rgeom.h-h)*align.y;
94 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
95 rgeom.x += (rgeom.w-w)*align.x;
100 GL::MeshBuilder bld(part_cache.create_mesh(part, *image));
101 bld.color(1.0f, 1.0f, 1.0f);
102 bld.begin(GL::TRIANGLE_STRIP);
103 bld.texcoord(0.0, 1.0);
104 bld.vertex(rgeom.x, rgeom.y+rgeom.h);
105 bld.texcoord(0.0, 0.0);
106 bld.vertex(rgeom.x, rgeom.y);
107 bld.texcoord(1.0, 1.0);
108 bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
109 bld.texcoord(1.0, 0.0);
110 bld.vertex(rgeom.x+rgeom.w, rgeom.y);
115 void Image::on_style_change()
117 if(!icon_name.empty())
121 void Image::on_reparent()
123 if(!icon_name.empty())
128 Image::Loader::Loader(Image &img):
129 DataFile::DerivedObjectLoader<Image, Widget::Loader>(img)
131 add("icon", &Image::icon_name);