]> git.tdb.fi Git - libs/gltk.git/blob - source/part.cpp
Rework event passing system to allow for pointer grabs
[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         width(1),
14         height(1),
15         fill_x(true),
16         fill_y(true)
17 {
18         for(unsigned i=0; i<N_STATES_; ++i)
19                 graphic[i]=0;
20 }
21
22 const Graphic *Part::get_graphic(State state) const
23 {
24         if(state>N_STATES_)
25                 throw InvalidParameterValue("Invalid state");
26
27         return graphic[state];
28 }
29
30 void Part::render(const Geometry &geom, State state) const
31 {
32         unsigned gw=(fill_x ? geom.w : width);
33         unsigned gh=(fill_y ? geom.h : height);
34         align.apply(geom, gw, gh);
35         graphic[state]->render(gw, gh);
36 }
37
38
39 Part::Loader::Loader(Part &p):
40         part(p)
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]=&part.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