| 1 |
{%MainUnit castlevectors.pas} |
| 2 |
{ |
| 3 |
Copyright 2019-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ TBorder. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ Configurable border size for @link(TCastleUserInterface.Border). } |
| 23 |
TBorder = class(TCastleComponent) |
| 24 |
strict private |
| 25 |
FTop, FRight, FBottom, FLeft, FAllSides: Single; |
| 26 |
FOnChange: TNotifyEvent; |
| 27 |
procedure SetAllSides(const AValue: Single); |
| 28 |
procedure SetBottom(const AValue: Single); |
| 29 |
procedure SetLeft(const AValue: Single); |
| 30 |
procedure SetRight(const AValue: Single); |
| 31 |
procedure SetTop(const AValue: Single); |
| 32 |
public |
| 33 |
OnChange: TNotifyEvent; |
| 34 |
|
| 35 |
procedure Assign(Source: TPersistent); override; |
| 36 |
function ValueIsStreamed: Boolean; override; |
| 37 |
function ToString: String; override; |
| 38 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 39 |
|
| 40 |
{ Total top border (Top + AllSides). } |
| 41 |
function TotalTop: Single; |
| 42 |
{ Total right border (Right + AllSides). } |
| 43 |
function TotalRight: Single; |
| 44 |
{ Total bottom border (Bottom + AllSides). } |
| 45 |
function TotalBottom: Single; |
| 46 |
{ Total left border (Left + AllSides). } |
| 47 |
function TotalLeft: Single; |
| 48 |
|
| 49 |
{ Total horizontal border (TotalLeft + TotalRight). } |
| 50 |
function TotalWidth: Single; |
| 51 |
{ Total vertical border (TotalTop + TotalBottom). } |
| 52 |
function TotalHeight: Single; |
| 53 |
|
| 54 |
{ Anything not zero? } |
| 55 |
function Exists: Boolean; |
| 56 |
published |
| 57 |
property Top: Single read FTop write SetTop {$ifdef FPC} default 0 {$endif}; |
| 58 |
property Right: Single read FRight write SetRight {$ifdef FPC} default 0 {$endif}; |
| 59 |
property Bottom: Single read FBottom write SetBottom {$ifdef FPC} default 0 {$endif}; |
| 60 |
property Left: Single read FLeft write SetLeft {$ifdef FPC} default 0 {$endif}; |
| 61 |
property AllSides: Single read FAllSides write SetAllSides {$ifdef FPC} default 0 {$endif}; |
| 62 |
end; |
| 63 |
|
| 64 |
{$endif read_interface} |
| 65 |
|
| 66 |
{$ifdef read_implementation} |
| 67 |
|
| 68 |
{ TBorder -------------------------------------------------------------------- } |
| 69 |
|
| 70 |
procedure TBorder.SetAllSides(const AValue: Single); |
| 71 |
begin |
| 72 |
if FAllSides = AValue then Exit; |
| 73 |
FAllSides := AValue; |
| 74 |
if Assigned(FOnChange) then FOnChange(Self); |
| 75 |
end; |
| 76 |
|
| 77 |
procedure TBorder.SetBottom(const AValue: Single); |
| 78 |
begin |
| 79 |
if FBottom = AValue then Exit; |
| 80 |
FBottom := AValue; |
| 81 |
if Assigned(FOnChange) then FOnChange(Self); |
| 82 |
end; |
| 83 |
|
| 84 |
procedure TBorder.SetLeft(const AValue: Single); |
| 85 |
begin |
| 86 |
if FLeft = AValue then Exit; |
| 87 |
FLeft := AValue; |
| 88 |
if Assigned(FOnChange) then FOnChange(Self); |
| 89 |
end; |
| 90 |
|
| 91 |
procedure TBorder.SetRight(const AValue: Single); |
| 92 |
begin |
| 93 |
if FRight = AValue then Exit; |
| 94 |
FRight := AValue; |
| 95 |
if Assigned(FOnChange) then FOnChange(Self); |
| 96 |
end; |
| 97 |
|
| 98 |
procedure TBorder.SetTop(const AValue: Single); |
| 99 |
begin |
| 100 |
if FTop = AValue then Exit; |
| 101 |
FTop := AValue; |
| 102 |
if Assigned(FOnChange) then FOnChange(Self); |
| 103 |
end; |
| 104 |
|
| 105 |
function TBorder.TotalTop: Single; |
| 106 |
begin |
| 107 |
Result := FAllSides + FTop; |
| 108 |
end; |
| 109 |
|
| 110 |
function TBorder.TotalRight: Single; |
| 111 |
begin |
| 112 |
Result := FAllSides + FRight; |
| 113 |
end; |
| 114 |
|
| 115 |
function TBorder.TotalBottom: Single; |
| 116 |
begin |
| 117 |
Result := FAllSides + FBottom; |
| 118 |
end; |
| 119 |
|
| 120 |
function TBorder.TotalLeft: Single; |
| 121 |
begin |
| 122 |
Result := FAllSides + FLeft; |
| 123 |
end; |
| 124 |
|
| 125 |
function TBorder.TotalWidth: Single; |
| 126 |
begin |
| 127 |
Result := 2 * FAllSides + FRight + FLeft; |
| 128 |
end; |
| 129 |
|
| 130 |
function TBorder.TotalHeight: Single; |
| 131 |
begin |
| 132 |
Result := 2 * FAllSides + FTop + FBottom; |
| 133 |
end; |
| 134 |
|
| 135 |
function TBorder.Exists: Boolean; |
| 136 |
begin |
| 137 |
Result := (FAllSides <> 0) or |
| 138 |
(FTop <> 0) or |
| 139 |
(FRight <> 0) or |
| 140 |
(FBottom <> 0) or |
| 141 |
(FLeft <> 0); |
| 142 |
end; |
| 143 |
|
| 144 |
function TBorder.ValueIsStreamed: Boolean; |
| 145 |
begin |
| 146 |
{ For now, TBorder is by default always equal zero. |
| 147 |
So for now, this method is equal to "Exists". |
| 148 |
(This may change some day if we'll use TBorder as subcomponent |
| 149 |
and will want to customize it's default value in parent.) } |
| 150 |
Result := Exists; |
| 151 |
end; |
| 152 |
|
| 153 |
procedure TBorder.Assign(Source: TPersistent); |
| 154 |
var |
| 155 |
SourceBorder: TBorder; |
| 156 |
begin |
| 157 |
if Source is TBorder then |
| 158 |
begin |
| 159 |
SourceBorder := TBorder(Source); |
| 160 |
Top := SourceBorder.Top; |
| 161 |
Right := SourceBorder.Right; |
| 162 |
Bottom := SourceBorder.Bottom; |
| 163 |
Left := SourceBorder.Left; |
| 164 |
AllSides := SourceBorder.AllSides; |
| 165 |
end else |
| 166 |
{ Since TBorder is a direct TPersistent descendant, |
| 167 |
it calls inherited ONLY when it cannot handle Source class. } |
| 168 |
inherited Assign(Source); |
| 169 |
end; |
| 170 |
|
| 171 |
function TBorder.ToString: String; |
| 172 |
var |
| 173 |
V: TVector4; |
| 174 |
begin |
| 175 |
V := Vector4( |
| 176 |
FAllSides + FTop, |
| 177 |
FAllSides + FRight, |
| 178 |
FAllSides + FBottom, |
| 179 |
FAllSides + FLeft |
| 180 |
); |
| 181 |
Result := V.ToString; |
| 182 |
end; |
| 183 |
|
| 184 |
function TBorder.PropertySections(const PropertyName: String): TPropertySections; |
| 185 |
begin |
| 186 |
if ArrayContainsString(PropertyName, [ |
| 187 |
'Left', 'Bottom', 'Top', 'Right', 'AllSides' |
| 188 |
]) then |
| 189 |
Result := [psBasic, psLayout] |
| 190 |
else |
| 191 |
Result := inherited PropertySections(PropertyName); |
| 192 |
end; |
| 193 |
|
| 194 |
{$endif read_implementation} |