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