]> git.tdb.fi Git - libs/gltk.git/blob - source/part.cpp
Refactor filling from Part to Alignment
[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 }
54
55 Part::Loader::~Loader()
56 {
57         for(unsigned i=0; i<N_STATES_; ++i)
58         {
59                 if(part.graphic[i])
60                 {
61                         const Sides &shadow=part.graphic[i]->get_shadow();
62                         part.geom.w=max(part.geom.w, part.graphic[i]->get_width()-shadow.left-shadow.right);
63                         part.geom.h=max(part.geom.h, part.graphic[i]->get_height()-shadow.bottom-shadow.top);
64                 }
65                 else
66                         part.graphic[i]=part.graphic[NORMAL];
67         }
68 }
69
70 void Part::Loader::graphic(State s, const string &n)
71 {
72         part.graphic[s]=res.get<Graphic>(n);
73 }
74
75 void Part::Loader::align(float x, float y)
76 {
77         part.align.x=x;
78         part.align.y=y;
79 }
80
81 void Part::Loader::fill(float w, float h)
82 {
83         part.align.w=w;
84         part.align.h=h;
85 }
86
87 void Part::Loader::margin()
88 {
89         load_sub(part.margin);
90 }
91
92 } // namespace GLtk
93 } // namespace Msp