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.

Source: files/castlecomponentserialize.pas (line 124).

Hierarchy

Show Additional Members:

Overview

Methods

Protected function GetInternalText: String; virtual;
Protected procedure SetInternalText(const Value: String); virtual;
Protected procedure SetName(const Value: TComponentName); override;
Protected procedure TranslateProperties(const TranslatePropertyEvent: TTranslatePropertyEvent); virtual;
Public destructor Destroy; override;
Public procedure CustomSerialization(const SerializationProcess: TSerializationProcess); virtual;
Public function PropertySections(const PropertyName: String): TPropertySections; virtual;
Public procedure SetTransient;
Public procedure AddNonVisualComponent(const NonVisualComponent: TComponent);
Public procedure InsertNonVisualComponent(const Index: Integer; const NonVisualComponent: TComponent);
Public procedure RemoveNonVisualComponent(const NonVisualComponent: TComponent);
Public function NonVisualComponentsIndexOf(const NonVisualComponent: TComponent): Integer;
Public function NonVisualComponentsCount: Integer;
Public function NonVisualComponentsEnumerate: TNonVisualComponentsEnumerator;
Public function ValueIsStreamed: Boolean; virtual;
Public procedure DesignerInfo(const SList: TStrings); virtual;
Public procedure DesignerWarnings(const SList: TStrings); virtual;
Public constructor Create(AOwner: TComponent); overload; override;
Public function PropertySections(const PropertyName: String): TPropertySections; override;
Public constructor Create(const AUrl: String); overload; deprecated 'use Create(AOwner) overload, then set Url to load';
Public constructor Create(const Contents: TStream; const ATranslationGroupName: String); overload; deprecated 'use Create(AOwner) overload, then LoadFromStream';
Public destructor Destroy; override;
Public procedure LoadFromStream(const Contents: TStream; const ATranslationGroupName: String);
Public procedure LoadFromComponent(const Template: TComponent);
Public function ComponentLoad(const InstanceOwner: TComponent; const AssociateReferences: TPersistent = nil): TComponent;

Properties

Public property NonVisualComponents [const Index: Integer]: TComponent read GetNonVisualComponents;
Public property IsLoading: Boolean read FIsLoading;
Published property Url: String read FUrl write SetUrl;

Description

Methods

Protected function GetInternalText: String; virtual;

This item has no description.

Source: base/castleclassutils.pas (line 575).

Protected procedure SetInternalText(const Value: String); virtual;

This item has no description.

Source: base/castleclassutils.pas (line 576).

Protected procedure SetName(const Value: TComponentName); override;

This item has no description.

Source: base/castleclassutils.pas (line 577).

Protected procedure TranslateProperties(const TranslatePropertyEvent: TTranslatePropertyEvent); virtual;

Enumerate all properties that are possible to translate in this component. E.g. in TCastleLabel it will return TCastleLabel.Caption, in TCastleEdit it will return TCastleEdit.Text and TCastleEdit.Placeholder.

Returns only non-empty properties, thus assuming that if current (by convention, English) text is empty, then there is no point in translating it. Moreover descendants may define boolean properties to exclude particular text from translating, e.g. TCastleLabel.CaptionTranslate, TCastleEdit.TextTranslate, TCastleEdit.PlaceholderTranslate.

