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