]> git.tdb.fi Git - libs/gltk.git/blob - source/geometry.cpp
Add various constructors to Sides (semantics inspired by CSS margins)
[libs/gltk.git] / source / geometry.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007-2008, 2010-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "geometry.h"
9
10 namespace Msp {
11 namespace GLtk {
12
13 bool Geometry::is_inside(int x_, int y_) const
14 {
15         return (x_>=x && x_<x+static_cast<int>(w) && y_>=y && y_<y+static_cast<int>(h));
16 }
17
18 bool Geometry::is_inside_relative(int x_, int y_) const
19 {
20         return (x_>=0 && x_<static_cast<int>(w) && y_>=0 && y_<static_cast<int>(h));
21 }
22
23
24 Sides::Sides():
25         top(0),
26         right(0),
27         bottom(0),
28         left(0)
29 { }
30
31 Sides::Sides(unsigned s):
32         top(s),
33         right(s),
34         bottom(s),
35         left(s)
36 { }
37
38 Sides::Sides(unsigned v, unsigned h):
39         top(v),
40         right(h),
41         bottom(v),
42         left(h)
43 { }
44
45 Sides::Sides(unsigned t, unsigned h, unsigned b):
46         top(t),
47         right(h),
48         bottom(b),
49         left(h)
50 { }
51
52 Sides::Sides(unsigned t, unsigned r, unsigned b, unsigned l):
53         top(t),
54         right(r),
55         bottom(b),
56         left(l)
57 { }
58
59
60 Sides::Loader::Loader(Sides &s):
61         DataFile::ObjectLoader<Sides>(s)
62 {
63         add("top",    &Sides::top);
64         add("right",  &Sides::right);
65         add("bottom", &Sides::bottom);
66         add("left",   &Sides::left);
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