| 1 |
{%MainUnit castlecontrols.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-2023 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 |
{ Indicate stretching approach for @link(TCastleImageControl.ProportionalScaling). } |
| 20 |
TProportionalScaling = ( |
| 21 |
{ Adjust to the requested area, ignoring proportions. } |
| 22 |
psNone, |
| 23 |
{ Adjust to the requested area, honoring proportions, such that the image |
| 24 |
may be larger than the requested area. } |
| 25 |
psEnclose, |
| 26 |
{ Adjust to the requested area, honoring proportions, such that the image |
| 27 |
may be smaller than the requested area. } |
| 28 |
psFit |
| 29 |
); |
| 30 |
|
| 31 |
{ Image control. |
| 32 |
Size is automatically adjusted to the image size, if Stretch is @false (default). |
| 33 |
You should set TCastleImageControl.Left, TCastleImageControl.Bottom properties, |
| 34 |
and load your image by setting TCastleImageControl.Url property |
| 35 |
or straight TCastleImageControl.Image. |
| 36 |
|
| 37 |
We automatically use alpha test or alpha blending based |
| 38 |
on loaded image alpha channel (see @link(TDrawableImage.Alpha)). |
| 39 |
You can influence this by @link(AlphaChannel) property. } |
| 40 |
TCastleImageControl = class(TCastleUserInterface) |
| 41 |
strict private |
| 42 |
FStretch: boolean; |
| 43 |
FProportionalScaling: TProportionalScaling; |
| 44 |
FDetectScaleFromUrl: Boolean; |
| 45 |
FContent: TCastleImagePersistent; |
| 46 |
|
| 47 |
{ Properties simply synchronized with the same properties of FContent. } |
| 48 |
function GetUrl: String; |
| 49 |
function GetDrawableImage: TDrawableImage; |
| 50 |
function GetOwnsDrawableImage: Boolean; |
| 51 |
function GetImage: TEncodedImage; |
| 52 |
function GetOwnsImage: Boolean; |
| 53 |
function GetRotation: Single; |
| 54 |
function GetRotationCenter: TVector2; |
| 55 |
function GetAlphaChannel: TAutoAlphaChannel; |
| 56 |
function GetColor: TCastleColor; |
| 57 |
function GetClip: boolean; |
| 58 |
function GetClipLine: TVector3; |
| 59 |
function GetCustomShader: TGLSLProgram; |
| 60 |
function GetFlipVertical: Boolean; |
| 61 |
function GetFlipHorizontal: Boolean; |
| 62 |
function GetSmoothScaling: Boolean; |
| 63 |
function GetCache: Boolean; |
| 64 |
function GetProtectedSides: TBorder; |
| 65 |
function GetRegion: TFloatRectangle; |
| 66 |
procedure SetUrl(const Value: String); |
| 67 |
procedure SetDrawableImage(const Value: TDrawableImage); |
| 68 |
procedure SetOwnsDrawableImage(const Value: Boolean); |
| 69 |
procedure SetImage(const Value: TEncodedImage); |
| 70 |
procedure SetOwnsImage(const Value: Boolean); |
| 71 |
procedure SetRotation(const Value: Single); |
| 72 |
procedure SetRotationCenter(const Value: TVector2); |
| 73 |
procedure SetAlphaChannel(const Value: TAutoAlphaChannel); |
| 74 |
procedure SetColor(const Value: TCastleColor); |
| 75 |
procedure SetClip(const Value: boolean); |
| 76 |
procedure SetClipLine(const Value: TVector3); |
| 77 |
procedure SetRegion(const Value: TFloatRectangle); |
| 78 |
procedure SetCustomShader(const Value: TGLSLProgram); |
| 79 |
procedure SetFlipVertical(const Value: Boolean); |
| 80 |
procedure SetFlipHorizontal(const Value: Boolean); |
| 81 |
procedure SetSmoothScaling(const Value: boolean); |
| 82 |
procedure SetCache(const Value: boolean); |
| 83 |
|
| 84 |
{ Properties maintained for backward compatibility. } |
| 85 |
function GetCenterX: Single; |
| 86 |
function GetCenterY: Single; |
| 87 |
procedure SetCenterX(const Value: Single); |
| 88 |
procedure SetCenterY(const Value: Single); |
| 89 |
function GetBlending: boolean; |
| 90 |
procedure SetBlending(const Value: boolean); |
| 91 |
function GetCorners: TVector4Integer; |
| 92 |
procedure SetCorners(const Value: TVector4Integer); |
| 93 |
|
| 94 |
procedure SetStretch(const Value: boolean); |
| 95 |
procedure SetProportionalScaling(const Value: TProportionalScaling); |
| 96 |
function GetProportional: boolean; |
| 97 |
procedure SetProportional(const Value: boolean); |
| 98 |
procedure SetDetectScaleFromUrl(const Value: Boolean); |
| 99 |
|
| 100 |
procedure ContentChanged(Sender: TObject); |
| 101 |
protected |
| 102 |
procedure PreferredSize(var PreferredWidth, PreferredHeight: Single); override; |
| 103 |
public |
| 104 |
constructor Create(AOwner: TComponent); override; |
| 105 |
destructor Destroy; override; |
| 106 |
procedure Render; override; |
| 107 |
procedure GLContextOpen; override; |
| 108 |
procedure EditorAllowResize( |
| 109 |
out ResizeWidth, ResizeHeight: Boolean; out Reason: String); override; |
| 110 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 111 |
|
| 112 |
{ Image contents, or @nil if none. You can set it by setting @link(Url), |
| 113 |
or by setting this property directly, |
| 114 |
or by setting @link(DrawableImage). |
| 115 |
|
| 116 |
Note that by default the TEncodedImage instance assigned here is owned |
| 117 |
by this component (see @link(OwnsImage)). |
| 118 |
So if you set this property to your custom TEncodedImage instance you should |
| 119 |
leave memory management of this instance to this component. |
| 120 |
You can either create a copy by TEncodedImage.MakeCopy |
| 121 |
if you want to give here only a copy, or you can change @link(OwnsImage) |
| 122 |
to @false. |
| 123 |
|
| 124 |
It is allowed to modify the contents or even size of this image. |
| 125 |
Just make sure to call @link(ImageChanged) after the modifications |
| 126 |
are done to update the rendered image (and possibly |
| 127 |
the UI control size, e.g. in case TCastleImageControl |
| 128 |
uses this TCastleImagePersistent, and TCastleImageControl.Stretch=false). |
| 129 |
|
| 130 |
@seealso TCastleImagePersistent.Image } |
| 131 |
property Image: TEncodedImage read GetImage write SetImage; |
| 132 |
|
| 133 |
{ Whether the memory management of assigned @link(Image) is automatic. |
| 134 |
See @link(Image) documentation for details. |
| 135 |
|
| 136 |
Note that setting @link(Url) changes @link(Image), |
| 137 |
and changes OwnsImage unconditionally to @true. |
| 138 |
|
| 139 |
Note that setting @link(DrawableImage) changes @link(Image) too, |
| 140 |
and changes OwnsImage to the value present in @link(TDrawableImage.OwnsImage). |
| 141 |
|
| 142 |
@seealso TCastleImagePersistent.OwnsImage } |
| 143 |
property OwnsImage: boolean read GetOwnsImage write SetOwnsImage default true; |
| 144 |
|
| 145 |
{ Image used for drawing. Never @nil. |
| 146 |
Always @link(TDrawableImage.Image) equals our @link(Image). |
| 147 |
|
| 148 |
Usually you do not want to change this property. |
| 149 |
You can change @link(Url), or @link(Image), and the drawable |
| 150 |
image will reflect this. |
| 151 |
But there are good use-cases to change the @link(DrawableImage) |
| 152 |
directly: e.g. you can use fast image-to-image |
| 153 |
drawing using @link(TDrawableImage.DrawFrom). |
| 154 |
Or you can set a new instance of TDrawableImage here. |
| 155 |
Be sure to adjust @link(OwnsDrawableImage) as needed. |
| 156 |
|
| 157 |
Note that when OpenGL(ES) context is lost and recreated (which can |
| 158 |
happen at any moment on mobile devices), the contents of this are |
| 159 |
reinitialized from @link(Image). |
| 160 |
|
| 161 |
@seealso TCastleImagePersistent.DrawableImage } |
| 162 |
property DrawableImage: TDrawableImage read GetDrawableImage write SetDrawableImage; |
| 163 |
|
| 164 |
{ Whether we should automatically free the @link(DrawableImage) instance. |
| 165 |
Note that this is restored to @true when we need to recreate the |
| 166 |
TDrawableImage internally, e.g. when @link(Image) instance changed |
| 167 |
(when even size changed, we have to recreate TDrawableImage). |
| 168 |
|
| 169 |
@seealso TCastleImagePersistent.OwnsDrawableImage } |
| 170 |
property OwnsDrawableImage: boolean read GetOwnsDrawableImage write SetOwnsDrawableImage default true; |
| 171 |
|
| 172 |
{ If you modified the @link(Image) contents |
| 173 |
(colors, or maybe even image size) you need to call this. |
| 174 |
|
| 175 |
@seealso TCastleImagePersistent.ImageChanged } |
| 176 |
procedure ImageChanged; |
| 177 |
|
| 178 |
{ Color tint of the image. This simply multiplies the image RGBA components, |
| 179 |
just like @link(TDrawableImage.Color). By default this is opaque white, |
| 180 |
which means that image colors are unchanged. |
| 181 |
|
| 182 |
@seealso TCastleImagePersistent.Color } |
| 183 |
property Color: TCastleColor read GetColor write SetColor; |
| 184 |
|
| 185 |
{$ifdef FPC} |
| 186 |
{ Corners and edges of the image that are not stretched even |
| 187 |
when @link(Stretch) is used. |
| 188 |
See @link(TDrawableImage.Draw3x3) for the details how such drawing works. } |
| 189 |
property Corners: TVector4Integer read GetCorners write SetCorners; |
| 190 |
deprecated 'use ProtectedSides'; |
| 191 |
|
| 192 |
{ X coordinate of the center of rotation. Value from 0 to 1. Default value 0.5. } |
| 193 |
property CenterX: Single read GetCenterX write SetCenterX default 0.5; |
| 194 |
deprecated 'use RotationCenter'; |
| 195 |
|
| 196 |
{ Y coordinate of the center of rotation. Value from 0 to 1. Default value 0.5. } |
| 197 |
property CenterY: Single read GetCenterY write SetCenterY default 0.5; |
| 198 |
deprecated 'use RotationCenter'; |
| 199 |
{$endif} |
| 200 |
|
| 201 |
{ Center of rotation. |
| 202 |
Expressed as a fraction within the drawn ScreenRectangle, |
| 203 |
(0,0) means bottom-left corner, (1,1) means top-right corner. |
| 204 |
Default (0.5,0.5). |
| 205 |
|
| 206 |
@seealso TCastleImagePersistent.RotationCenter } |
| 207 |
property RotationCenter: TVector2 read GetRotationCenter write SetRotationCenter; |
| 208 |
|
| 209 |
{ If @link(Clip), this is the line equation used to determine whether |
| 210 |
we clip the given pixel. Given a line (A, B, C) and pixel (x, y), |
| 211 |
the pixel is clipped (rejected) if @code(A * x + B * y + C < 0). |
| 212 |
|
| 213 |
The equation is calculated in the coordinates in which image |
| 214 |
X, Y spans from (0, 0) (bottom-left) to (1, 1) (top-right). |
| 215 |
For example ClipLine = (1, 0, -0.5) means that we reject pixels |
| 216 |
where @code(1 * x + 0 * y - 0.5 < 0). In other words, |
| 217 |
we reject pixels where @code(x < 0.5), so we reject the left half of the image. |
| 218 |
|
| 219 |
@seealso TCastleImagePersistent.ClipLine } |
| 220 |
property ClipLine: TVector3 read GetClipLine write SetClipLine; |
| 221 |
|
| 222 |
{ Image region to which we should limit the display. |
| 223 |
Empty (following @link(TFloatRectangle.Empty)) means using the whole image. } |
| 224 |
property Region: TFloatRectangle read GetRegion write SetRegion; |
| 225 |
|
| 226 |
{ Custom GLSL shading code. |
| 227 |
See TDrawableImage.CustomShader for explanation. |
| 228 |
|
| 229 |
@seealso TCastleImagePersistent.CustomShader } |
| 230 |
property CustomShader: TGLSLProgram read GetCustomShader write SetCustomShader; |
| 231 |
|
| 232 |
{$ifdef FPC} |
| 233 |
property Blending: boolean read GetBlending write SetBlending stored false; |
| 234 |
deprecated 'use AlphaChannel'; |
| 235 |
property Proportional: boolean read GetProportional write SetProportional stored false default false; |
| 236 |
deprecated 'use ProportionalScaling'; |
| 237 |
property FileName: string read GetUrl write SetUrl stored false; |
| 238 |
deprecated 'use Url'; |
| 239 |
{$endif} |
| 240 |
published |
| 241 |
{ Image contents to display. } |
| 242 |
property Content: TCastleImagePersistent read FContent; |
| 243 |
|
| 244 |
{ URL of the image. |
| 245 |
Set this to load a new image, you can set to '' to clear the image. |
| 246 |
|
| 247 |
@seealso TCastleImagePersistent.Url } |
| 248 |
property Url: String read GetUrl write SetUrl stored false; |
| 249 |
|
| 250 |
{ How to treat alpha channel of the assigned image. |
| 251 |
By default, this is acAuto, which means that image contents |
| 252 |
together with current @link(Color) determine how |
| 253 |
the alpha of image is treated (opaque, alpha test, alpha blending). |
| 254 |
Set this to force specific treatment. |
| 255 |
|
| 256 |
@seealso TCastleImagePersistent.AlphaChannel } |
| 257 |
property AlphaChannel: TAutoAlphaChannel |
| 258 |
read GetAlphaChannel write SetAlphaChannel stored false default acAuto; |
| 259 |
|
| 260 |
{ Is the image scaling mode smooth (bilinear filtering) |
| 261 |
or not (nearest-pixel filtering). |
| 262 |
See @link(TDrawableImage.SmoothScaling). |
| 263 |
|
| 264 |
@seealso TCastleImagePersistent.SmoothScaling } |
| 265 |
property SmoothScaling: boolean |
| 266 |
read GetSmoothScaling write SetSmoothScaling stored false default true; |
| 267 |
|
| 268 |
{ How does the loaded image size affect the size of the image control. |
| 269 |
|
| 270 |
@unorderedList( |
| 271 |
@item( |
| 272 |
When @link(FullSize) or @link(AutoSizeToChildren) is @true, |
| 273 |
then the value of @link(Stretch), and loaded image size, are ignored. |
| 274 |
When @link(AutoSizeToChildren), |
| 275 |
the image is always stretched to surround the children. |
| 276 |
When @link(FullSize), |
| 277 |
the image is always stretched to fill the parent. |
| 278 |
|
| 279 |
Otherwise: |
| 280 |
) |
| 281 |
|
| 282 |
@item(If Stretch = @false (default), then |
| 283 |
the displayed size corresponds to the underlying image size.) |
| 284 |
|
| 285 |
@item(If Stretch = @true, the image will be stretched to fill |
| 286 |
the requested area. The property |
| 287 |
@link(ProportionalScaling) determines how the image will |
| 288 |
be adjusted to fit the requested size |
| 289 |
(by @link(Width), @link(WidthFraction), @link(Height), @link(HeightFraction)). |
| 290 |
|
| 291 |
@definitionList( |
| 292 |
@itemLabel psNone |
| 293 |
@item(The image will be scaled to exactly fill |
| 294 |
the requested Width and Height |
| 295 |
(without paying attention to the aspect ratio of the image). |
| 296 |
|
| 297 |
This is the case when you fully force the displayed size |
| 298 |
and position, regardless of image size. Displayed image will |
| 299 |
always exactly fill the requested area.) |
| 300 |
|
| 301 |
@itemLabel psFit |
| 302 |
@item(The image will be proportionally scaled to fit within |
| 303 |
the requested Width and Height. |
| 304 |
If the aspect ratio of image |
| 305 |
will be different than aspect ratio of Width/Height, the scaled image |
| 306 |
will be centered inside the Width/Height.) |
| 307 |
|
| 308 |
@itemLabel psEnclose |
| 309 |
@item(The image will be proportionally scaled to enclode |
| 310 |
the requested Width and Height. |
| 311 |
If the aspect ratio of image |
| 312 |
will be different than aspect ratio of Width/Height, the scaled image |
| 313 |
will be larger then the requested area.) |
| 314 |
) |
| 315 |
) |
| 316 |
) |
| 317 |
|
| 318 |
@groupBegin } |
| 319 |
property Stretch: boolean read FStretch write SetStretch default false; |
| 320 |
property ProportionalScaling: TProportionalScaling |
| 321 |
read FProportionalScaling write SetProportionalScaling default psNone; |
| 322 |
{ @groupEnd } |
| 323 |
|
| 324 |
{ If @true, the image is loaded and freed using a cache. |
| 325 |
This can save memory and loading time a lot, if you reuse the same |
| 326 |
URL in many TCastleImageControl instances. |
| 327 |
|
| 328 |
@seealso TCastleImagePersistent.Cache } |
| 329 |
property Cache: boolean read GetCache write SetCache stored false default true; |
| 330 |
|
| 331 |
{ Display image horizontally flipped. |
| 332 |
@seealso TCastleImagePersistent.FlipHorizontal } |
| 333 |
property FlipHorizontal: Boolean read GetFlipHorizontal write SetFlipHorizontal |
| 334 |
stored false default false; |
| 335 |
|
| 336 |
{ Display image vertically flipped. |
| 337 |
@seealso TCastleImagePersistent.FlipVertical } |
| 338 |
property FlipVertical: Boolean read GetFlipVertical write SetFlipVertical |
| 339 |
stored false default false; |
| 340 |
|
| 341 |
{ Corners and edges of the image that are protected from scaling distortion |
| 342 |
when @link(Stretch) is used. |
| 343 |
We use the 9-slicing algorithm, |
| 344 |
described at @link(TDrawableImage.Draw3x3) |
| 345 |
and https://en.wikipedia.org/wiki/9-slice_scaling to scale the image. |
| 346 |
|
| 347 |
@seealso TCastleImagePersistent.ProtectedSides } |
| 348 |
property ProtectedSides: TBorder read GetProtectedSides stored false; |
| 349 |
|
| 350 |
{ Set this to auto-detect that image is scaled compared to it's desired size. |
| 351 |
This detects URL endings like '[email protected]', and we will automatically adjust |
| 352 |
to it (the size determined by AutoSize will be actually 2x smaller than |
| 353 |
the PNG size). |
| 354 |
Preparing images at a 2x or 4x larger size than "desired" is a useful trick |
| 355 |
when using UI scaling, when you want the image to look good at various scales. } |
| 356 |
property DetectScaleFromUrl: Boolean |
| 357 |
read FDetectScaleFromUrl write SetDetectScaleFromUrl default false; |
| 358 |
|
| 359 |
{ Rotation in radians. Default value 0. |
| 360 |
@seealso TCastleImagePersistent.Rotation } |
| 361 |
property Rotation: Single read GetRotation write SetRotation stored false {$ifdef FPC}default 0{$endif}; |
| 362 |
|
| 363 |
{ Clip the image by an arbitrary 2D line defined in @link(ClipLine). |
| 364 |
@seealso TCastleImagePersistent.Clip } |
| 365 |
property Clip: boolean read GetClip write SetClip stored false default false; |
| 366 |
|
| 367 |
{$define read_interface_class} |
| 368 |
{$I auto_generated_persistent_vectors/tcastleimagecontrol_persistent_vectors.inc} |
| 369 |
{$undef read_interface_class} |
| 370 |
end; |
| 371 |
|
| 372 |
{$endif read_interface} |
| 373 |
|
| 374 |
{$ifdef read_implementation} |
| 375 |
|
| 376 |
{ TCastleImageControl -------------------------------------------------------- } |
| 377 |
|
| 378 |
constructor TCastleImageControl.Create(AOwner: TComponent); |
| 379 |
begin |
| 380 |
inherited; |
| 381 |
FContent := TCastleImagePersistent.Create; |
| 382 |
FContent.OnChange := {$ifdef FPC}@{$endif}ContentChanged; |
| 383 |
|
| 384 |
{$define read_implementation_constructor} |
| 385 |
{$I auto_generated_persistent_vectors/tcastleimagecontrol_persistent_vectors.inc} |
| 386 |
{$undef read_implementation_constructor} |
| 387 |
end; |
| 388 |
|
| 389 |
destructor TCastleImageControl.Destroy; |
| 390 |
begin |
| 391 |
FreeAndNil(FContent); |
| 392 |
|
| 393 |
{$define read_implementation_destructor} |
| 394 |
{$I auto_generated_persistent_vectors/tcastleimagecontrol_persistent_vectors.inc} |
| 395 |
{$undef read_implementation_destructor} |
| 396 |
inherited; |
| 397 |
end; |
| 398 |
|
| 399 |
function TCastleImageControl.GetProtectedSides: TBorder; |
| 400 |
begin |
| 401 |
Result := FContent.ProtectedSides; |
| 402 |
end; |
| 403 |
|
| 404 |
function TCastleImageControl.GetRegion: TFloatRectangle; |
| 405 |
begin |
| 406 |
Result := FContent.Region; |
| 407 |
end; |
| 408 |
|
| 409 |
function TCastleImageControl.GetSmoothScaling: Boolean; |
| 410 |
begin |
| 411 |
Result := FContent.SmoothScaling; |
| 412 |
end; |
| 413 |
|
| 414 |
procedure TCastleImageControl.SetSmoothScaling(const Value: boolean); |
| 415 |
begin |
| 416 |
FContent.SmoothScaling := Value; |
| 417 |
end; |
| 418 |
|
| 419 |
function TCastleImageControl.GetClip: boolean; |
| 420 |
begin |
| 421 |
Result := FContent.Clip; |
| 422 |
end; |
| 423 |
|
| 424 |
procedure TCastleImageControl.SetClip(const Value: boolean); |
| 425 |
begin |
| 426 |
FContent.Clip := Value; |
| 427 |
end; |
| 428 |
|
| 429 |
function TCastleImageControl.GetClipLine: TVector3; |
| 430 |
begin |
| 431 |
Result := FContent.ClipLine; |
| 432 |
end; |
| 433 |
|
| 434 |
procedure TCastleImageControl.SetClipLine(const Value: TVector3); |
| 435 |
begin |
| 436 |
FContent.ClipLine := Value; |
| 437 |
end; |
| 438 |
|
| 439 |
procedure TCastleImageControl.SetRegion(const Value: TFloatRectangle); |
| 440 |
begin |
| 441 |
FContent.Region := Value; |
| 442 |
end; |
| 443 |
|
| 444 |
function TCastleImageControl.GetRotation: Single; |
| 445 |
begin |
| 446 |
Result := FContent.Rotation; |
| 447 |
end; |
| 448 |
|
| 449 |
procedure TCastleImageControl.SetRotation(const Value: Single); |
| 450 |
begin |
| 451 |
FContent.Rotation := Value; |
| 452 |
end; |
| 453 |
|
| 454 |
function TCastleImageControl.GetRotationCenter: TVector2; |
| 455 |
begin |
| 456 |
Result := FContent.RotationCenter; |
| 457 |
end; |
| 458 |
|
| 459 |
procedure TCastleImageControl.SetRotationCenter(const Value: TVector2); |
| 460 |
begin |
| 461 |
FContent.RotationCenter := Value; |
| 462 |
end; |
| 463 |
|
| 464 |
procedure TCastleImageControl.GLContextOpen; |
| 465 |
begin; |
| 466 |
inherited; |
| 467 |
{ Calling this is not strictly necessary. It means we load the OpenGL |
| 468 |
resources *now*, instead of on-demand right before 1st render. } |
| 469 |
FContent.DrawableImage.PrepareResources; |
| 470 |
end; |
| 471 |
|
| 472 |
procedure TCastleImageControl.Render; |
| 473 |
begin |
| 474 |
inherited; |
| 475 |
FContent.DrawUiBegin(UIScale); |
| 476 |
FContent.Draw(RenderRect); |
| 477 |
FContent.DrawUiEnd; |
| 478 |
end; |
| 479 |
|
| 480 |
procedure TCastleImageControl.PreferredSize(var PreferredWidth, PreferredHeight: Single); |
| 481 |
var |
| 482 |
ImageWidth, ImageHeight: Single; |
| 483 |
begin |
| 484 |
inherited; |
| 485 |
|
| 486 |
ImageWidth := FContent.Width; |
| 487 |
ImageHeight := FContent.Height; |
| 488 |
if FDetectScaleFromUrl and (FContent.ScaleFromUrl <> 1) then |
| 489 |
begin |
| 490 |
ImageWidth := ImageWidth / FContent.ScaleFromUrl; |
| 491 |
ImageHeight := ImageHeight / FContent.ScaleFromUrl; |
| 492 |
end; |
| 493 |
|
| 494 |
if not Stretch then |
| 495 |
begin |
| 496 |
PreferredWidth := ImageWidth * UIScale; |
| 497 |
PreferredHeight := ImageHeight * UIScale; |
| 498 |
end else |
| 499 |
if (ProportionalScaling in [psFit, psEnclose]) and |
| 500 |
(ImageWidth <> 0) and |
| 501 |
(ImageHeight <> 0) then |
| 502 |
begin |
| 503 |
if (ProportionalScaling = psFit) = |
| 504 |
(PreferredWidth / PreferredHeight > |
| 505 |
ImageWidth / ImageHeight) then |
| 506 |
begin |
| 507 |
PreferredWidth := ImageWidth * PreferredHeight / ImageHeight; |
| 508 |
end else |
| 509 |
begin |
| 510 |
PreferredHeight := ImageHeight * PreferredWidth / ImageWidth; |
| 511 |
end; |
| 512 |
end; |
| 513 |
end; |
| 514 |
|
| 515 |
procedure TCastleImageControl.EditorAllowResize( |
| 516 |
out ResizeWidth, ResizeHeight: Boolean; out Reason: String); |
| 517 |
begin |
| 518 |
inherited; |
| 519 |
if not Stretch then |
| 520 |
begin |
| 521 |
ResizeWidth := false; |
| 522 |
ResizeHeight := false; |
| 523 |
Reason := SAppendPart(Reason, NL, 'Turn on "TCastleImageControl.Stretch" to change size.'); |
| 524 |
end; |
| 525 |
end; |
| 526 |
|
| 527 |
procedure TCastleImageControl.ContentChanged(Sender: TObject); |
| 528 |
begin |
| 529 |
VisibleChange([chRectangle]); |
| 530 |
end; |
| 531 |
|
| 532 |
procedure TCastleImageControl.ImageChanged; |
| 533 |
begin |
| 534 |
FContent.ImageChanged; |
| 535 |
end; |
| 536 |
|
| 537 |
function TCastleImageControl.GetAlphaChannel: TAutoAlphaChannel; |
| 538 |
begin |
| 539 |
Result := FContent.AlphaChannel; |
| 540 |
end; |
| 541 |
|
| 542 |
procedure TCastleImageControl.SetAlphaChannel(const Value: TAutoAlphaChannel); |
| 543 |
begin |
| 544 |
FContent.AlphaChannel := Value; |
| 545 |
end; |
| 546 |
|
| 547 |
function TCastleImageControl.GetBlending: boolean; |
| 548 |
begin |
| 549 |
Result := AlphaChannel <> acBlending; |
| 550 |
end; |
| 551 |
|
| 552 |
procedure TCastleImageControl.SetBlending(const Value: boolean); |
| 553 |
begin |
| 554 |
if Value then |
| 555 |
AlphaChannel := acBlending |
| 556 |
else |
| 557 |
AlphaChannel := acTest; |
| 558 |
end; |
| 559 |
|
| 560 |
procedure TCastleImageControl.SetStretch(const Value: boolean); |
| 561 |
begin |
| 562 |
if FStretch <> Value then |
| 563 |
begin |
| 564 |
FStretch := Value; |
| 565 |
VisibleChange([chRectangle]); |
| 566 |
end; |
| 567 |
end; |
| 568 |
|
| 569 |
function TCastleImageControl.GetProportional: boolean; |
| 570 |
begin |
| 571 |
Result := ProportionalScaling <> psNone; |
| 572 |
end; |
| 573 |
|
| 574 |
procedure TCastleImageControl.SetProportional(const Value: boolean); |
| 575 |
begin |
| 576 |
if Value then |
| 577 |
ProportionalScaling := psFit |
| 578 |
else |
| 579 |
ProportionalScaling := psNone; |
| 580 |
end; |
| 581 |
|
| 582 |
procedure TCastleImageControl.SetProportionalScaling(const Value: TProportionalScaling); |
| 583 |
begin |
| 584 |
if FProportionalScaling <> Value then |
| 585 |
begin |
| 586 |
FProportionalScaling := Value; |
| 587 |
VisibleChange([chRectangle]); |
| 588 |
end; |
| 589 |
end; |
| 590 |
|
| 591 |
function TCastleImageControl.GetUrl: String; |
| 592 |
begin |
| 593 |
Result := FContent.Url; |
| 594 |
end; |
| 595 |
|
| 596 |
procedure TCastleImageControl.SetUrl(const Value: String); |
| 597 |
begin |
| 598 |
FContent.Url := Value; |
| 599 |
end; |
| 600 |
|
| 601 |
function TCastleImageControl.GetColor: TCastleColor; |
| 602 |
begin |
| 603 |
Result := FContent.Color; |
| 604 |
end; |
| 605 |
|
| 606 |
procedure TCastleImageControl.SetColor(const Value: TCastleColor); |
| 607 |
begin |
| 608 |
FContent.Color := Value; |
| 609 |
end; |
| 610 |
|
| 611 |
function TCastleImageControl.GetCustomShader: TGLSLProgram; |
| 612 |
begin |
| 613 |
Result := FContent.CustomShader; |
| 614 |
end; |
| 615 |
|
| 616 |
procedure TCastleImageControl.SetCustomShader(const Value: TGLSLProgram); |
| 617 |
begin |
| 618 |
FContent.CustomShader := Value; |
| 619 |
end; |
| 620 |
|
| 621 |
function TCastleImageControl.GetDrawableImage: TDrawableImage; |
| 622 |
begin |
| 623 |
Result := FContent.DrawableImage; |
| 624 |
end; |
| 625 |
|
| 626 |
procedure TCastleImageControl.SetDrawableImage(const Value: TDrawableImage); |
| 627 |
begin |
| 628 |
FContent.DrawableImage := Value; |
| 629 |
end; |
| 630 |
|
| 631 |
function TCastleImageControl.GetOwnsDrawableImage: Boolean; |
| 632 |
begin |
| 633 |
Result := FContent.OwnsDrawableImage; |
| 634 |
end; |
| 635 |
|
| 636 |
procedure TCastleImageControl.SetOwnsDrawableImage(const Value: Boolean); |
| 637 |
begin |
| 638 |
FContent.OwnsDrawableImage := Value; |
| 639 |
end; |
| 640 |
|
| 641 |
function TCastleImageControl.GetImage: TEncodedImage; |
| 642 |
begin |
| 643 |
Result := FContent.Image; |
| 644 |
end; |
| 645 |
|
| 646 |
procedure TCastleImageControl.SetImage(const Value: TEncodedImage); |
| 647 |
begin |
| 648 |
FContent.Image := Value; |
| 649 |
end; |
| 650 |
|
| 651 |
function TCastleImageControl.GetOwnsImage: Boolean; |
| 652 |
begin |
| 653 |
Result := FContent.OwnsImage; |
| 654 |
end; |
| 655 |
|
| 656 |
procedure TCastleImageControl.SetOwnsImage(const Value: Boolean); |
| 657 |
begin |
| 658 |
FContent.OwnsImage := Value; |
| 659 |
end; |
| 660 |
|
| 661 |
function TCastleImageControl.GetFlipVertical: Boolean; |
| 662 |
begin |
| 663 |
Result := FContent.FlipVertical; |
| 664 |
end; |
| 665 |
|
| 666 |
procedure TCastleImageControl.SetFlipVertical(const Value: Boolean); |
| 667 |
begin |
| 668 |
FContent.FlipVertical := Value; |
| 669 |
end; |
| 670 |
|
| 671 |
function TCastleImageControl.GetFlipHorizontal: Boolean; |
| 672 |
begin |
| 673 |
Result := FContent.FlipHorizontal; |
| 674 |
end; |
| 675 |
|
| 676 |
procedure TCastleImageControl.SetFlipHorizontal(const Value: Boolean); |
| 677 |
begin |
| 678 |
FContent.FlipHorizontal := Value; |
| 679 |
end; |
| 680 |
|
| 681 |
function TCastleImageControl.GetCache: Boolean; |
| 682 |
begin |
| 683 |
Result := FContent.Cache; |
| 684 |
end; |
| 685 |
|
| 686 |
procedure TCastleImageControl.SetCache(const Value: boolean); |
| 687 |
begin |
| 688 |
FContent.Cache := Value; |
| 689 |
end; |
| 690 |
|
| 691 |
procedure TCastleImageControl.SetDetectScaleFromUrl(const Value: Boolean); |
| 692 |
begin |
| 693 |
if FDetectScaleFromUrl <> Value then |
| 694 |
begin |
| 695 |
FDetectScaleFromUrl := Value; |
| 696 |
VisibleChange([chRender]); |
| 697 |
end; |
| 698 |
end; |
| 699 |
|
| 700 |
function TCastleImageControl.GetCorners: TVector4Integer; |
| 701 |
begin |
| 702 |
Result := Vector4Integer( |
| 703 |
Round(ProtectedSides.TotalTop), |
| 704 |
Round(ProtectedSides.TotalRight), |
| 705 |
Round(ProtectedSides.TotalBottom), |
| 706 |
Round(ProtectedSides.TotalLeft) |
| 707 |
); |
| 708 |
end; |
| 709 |
|
| 710 |
procedure TCastleImageControl.SetCorners(const Value: TVector4Integer); |
| 711 |
begin |
| 712 |
ProtectedSides.AllSides := 0; |
| 713 |
ProtectedSides.Top := Value[0]; |
| 714 |
ProtectedSides.Right := Value[1]; |
| 715 |
ProtectedSides.Bottom := Value[2]; |
| 716 |
ProtectedSides.Left := Value[3]; |
| 717 |
end; |
| 718 |
|
| 719 |
function TCastleImageControl.PropertySections( |
| 720 |
const PropertyName: String): TPropertySections; |
| 721 |
begin |
| 722 |
if ArrayContainsString(PropertyName, ['Url', 'Stretch', 'ProportionalScaling', |
| 723 |
'ColPersistent', 'SmoothScaling', 'AlphaChannel', 'FlipHizontal', |
| 724 |
'FlipVertical', 'ProtectedSides', 'RegionPersistent', |
| 725 |
'Rotation', 'Content' |
| 726 |
]) then |
| 727 |
Result := [psBasic] |
| 728 |
else |
| 729 |
Result := inherited PropertySections(PropertyName); |
| 730 |
end; |
| 731 |
|
| 732 |
function TCastleImageControl.GetCenterX: Single; |
| 733 |
begin |
| 734 |
Result := RotationCenter.X; |
| 735 |
end; |
| 736 |
|
| 737 |
function TCastleImageControl.GetCenterY: Single; |
| 738 |
begin |
| 739 |
Result := RotationCenter.Y; |
| 740 |
end; |
| 741 |
|
| 742 |
procedure TCastleImageControl.SetCenterX(const Value: Single); |
| 743 |
var |
| 744 |
V: TVector2; |
| 745 |
begin |
| 746 |
V := RotationCenter; |
| 747 |
V.X := Value; |
| 748 |
RotationCenter := V; |
| 749 |
end; |
| 750 |
|
| 751 |
procedure TCastleImageControl.SetCenterY(const Value: Single); |
| 752 |
var |
| 753 |
V: TVector2; |
| 754 |
begin |
| 755 |
V := RotationCenter; |
| 756 |
V.Y := Value; |
| 757 |
RotationCenter := V; |
| 758 |
end; |
| 759 |
|
| 760 |
{$define read_implementation_methods} |
| 761 |
{$I auto_generated_persistent_vectors/tcastleimagecontrol_persistent_vectors.inc} |
| 762 |
{$undef read_implementation_methods} |
| 763 |
|
| 764 |
{$endif read_implementation} |