]> git.tdb.fi Git - libs/gltk.git/blob - image.cpp
a2d44269b86a394115d696796cbc8fbb6fbb5f7c
[libs/gltk.git] / 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, CachedPart &cache)
57 {
58         if(part.get_name()=="image")
59         {
60                 if(!image)
61                 {
62                         cache.texture = 0;
63                         return;
64                 }
65
66                 const Alignment &align = part.get_alignment();
67                 Geometry rgeom = part.get_geometry();
68                 align.apply(rgeom, geom, part.get_margin());
69
70                 if(keep_aspect)
71                 {
72                         float aspect = static_cast<float>(image->get_width())/image->get_height();
73                         if(rgeom.w<rgeom.h*aspect)
74                         {
75                                 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
76                                 rgeom.y += (rgeom.h-h)*align.y;
77                                 rgeom.h = h;
78                         }
79                         else
80                         {
81                                 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
82                                 rgeom.x += (rgeom.w-w)*align.x;
83                                 rgeom.w = w;
84                         }
85                 }
86
87                 cache.texture = image;
88                 cache.clear_mesh();
89
90                 GL::MeshBuilder bld(*cache.mesh);
91                 bld.color(1.0f, 1.0f, 1.0f);
92                 bld.begin(GL::QUADS);
93                 bld.texcoord(0.0, 0.0);
94                 bld.vertex(rgeom.x, rgeom.y);
95                 bld.texcoord(1.0, 0.0);
96                 bld.vertex(rgeom.x+rgeom.w, rgeom.y);
97                 bld.texcoord(1.0, 1.0);
98                 bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
99                 bld.texcoord(0.0, 1.0);
100                 bld.vertex(rgeom.x, rgeom.y+rgeom.h);
101                 bld.end();
102         }
103 }
104
105 } // namespace GLtk
106 } // namespace Msp