]> git.tdb.fi Git - libs/gltk.git/blob - source/geometry.cpp
Simplify constructors with C++11
[libs/gltk.git] / source / geometry.cpp
1 #include "geometry.h"
2
3 namespace Msp {
4 namespace GLtk {
5
6 bool Geometry::is_inside(int x_, int y_) const
7 {
8         return (x_>=x && x_<x+static_cast<int>(w) && y_>=y && y_<y+static_cast<int>(h));
9 }
10
11 bool Geometry::is_inside_relative(int x_, int y_) const
12 {
13         return (x_>=0 && x_<static_cast<int>(w) && y_>=0 && y_<static_cast<int>(h));
14 }
15
16
17 Sides::Sides(unsigned s):
18         top(s),
19         right(s),
20         bottom(s),
21         left(s)
22 { }
23
24 Sides::Sides(unsigned v, unsigned h):
25         top(v),
26         right(h),
27         bottom(v),
28         left(h)
29 { }
30
31 Sides::Sides(unsigned t, unsigned h, unsigned b):
32         top(t),
33         right(h),
34         bottom(b),
35         left(h)
36 { }
37
38 Sides::Sides(unsigned t, unsigned r, unsigned b, unsigned l):
39         top(t),
40         right(r),
41         bottom(b),
42         left(l)
43 { }
44
45
46 Sides::Loader::Loader(Sides &s):
47         DataFile::ObjectLoader<Sides>(s)
48 {
49         add("horizontal", &Loader::horizontal);
50         add("vertical",   &Loader::vertical);
51         add("top",    &Sides::top);
52         add("right",  &Sides::right);
53         add("bottom", &Sides::bottom);
54         add("left",   &Sides::left);
55 }
56
57 void Sides::Loader::horizontal(unsigned h)
58 {
59         obj.right = h;
60         obj.left = h;
61 }
62
63 void Sides::Loader::vertical(unsigned v)
64 {
65         obj.top = v;
66         obj.bottom = v;
67 }
68
69
70 void Alignment::apply(Geometry &geom, const Geometry &parent) const
71 {
72         if(parent.w>geom.w)
73         {
74                 geom.w += static_cast<unsigned>((parent.w-geom.w)*w);
75                 geom.x += static_cast<int>((parent.w-geom.w)*x);
76         }
77         if(parent.h>geom.h)
78         {
79                 geom.h += static_cast<unsigned>((parent.h-geom.h)*h);
80                 geom.y += static_cast<int>((parent.h-geom.h)*y);
81         }
82 }
83
84 void Alignment::apply(Geometry &geom, const Geometry &parent, const Sides &margin) const
85 {
86         Geometry content = parent;
87         content.w -= margin.left+margin.right;
88         content.h -= margin.bottom+margin.top;
89
90         geom.x += margin.left;
91         geom.y += margin.bottom;
92
93         apply(geom, content);
94 }
95
96 } // namespace GLtk
97 } // namespace Msp