| 1 |
{%MainUnit castlecontrols.pas} |
| 2 |
{ |
| 3 |
Copyright 2018-2024 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 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
EMaximumDesignDepth = class(Exception); |
| 20 |
|
| 21 |
{ Contents of this user-interface control are loaded from an indicated file |
| 22 |
(by the @link(Url) property). |
| 23 |
|
| 24 |
The file should be in the format created by Castle Game Engine Editor |
| 25 |
(serialized by CastleComponentSerialize routines) with .castle-user-interface extension. } |
| 26 |
TCastleDesign = class(TCastleUserInterface) |
| 27 |
strict private |
| 28 |
class var |
| 29 |
LoadDepth: Cardinal; |
| 30 |
var |
| 31 |
FDesign: TCastleUserInterface; |
| 32 |
{ We don't use Self as Owner of FDesign, to allow freeing everything |
| 33 |
loaded by UserInterfaceLoad by simple FreeAndNil(FDesignOwner). } |
| 34 |
FDesignOwner: TComponent; |
| 35 |
FDesignObserver: TFreeNotificationObserver; |
| 36 |
FUrl: String; |
| 37 |
FUrlMonitoring: TUrlMonitoring; |
| 38 |
FRevertUrl: String; |
| 39 |
FStretchWhenNotLoaded: Boolean; |
| 40 |
procedure SetUrl(const Value: String); |
| 41 |
function GetStretch: Boolean; |
| 42 |
procedure SetStretch(const Value: Boolean); |
| 43 |
procedure DesignFreeNotification(const Sender: TFreeNotificationObserver); |
| 44 |
public |
| 45 |
constructor Create(AOwner: TComponent); override; |
| 46 |
destructor Destroy; override; |
| 47 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 48 |
procedure BeforeDestruction; override; |
| 49 |
|
| 50 |
{ Find by name a component in currently loaded design. |
| 51 |
When the name is not found, raises exception (unless Required is @false, |
| 52 |
then it returns @nil). } |
| 53 |
function DesignedComponent(const ComponentName: String; |
| 54 |
const Required: Boolean = true): TComponent; |
| 55 |
|
| 56 |
function FindRequiredComponent(const ComponentName: String): TComponent; deprecated 'use DesignedComponent(ComponentName)'; |
| 57 |
function FindComponent(const ComponentName: String): TComponent; deprecated 'use DesignedComponent(ComponentName, false)'; |
| 58 |
|
| 59 |
{ Loaded design, only for internal usage. @exclude } |
| 60 |
property InternalDesign: TCastleUserInterface read FDesign; |
| 61 |
|
| 62 |
{ Load again the design from @link(Url). |
| 63 |
This makes sense to be used when underlying file on disk |
| 64 |
changed, and you want to reload it. } |
| 65 |
procedure ReloadUrl; |
| 66 |
published |
| 67 |
{ Load file with the user interface design. |
| 68 |
The file should be in the format created by Castle Game Engine Editor |
| 69 |
(see CastleComponentSerialize unit) with .castle-user-interface extension. |
| 70 |
Setting this property automatically unloads the previous design, |
| 71 |
and loads a new one (if the value is <> ''). |
| 72 |
|
| 73 |
After loading a design, you can use @link(DesignedComponent) |
| 74 |
to find the components inside. |
| 75 |
So e.g. this works: |
| 76 |
|
| 77 |
@longCode(# |
| 78 |
var |
| 79 |
MyDesign: TCastleDesign; |
| 80 |
MyButton: TCastleButton; |
| 81 |
begin |
| 82 |
MyDesign := TCastleDesign.Create(...); |
| 83 |
MyDesign.Url := 'castle-data:/my_user_interface.castle-user-interface'; |
| 84 |
MyButton := MyDesign.DesignedComponent('MyButton') as TCastleButton; |
| 85 |
MyButton.OnClick := ...; |
| 86 |
end; |
| 87 |
#) |
| 88 |
} |
| 89 |
property Url: String read FUrl write SetUrl; |
| 90 |
|
| 91 |
{ Whether the loaded component (from @link(Url)) fills the rectangle |
| 92 |
of this TCastleDesign instance. |
| 93 |
|
| 94 |
By default this is @true, which makes sense if the loaded component |
| 95 |
can be freely stretched. In this case, the loaded component will have |
| 96 |
@link(FullSize) set to @true, so it fills this TCastleDesign instance. |
| 97 |
And you can control the size of everything by controlling the size |
| 98 |
of this TCastleDesign instance. |
| 99 |
|
| 100 |
You can set this to @false if the loaded component should not |
| 101 |
be arbitrarily stretched. E.g. maybe it is a TCastleImageControl |
| 102 |
with @link(TCastleImageControl.Stretch) = @true. |
| 103 |
Maybe it is TCastleButton with @link(TCastleButton.AutoSize) = @true. |
| 104 |
While every component can be forced to be stretched (by setting FullSize |
| 105 |
to @true), it may look bad in some circumstances. |
| 106 |
In such case, you should set this property to @false. |
| 107 |
You can also set @link(AutoSizeToChildren) to @true, to adjust the size of this |
| 108 |
TCastleDesign instance to the child. |
| 109 |
} |
| 110 |
property Stretch: Boolean read GetStretch write SetStretch default true; |
| 111 |
|
| 112 |
{ URL used to perform editor "Revert" operation. } |
| 113 |
property RevertUrl: String read FRevertUrl write FRevertUrl; |
| 114 |
end; |
| 115 |
|
| 116 |
{$endif read_interface} |
| 117 |
|
| 118 |
{$ifdef read_implementation} |
| 119 |
|
| 120 |
{ TCastleDesign -------------------------------------------------------------- } |
| 121 |
|
| 122 |
constructor TCastleDesign.Create(AOwner: TComponent); |
| 123 |
begin |
| 124 |
inherited; |
| 125 |
FDesignObserver := TFreeNotificationObserver.Create(Self); |
| 126 |
FDesignObserver.OnFreeNotification := {$ifdef FPC}@{$endif}DesignFreeNotification; |
| 127 |
FStretchWhenNotLoaded := true; |
| 128 |
FUrlMonitoring.Init({$ifdef FPC}@{$endif} ReloadUrl); |
| 129 |
end; |
| 130 |
|
| 131 |
destructor TCastleDesign.Destroy; |
| 132 |
begin |
| 133 |
inherited; |
| 134 |
end; |
| 135 |
|
| 136 |
procedure TCastleDesign.BeforeDestruction; |
| 137 |
begin |
| 138 |
FUrlMonitoring.Finish(FUrl); |
| 139 |
inherited; |
| 140 |
end; |
| 141 |
|
| 142 |
procedure TCastleDesign.DesignFreeNotification(const Sender: TFreeNotificationObserver); |
| 143 |
begin |
| 144 |
FDesign := nil; |
| 145 |
end; |
| 146 |
|
| 147 |
procedure TCastleDesign.SetUrl(const Value: String); |
| 148 |
const |
| 149 |
MaxLoadDepth = 32; // 64 is too much for macOS/x86_64; although for Linux/x86_64 even 1024 was OK |
| 150 |
begin |
| 151 |
if FUrl <> Value then |
| 152 |
begin |
| 153 |
if FDesign <> nil then |
| 154 |
begin |
| 155 |
FStretchWhenNotLoaded := FDesign.FullSize; |
| 156 |
FreeAndNil(FDesignOwner); |
| 157 |
Assert(FDesign = nil); // DesignFreeNotification should set FDesign to nil |
| 158 |
end; |
| 159 |
|
| 160 |
{ TODO: Disable auto-reloading of designs for now, fearing it will cause |
| 161 |
more problems than gains: reloading the design invalidates all |
| 162 |
the references to components inside. } |
| 163 |
//FUrlMonitoring.ChangeUrl(FUrl, Value); |
| 164 |
FUrl := Value; |
| 165 |
|
| 166 |
if Value <> '' then |
| 167 |
begin |
| 168 |
try |
| 169 |
{ Check LoadDepth = MaxLoadDepth to prevent infinite loop when referencing your own design. } |
| 170 |
if LoadDepth = MaxLoadDepth then |
| 171 |
raise EMaximumDesignDepth.CreateFmt('Exceeded maximum depth (%d) when loading TCastleDesign within TCastleDesign. It is possible that TCastleDesign references its own design', [ |
| 172 |
MaxLoadDepth |
| 173 |
]); |
| 174 |
Inc(LoadDepth); |
| 175 |
try |
| 176 |
FDesignOwner := TComponent.Create(Self); |
| 177 |
FDesign := UserInterfaceLoad(Value, FDesignOwner); |
| 178 |
finally Dec(LoadDepth) end; |
| 179 |
except |
| 180 |
{ If loading design file failed, and we're inside CGE editor, |
| 181 |
merely report a warning. This allows deserializing in CGE editor |
| 182 |
designs with broken URLs. } |
| 183 |
on E: Exception do |
| 184 |
begin |
| 185 |
if CastleDesignMode then |
| 186 |
begin |
| 187 |
WritelnWarning('TCastleDesign', 'Failed to load design "%s": %s', [UriDisplay(Url), ExceptMessage(E)]); |
| 188 |
Exit; |
| 189 |
end else |
| 190 |
raise; |
| 191 |
end; |
| 192 |
end; |
| 193 |
|
| 194 |
{ This prevents this child from |
| 195 |
- being serialized (in case TCastleDesign is placed inside |
| 196 |
a hierachy saved using UserInterfaceSave. Only URL should be saved.) |
| 197 |
- or even be visible in the hierachy of castle-editor. |
| 198 |
} |
| 199 |
FDesign.SetTransient; |
| 200 |
FDesign.FullSize := FStretchWhenNotLoaded; |
| 201 |
FDesignObserver.Observed := FDesign; |
| 202 |
InsertFront(FDesign); |
| 203 |
end; |
| 204 |
end; |
| 205 |
end; |
| 206 |
|
| 207 |
function TCastleDesign.GetStretch: Boolean; |
| 208 |
begin |
| 209 |
{ Keep this property synchronized with FDesign.FullSize, when FDesign exists. |
| 210 |
This way when user changes FDesign.FullSize directly, |
| 211 |
then the TCastleDesign.Stretch property also changes automatically. } |
| 212 |
if FDesign <> nil then |
| 213 |
Result := FDesign.FullSize |
| 214 |
else |
| 215 |
Result := FStretchWhenNotLoaded; |
| 216 |
end; |
| 217 |
|
| 218 |
procedure TCastleDesign.SetStretch(const Value: Boolean); |
| 219 |
begin |
| 220 |
if FDesign <> nil then |
| 221 |
FDesign.FullSize := Value |
| 222 |
else |
| 223 |
FStretchWhenNotLoaded := Value; |
| 224 |
end; |
| 225 |
|
| 226 |
function TCastleDesign.PropertySections( |
| 227 |
const PropertyName: String): TPropertySections; |
| 228 |
begin |
| 229 |
if (PropertyName = 'Url') or |
| 230 |
(PropertyName = 'Stretch') then |
| 231 |
Result := [psBasic] |
| 232 |
else |
| 233 |
Result := inherited PropertySections(PropertyName); |
| 234 |
end; |
| 235 |
|
| 236 |
function TCastleDesign.DesignedComponent(const ComponentName: String; |
| 237 |
const Required: Boolean): TComponent; |
| 238 |
begin |
| 239 |
if FDesignOwner <> nil then |
| 240 |
Result := FDesignOwner.FindComponent(ComponentName) |
| 241 |
else |
| 242 |
Result := nil; |
| 243 |
|
| 244 |
if Required and (Result = nil) then |
| 245 |
raise EComponentNotFound.CreateFmt('Cannot find component named "%s" in design "%s"', [ComponentName, Name]); |
| 246 |
end; |
| 247 |
|
| 248 |
function TCastleDesign.FindRequiredComponent(const ComponentName: String): TComponent; |
| 249 |
begin |
| 250 |
Result := DesignedComponent(ComponentName, true); |
| 251 |
end; |
| 252 |
|
| 253 |
function TCastleDesign.FindComponent(const ComponentName: String): TComponent; |
| 254 |
begin |
| 255 |
Result := DesignedComponent(ComponentName, false); |
| 256 |
end; |
| 257 |
|
| 258 |
procedure TCastleDesign.ReloadUrl; |
| 259 |
var |
| 260 |
SavedUrl: String; |
| 261 |
begin |
| 262 |
SavedUrl := Url; |
| 263 |
Url := ''; |
| 264 |
Url := SavedUrl; |
| 265 |
end; |
| 266 |
|
| 267 |
{$endif read_implementation} |