1 /** DGui project file.
2 
3 Copyright: Trogu Antonio Davide 2011-2013
4 
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 
7 Authors: Trogu Antonio Davide
8 */
9 module dguihub.core.geometry;
10 
11 import dguihub.core.winapi;
12 
13 struct Rect {
14    public union {
15       align(1) struct {
16          uint left = 0;
17          uint top = 0;
18          uint right = 0;
19          uint bottom = 0;
20       }
21 
22       RECT rect;
23    }
24 
25    public static Rect opCall(Point pt, Size sz) {
26       return opCall(pt.x, pt.y, sz.width, sz.height);
27    }
28 
29    public static Rect opCall(uint l, uint t, uint w, uint h) {
30       Rect r = void; //Viene inizializzata sotto.
31 
32       r.left = l;
33       r.top = t;
34       r.right = l + w;
35       r.bottom = t + h;
36 
37       return r;
38    }
39 
40    public const bool opEquals(ref const Rect r) {
41       return this.left == r.left && this.top == r.top && this.right == r.right
42          && this.bottom == r.bottom;
43    }
44 
45    @property public int x() {
46       return this.left;
47    }
48 
49    @property public void x(int newX) {
50       int w = this.width;
51 
52       this.left = newX;
53       this.right = newX + w;
54    }
55 
56    @property public int y() {
57       return this.top;
58    }
59 
60    @property public void y(int newY) {
61       int h = this.height;
62 
63       this.top = newY;
64       this.bottom = newY + h;
65    }
66 
67    @property public int width() {
68       if (this.right != CW_USEDEFAULT) {
69          return this.right - this.left;
70       }
71 
72       return CW_USEDEFAULT;
73    }
74 
75    @property public void width(int w) {
76       this.right = this.left + w;
77    }
78 
79    @property public int height() {
80       if (this.bottom != CW_USEDEFAULT) {
81          return this.bottom - this.top;
82       }
83 
84       return CW_USEDEFAULT;
85    }
86 
87    @property public void height(int h) {
88       this.bottom = this.top + h;
89    }
90 
91    @property public Point position() {
92       return Point(this.left, this.top);
93    }
94 
95    @property public void position(Point pt) {
96       Size sz = this.size; //Copia dimensioni
97 
98       this.left = pt.x;
99       this.top = pt.y;
100       this.right = this.left + sz.width;
101       this.bottom = this.top + sz.height;
102    }
103 
104    @property public Size size() {
105       return Size(this.width, this.height);
106    }
107 
108    @property public void size(Size sz) {
109       this.right = this.left + sz.width;
110       this.bottom = this.top + sz.height;
111    }
112 
113    @property public bool empty() {
114       return this.width <= 0 && this.height <= 0;
115    }
116 
117    public static Rect fromRECT(RECT* pWinRect) {
118       Rect r = void; //Inizializzata sotto
119 
120       r.rect = *pWinRect;
121       return r;
122    }
123 }
124 
125 struct Point {
126    public union {
127       align(1) struct {
128          uint x = 0;
129          uint y = 0;
130       }
131 
132       POINT point;
133    }
134 
135    public bool inRect(Rect r) {
136       if (point.x < r.left || point.y < r.top || point.x > r.right || point.y > r.bottom) {
137          return false;
138       }
139 
140       return true;
141    }
142 
143    public bool opEquals(ref const Point pt) const {
144       return this.x == pt.x && this.y == pt.y;
145    }
146 
147    public static Point opCall(int x, int y) {
148       Point pt = void; //Viene inizializzata sotto.
149 
150       pt.x = x;
151       pt.y = y;
152       return pt;
153    }
154 }
155 
156 struct Size {
157    public union {
158       align(1) struct {
159          uint width = 0;
160          uint height = 0;
161       }
162 
163       SIZE size;
164    }
165 
166    public bool opEquals(ref const Size sz) const {
167       return this.width == sz.width && this.height == sz.height;
168    }
169 
170    public static Size opCall(int w, int h) {
171       Size sz = void;
172 
173       sz.width = w;
174       sz.height = h;
175       return sz;
176    }
177 }
178 
179 public const Rect nullRect; // = Rect.init;
180 public const Point nullPoint; // = Point.init;
181 public const Size nullSize; // = Size.init;