-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathd2dGUIUtils.pas
More file actions
98 lines (88 loc) · 3.74 KB
/
d2dGUIUtils.pas
File metadata and controls
98 lines (88 loc) · 3.74 KB
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
//---------------------------------------------------------------------------
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//---------------------------------------------------------------------------
unit d2dGUIUtils;
interface
uses
d2dTypes,
d2dGUITypes;
// aMasterRect - is master rect which target rect is relative to
// aDim - dimensions (width and height) of the target rect
// returns the point of top left corner of the target rect
function D2DAlignRect(const aMasterRect: Td2dRect; const aDim: Td2dPoint; aAlign: Td2dAlign): Td2dPoint;
function D2DAlignToStr(const aAlign: Td2dAlign): string;
function D2DStrToAlign(aStr: string): Td2dAlign;
implementation
uses
d2dCore, SysUtils;
const
c_AlignString : array [Td2dAlign] of string = ('LT','LB','LC','RT','RB','RC','TL','TR','TC','BL','BR','BC');
function D2DAlignRect(const aMasterRect: Td2dRect; const aDim: Td2dPoint; aAlign: Td2dAlign): Td2dPoint;
var
l_MasterHalf: Td2dPoint;
begin
l_MasterHalf.X := (aMasterRect.Right - aMasterRect.Left)/2;
l_MasterHalf.Y := (aMasterRect.Bottom - aMasterRect.Top)/2;
// initial relocation
case aAlign of
alLeftTop, alLeftBottom, alLeftCenter : Result.X := aMasterRect.Left - aDim.X - 1;
alRightTop, alRightBottom, alRightCenter : Result.X := aMasterRect.Right + 1;
alTopLeft, alTopRight, alTopCenter : Result.Y := aMasterRect.Top - aDim.Y - 1;
alBottomLeft, alBottomRight, alBottomCenter : Result.Y := aMasterRect.Bottom + 1;
end;
case aAlign of
alLeftTop, alRightTop : Result.Y := aMasterRect.Top;
alLeftBottom, alRightBottom : Result.Y := aMasterRect.Bottom - aDim.Y;
alLeftCenter, alRightCenter : Result.Y := aMasterRect.Top + l_MasterHalf.Y - (aDim.Y/2);
alTopLeft, alBottomLeft : Result.X := aMasterRect.Left;
alTopRight, alBottomRight : Result.X := aMasterRect.Right - aDim.X;
alTopCenter, alBottomCenter : Result.X := aMasterRect.Left + l_MasterHalf.X - (aDim.X/2);
end;
// now if we out of the screen because of MAIN relocation functor then we should flip result
// i.e. if alRightTop is out of the right edge of screen we should make it alLeftTop
if (aAlign in [alLeftTop, alLeftBottom, alLeftCenter]) and (Result.X < 0) then
Result.X := aMasterRect.Right + 1;
if (aAlign in [alRightTop, alRightBottom, alRightCenter]) and (Result.X + aDim.X > gD2DE.ScreenWidth) then
Result.X := aMasterRect.Left - aDim.X - 1;
if (aAlign in [alTopLeft, alTopRight, alTopCenter]) and (Result.Y < 0) then
Result.Y := aMasterRect.Bottom + 1;
if (aAlign in [alBottomLeft, alBottomRight, alBottomCenter]) and (Result.Y + aDim.Y > gD2DE.ScreenHeight) then
Result.Y := aMasterRect.Top - aDim.Y - 1;
// now is the final aligment by edges of the screen
if Result.X + aDim.X > gD2DE.ScreenWidth then
Result.X := gD2DE.ScreenWidth - aDim.X;
if Result.X < 0 then
Result.X := 0;
if Result.Y + aDim.Y > gD2DE.ScreenHeight then
Result.Y := gD2DE.ScreenHeight - aDim.Y;
if Result.Y < 0 then
Result.Y := 0;
Result.X := Round(Result.X);
Result.Y := Round(Result.Y);
end;
function D2DAlignToStr(const aAlign: Td2dAlign): string;
begin
Result := c_AlignString[aAlign];
end;
function D2DStrToAlign(aStr: string): Td2dAlign;
var
A: Td2dAlign;
begin
Result := alTopLeft;
aStr := UpperCase(aStr);
for A := alLeftTop to alBottomCenter do
if aStr = c_AlignString[A] then
begin
Result := A;
Break;
end;
end;
end.