]> git.tdb.fi Git - libs/gltk.git/blob - source/part.cpp
dbccc060e828c8b4b5f1f07a5f650dfc94d638fa
[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
16 const Graphic *Part::get_graphic(State state) const
17 {
18         if(state>N_STATES_)
19                 throw invalid_argument("Part::get_graphic");
20
21         return graphic[state];
22 }
23
24 void Part::build(const Geometry &parent, State state, PartCache &cache) const
25 {
26         if(!graphic[state] || !graphic[state]->get_texture())
27                 return;
28
29         Geometry rgeom = geom;
30         align.apply(rgeom, parent, margin);
31         GL::MeshBuilder bld(cache.create_mesh(*this, *graphic[state]->get_texture()));
32         bld.transform(GL::Matrix::translation(rgeom.x, rgeom.y, 0));
33         graphic[state]->build(rgeom.w, rgeom.h, bld);
34 }
35
36
37 Part::Loader::Loader(Part &p, Resources &r):
38         DataFile::CollectionObjectLoader<Part>(p, &r)
39 {
40         add("graphic", &Loader::graphic);
41         add("align",   &Loader::align);
42         add("fill",    &Loader::fill);
43         add("margin",  &Loader::margin);
44         add("size",    &Loader::size);
45 }
46
47 Part::Loader::~Loader()
48 {
49         for(unsigned i=0; i<N_STATES_; ++i)
50                 if(obj.graphic[i])
51                 {
52                         const Graphic &grph = *obj.graphic[i];
53                         const Sides &shadow = grph.get_shadow();
54                         obj.geom.w = max(obj.geom.w, grph.get_width()-shadow.left-shadow.right);
55                         obj.geom.h = max(obj.geom.h, grph.get_height()-shadow.bottom-shadow.top);
56                 }
57 }
58
59 void Part::Loader::graphic_normal(const string &n)
60 {
61         graphic(NORMAL, n);
62 }
63
64 void Part::Loader::graphic(State s, const string &n)
65 {
66         Graphic *grph = (n.empty() ? nullptr : &get_collection().get<Graphic>(n));
67         for(int i=0; i<N_STATES_; ++i)
68                 if((i&s)==s)
69                         obj.graphic[i] = grph;
70 }
71
72 void Part::Loader::align(float x, float y)
73 {
74         obj.align.x = x;
75         obj.align.y = y;
76 }
77
78 void Part::Loader::fill(float w, float h)
79 {
80         obj.align.w = w;
81         obj.align.h = h;
82 }
83
84 void Part::Loader::margin()
85 {
86         load_sub(obj.margin);
87 }
88
89 void Part::Loader::size(unsigned w, unsigned h)
90 {
91         obj.geom.w = w;
92         obj.geom.h = h;
93 }
94
95 } // namespace GLtk
96 } // namespace Msp