Class TCastleImageControl

Unit

Declaration

type TCastleImageControl = class(TCastleUserInterface)

Description

Image control. Size is automatically adjusted to the image size, if Stretch is False (default). You should set TCastleImageControl.Left, TCastleImageControl.Bottom properties, and load your image by setting TCastleImageControl.URL property or straight TCastleImageControl.Image.

We automatically use alpha test or alpha blending based on loaded image alpha channel (see TGLImageCore.Alpha). You can influence this by AlphaChannel property.

Hierarchy

Overview

Methods

Protected procedure PreferredSize(var PreferredWidth, PreferredHeight: Single); override;
Public constructor Create(AOwner: TComponent); override;
Public destructor Destroy; override;
Public procedure Render; override;
Public procedure GLContextOpen; override;
Public procedure EditorAllowResize( out ResizeWidth, ResizeHeight: Boolean; out Reason: String); override;
Public procedure ImageChanged;

Properties

Public property Image: TEncodedImage read FImage write SetImage;
Public property OwnsImage: boolean read FOwnsImage write FOwnsImage default true;
Public property DrawableImage: TGLImage read FDrawableImage write SetDrawableImage;
Public property OwnsDrawableImage: boolean read FOwnsDrawableImage write FOwnsDrawableImage default true;
Public property Color: TCastleColor read GetColor write SetColor;
Public property Corners: TVector4Integer read GetCorners write SetCorners; deprecated 'use ProtectedSides';
Public property CenterX: Single read GetCenterX write SetCenterX default 0.5;
Public property CenterY: Single read GetCenterY write SetCenterY default 0.5;
Public property Rotation: Single read GetRotation write SetRotation default 0;
Public property Clip: boolean read GetClip write SetClip;
Public property ClipLine: TVector3 read GetClipLine write SetClipLine;
Public property CustomShader: TGLSLProgram read GetCustomShader write SetCustomShader;
Public property Blending: boolean read GetBlending write SetBlending stored false; deprecated 'use AlphaChannel';
Public property Proportional: boolean read GetProportional write SetProportional stored false default false; deprecated 'use ProportionalScaling';
Public property FileName: string read FURL write SetURL; deprecated;
Published property URL: string read FURL write SetURL;
Published property AlphaChannel: TAutoAlphaChannel read GetAlphaChannel write SetAlphaChannel default acAuto;
Published property SmoothScaling: boolean read GetSmoothScaling write SetSmoothScaling default true;
Published property Stretch: boolean read FStretch write SetStretch default false;
Published property ProportionalScaling: TProportionalScaling read FProportionalScaling write SetProportionalScaling default psNone;
Published property Cache: boolean read FCache write FCache default false;
Published property FlipHorizontal: Boolean read FFlipHorizontal write SetFlipHorizontal default false;
Published property FlipVertical: Boolean read FFlipVertical write SetFlipVertical default false;
Published property ProtectedSides: TBorder read FProtectedSides;
Published property DetectScaleFromUrl: Boolean read FDetectScaleFromUrl write SetDetectScaleFromUrl default false;
Published property ColorPersistent: TCastleColorPersistent read FColorPersistent;

Description

Methods

Protected procedure PreferredSize(var PreferredWidth, PreferredHeight: Single); override;
 
Public constructor Create(AOwner: TComponent); override;
 
Public destructor Destroy; override;
 
Public procedure Render; override;
 
Public procedure GLContextOpen; override;
 
Public procedure EditorAllowResize( out ResizeWidth, ResizeHeight: Boolean; out Reason: String); override;
 
Public procedure ImageChanged;
 

Properties

Public property Image: TEncodedImage read FImage write SetImage;

Image displayed, or Nil if none. You can set it by setting URL, or you can set this property directly if you loaded/created the image contents yourself.

Note that by default the TEncodedImage instance assigned here is owned by this component (see OwnsImage). So if you set this property to your custom TEncodedImage instance you should leave memory management of this instance to this component. You can either create a copy by TEncodedImage.MakeCopy if you want to give here only a copy, or you can change OwnsImage to False.

It is allowed to modify the contents or even size of this image. Just make sure to call ImageChanged after the modifications are done to update the actual rendered image. The control size will be updated immediately (taking into account current Stretch and related properties values).

