]> git.tdb.fi Git - libs/gltk.git/blob - source/part.cpp
Adapt to DataFile changes
[libs/gltk.git] / source / part.cpp
1 #include "geometry.h"
2 #include "part.h"
3 #include "resources.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GLtk {
9
10 Part::Part(const string &n):
11         name(n),
12         width(1),
13         height(1),
14         fill_x(true),
15         fill_y(true)
16 {
17         for(unsigned i=0; i<N_STATES_; ++i)
18                 graphic[i]=0;
19 }
20
21 const Graphic *Part::get_graphic(State state) const
22 {
23         if(state>N_STATES_)
24                 throw InvalidParameterValue("Invalid state");
25
26         return graphic[state];
27 }
28
29 void Part::render(const Geometry &geom, State state) const
30 {
31         unsigned gw=(fill_x ? geom.w : width);
32         unsigned gh=(fill_y ? geom.h : height);
33         align.apply(geom, gw, gh);
34         graphic[state]->render(gw, gh);
35 }
36
37
38 Part::Loader::Loader(Part &p, Resources &r):
39         part(p),
40         res(r)
41 {
42         add("graphic", &Loader::graphic);
43         add("align",   &Loader::align);
44         add("fill",    &Loader::fill);
45 }
46
47 Part::Loader::~Loader()
48 {
49         for(unsigned i=0; i<N_STATES_; ++i)
50         {
51                 if(part.graphic[i])
52                 {
53                         const Sides &shadow=part.graphic[i]->get_shadow();
54                         part.width=max(part.width, part.graphic[i]->get_width()-shadow.left-shadow.right);
55                         part.height=max(part.height, part.graphic[i]->get_height()-shadow.bottom-shadow.top);
56                 }
57                 else
58                         part.graphic[i]=part.graphic[NORMAL];
59         }
60 }
61
62 void Part::Loader::graphic(State s, const string &n)
63 {
64         part.graphic[s]=res.get<Graphic>(n);
65 }
66
67 void Part::Loader::align(int x, int y)
68 {
69         part.align.x=x;
70         part.align.y=y;
71 }
72
73 void Part::Loader::fill(bool x, bool y)
74 {
75         part.fill_x=x;
76         part.fill_y=y;
77 }
78
79 } // namespace GLtk
80 } // namespace Msp