Unit CastleComponentSerialize
Description
Reading and writing a hierachy of CGE components to/from files. This is used by CGE editor to read/write components, and it can be used at runtime by games to instantiate components designed in the CGE editor.
Most code should use UserInterfaceLoad, UserInterfaceSave, TransformLoad, TransformSave which are defined in other units, that rely on this unit for base ComponentLoad, ComponentSave implementation.
Uses
- SysUtils
- Classes
- FpJson
- FpJsonRtti
- Generics.Collections
- TypInfo
- CastleClassUtils
Overview
Classes, Interfaces, Objects and Records
Name | Description |
---|---|
Class EInvalidComponentFile |
|
Class TRegisteredComponent |
Describes a component registered using RegisterSerializableComponent, enumerated using RegisteredComponents list. |
Class EComponentNotFound |
|
Class TComponentHelper |
|
Class TCastleComponentFactory |
Load a serialized component (from a design file, like .castle-user-interface, .castle-transform, .castle-component) and instantiate it multiple times. |
Functions and Procedures
procedure ComponentSave(const C: TComponent; const Url: String); |
function ComponentLoad(const Url: String; const Owner: TComponent): TComponent; |
function ComponentToString(const C: TComponent): String; |
function StringToComponent(const Contents: String; const Owner: TComponent): TComponent; |
procedure RegisterSerializableComponent(const ComponentClass: TComponentClass; const Caption: array of String); overload; |
procedure RegisterSerializableComponent(const ComponentClass: TComponentClass; const CaptionOnePart: String); overload; |
procedure RegisterSerializableComponent(const C: TRegisteredComponent); overload; |
function RegisteredComponents: TRegisteredComponents; |
function ComponentClone(const C: TComponent; const NewComponentOwner: TComponent): TComponent; |
Types
TRegisteredComponents = specialize TObjectList<TRegisteredComponent>; |
TSerializedComponent = TCastleComponentFactory deprecated 'use TCastleComponentFactory'; |
Description
Functions and Procedures
procedure ComponentSave(const C: TComponent; const Url: String); |
Save / load TComponent (or any descendant) to a .castle-component, .castle-user-interface or .castle-transform file. If you have a TCastleUserInterface or TCastleTransform then it is advised to use instead stronger typed UserInterfaceSave, UserInterfaceLoad, TransformSave, TransformLoad. |
function ComponentLoad(const Url: String; const Owner: TComponent): TComponent; |
This item has no description. |
function ComponentToString(const C: TComponent): String; |
Save / load TComponent (or descendant) to a string. The string contents have the same format as a .castle-component, .castle-user-interface or .castle-transform file. |
function StringToComponent(const Contents: String; const Owner: TComponent): TComponent; |
This item has no description. |
procedure RegisterSerializableComponent(const ComponentClass: TComponentClass; const Caption: array of String); overload; |
Register a component that can be serialized and edited using CGE editor. In case of the overloaded version that gets TRegisteredComponent instance, the TRegisteredComponent instance becomes internally owned in this unit (do not free it yourself). |
procedure RegisterSerializableComponent(const ComponentClass: TComponentClass; const CaptionOnePart: String); overload; |
This item has no description. |
procedure RegisterSerializableComponent(const C: TRegisteredComponent); overload; |
This item has no description. |
function RegisteredComponents: TRegisteredComponents; |
Read-only list of currently registered (using RegisterSerializableComponent) components. |
function ComponentClone(const C: TComponent; const NewComponentOwner: TComponent): TComponent; |
Create a clone ("deep copy") of given component. Works out-of-the-box on any TComponent, just like our serialization / deserialization system, copying the "published" members of the component. Copies the given component, as well as anything that our serialization system considers to be contained within it. Like TCastleTransform children, TCastleTransform behaviors, TCastleUserInterface children, non-visual components in TCastleComponent.NonVisualComponents. The new component hierarchy will have all new components owned by the NewComponentOwner. This is similar to TCastleFactoryComponent.LoadFromComponent followed by the TCastleFactoryComponent.ComponentLoad, but it is easier to use if you only want to create a single clone of the component. If you want to create multiple clones, better use TCastleFactoryComponent, as then you can call TCastleFactoryComponent.LoadFromComponent once, and call TCastleFactoryComponent.ComponentLoad multiple times – this will be more efficient. Like this: // unoptimal NewComponent1 := ComponentClone(Template, NewComponentOwner); NewComponent2 := ComponentClone(Template, NewComponentOwner); NewComponent3 := ComponentClone(Template, NewComponentOwner); // equivalent, but more efficient Factory := TCastleFactoryComponent.Create(nil); try Factory.LoadFromComponent(Template); NewComponent1 := Factory.ComponentLoad(NewComponentOwner); NewComponent2 := Factory.ComponentLoad(NewComponentOwner); NewComponent3 := Factory.ComponentLoad(NewComponentOwner); finally FreeAndNil(Factory) end;
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. |
Types
TRegisteredComponents = specialize TObjectList<TRegisteredComponent>; |
This item has no description. |
TSerializedComponent = TCastleComponentFactory deprecated 'use TCastleComponentFactory'; |
Warning: this symbol is deprecated: use TCastleComponentFactory This item has no description. |
Generated by PasDoc 0.16.0-snapshot.