Public property OwnsImage: boolean read FOwnsImage write FOwnsImage default true;

Whether the memory management of assigned Image is automatic. See Image documentation for details.

Public property DrawableImage: TGLImage read FDrawableImage write SetDrawableImage;

Image used for drawing. In most circumstances, you should treat it as an internal property, automatically managed by this class. You do not need to explicitly use it in any way. Usually you should change the Image instead, if you want to change the image at runtime.

But in special cases it may make sense to change the DrawableImage explicitly. E.g. you can change it's contents using fast image-to-image drawing using TGLImage.DrawFrom. Or you can set a new instance of TGLImage here. Be sure to adjust OwnsDrawableImage as needed.

If you set this property manually, to your own TGLImage instance:

  • Make sure that TGLImage.OwnsImage is always False. We depend on it, otherwise it would conflict with our own memory management controlled by OwnsImage and Cache.

  • The TGLImage.Image should be equal to our current Image (which can be Nil). Although this is not strictly necessary now.

  • Note that various properties of this class are actually references to analogous properties of TGLImage or TGLImageCore. E.g. getting/setting our Rotation just gets/sets TGLImageCore.Rotation. So if you directly change the DrawableImage, you're also changing these properties.

Note that when OpenGL(ES) context is lost and recreated (which can happen at any moment on mobile devices), the contents of this are reinitialized from Image. This functionality is actually provided by the TGLImage itself.

Public property OwnsDrawableImage: boolean read FOwnsDrawableImage write FOwnsDrawableImage default true;

Whether we should automatically free the DrawableImage instance. Note that this is restored to True when we need to recreate the TGLImage internally, e.g. when Image instance changed (when even size changed, we have to recreate TGLImage).

Public property Color: TCastleColor read GetColor write SetColor;

Color tint of the image. This simply multiplies the image RGBA components, just like TGLImageCore.Color. By default this is opaque white, which means that image colors are unchanged.

Public property Corners: TVector4Integer read GetCorners write SetCorners; deprecated 'use ProtectedSides';

Warning: this symbol is deprecated: use ProtectedSides

Corners and edges of the image that are not stretched even when Stretch is used. See TGLImageCore.Draw3x3 for the details how such drawing works.

Public property CenterX: Single read GetCenterX write SetCenterX default 0.5;

X coordinate of the center of rotation. Value from 0 to 1. Default value 0.5.

Public property CenterY: Single read GetCenterY write SetCenterY default 0.5;

Y coordinate of the center of rotation. Value from 0 to 1. Default value 0.5.

Public property Rotation: Single read GetRotation write SetRotation default 0;

Rotation in radians. Default value 0.

Public property Clip: boolean read GetClip write SetClip;

Clip the image by an arbitrary 2D line defined in ClipLine.

Public property ClipLine: TVector3 read GetClipLine write SetClipLine;

If Clip, this is the line equation used to determine whether we clip the given pixel. Given a line (A, B, C) and pixel (x, y), the pixel is clipped (rejected) if A * x + B * y + C < 0.

The equation is calculated in the final scaled screen coordinates (not in the local, unscaled pixels). Adjust it to match the RenderRect if necessary, to make it work with UI scaling.

Public property CustomShader: TGLSLProgram read GetCustomShader write SetCustomShader;

Custom GLSL shading code. See TGLImage.CustomShader for explanation.

Public property Blending: boolean read GetBlending write SetBlending stored false; deprecated 'use AlphaChannel';

Warning: this symbol is deprecated: use AlphaChannel

Deprecated, use more flexible AlphaChannel instead.

Public property Proportional: boolean read GetProportional write SetProportional stored false default false; deprecated 'use ProportionalScaling';

Warning: this symbol is deprecated: use ProportionalScaling

 
Public property FileName: string read FURL write SetURL; deprecated;

Warning: this symbol is deprecated.

Deprecated name for URL.

Published property URL: string read FURL write SetURL;

URL of the image. Setting this also sets Image. Set this to '' to clear the image.

Published property AlphaChannel: TAutoAlphaChannel read GetAlphaChannel write SetAlphaChannel default acAuto;

How to treat alpha channel of the assigned image. By default, this is acAuto, which means that image contents together with current Color determine how the alpha of image is treated (opaque, alpha test, alpha blending). Set this to force specific treatment.

