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