It is not recursive (it doesn't enumerate children properties). Use global TranslateProperties procedure to call this on a hierarchy of TComponent.

You usually don't want to call this method (it is called by other engine routines). But you may find it useful to override this, if you define new component.

Source: base/castleclassutils.pas (line 598).

Public destructor Destroy; override;

This item has no description.

Source: base/castleclassutils.pas (line 604).

Public procedure CustomSerialization(const SerializationProcess: TSerializationProcess); virtual;

Override this method to call various methods of SerializationProcess, which in turn allows to serialize/deserialize things that are not published. This allows to serialize/deserialize with more freedom, e.g. to serialize/deserialize some private field.

Source: base/castleclassutils.pas (line 610).

Public function PropertySections(const PropertyName: String): TPropertySections; virtual;

Section where to show property in the editor.

Source: base/castleclassutils.pas (line 631).

Public procedure SetTransient;

Ignore this component when serializing parent's TCastleUserInterface.Controls list or TCastleTransform.List, and do not show this component in CGE editor. This simply sets csTransient flag in ComponentStyle.

This is useful for children that are automatically managed by the parent, and should not be modified by user code. For example, TCastleCheckbox is internally composed from TCastleImageControl and TCastleLabel children, but we don't want to serialize or even show these children to user.

Note that if you want to prevent this component from serializing as part of TCastleUserInterface.Controls list or TCastleTransform.List, but you still want it to be visible in CGE editor, then make it a "subcomponent" instead, by SetSubComponent(true).

Note that both csSubComponent and csTransient only disable the component serialization as part of parent's lists enumerated by CustomSerialization (see internal TCastleUserInterface.SerializeChildrenEnumerate , TCastleTransform.SerializeChildrenEnumerate, TCastleTransform.SerializeBehaviorsEnumerate).

If you will make the component published in its own property (which is normal for "subcomponents") then it will be serialized anyway, just as part of it's own property (like TCastleScrollView.ScrollArea). So to really avoid serializing a children component make it csSubComponent and/or csTransient, and do not publish it.

Source: base/castleclassutils.pas (line 663).

Public procedure AddNonVisualComponent(const NonVisualComponent: TComponent);

Add non-visual component to this component. This is used to organize non-visual components in a tree hierarchy, in CGE designs and editor.

See also
NonVisualComponentsCount
Count of components added by AddNonVisualComponent.
NonVisualComponents
Components added by AddNonVisualComponent.
NonVisualComponentsEnumerate
You can enumerate current non-visual components using loop like for C in MyComponent.NonVisualComponentsEnumerate do ....

Source: base/castleclassutils.pas (line 672).

Public procedure InsertNonVisualComponent(const Index: Integer; const NonVisualComponent: TComponent);

Insert non-visual component to this component. This is used to organize non-visual components in a tree hierarchy, in CGE designs and editor.

See also
NonVisualComponentsCount
Count of components added by AddNonVisualComponent.
NonVisualComponents
Components added by AddNonVisualComponent.
NonVisualComponentsEnumerate
You can enumerate current non-visual components using loop like for C in MyComponent.NonVisualComponentsEnumerate do ....

Source: base/castleclassutils.pas (line 681).

Public procedure RemoveNonVisualComponent(const NonVisualComponent: TComponent);

Removes the component previously added by AddNonVisualComponent.

Source: base/castleclassutils.pas (line 684).

Public function NonVisualComponentsIndexOf(const NonVisualComponent: TComponent): Integer;

Index of the previously added non-visual component. Returns -1 if the component was not found.

Source: base/castleclassutils.pas (line 688).

Public function NonVisualComponentsCount: Integer;

Count of components added by AddNonVisualComponent.

See also
AddNonVisualComponent
Add non-visual component to this component.
NonVisualComponents
Components added by AddNonVisualComponent.
NonVisualComponentsEnumerate
You can enumerate current non-visual components using loop like for C in MyComponent.NonVisualComponentsEnumerate do ....

Source: base/castleclassutils.pas (line 695).

Public function NonVisualComponentsEnumerate: TNonVisualComponentsEnumerator;

You can enumerate current non-visual components using loop like for C in MyComponent.NonVisualComponentsEnumerate do .... Do not call this method in other contexts, it is only useful for "for..in" construction.

See also
AddNonVisualComponent
Add non-visual component to this component.

Source: base/castleclassutils.pas (line 705).

Public function ValueIsStreamed: Boolean; virtual;

Whether the current value of this object should be written to the stream. This should be True if anything inside this object should be serialized (which means it has non-default value or "stored" specifier indicates that it should be serialized).

This is used by CastleComponentSerialize, which is used in Castle Game Engine for all serialization.

In simple cases, this just says whether the current value of this object equals to some default value.

The default implementation of this class returns True (so always write).

Descendants that override this to sometimes return False (so no need to write) must be very careful: any addition of a new field requires extending this method, otherwise new field may not be saved sometimes (when all other fields are default). Descentants of such classes must also be aware of it. This check must include everything that is inside this object in JSON, including subcomponents and children objects (as done e.g. by TSerializationProcess.ReadWriteList). In practice, overriding this method is only reasonable for simple classes that will not change much in the future, like TCastleVector3Persistent.

The name of this method is consistent with TPropertyEditor.ValueIsStreamed in LCL.

Source: base/castleclassutils.pas (line 739).

Public procedure DesignerInfo(const SList: TStrings); virtual;

Override to add information that should be visible at design-time. Call SList.Add for each new line of information.

Source: base/castleclassutils.pas (line 743).

Public procedure DesignerWarnings(const SList: TStrings); virtual;

Override to add warnings that should be visible at design-time. Call SList.Add for each new warning.

Source: base/castleclassutils.pas (line 747).

Public constructor Create(AOwner: TComponent); overload; override;

Main constructor. Does not load anything immediately. Set Url or call LoadFromStream to load component.

Source: files/castlecomponentserialize.pas (line 138).

Public function PropertySections(const PropertyName: String): TPropertySections; override;

This item has no description. Showing description inherited from TCastleComponent.PropertySections.

Section where to show property in the editor.

Source: files/castlecomponentserialize.pas (line 140).

Public constructor Create(const AUrl: String); overload; deprecated 'use Create(AOwner) overload, then set Url to load';

Warning: this symbol is deprecated: use Create(AOwner) overload, then set Url to load

Avoid Delphi warnings about constructor hiding. In the long-run, these deprecated constructors should be removed, once everyone got chance to migrate.

Source: files/castlecomponentserialize.pas (line 146).

Public constructor Create(const Contents: TStream; const ATranslationGroupName: String); overload; deprecated 'use Create(AOwner) overload, then LoadFromStream';

Warning: this symbol is deprecated: use Create(AOwner) overload, then LoadFromStream

This item has no description.

Source: files/castlecomponentserialize.pas (line 148).

Public destructor Destroy; override;

This item has no description.

Source: files/castlecomponentserialize.pas (line 152).

Public 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.

Source: files/castlecomponentserialize.pas (line 164).

Public 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.

Source: files/castlecomponentserialize.pas (line 178).

Public 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 Nil, in which case you have to free it manually). Do not confuse this with the owner of this TCastleComponentFactory – which may be the same or different component.

