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