]> git.tdb.fi Git - libs/gltk.git/blob - source/part.cpp
Initial revision
[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 Resources &r, const string &n):
11         res(r),
12         name(n),
13         fill_x(true),
14         fill_y(true)
15 {
16         for(unsigned i=0; i<N_STATES_; ++i)
17                 graphic[i]=0;
18 }
19
20 const Graphic *Part::get_graphic(State state) const
21 {
22         if(state>N_STATES_)
23                 throw InvalidParameterValue("Invalid state");
24
25         return graphic[state];
26 }
27
28 void Part::render(const Geometry &geom, State state) const
29 {
30         const Graphic::Sides &shadow=graphic[state]->get_shadow();
31         unsigned gw=(fill_x ? geom.w : graphic[state]->get_width())-shadow.left-shadow.right;
32         unsigned gh=(fill_y ? geom.h : graphic[state]->get_height())-shadow.top-shadow.bottom;
33         align.apply(geom, gw, gh);
34         graphic[state]->render(gw, gh);
35 }
36
37
38 Part::Loader::Loader(Part &p):
39         part(p)
40 {
41         add("graphic", &Loader::graphic);
42         add("align",   &Loader::align);
43         add("fill",    &Loader::fill);
44 }
45
46 void Part::Loader::graphic(State s, const string &n)
47 {
48         part.graphic[s]=&part.res.get_graphic(n);
49         if(s==NORMAL)
50         {
51                 for(unsigned i=0; i<N_STATES_; ++i)
52                         if(!part.graphic[i])
53                                 part.graphic[i]=part.graphic[s];
54         }
55 }
56
57 void Part::Loader::align(int x, int y)
58 {
59         part.align.x=x;
60         part.align.y=y;
61 }
62
63 void Part::Loader::fill(bool x, bool y)
64 {
65         part.fill_x=x;
66         part.fill_y=y;
67 }
68
69 } // namespace GLtk
70 } // namespace Msp