]> git.tdb.fi Git - libs/gltk.git/blob - source/part.cpp
a74c91141ca717c86262c8a634f8db75ff359226
[libs/gltk.git] / source / part.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/gl/transform.h>
9 #include "geometry.h"
10 #include "part.h"
11 #include "resources.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GLtk {
17
18 Part::Part(const string &n):
19         name(n)
20 {
21         for(unsigned i=0; i<N_STATES_; ++i)
22                 graphic[i]=0;
23 }
24
25 const Graphic *Part::get_graphic(State state) const
26 {
27         if(state>N_STATES_)
28                 throw InvalidParameterValue("Invalid state");
29
30         return graphic[state];
31 }
32
33 void Part::render(const Geometry &parent, State state) const
34 {
35         if(!graphic[state])
36                 return;
37
38         Geometry rgeom=geom;
39         align.apply(rgeom, parent, margin);
40         GL::translate(rgeom.x, rgeom.y, 0);
41         graphic[state]->render(rgeom.w, rgeom.h);
42 }
43
44
45 Part::Loader::Loader(Part &p, Resources &r):
46         part(p),
47         res(r)
48 {
49         add("graphic", &Loader::graphic);
50         add("align",   &Loader::align);
51         add("fill",    &Loader::fill);
52         add("margin",  &Loader::margin);
53         add("size",    &Loader::size);
54 }
55
56 Part::Loader::~Loader()
57 {
58         for(unsigned i=0; i<N_STATES_; ++i)
59         {
60                 if(part.graphic[i])
61                 {
62                         const Sides &shadow=part.graphic[i]->get_shadow();
63                         part.geom.w=max(part.geom.w, part.graphic[i]->get_width()-shadow.left-shadow.right);
64                         part.geom.h=max(part.geom.h, part.graphic[i]->get_height()-shadow.bottom-shadow.top);
65                 }
66                 else
67                         part.graphic[i]=part.graphic[NORMAL];
68         }
69 }
70
71 void Part::Loader::graphic(State s, const string &n)
72 {
73         part.graphic[s]=res.get<Graphic>(n);
74 }
75
76 void Part::Loader::align(float x, float y)
77 {
78         part.align.x=x;
79         part.align.y=y;
80 }
81
82 void Part::Loader::fill(float w, float h)
83 {
84         part.align.w=w;
85         part.align.h=h;
86 }
87
88 void Part::Loader::margin()
89 {
90         load_sub(part.margin);
91 }
92
93 void Part::Loader::size(unsigned w, unsigned h)
94 {
95         part.geom.w=w;
96         part.geom.h=h;
97 }
98
99 } // namespace GLtk
100 } // namespace Msp