If AssociateReferences is not Nil, then published fields within AssociateReferences that match the names of components within the loaded design will be set.

The InstanceOwner is useful for 2 things:

  1. To manage memory (freeing the InstanceOwner will also free the loaded component).

  2. And to search for named components. All the loaded components are owned by InstanceOwner and you can query for them using InstanceOwner.FindComponent or InstanceOwner.FindRequiredComponent.

    Note that loaded components are renamed, if necessary, to be unique within InstanceOwner — this is important if your InstanceOwner owns other things as well. If you want to reliably search for names, make sure to use different InstanceOwner for each load. Or use AssociateReferences instead.

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 AssociateReferences is useful to connect, at load time, the names of components within the loaded design to published fields inside AssociateReferences. Compared to using InstanceOwner.FindComponent or InstanceOwner.FindRequiredComponent for the same purpose:

  • It is simpler to use, because you don't need to manage the InstanceOwner lifetime.

    This matters in case you created InstanceOwner only for the purpose of loading the given design.

    The AssociateReferences instance can be freed immediately after the load, and it will not affect the loaded components. Or you can keep it existing for a longer time, and reuse for loading all components – it will not mess anything, as loaded components are not renamed to be unique within it.

  • Associates the original component names, regardless of whether they later required renaming to be unique within InstanceOwner.

    This matters in case your InstanceOwner owns many other things.

  • Note also one disadvantage of AssociateReferences: It doesn't check that field type matches. If a field name has a typo, it will be just left Nil, causing "Access Violation" if you try to access it.

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;

Source: files/castlecomponentserialize.pas (line 287).

Properties

Public property NonVisualComponents [const Index: Integer]: TComponent read GetNonVisualComponents;

Components added by AddNonVisualComponent.

Source: base/castleclassutils.pas (line 698).

Public property IsLoading: Boolean read FIsLoading;

Is the component during deserialization now.

Note: We can't use csLoading in ComponentState because in Delphi it is not possible to control it from CastleComponentSerialize.

Source: base/castleclassutils.pas (line 711).

Published 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 "castle-data:/foo/my_design.castle-user-interface" will use translation group "my_design".

Source: files/castlecomponentserialize.pas (line 298).


Generated by PasDoc 0.17.0.snapshot.