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