Class TCastleComponentFactory
Unit
Declaration
type TCastleComponentFactory = class(TCastleComponent)
Description
Load a serialized component (from a design file, like .castle-user-interface, .castle-transform, .castle-component) and instantiate it multiple times.
Set Url or call LoadFromStream to load the design.
Then instantiate it (multiple times if needed) using ComponentLoad.
Hierarchy
- TObject
- TPersistent
- TComponent
- TCastleComponent
- TCastleComponentFactory
Overview
Methods
constructor Create(AOwner: TComponent); overload; override; |
|
constructor Create(const AUrl: String); overload; deprecated 'use Create(AOwner) overload, then set Url to load'; |
|
constructor Create(const Contents: TStream; const ATranslationGroupName: String); overload; deprecated 'use Create(AOwner) overload, then LoadFromStream'; |
|
destructor Destroy; override; |
|
procedure LoadFromStream(const Contents: TStream; const ATranslationGroupName: String); |
|
procedure LoadFromComponent(const Template: TComponent); |
|
function ComponentLoad(const InstanceOwner: TComponent; const AssociateReferences: TPersistent = nil): TComponent; |
Properties
property Url: String read FUrl write SetUrl; |
Description
Methods
constructor Create(AOwner: TComponent); overload; override; |
|
Main constructor. Does not load anything immediately. Set Url or call LoadFromStream to load component. |
destructor Destroy; override; |
|
This item has no description. |
procedure LoadFromStream(const Contents: TStream; const ATranslationGroupName: String); |
|
Load design from given TStream. If you set up localization (see https://castle-engine.io/manual_text.php#section_localization_gettext ), then upon loading, the design will be automatically localized using the translation group specified. Just leave ATranslationGroupName = '' to not localize. This Url value is unchanged by this. In most common case, you don't really care about the Url value of this component, if you load the contents from TStream. |
procedure LoadFromComponent(const Template: TComponent); |
|
Load given component as a template. Effectively every TCastleComponentFactory.ComponentLoad will create a deep clone of Template. This is a way to clone ("deep copy") any TComponent using serialization + deserialization underneath. Template is converted to the JSON classes (though not JSON string) and then deserialized back to a new component. It is not the most efficient way to clone (since the intermeiate JSON representation is created; though note that we don't save / parse JSON as a string). But it is absolutely general and works out-of-the-box on any TComponent, just like our serialization/deserialization. |
function ComponentLoad(const InstanceOwner: TComponent; const AssociateReferences: TPersistent = nil): TComponent; |
|
Instantiate component. Using this is similar to using global CastleComponentSerialize.ComponentLoad, but it is much faster if you want to instantiate the same file many times. Newly created component will be owned by InstanceOwner (it can be If AssociateReferences is not The
For example, this example creates an owner dedicated for refering to names in the given instance of a Rock: procedure SpawnRock; var RockOwner: TComponent; Rock: TCastleTransform; RockRigidBody: TCastleRigidBody; SceneToRotate: TCastleTransform; begin RockOwner := TComponent.Create(Self); Rock := RockFactory.ComponentLoad(RockOwner) as TCastleTransform; RockRigidBody := RockOwner.FindRequiredComponent('RockRigidBody') as TCastleRigidBody; SceneToRotate := RockOwner.FindRequiredComponent('SceneToRotate') as TCastleTransform; // here use RockRigidBody, SceneToRotate end;
The
For example: type TRockDesign = class(TPersistent) published SceneToRotate: TCastleTransform; RockRigidBody: TCastleRigidBody; end; procedure SpawnRock; var Rock: TCastleTransform; RockDesign: TRockDesign; begin RockDesign := TRockDesign.Create; try Rock := RockFactory.ComponentLoad(Self, RockDesign) as TCastleTransform; // here use RockDesign.RockRigidBody, RockDesign.SceneToRotate finally FreeAndNil(RockDesign) end; end; |
Properties
property Url: String read FUrl write SetUrl; |
|
Load design from given URL. Set to '' to clear the loaded component. If you set up localization (see https://castle-engine.io/manual_text.php#section_localization_gettext ), then upon loading, the design will be automatically localized using the translation group derived from URL base name. E.g. loading |
Generated by PasDoc 0.16.0-snapshot.