]> git.tdb.fi Git - libs/gltk.git/blob - source/part.cpp
Loader improvements
[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         DataFile::CollectionObjectLoader<Part>(p, &r)
47 {
48         add("graphic", &Loader::graphic);
49         add("align",   &Loader::align);
50         add("fill",    &Loader::fill);
51         add("margin",  &Loader::margin);
52         add("size",    &Loader::size);
53 }
54
55 Part::Loader::~Loader()
56 {
57         for(unsigned i=0; i<N_STATES_; ++i)
58                 if(obj.graphic[i])
59                 {
60                         const Graphic &grph = *obj.graphic[i];
61                         const Sides &shadow = grph.get_shadow();
62                         obj.geom.w = max(obj.geom.w, grph.get_width()-shadow.left-shadow.right);
63                         obj.geom.h = max(obj.geom.h, grph.get_height()-shadow.bottom-shadow.top);
64                 }
65 }
66
67 void Part::Loader::graphic(State s, const string &n)
68 {
69         Graphic *grph = get_collection().get<Graphic>(n);
70         for(int i=0; i<N_STATES_; ++i)
71                 if((i&s)==s)
72                         obj.graphic[i] = grph;
73 }
74
75 void Part::Loader::align(float x, float y)
76 {
77         obj.align.x = x;
78         obj.align.y = y;
79 }
80
81 void Part::Loader::fill(float w, float h)
82 {
83         obj.align.w = w;
84         obj.align.h = h;
85 }
86
87 void Part::Loader::margin()
88 {
89         load_sub(obj.margin);
90 }
91
92 void Part::Loader::size(unsigned w, unsigned h)
93 {
94         obj.geom.w = w;
95         obj.geom.h = h;
96 }
97
98 } // namespace GLtk
99 } // namespace Msp