1 #include <msp/gl/meshbuilder.h>
13 Image::Image(const GL::Texture2D *i):
17 void Image::autosize_special(const Part &part, Geometry &ageom) const
19 if(part.get_name()=="image")
21 const Sides &margin = part.get_margin();
24 ageom.w = max(ageom.w, image->get_width()+margin.left+margin.right);
25 ageom.h = max(ageom.h, image->get_height()+margin.top+margin.bottom);
29 ageom.w = max(ageom.w, margin.left+margin.right);
30 ageom.h = max(ageom.h, margin.top+margin.bottom);
35 void Image::set_image(const GL::Texture2D *i)
39 signal_autosize_changed.emit();
43 void Image::set_icon(const string &n)
49 void Image::set_keep_aspect(bool ka)
55 void Image::update_icon()
61 Root *root = find_ancestor<Root>();
65 if(!icon_name.empty())
66 image = &root->get_resources().get<GL::Texture2D>(icon_name);
67 signal_autosize_changed.emit();
71 void Image::rebuild_special(const Part &part)
73 if(part.get_name()=="image")
78 const Alignment &align = part.get_alignment();
79 Geometry rgeom = part.get_geometry();
80 align.apply(rgeom, geom, part.get_margin());
84 float aspect = static_cast<float>(image->get_width())/image->get_height();
85 if(rgeom.w<rgeom.h*aspect)
87 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
88 rgeom.y += (rgeom.h-h)*align.y;
93 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
94 rgeom.x += (rgeom.w-w)*align.x;
99 GL::MeshBuilder bld(part_cache.create_mesh(part, *image));
100 bld.color(1.0f, 1.0f, 1.0f);
101 bld.begin(GL::TRIANGLE_STRIP);
102 bld.texcoord(0.0, 1.0);
103 bld.vertex(rgeom.x, rgeom.y+rgeom.h);
104 bld.texcoord(0.0, 0.0);
105 bld.vertex(rgeom.x, rgeom.y);
106 bld.texcoord(1.0, 1.0);
107 bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
108 bld.texcoord(1.0, 0.0);
109 bld.vertex(rgeom.x+rgeom.w, rgeom.y);
114 void Image::on_style_change()
116 if(!icon_name.empty())
120 void Image::on_reparent()
122 if(!icon_name.empty())
127 Image::Loader::Loader(Image &img):
128 DataFile::DerivedObjectLoader<Image, Widget::Loader>(img)
130 add("icon", &Image::icon_name);