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