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