]> git.tdb.fi Git - libs/gltk.git/blob - source/image.cpp
8dbf6cf74fe881e6e871be3894e0ae51aa3147b6
[libs/gltk.git] / source / image.cpp
1 #include <msp/gl/meshbuilder.h>
2 #include "image.h"
3 #include "part.h"
4 #include "resources.h"
5 #include "root.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 {
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         icon_name = string();
40         signal_autosize_changed.emit();
41         mark_rebuild();
42 }
43
44 void Image::set_icon(const string &n)
45 {
46         icon_name = n;
47         update_icon();
48 }
49
50 void Image::set_keep_aspect(bool ka)
51 {
52         keep_aspect = ka;
53         mark_rebuild();
54 }
55
56 void Image::update_icon()
57 {
58         if(style)
59         {
60                 Root *root = find_ancestor<Root>();
61                 if(root)
62                 {
63                         if(icon_name.empty())
64                                 image = 0;
65                         else
66                                 image = &root->get_resources().get<GL::Texture2D>(icon_name);
67                         signal_autosize_changed.emit();
68                         mark_rebuild();
69                         return;
70                 }
71         }
72
73         image = 0;
74 }
75
76 void Image::rebuild_special(const Part &part)
77 {
78         if(part.get_name()=="image")
79         {
80                 if(!image)
81                         return;
82
83                 const Alignment &align = part.get_alignment();
84                 Geometry rgeom = part.get_geometry();
85                 align.apply(rgeom, geom, part.get_margin());
86
87                 if(keep_aspect)
88                 {
89                         float aspect = static_cast<float>(image->get_width())/image->get_height();
90                         if(rgeom.w<rgeom.h*aspect)
91                         {
92                                 unsigned h = static_cast<unsigned>(rgeom.w/aspect);
93                                 rgeom.y += (rgeom.h-h)*align.y;
94                                 rgeom.h = h;
95                         }
96                         else
97                         {
98                                 unsigned w = static_cast<unsigned>(rgeom.h*aspect);
99                                 rgeom.x += (rgeom.w-w)*align.x;
100                                 rgeom.w = w;
101                         }
102                 }
103
104                 GL::MeshBuilder bld(part_cache.create_mesh(part, *image));
105                 bld.color(1.0f, 1.0f, 1.0f);
106                 bld.begin(GL::TRIANGLE_STRIP);
107                 bld.texcoord(0.0, 1.0);
108                 bld.vertex(rgeom.x, rgeom.y+rgeom.h);
109                 bld.texcoord(0.0, 0.0);
110                 bld.vertex(rgeom.x, rgeom.y);
111                 bld.texcoord(1.0, 1.0);
112                 bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
113                 bld.texcoord(1.0, 0.0);
114                 bld.vertex(rgeom.x+rgeom.w, rgeom.y);
115                 bld.end();
116         }
117 }
118
119 void Image::on_style_change()
120 {
121         if(!icon_name.empty())
122                 update_icon();
123 }
124
125 void Image::on_reparent()
126 {
127         if(!icon_name.empty())
128                 update_icon();
129 }
130
131
132 Image::Loader::Loader(Image &img):
133         DataFile::DerivedObjectLoader<Image, Widget::Loader>(img)
134 {
135         add("icon", &Image::icon_name);
136 }
137
138 } // namespace GLtk
139 } // namespace Msp