]> git.tdb.fi Git - libs/gltk.git/blob - source/image.cpp
Improve widget part caching
[libs/gltk.git] / source / image.cpp
1 #include <msp/gl/meshbuilder.h>
2 #include "image.h"
3 #include "part.h"
4 #include "style.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GLtk {
10
11 Image::Image(const GL::Texture2D *i):
12         image(i),
13         keep_aspect(true)
14 {
15         focusable = false;
16 }
17
18 void Image::autosize()
19 {
20         if(!style)
21                 return;
22
23         Widget::autosize();
24
25         if(const Part *image_part = style->get_part("image"))
26         {
27                 const Sides &margin = image_part->get_margin();
28                 if(image)
29                 {
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);
32                 }
33                 else
34                 {
35                         geom.w = max(geom.w, margin.left+margin.right);
36                         geom.h = max(geom.h, margin.top+margin.bottom);
37                 }
38         }
39
40         rebuild();
41 }
42
43 void Image::set_image(const GL::Texture2D *i)
44 {
45         image = i;
46         signal_autosize_changed.emit();
47         rebuild();
48 }
49
50 void Image::set_keep_aspect(bool ka)
51 {
52         keep_aspect = ka;
53         rebuild();
54 }
55
56 void Image::rebuild_special(const Part &part)
57 {
58         if(part.get_name()=="image")
59         {
60                 if(!image)
61                         return;
62
63                 const Alignment &align = part.get_alignment();
64                 Geometry rgeom = part.get_geometry();
65                 align.apply(rgeom, geom, part.get_margin());
66
67                 if(keep_aspect)
68                 {
69                         float aspect = static_cast<float>(image->get_width())/image->get_height();
70                         if(rgeom.w<rgeom.h*aspect)
71                         {
72                                 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
73                                 rgeom.y += (rgeom.h-h)*align.y;
74                                 rgeom.h = h;
75                         }
76                         else
77                         {
78                                 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
79                                 rgeom.x += (rgeom.w-w)*align.x;
80                                 rgeom.w = w;
81                         }
82                 }
83
84                 GL::MeshBuilder bld(part_cache.create_mesh(part, *image));
85                 bld.color(1.0f, 1.0f, 1.0f);
86                 bld.begin(GL::QUADS);
87                 bld.texcoord(0.0, 0.0);
88                 bld.vertex(rgeom.x, rgeom.y);
89                 bld.texcoord(1.0, 0.0);
90                 bld.vertex(rgeom.x+rgeom.w, rgeom.y);
91                 bld.texcoord(1.0, 1.0);
92                 bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
93                 bld.texcoord(0.0, 1.0);
94                 bld.vertex(rgeom.x, rgeom.y+rgeom.h);
95                 bld.end();
96         }
97 }
98
99 } // namespace GLtk
100 } // namespace Msp