Published property SmoothScaling: boolean read GetSmoothScaling write SetSmoothScaling default true;

Is the image scaling mode smooth (bilinear filtering) or not (nearest-pixel filtering). See TGLImageCore.SmoothScaling.

Published property Stretch: boolean read FStretch write SetStretch default false;

How does the loaded image size affect the size of the image control.

  • When FullSize or AutoSizeToChildren is True, then the value of Stretch, and loaded image size, are ignored. When AutoSizeToChildren, the image is always stretched to surround the children. When FullSize, the image is always stretched to fill the parent.

    Otherwise:

  • If Stretch = False (default), then the displayed size corresponds to the underlying image size.

  • If Stretch = True, the image will be stretched to fill the requested area. The property ProportionalScaling determines how the image will be adjusted to fit the requested size (by Width, WidthFraction, Height, HeightFraction).

    psNone

    The image will be scaled to exactly fill the requested Width and Height (without paying attention to the aspect ratio of the image).

    This is the case when you fully force the displayed size and position, regardless of image size. Displayed image will always exactly fill the requested area.

    psFit

    The image will be proportionally scaled to fit within the requested Width and Height. If the aspect ratio of image will be different than aspect ratio of Width/Height, the scaled image will be centered inside the Width/Height.

    psEnclose

    The image will be proportionally scaled to enclode the requested Width and Height. If the aspect ratio of image will be different than aspect ratio of Width/Height, the scaled image will be larger then the requested area.

Published property ProportionalScaling: TProportionalScaling read FProportionalScaling write SetProportionalScaling default psNone;
 
Published property Cache: boolean read FCache write FCache default false;

If True, the images are loaded and freed using the cache. That is, the TEncodedImage in Image is loaded and freed using TextureCache.TextureImage_IncReference and TextureCache.TextureImage_DecReference instead of LoadEncodedImage and FreeAndNil.

This can save memory and loading time a lot, if you reuse the same image in many TCastleControlImage instances. The limitation is that you shouldn't change the Image contents (e.g. changing TCastleImage.Colors), or you will change all shared images.

This is also useful to "preload" images before creating multiple complicated complicated UI using TCastleImageControl. Like this:

CachedImage := TextureCache.TextureImage_IncReference('my_image.png');
// now creating TCastleImageControl with this image
// and cache will be very fast
ImageControl := TCastleImageControl.Create(Application);
ImageControl.Cache := true:
ImageControl.URL := 'my_image.png'; // use already cached URL
// somewhere much later
TextureCache.TextureImage_DecReference(CachedImage);

This also affects releasing your custom images, if you assign Image property (instead of URL) and leave OwnsImage as True. Be sure in such case that your image was loaded from cache too.

Right now we don't cache and share the TGLImage contents, so the memory on GPU is not shared, but this may change at some point to also be shared.

Right now, this should be set before assigning Image or URL, otherwise the image will not be loaded/freed using the cache. This may change at some point.

Published property FlipHorizontal: Boolean read FFlipHorizontal write SetFlipHorizontal default false;
 
Published property FlipVertical: Boolean read FFlipVertical write SetFlipVertical default false;
 
Published property ProtectedSides: TBorder read FProtectedSides;

Corners and edges of the image that are protected from scaling distortion when Stretch is used. We use the 9-slicing algorithm, described at TGLImageCore.Draw3x3 and https://en.wikipedia.org/wiki/9-slice_scaling to scale the image.

Published property DetectScaleFromUrl: Boolean read FDetectScaleFromUrl write SetDetectScaleFromUrl default false;

Set this to auto-detect that image is scaled compared to it's desired size. This detects URL endings like '[email protected]', and we will automatically adjust to it (the size determined by AutoSize will be actually 2x smaller than the PNG size). Preparing images at a 2x or 4x larger size than "desired" is a useful trick when using UI scaling, when you want the image to look good at various scales.

Published property ColorPersistent: TCastleColorPersistent read FColorPersistent;

Color that can be visually edited in Castle Game Engine Editor, Lazarus and Delphi. Normal user code does not need to deal with this, instead read or write Color directly.

See also
Color
Color tint of the image.

Generated by PasDoc 0.15.0.