]> git.tdb.fi Git - libs/gltk.git/blob - source/image.cpp
383f906911993d2a83d8d434ec791f8c0e90d303
[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 }
16
17 void Image::autosize_special(const Part &part, Geometry &ageom) const
18 {
19         if(part.get_name()=="image")
20         {
21                 const Sides &margin = part.get_margin();
22                 if(image)
23                 {
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);
26                 }
27                 else
28                 {
29                         ageom.w = max(ageom.w, margin.left+margin.right);
30                         ageom.h = max(ageom.h, margin.top+margin.bottom);
31                 }
32         }
33 }
34
35 void Image::set_image(const GL::Texture2D *i)
36 {
37         image = i;
38         signal_autosize_changed.emit();
39         rebuild();
40 }
41
42 void Image::set_keep_aspect(bool ka)
43 {
44         keep_aspect = ka;
45         rebuild();
46 }
47
48 void Image::rebuild_special(const Part &part)
49 {
50         if(part.get_name()=="image")
51         {
52                 if(!image)
53                         return;
54
55                 const Alignment &align = part.get_alignment();
56                 Geometry rgeom = part.get_geometry();
57                 align.apply(rgeom, geom, part.get_margin());
58
59                 if(keep_aspect)
60                 {
61                         float aspect = static_cast<float>(image->get_width())/image->get_height();
62                         if(rgeom.w<rgeom.h*aspect)
63                         {
64                                 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
65                                 rgeom.y += (rgeom.h-h)*align.y;
66                                 rgeom.h = h;
67                         }
68                         else
69                         {
70                                 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
71                                 rgeom.x += (rgeom.w-w)*align.x;
72                                 rgeom.w = w;
73                         }
74                 }
75
76                 GL::MeshBuilder bld(part_cache.create_mesh(part, *image));
77                 bld.color(1.0f, 1.0f, 1.0f);
78                 bld.begin(GL::TRIANGLE_STRIP);
79                 bld.texcoord(0.0, 1.0);
80                 bld.vertex(rgeom.x, rgeom.y+rgeom.h);
81                 bld.texcoord(0.0, 0.0);
82                 bld.vertex(rgeom.x, rgeom.y);
83                 bld.texcoord(1.0, 1.0);
84                 bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
85                 bld.texcoord(1.0, 0.0);
86                 bld.vertex(rgeom.x+rgeom.w, rgeom.y);
87                 bld.end();
88         }
89 }
90
91 } // namespace GLtk
92 } // namespace Msp