]> git.tdb.fi Git - libs/gltk.git/blob - source/part.cpp
Combine common parts of Column and Row into LinearArrangement
[libs/gltk.git] / source / part.cpp
1 #include <msp/gl/meshbuilder.h>
2 #include "geometry.h"
3 #include "part.h"
4 #include "partcache.h"
5 #include "resources.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GLtk {
11
12 Part::Part(const string &n):
13         name(n)
14 {
15         fill(graphic, graphic+N_STATES_, static_cast<Graphic *>(0));
16 }
17
18 const Graphic *Part::get_graphic(State state) const
19 {
20         if(state>N_STATES_)
21                 throw invalid_argument("Part::get_graphic");
22
23         return graphic[state];
24 }
25
26 void Part::build(const Geometry &parent, State state, CachedPart &cache) const
27 {
28         if(!graphic[state])
29         {
30                 cache.texture = 0;
31                 return;
32         }
33
34         cache.texture = graphic[state]->get_texture();
35         cache.clear_mesh();
36
37         Geometry rgeom = geom;
38         align.apply(rgeom, parent, margin);
39         GL::MeshBuilder bld(*cache.mesh);
40         bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
41         graphic[state]->build(rgeom.w, rgeom.h, bld);
42 }
43
44
45 Part::Loader::Loader(Part &p, Resources &r):
46         DataFile::CollectionObjectLoader<Part>(p, &r)
47 {
48         add("graphic", &Loader::graphic);
49         add("align",   &Loader::align);
50         add("fill",    &Loader::fill);
51         add("margin",  &Loader::margin);
52         add("size",    &Loader::size);
53 }
54
55 Part::Loader::~Loader()
56 {
57         for(unsigned i=0; i<N_STATES_; ++i)
58                 if(obj.graphic[i])
59                 {
60                         const Graphic &grph = *obj.graphic[i];
61                         const Sides &shadow = grph.get_shadow();
62                         obj.geom.w = max(obj.geom.w, grph.get_width()-shadow.left-shadow.right);
63                         obj.geom.h = max(obj.geom.h, grph.get_height()-shadow.bottom-shadow.top);
64                 }
65 }
66
67 void Part::Loader::graphic(State s, const string &n)
68 {
69         Graphic *grph = &get_collection().get<Graphic>(n);
70         for(int i=0; i<N_STATES_; ++i)
71                 if((i&s)==s)
72                         obj.graphic[i] = grph;
73 }
74
75 void Part::Loader::align(float x, float y)
76 {
77         obj.align.x = x;
78         obj.align.y = y;
79 }
80
81 void Part::Loader::fill(float w, float h)
82 {
83         obj.align.w = w;
84         obj.align.h = h;
85 }
86
87 void Part::Loader::margin()
88 {
89         load_sub(obj.margin);
90 }
91
92 void Part::Loader::size(unsigned w, unsigned h)
93 {
94         obj.geom.w = w;
95         obj.geom.h = h;
96 }
97
98 } // namespace GLtk
99 } // namespace Msp