1 #include <msp/gl/meshbuilder.h>
13 Image::Image(const GL::Texture2D *i):
19 void Image::autosize_special(const Part &part, Geometry &ageom) const
21 if(part.get_name()=="image")
23 const Sides &margin = part.get_margin();
26 ageom.w = max(ageom.w, image->get_width()+margin.left+margin.right);
27 ageom.h = max(ageom.h, image->get_height()+margin.top+margin.bottom);
31 ageom.w = max(ageom.w, margin.left+margin.right);
32 ageom.h = max(ageom.h, margin.top+margin.bottom);
37 void Image::set_image(const GL::Texture2D *i)
41 signal_autosize_changed.emit();
45 void Image::set_icon(const string &n)
51 void Image::set_keep_aspect(bool ka)
57 void Image::update_icon()
61 Root *root = find_ancestor<Root>();
67 image = &root->get_resources().get<GL::Texture2D>(icon_name);
68 signal_autosize_changed.emit();
77 void Image::rebuild_special(const Part &part)
79 if(part.get_name()=="image")
84 const Alignment &align = part.get_alignment();
85 Geometry rgeom = part.get_geometry();
86 align.apply(rgeom, geom, part.get_margin());
90 float aspect = static_cast<float>(image->get_width())/image->get_height();
91 if(rgeom.w<rgeom.h*aspect)
93 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
94 rgeom.y += (rgeom.h-h)*align.y;
99 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
100 rgeom.x += (rgeom.w-w)*align.x;
105 GL::MeshBuilder bld(part_cache.create_mesh(part, *image));
106 bld.color(1.0f, 1.0f, 1.0f);
107 bld.begin(GL::TRIANGLE_STRIP);
108 bld.texcoord(0.0, 1.0);
109 bld.vertex(rgeom.x, rgeom.y+rgeom.h);
110 bld.texcoord(0.0, 0.0);
111 bld.vertex(rgeom.x, rgeom.y);
112 bld.texcoord(1.0, 1.0);
113 bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
114 bld.texcoord(1.0, 0.0);
115 bld.vertex(rgeom.x+rgeom.w, rgeom.y);
120 void Image::on_style_change()
122 if(!icon_name.empty())
126 void Image::on_reparent()
128 if(!icon_name.empty())
133 Image::Loader::Loader(Image &img):
134 DataFile::DerivedObjectLoader<Image, Widget::Loader>(img)
136 add("icon", &Image::icon_name);