| 1 |
{%MainUnit castletransform.pas} |
| 2 |
{ |
| 3 |
Copyright 2017-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 |
//type |
| 20 |
EMaximumTransformDesignDepth = class(Exception); |
| 21 |
|
| 22 |
{ Contents are loaded from an indicated castle-transform file |
| 23 |
(by the @link(Url) property). |
| 24 |
|
| 25 |
The file should be in the format created by Castle Game Engine Editor |
| 26 |
(serialized by CastleComponentSerialize routines) with .castle-transform extension. } |
| 27 |
TCastleTransformDesign = class(TCastleTransform) |
| 28 |
strict private |
| 29 |
class var |
| 30 |
LoadDepth: Cardinal; |
| 31 |
var |
| 32 |
FDesign: TCastleTransform; |
| 33 |
{ We don't use Self as Owner of FDesign, to allow freeing everything |
| 34 |
loaded by TransformLoad by simple FreeAndNil(FDesignOwner). } |
| 35 |
FDesignOwner: TComponent; |
| 36 |
FDesignObserver: TFreeNotificationObserver; |
| 37 |
FUrl: String; |
| 38 |
FUrlMonitoring: TUrlMonitoring; |
| 39 |
FRevertUrl: String; |
| 40 |
procedure SetUrl(const Value: String); |
| 41 |
procedure DesignFreeNotification(const Sender: TFreeNotificationObserver); |
| 42 |
public |
| 43 |
constructor Create(AOwner: TComponent); override; |
| 44 |
destructor Destroy; override; |
| 45 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 46 |
procedure BeforeDestruction; override; |
| 47 |
|
| 48 |
{ Find by name a component in currently loaded design. |
| 49 |
When the name is not found, raises exception (unless Required is @false, |
| 50 |
then it returns @nil). } |
| 51 |
function DesignedComponent(const ComponentName: String; |
| 52 |
const Required: Boolean = true): TComponent; |
| 53 |
|
| 54 |
function FindRequiredComponent(const ComponentName: String): TComponent; deprecated 'use DesignedComponent(ComponentName)'; |
| 55 |
function FindComponent(const ComponentName: String): TComponent; deprecated 'use DesignedComponent(ComponentName, false)'; |
| 56 |
|
| 57 |
{ Load again the design from @link(Url). |
| 58 |
This makes sense to be used when underlying file on disk |
| 59 |
changed, and you want to reload it. } |
| 60 |
procedure ReloadUrl; |
| 61 |
published |
| 62 |
{ Load file with the transform design. |
| 63 |
The file should be in the format created by Castle Game Engine Editor |
| 64 |
(see CastleComponentSerialize unit) with .castle-transform extension. |
| 65 |
Setting this property automatically unloads the previous design, |
| 66 |
and loads a new one (if the value is <> ''). |
| 67 |
|
| 68 |
After loading a design, you can use @link(DesignedComponent) |
| 69 |
to find the components inside. |
| 70 |
So e.g. this works: |
| 71 |
|
| 72 |
@longCode(# |
| 73 |
var |
| 74 |
MyDesign: TCastleTransformDesign; |
| 75 |
MyScene: TCastleScene; |
| 76 |
begin |
| 77 |
MyDesign := TCastleTransformDesign.Create(...); |
| 78 |
MyDesign.Url := 'castle-data:/my_transform.castle-transform'; |
| 79 |
MyScene := MyDesign.DesignedComponent('MyScene') as TCastleScene; |
| 80 |
MyScene.Translation := Vector3(1, 2, 3); |
| 81 |
end; |
| 82 |
#) |
| 83 |
} |
| 84 |
property Url: String read FUrl write SetUrl; |
| 85 |
|
| 86 |
{ URL used to perform editor "Revert" operation. } |
| 87 |
property RevertUrl: String read FRevertUrl write FRevertUrl; |
| 88 |
end; |
| 89 |
|
| 90 |
{$endif read_interface} |
| 91 |
|
| 92 |
{$ifdef read_implementation} |
| 93 |
|
| 94 |
{ TCastleTransformDesign ----------------------------------------------------- } |
| 95 |
|
| 96 |
constructor TCastleTransformDesign.Create(AOwner: TComponent); |
| 97 |
begin |
| 98 |
inherited; |
| 99 |
FDesignObserver := TFreeNotificationObserver.Create(Self); |
| 100 |
FDesignObserver.OnFreeNotification := {$ifdef FPC}@{$endif}DesignFreeNotification; |
| 101 |
FUrlMonitoring.Init({$ifdef FPC}@{$endif} ReloadUrl); |
| 102 |
end; |
| 103 |
|
| 104 |
destructor TCastleTransformDesign.Destroy; |
| 105 |
begin |
| 106 |
inherited; |
| 107 |
end; |
| 108 |
|
| 109 |
procedure TCastleTransformDesign.BeforeDestruction; |
| 110 |
begin |
| 111 |
FUrlMonitoring.Finish(FUrl); |
| 112 |
inherited; |
| 113 |
end; |
| 114 |
|
| 115 |
procedure TCastleTransformDesign.DesignFreeNotification(const Sender: TFreeNotificationObserver); |
| 116 |
begin |
| 117 |
FDesign := nil; |
| 118 |
end; |
| 119 |
|
| 120 |
procedure TCastleTransformDesign.SetUrl(const Value: String); |
| 121 |
const |
| 122 |
MaxLoadDepth = 64; // 128 is too much for macOS/x86_64; although for Linux/x86_64 even 1024 was OK |
| 123 |
begin |
| 124 |
if FUrl <> Value then |
| 125 |
begin |
| 126 |
if FDesign <> nil then |
| 127 |
begin |
| 128 |
FreeAndNil(FDesignOwner); |
| 129 |
Assert(FDesign = nil); // DesignFreeNotification should set FDesign to nil |
| 130 |
end; |
| 131 |
|
| 132 |
{ TODO: Disable auto-reloading of designs for now, fearing it will cause |
| 133 |
more problems than gains: reloading the design invalidates all |
| 134 |
the references to components inside. } |
| 135 |
//FUrlMonitoring.ChangeUrl(FUrl, Value); |
| 136 |
FUrl := Value; |
| 137 |
|
| 138 |
if Value <> '' then |
| 139 |
begin |
| 140 |
try |
| 141 |
{ Check LoadDepth = MaxLoadDepth to prevent infinite loop when referencing your own design. } |
| 142 |
if LoadDepth = MaxLoadDepth then |
| 143 |
raise EMaximumTransformDesignDepth.CreateFmt('Exceeded maximum depth (%d) when loading TCastleTransformDesign within TCastleTransformDesign. It is possible that TCastleTransformDesign references its own design', [ |
| 144 |
MaxLoadDepth |
| 145 |
]); |
| 146 |
Inc(LoadDepth); |
| 147 |
try |
| 148 |
FDesignOwner := TComponent.Create(Self); |
| 149 |
FDesign := TransformLoad(Value, FDesignOwner); |
| 150 |
finally Dec(LoadDepth) end; |
| 151 |
except |
| 152 |
{ If loading design file failed, and we're inside CGE editor, |
| 153 |
merely report a warning. This allows deserializing in CGE editor |
| 154 |
designs with broken URLs. } |
| 155 |
on E: Exception do |
| 156 |
begin |
| 157 |
if CastleDesignMode then |
| 158 |
begin |
| 159 |
WritelnWarning('TCastleTransformDesign', 'Failed to load design "%s": %s', [UriDisplay(Url), ExceptMessage(E)]); |
| 160 |
Exit; |
| 161 |
end else |
| 162 |
raise; |
| 163 |
end; |
| 164 |
end; |
| 165 |
|
| 166 |
{ This prevents this child from |
| 167 |
- being serialized (in case TCastleTransformDesign is placed inside |
| 168 |
a hierachy saved using TransformSave. Only URL should be saved.) |
| 169 |
- or even be visible in the hierachy of castle-editor. |
| 170 |
} |
| 171 |
FDesign.SetTransient; |
| 172 |
FDesignObserver.Observed := FDesign; |
| 173 |
Add(FDesign); |
| 174 |
end; |
| 175 |
end; |
| 176 |
end; |
| 177 |
|
| 178 |
function TCastleTransformDesign.PropertySections( |
| 179 |
const PropertyName: String): TPropertySections; |
| 180 |
begin |
| 181 |
if PropertyName = 'Url' then |
| 182 |
Result := [psBasic] |
| 183 |
else |
| 184 |
Result := inherited PropertySections(PropertyName); |
| 185 |
end; |
| 186 |
|
| 187 |
function TCastleTransformDesign.DesignedComponent(const ComponentName: String; |
| 188 |
const Required: Boolean): TComponent; |
| 189 |
begin |
| 190 |
if FDesignOwner <> nil then |
| 191 |
Result := FDesignOwner.FindComponent(ComponentName) |
| 192 |
else |
| 193 |
Result := nil; |
| 194 |
|
| 195 |
if Required and (Result = nil) then |
| 196 |
raise EComponentNotFound.CreateFmt('Cannot find component named "%s" in design "%s"', [ComponentName, Name]); |
| 197 |
end; |
| 198 |
|
| 199 |
function TCastleTransformDesign.FindRequiredComponent(const ComponentName: String): TComponent; |
| 200 |
begin |
| 201 |
Result := DesignedComponent(ComponentName, true); |
| 202 |
end; |
| 203 |
|
| 204 |
function TCastleTransformDesign.FindComponent(const ComponentName: String): TComponent; |
| 205 |
begin |
| 206 |
Result := DesignedComponent(ComponentName, false); |
| 207 |
end; |
| 208 |
|
| 209 |
procedure TCastleTransformDesign.ReloadUrl; |
| 210 |
var |
| 211 |
SavedUrl: String; |
| 212 |
begin |
| 213 |
SavedUrl := Url; |
| 214 |
Url := ''; |
| 215 |
Url := SavedUrl; |
| 216 |
end; |
| 217 |
|
| 218 |
{$endif read_implementation} |