-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.Rects.pas
427 lines (349 loc) · 11.9 KB
/
JPL.Rects.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
unit JPL.Rects;
{
Helper record and routines for operations on rectangles.
}
{$I .\..\jp.inc}
{$IFDEF FPC}
{$mode delphi}
{$MODESWITCH ADVANCEDRECORDS}
{$ENDIF}
interface
uses
{$IFDEF MSWINDOWS}Windows,{$ENDIF}
SysUtils, Types;
type
TRectPos = (
rpTopLeft, rpTopCenter, rpTopRight,
rpLeftCenter, rpCenter, rpRightCenter,
rpBottomLeft, rpBottomCenter, rpBottomRight
);
TRectHelper = record helper for TRect
private
{$IFNDEF HAS_ADVANCED_TRECT}
function GetWidth: integer;
procedure SetWidth(const Value: integer);
function GetHeight: integer;
procedure SetHeight(const Value: integer);
{$ENDIF}
public
{$IFNDEF HAS_ADVANCED_TRECT}
constructor Create(const Origin: TPoint); overload; // empty rect at given origin
constructor Create(const Origin: TPoint; Width, Height: Integer); overload;
constructor Create(const Left, Top, Right, Bottom: Integer); overload;
constructor Create(const P1, P2: TPoint; Normalize: Boolean = False); overload;
constructor Create(const R: TRect; Normalize: Boolean = False); overload;
class function Empty: TRect; inline; static;
procedure NormalizeRect;
function Contains(const Pt: TPoint): Boolean; overload;
function Contains(const R: TRect): Boolean; overload;
class function Union(const R1: TRect; const R2: TRect): TRect; overload; static;
procedure Union(const R: TRect); overload;
class function Union(const Points: array of TPoint): TRect; overload; static;
procedure Offset(const DX, DY: Integer); overload;
procedure Offset(const Point: TPoint); overload;
procedure SetLocation(const X, Y: Integer); overload;
procedure SetLocation(const Point: TPoint); overload;
procedure Inflate(const DX, DY: Integer); overload;
procedure Inflate(const DL, DT, DR, DB: Integer); overload;
{$ENDIF}
function InflatedRect(const dx, dy: integer): TRect; overload;
function InflatedRect(const Pt: TPoint): TRect; overload;
class procedure CenterInRect(const MainRect: TRect; var CenteredRect: TRect; CenterHorizontally: Boolean = True; CenterVertically: Boolean = True); overload; static;
procedure CenterInRect(const R: TRect; CenterHorizontally: Boolean = True; CenterVertically: Boolean = True); overload;
class procedure Align(const MainRect: TRect; var AlignedRect: TRect; const RectPos: TRectPos); static;
procedure AlignInRect(const R: TRect; const RectPos: TRectPos);
{$IFNDEF HAS_ADVANCED_TRECT}
property Width: integer read GetWidth write SetWidth;
property Height: integer read GetHeight write SetHeight;
{$ENDIF}
end;
function RectWidth(const R: TRect): integer;
function RectHeight(const R: TRect): integer;
function PointInRect(const Point: TPoint; const Rect: TRect; IncludeRightAndBottomSides: Boolean = False): Boolean;
procedure CenterRect(const MainRect: TRect; var CenteredRect: TRect; CenterHorizontally: Boolean = True; CenterVertically: Boolean = True);
procedure AlignRect(const MainRect: TRect; var AlignedRect: TRect; const RectPos: TRectPos);
procedure InflateRectEx(var ARect: TRect; const DeltaLeft, DeltaRight, DeltaTop, DeltaBottom: integer);
procedure DeflateRect(var ARect: TRect; const DX, DY: integer);
function GetTextMiddlePosY(const R: TRect; const TextHeight: integer): integer;
function GetTextMiddlePosX(const R: TRect; const TextWidth: integer): integer;
implementation
{$Region ' Helper routines '}
function RectWidth(const R: TRect): integer;
begin
Result := R.Right - R.Left;
end;
function RectHeight(const R: TRect): integer;
begin
Result := R.Bottom - R.Top;
end;
{
A point is within a rectangle if it lies on the left or top side or is within all four sides.
A point on the right or bottom side is considered **outside** the rectangle.
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-ptinrect
}
function PointInRect(const Point: TPoint; const Rect: TRect; IncludeRightAndBottomSides: Boolean = False): Boolean;
begin
if IncludeRightAndBottomSides then
Result := (Point.X >= Rect.Left) and (Point.X <= Rect.Right) and (Point.Y >= Rect.Top) and (Point.Y <= Rect.Bottom)
else
Result := (Point.X >= Rect.Left) and (Point.X < Rect.Right) and (Point.Y >= Rect.Top) and (Point.Y < Rect.Bottom);
end;
procedure CenterRect(const MainRect: TRect; var CenteredRect: TRect; CenterHorizontally: Boolean = True; CenterVertically: Boolean = True);
var
xp: integer;
begin
if CenterHorizontally then
begin
xp := MainRect.Left + (MainRect.Width div 2) - (CenteredRect.Width div 2);
CenteredRect.SetLocation(xp, CenteredRect.Top);
end;
if CenterVertically then
begin
xp := MainRect.Top + (MainRect.Height div 2) - (CenteredRect.Height div 2);
CenteredRect.SetLocation(CenteredRect.Left, xp);
end;
end;
procedure AlignRect(const MainRect: TRect; var AlignedRect: TRect; const RectPos: TRectPos);
var
x, y: integer;
begin
x := AlignedRect.Left;
y := AlignedRect.Top;
case RectPos of
rpTopLeft:
begin
x := MainRect.Left;
y := MainRect.Top;
end;
rpTopCenter:
begin
x := MainRect.Left + (MainRect.Width div 2) - (AlignedRect.Width div 2);
y := MainRect.Top;
end;
rpTopRight:
begin
x := MainRect.Left + MainRect.Width - AlignedRect.Width;
y := MainRect.Top;
end;
rpLeftCenter:
begin
x := MainRect.Left;
y := MainRect.Top + (MainRect.Height div 2) - (AlignedRect.Height div 2);
end;
rpCenter:
begin
x := MainRect.Left + (MainRect.Width div 2) - (AlignedRect.Width div 2);
y := MainRect.Top + (MainRect.Height div 2) - (AlignedRect.Height div 2);
end;
rpRightCenter:
begin
x := MainRect.Left + MainRect.Width - AlignedRect.Width;
y := MainRect.Top + (MainRect.Height div 2) - (AlignedRect.Height div 2);
end;
rpBottomLeft:
begin
x := MainRect.Left;
y := MainRect.Top + MainRect.Height - AlignedRect.Height;
end;
rpBottomCenter:
begin
x := MainRect.Left + (MainRect.Width div 2) - (AlignedRect.Width div 2);
y := MainRect.Top + MainRect.Height - AlignedRect.Height;
end;
rpBottomRight:
begin
x := MainRect.Left + MainRect.Width - AlignedRect.Width;
y := MainRect.Top + MainRect.Height - AlignedRect.Height;
end;
end;
AlignedRect.SetLocation(x, y);
end;
procedure InflateRectEx(var ARect: TRect; const DeltaLeft, DeltaRight, DeltaTop, DeltaBottom: integer);
begin
{
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-inflaterect
The InflateRect function increases or decreases the width and height of the specified rectangle.
The InflateRect function adds -dx units to the left end and dx to the right end of the rectangle and -dy units
to the top and dy to the bottom. The dx and dy parameters are signed values; positive values increase the width
and height, and negative values decrease them.
}
ARect.Inflate(DeltaLeft, DeltaTop, DeltaRight, DeltaBottom);
end;
procedure DeflateRect(var ARect: TRect; const DX, DY: integer);
begin
InflateRect(ARect, -DX, -DY);
end;
function GetTextMiddlePosY(const R: TRect; const TextHeight: integer): integer;
begin
Result := R.Top + (RectHeight(R) div 2) - (TextHeight div 2);
end;
function GetTextMiddlePosX(const R: TRect; const TextWidth: integer): integer;
begin
Result := R.Left + (RectWidth(R) div 2) - (TextWidth div 2);
end;
{$endregion Helper routines}
{$region ' TRectHelper '}
{$IFNDEF HAS_ADVANCED_TRECT}
constructor TRectHelper.Create(const Origin: TPoint; Width, Height: Integer);
begin
Create(Origin.X, Origin.Y, Origin.X + Width, Origin.Y + Height);
end;
constructor TRectHelper.Create(const Origin: TPoint);
begin
Create(Origin.X, Origin.Y, Origin.X, Origin.Y);
end;
constructor TRectHelper.Create(const Left, Top, Right, Bottom: Integer);
begin
Self.Left := Left;
Self.Top := Top;
Self.Right := Right;
Self.Bottom := Bottom;
end;
constructor TRectHelper.Create(const R: TRect; Normalize: Boolean);
begin
Self.TopLeft := R.TopLeft;
Self.BottomRight := R.BottomRight;
if Normalize then Self.NormalizeRect;
end;
constructor TRectHelper.Create(const P1, P2: TPoint; Normalize: Boolean);
begin
Self.TopLeft := P1;
Self.BottomRight := P2;
if Normalize then Self.NormalizeRect;
end;
class function TRectHelper.Empty: TRect;
begin
Result := TRect.Create(0, 0, 0, 0);
end;
procedure TRectHelper.NormalizeRect;
begin
if Top > Bottom then
begin
Top := Top xor Bottom;
Bottom := Top xor Bottom;
Top := Top xor Bottom;
end;
if Left > Right then
begin
Left := Left xor Right;
Right:= Left xor Right;
Left := Left xor Right;
end
end;
function TRectHelper.Contains(const Pt: TPoint): Boolean;
begin
Result := PointInRect(Pt, Self);
end;
function TRectHelper.Contains(const R: TRect): Boolean;
begin
Result := Contains(R.TopLeft) and Contains(R.BottomRight);
end;
class function TRectHelper.Union(const R1, R2: TRect): TRect;
begin
UnionRect(Result, R1, R2);
end;
procedure TRectHelper.Union(const R: TRect);
begin
Self := Union(Self, R);
end;
class function TRectHelper.Union(const Points: array of TPoint): TRect;
var
i: Integer;
begin
if Length(Points) > 0 then
begin
Result.TopLeft := Points[Low(Points)];
Result.BottomRight := Points[Low(Points)];
for i := Low(Points) + 1 to High(Points) do
begin
if Points[i].X < Result.Left then Result.Left := Points[i].X;
if Points[i].X > Result.Right then Result.Right := Points[i].X;
if Points[i].Y < Result.Top then Result.Top := Points[i].Y;
if Points[i].Y > Result.Bottom then Result.Bottom := Points[i].Y;
end;
end
else
Result := Empty;
end;
procedure TRectHelper.Offset(const DX, DY: Integer);
begin
Inc(Left, DX);
Inc(Right, DX);
Inc(Top, DY);
Inc(Bottom, DY);
end;
procedure TRectHelper.Offset(const Point: TPoint);
begin
Inc(Left, Point.X);
Inc(Right, Point.X);
Inc(Top, Point.Y);
Inc(Bottom, Point.Y);
end;
procedure TRectHelper.SetLocation(const X, Y: Integer);
begin
Offset(X - Left, Y - Top);
end;
procedure TRectHelper.SetLocation(const Point: TPoint);
begin
Offset(Point.X - Left, Point.Y - Top);
end;
procedure TRectHelper.Inflate(const DX, DY: Integer);
begin
Dec(Left, DX);
Dec(Top, DY);
Inc(Right, DX);
Inc(Bottom, DY);
end;
procedure TRectHelper.Inflate(const DL, DT, DR, DB: Integer);
begin
Dec(Left, DL);
Dec(Top, DT);
Inc(Right, DR);
Inc(Bottom, DB);
end;
function TRectHelper.GetWidth: integer;
begin
Result := Self.Right - Self.Left;
end;
procedure TRectHelper.SetWidth(const Value: integer);
begin
Self.Right := Self.Left + Value;
end;
function TRectHelper.GetHeight: integer;
begin
Result := Self.Bottom - Self.Top;
end;
procedure TRectHelper.SetHeight(const Value: integer);
begin
Self.Bottom := Self.Top + Value;
end;
{$ENDIF} // HAS_ADVANCED_TRECT
function TRectHelper.InflatedRect(const dx, dy: integer): TRect;
begin
Result.Left := Left - dx;
Result.Right := Right + dx;
Result.Top := Top - dy;
Result.Bottom := Bottom + dy;
end;
function TRectHelper.InflatedRect(const Pt: TPoint): TRect;
begin
Result := InflatedRect(Pt.X, Pt.Y);
end;
procedure TRectHelper.CenterInRect(const R: TRect; CenterHorizontally: Boolean = True; CenterVertically: Boolean = True);
begin
CenterRect(R, Self, CenterHorizontally, CenterVertically);
end;
class procedure TRectHelper.CenterInRect(const MainRect: TRect; var CenteredRect: TRect; CenterHorizontally: Boolean = True; CenterVertically: Boolean = True);
begin
CenterRect(MainRect, CenteredRect, CenterHorizontally, CenterVertically);
end;
procedure TRectHelper.AlignInRect(const R: TRect; const RectPos: TRectPos);
begin
AlignRect(R, Self, RectPos);
end;
class procedure TRectHelper.Align(const MainRect: TRect; var AlignedRect: TRect; const RectPos: TRectPos);
begin
AlignRect(MainRect, AlignedRect, RectPos);
end;
{$endregion TRectHelper}
end.