| 1 |
{%MainUnit castleglimages.pas} |
| 2 |
{ |
| 3 |
Copyright 2019-2024 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 |
{ Part of CastleGLImages unit: TCastleImagePersistent. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
{$ifdef read_interface_type} |
| 22 |
// type (continue type section from previous include file) |
| 23 |
|
| 24 |
{ How is @link(TCastleImagePersistent.ProtectedSides) applies. |
| 25 |
Determines how does the 9-slices algorithm work. } |
| 26 |
TProtectedSidesScaling = ( |
| 27 |
{ All 4 sides of the image are preserved, and between them the middle |
| 28 |
may be arbitrary scaled vertically or horizontally. |
| 29 |
|
| 30 |
The target drawing area must have at least the size of all protected sides, |
| 31 |
otherwise the rendering cannot look perfect -- as some parts of corners will not fit. |
| 32 |
|
| 33 |
This is what 9-slices does by default. } |
| 34 |
pssDefault, |
| 35 |
|
| 36 |
{ The protected sides are all scaled such that the image is stretched in |
| 37 |
a uniform way vertically. |
| 38 |
|
| 39 |
This makes the vertical protected sides (top, bottom) meaningless: |
| 40 |
they don't matter, the image is effectively always scaled uniformly |
| 41 |
to match the target area height. |
| 42 |
|
| 43 |
However, the horizontal protected sides (left, right) now behave in more interesting |
| 44 |
way: they will be scaled just like the vertical sides. |
| 45 |
Just makes sense e.g. when your image has circular endigns on left and right sides |
| 46 |
(see default TCastleAbstractSlider background image for example). } |
| 47 |
pssMatchHeight |
| 48 |
); |
| 49 |
|
| 50 |
{ Image that can be easily loaded from URL (possibly reusing a cache), |
| 51 |
drawn, and serialized to/from file. |
| 52 |
|
| 53 |
@unorderedList( |
| 54 |
@item( |
| 55 |
Wraps TEncodedImage (image data in normal memory, |
| 56 |
usually one of TCastleImage descendants) |
| 57 |
and TDrawableImage (image data on GPU).) |
| 58 |
|
| 59 |
@item( |
| 60 |
Adds a cache of TDrawableImage underneath |
| 61 |
(which can be turned off, see @link(Cache)).) |
| 62 |
|
| 63 |
@item( |
| 64 |
Can be serialized (so you can use this as a SubComponent |
| 65 |
in various TCastleUserInterface descendants that want to expose |
| 66 |
some image).) |
| 67 |
) |
| 68 |
} |
| 69 |
TCastleImagePersistent = class(TPersistent) |
| 70 |
strict private |
| 71 |
FUrl: String; |
| 72 |
FUrlMonitoring: TUrlMonitoring; |
| 73 |
FCache: boolean; |
| 74 |
FScaleFromUrl: Single; |
| 75 |
FDrawableImage: TDrawableImage; |
| 76 |
FDrawableImageFromCache: boolean; |
| 77 |
FOwnsDrawableImage: Boolean; |
| 78 |
FOnChange: TNotifyEvent; |
| 79 |
FInternalVisualizeProtectedSides: Boolean; |
| 80 |
|
| 81 |
{ Parameters passed straight into FDrawableImage before Draw. } |
| 82 |
FSmoothScaling: Boolean; |
| 83 |
FClip: Boolean; |
| 84 |
FClipLine: TVector3; |
| 85 |
FRotation: Single; |
| 86 |
FRotationCenter: TVector2; |
| 87 |
FAlphaChannel: TAutoAlphaChannel; |
| 88 |
FColor: TCastleColor; |
| 89 |
FCustomShader: TGLSLProgram; |
| 90 |
FScaleCorners: Single; |
| 91 |
|
| 92 |
{ Saved / restored by DrawUiBegin / DrawUiEnd. } |
| 93 |
FSavedOnChange: TNotifyEvent; |
| 94 |
FSavedColor: TCastleColor; |
| 95 |
FSavedScaleCorners: Single; |
| 96 |
|
| 97 |
{ Parameters used in Draw. } |
| 98 |
FProtectedSides: TBorder; |
| 99 |
FRegion: TFloatRectangle; |
| 100 |
FFlipHorizontal: Boolean; |
| 101 |
FFlipVertical: Boolean; |
| 102 |
FProtectedSidesScaling: TProtectedSidesScaling; |
| 103 |
|
| 104 |
function GetImage: TEncodedImage; |
| 105 |
function GetOwnsImage: boolean; |
| 106 |
procedure SetOwnsImage(const Value: boolean); |
| 107 |
procedure SetCache(const Value: Boolean); |
| 108 |
procedure SetRegion(AValue: TFloatRectangle); |
| 109 |
procedure SetUrl(const Value: String); |
| 110 |
procedure SetImage(const Value: TEncodedImage); |
| 111 |
procedure SetDrawableImage(const Value: TDrawableImage); |
| 112 |
procedure SetSmoothScaling(const Value: boolean); |
| 113 |
procedure SetFlipVertical(const Value: Boolean); |
| 114 |
procedure SetFlipHorizontal(const Value: Boolean); |
| 115 |
procedure SetRotation(const Value: Single); |
| 116 |
procedure SetRotationCenter(const Value: TVector2); |
| 117 |
procedure SetAlphaChannel(const Value: TAutoAlphaChannel); |
| 118 |
procedure SetColor(const Value: TCastleColor); |
| 119 |
procedure SetCustomShader(const Value: TGLSLProgram); |
| 120 |
procedure SetScaleCorners(const Value: Single); |
| 121 |
procedure SetClip(const Value: Boolean); |
| 122 |
procedure SetClipLine(const Value: TVector3); |
| 123 |
procedure SetProtectedSidesScaling(const Value: TProtectedSidesScaling); |
| 124 |
|
| 125 |
procedure ProtectedSidesChange(Sender: TObject); |
| 126 |
procedure CreateDrawableImage; |
| 127 |
{ Free FDrawableImage if needed, and make it @nil. } |
| 128 |
procedure FreeDrawableImage; |
| 129 |
protected |
| 130 |
procedure DoChange; virtual; |
| 131 |
public |
| 132 |
constructor Create; |
| 133 |
destructor Destroy; override; |
| 134 |
procedure BeforeDestruction; override; |
| 135 |
|
| 136 |
{ Reserved for parent UI control to listen for changes. } |
| 137 |
property OnChange: TNotifyEvent read FOnChange write FOnChange; |
| 138 |
|
| 139 |
{ Image contents, or @nil if none. You can set it by setting @link(Url), |
| 140 |
or by setting this property directly, |
| 141 |
or by setting @link(DrawableImage). |
| 142 |
|
| 143 |
Note that by default the TEncodedImage instance assigned here is owned |
| 144 |
by this component (see @link(OwnsImage)). |
| 145 |
So if you set this property to your custom TEncodedImage instance you should |
| 146 |
leave memory management of this instance to this component. |
| 147 |
You can either create a copy by TEncodedImage.MakeCopy |
| 148 |
if you want to give here only a copy, or you can change @link(OwnsImage) |
| 149 |
to @false. |
| 150 |
|
| 151 |
It is allowed to modify the contents or even size of this image. |
| 152 |
Just make sure to call @link(ImageChanged) after the modifications |
| 153 |
are done to update the rendered image (and possibly |
| 154 |
the UI control size, e.g. in case TCastleImageControl |
| 155 |
uses this TCastleImagePersistent, and TCastleImageControl.Stretch=false). } |
| 156 |
property Image: TEncodedImage read GetImage write SetImage; |
| 157 |
|
| 158 |
{ Whether the memory management of assigned @link(Image) is automatic. |
| 159 |
See @link(Image) documentation for details. |
| 160 |
|
| 161 |
Note that setting @link(Url) changes @link(Image), |
| 162 |
and changes OwnsImage unconditionally to @true. |
| 163 |
|
| 164 |
Note that setting @link(DrawableImage) changes @link(Image) too, |
| 165 |
and changes OwnsImage to the value present in @link(TDrawableImage.OwnsImage). } |
| 166 |
property OwnsImage: boolean read GetOwnsImage write SetOwnsImage default true; |
| 167 |
|
| 168 |
{ Image used for drawing. Never @nil. |
| 169 |
Always @link(TDrawableImage.Image) equals our @link(Image). |
| 170 |
|
| 171 |
Usually you do not want to change this property. |
| 172 |
You can change @link(Url), or @link(Image), and the drawable |
| 173 |
image will reflect this. |
| 174 |
But there are good use-cases to change the @link(DrawableImage) |
| 175 |
directly: e.g. you can use fast image-to-image |
| 176 |
drawing using @link(TDrawableImage.DrawFrom). |
| 177 |
Or you can set a new instance of TDrawableImage here. |
| 178 |
Be sure to adjust @link(OwnsDrawableImage) as needed. |
| 179 |
|
| 180 |
Note that when OpenGL(ES) context is lost and recreated (which can |
| 181 |
happen at any moment on mobile devices), the contents of this are |
| 182 |
reinitialized from @link(Image). } |
| 183 |
property DrawableImage: TDrawableImage read FDrawableImage write SetDrawableImage; |
| 184 |
|
| 185 |
{ Whether we should automatically free the @link(DrawableImage) instance. |
| 186 |
Note that this is restored to @true when we need to recreate the |
| 187 |
TDrawableImage internally, e.g. when @link(Image) instance changed |
| 188 |
(when even size changed, we have to recreate TDrawableImage). } |
| 189 |
property OwnsDrawableImage: boolean read FOwnsDrawableImage write FOwnsDrawableImage default true; |
| 190 |
|
| 191 |
{ Center of rotation. |
| 192 |
Expressed as a fraction within the drawn ScreenRectangle, |
| 193 |
(0,0) means bottom-left corner, (1,1) means top-right corner. |
| 194 |
Default (0.5,0.5). } |
| 195 |
property RotationCenter: TVector2 read FRotationCenter write SetRotationCenter; |
| 196 |
|
| 197 |
{ If @link(Clip), this is the line equation used to determine whether |
| 198 |
we clip the given pixel. Given a line (A, B, C) and pixel (x, y), |
| 199 |
the pixel is clipped (rejected) if @code(A * x + B * y + C < 0). |
| 200 |
|
| 201 |
The equation is calculated in the coordinates in which image |
| 202 |
X, Y spans from (0, 0) (bottom-left) to (1, 1) (top-right). |
| 203 |
For example ClipLine = (1, 0, -0.5) means that we reject pixels |
| 204 |
where @code(1 * x + 0 * y - 0.5 < 0). In other words, |
| 205 |
we reject pixels where @code(x < 0.5), so we reject the left half of the image. } |
| 206 |
property ClipLine: TVector3 read FClipLine write SetClipLine; |
| 207 |
|
| 208 |
{ Custom GLSL shading code. |
| 209 |
See TDrawableImage.CustomShader for explanation. } |
| 210 |
property CustomShader: TGLSLProgram read FCustomShader write SetCustomShader; |
| 211 |
|
| 212 |
{ In case of @link(TDrawableImage.Draw3x3) the corners |
| 213 |
on screen are scaled by this amount. This is especially useful |
| 214 |
for UI scaling, see @link(TCastleContainer.UIScaling) |
| 215 |
and @link(TCastleUserInterface.UIScale). |
| 216 |
|
| 217 |
This is deliberately not published, because in typical usage |
| 218 |
this value is updated by UI control that owns this, |
| 219 |
before every render. } |
| 220 |
property ScaleCorners: Single |
| 221 |
read FScaleCorners write SetScaleCorners {$ifdef FPC}default 1{$endif}; |
| 222 |
|
| 223 |
{ Color tint of the image. This simply multiplies the image RGBA components, |
| 224 |
just like @link(TDrawableImage.Color). By default this is opaque white, |
| 225 |
which means that image colors are unchanged. } |
| 226 |
property Color: TCastleColor read FColor write SetColor; |
| 227 |
|
| 228 |
{ Image region to which we should limit the display. |
| 229 |
Empty (following @link(TFloatRectangle.Empty)) means using the whole image. } |
| 230 |
property Region: TFloatRectangle read FRegion write SetRegion; |
| 231 |
|
| 232 |
property InternalVisualizeProtectedSides: Boolean |
| 233 |
read FInternalVisualizeProtectedSides write FInternalVisualizeProtectedSides default true; |
| 234 |
|
| 235 |
property ScaleFromUrl: Single read FScaleFromUrl; |
| 236 |
|
| 237 |
{ If you modified the @link(Image) contents |
| 238 |
(colors, or maybe even image size) you need to call this. } |
| 239 |
procedure ImageChanged; |
| 240 |
|
| 241 |
{ Width of drawn image. This is like DrawableImage.Width, but limited by Region. } |
| 242 |
function Width: Cardinal; |
| 243 |
|
| 244 |
{ Height of drawn image. This is like DrawableImage.Height, but limited by Region. } |
| 245 |
function Height: Cardinal; |
| 246 |
|
| 247 |
{ Are both @link(Width) and @link(Height) equal to zero. } |
| 248 |
function Empty: Boolean; |
| 249 |
|
| 250 |
{ Draw the image in given screen area. } |
| 251 |
procedure Draw(const ScreenRectangle: TFloatRectangle); |
| 252 |
|
| 253 |
{ Prepare image for typical drawing from UI. |
| 254 |
|
| 255 |
What it does now: |
| 256 |
|
| 257 |
@orderedList( |
| 258 |
@item(Saves OnChange and Color (to be later restored by DrawUiEnd).) |
| 259 |
|
| 260 |
@item(Clears OnChange (so that changes between DrawUiBegin and DrawUiEnd |
| 261 |
do not cause OnChange, which is typically used to make repaint). |
| 262 |
|
| 263 |
Together with the previous point, this allows the rendering code to easily customize |
| 264 |
color between DrawUiBegin and DrawUiEnd, e.g. doing |
| 265 |
|
| 266 |
@longCode(# |
| 267 |
MyPersistentImage.DrawUiBegin(...); |
| 268 |
MyPersistentImage.Color := MyPersistentImage.Color * Tint; |
| 269 |
MyPersistentImage.Draw(...); |
| 270 |
MyPersistentImage.DrawUiEnd; |
| 271 |
#) |
| 272 |
|
| 273 |
Such change to color is guaranteed to be temporary and internal: |
| 274 |
DrawUiEnd will revert it, OnChange will not report it outside. |
| 275 |
) |
| 276 |
|
| 277 |
@item(Sets ScaleCorners.) |
| 278 |
) |
| 279 |
|
| 280 |
Must be followed by DrawUiEnd. |
| 281 |
} |
| 282 |
procedure DrawUiBegin(const NewScaleCorners: Single); |
| 283 |
|
| 284 |
{ Finish image usage from typical UI drawing. |
| 285 |
|
| 286 |
What it does now: |
| 287 |
|
| 288 |
@orderedList( |
| 289 |
@item(Restores OnChange and Color.) |
| 290 |
) } |
| 291 |
procedure DrawUiEnd; |
| 292 |
|
| 293 |
{ Load again the image from current URL. |
| 294 |
This makes sense to be used when underlying file on disk |
| 295 |
changed, and you want to reload it. |
| 296 |
|
| 297 |
TODO: In case image is shared with other TCastleImagePersistent instances, |
| 298 |
for now this method will not load new image from disk. |
| 299 |
That's because the previous image contents will "survive" in the cache. } |
| 300 |
procedure ReloadUrl; |
| 301 |
published |
| 302 |
{ URL of the image. |
| 303 |
Set this to load a new image, you can set to '' to clear the image. |
| 304 |
|
| 305 |
Changing this also changes @link(Image) and @link(DrawableImage). |
| 306 |
@link(OwnsDrawableImage) and @link(OwnsImage) always change to @true. } |
| 307 |
property Url: String read FUrl write SetUrl; |
| 308 |
|
| 309 |
{ How to treat alpha channel of the assigned image. |
| 310 |
By default, this is acAuto, which means that image contents |
| 311 |
together with current @link(Color) determine how |
| 312 |
the alpha of image is treated (opaque, alpha test, alpha blending). |
| 313 |
Set this to force specific treatment. } |
| 314 |
property AlphaChannel: TAutoAlphaChannel |
| 315 |
read FAlphaChannel write SetAlphaChannel default acAuto; |
| 316 |
|
| 317 |
{ If @true, the @link(DrawableImage) is loaded and freed using a cache. |
| 318 |
This can save memory and loading time a lot, if you reuse the same |
| 319 |
URL in many TCastleImagePersistent instances. |
| 320 |
|
| 321 |
The limitation is |
| 322 |
that you shouldn't change the @link(Image) contents |
| 323 |
(e.g. changing @link(TCastleImage.Colors)) |
| 324 |
or @link(DrawableImage) contents |
| 325 |
(e.g. by using @link(TDrawableImage.RenderToImageBegin)), |
| 326 |
or you will change all shared images. |
| 327 |
|
| 328 |
Note that if you assign @link(DrawableImage) (instead of @link(Url)) |
| 329 |
and we should free it ourselves (because @link(OwnsDrawableImage)) |
| 330 |
then we assume it was @italic(not) loaded through our internal cache. |
| 331 |
|
| 332 |
Note that if you assign @link(Image) (instead of @link(Url)) |
| 333 |
then we assume it was @italic(not) loaded through our internal cache. |
| 334 |
(We make sure to detach the TDrawableImage from cache too, in this case.) |
| 335 |
|
| 336 |
Changing this property reloads the image (if some @link(Url) was set). } |
| 337 |
property Cache: Boolean read FCache write SetCache default true; |
| 338 |
|
| 339 |
{ Is the image scaling mode smooth (bilinear filtering) |
| 340 |
or not (nearest-pixel filtering). |
| 341 |
See @link(TDrawableImage.SmoothScaling). } |
| 342 |
property SmoothScaling: boolean |
| 343 |
read FSmoothScaling write SetSmoothScaling default true; |
| 344 |
|
| 345 |
{ Rotation in radians. Default value 0. } |
| 346 |
property Rotation: Single read FRotation write SetRotation {$ifdef FPC}default 0{$endif}; |
| 347 |
|
| 348 |
{ Clip the image by an arbitrary 2D line defined in @link(ClipLine). } |
| 349 |
property Clip: boolean read FClip write SetClip default false; |
| 350 |
|
| 351 |
property FlipHorizontal: Boolean read FFlipHorizontal write SetFlipHorizontal |
| 352 |
default false; |
| 353 |
property FlipVertical: Boolean read FFlipVertical write SetFlipVertical |
| 354 |
default false; |
| 355 |
|
| 356 |
{ Corners and edges of the image that are protected from scaling distortion |
| 357 |
when @link(TCastleImageControl.Stretch) is used. |
| 358 |
We use the 9-slicing algorithm, |
| 359 |
described at @link(TDrawableImage.Draw3x3) |
| 360 |
and https://en.wikipedia.org/wiki/9-slice_scaling to scale the image. } |
| 361 |
property ProtectedSides: TBorder read FProtectedSides; |
| 362 |
|
| 363 |
{ How are protected sides scaled to match the final drawing area on the screen. } |
| 364 |
property ProtectedSidesScaling: TProtectedSidesScaling |
| 365 |
read FProtectedSidesScaling write SetProtectedSidesScaling default pssDefault; |
| 366 |
|
| 367 |
{$define read_interface_class} |
| 368 |
{$I auto_generated_persistent_vectors/tcastleimagepersistent_persistent_vectors.inc} |
| 369 |
{$undef read_interface_class} |
| 370 |
end; |
| 371 |
{$endif read_interface_type} |
| 372 |
{$endif read_interface} |
| 373 |
|
| 374 |
{$ifdef read_implementation} |
| 375 |
|
| 376 |
constructor TCastleImagePersistent.Create; |
| 377 |
begin |
| 378 |
inherited; |
| 379 |
{ assign default values of properties } |
| 380 |
FCache := true; |
| 381 |
FSmoothScaling := true; |
| 382 |
FRotationCenter := Vector2(0.5, 0.5); |
| 383 |
FColor := White; |
| 384 |
FScaleCorners := 1; |
| 385 |
FScaleFromUrl := 1; |
| 386 |
FInternalVisualizeProtectedSides := true; |
| 387 |
FRegion := TFloatRectangle.Empty; |
| 388 |
|
| 389 |
FProtectedSides := TBorder.Create(nil); |
| 390 |
FProtectedSides.SetSubComponent(true); |
| 391 |
FProtectedSides.OnChange := {$ifdef FPC}@{$endif} ProtectedSidesChange; |
| 392 |
CreateDrawableImage; |
| 393 |
|
| 394 |
FUrlMonitoring.Init({$ifdef FPC}@{$endif} ReloadUrl); |
| 395 |
|
| 396 |
{$define read_implementation_constructor} |
| 397 |
{$I auto_generated_persistent_vectors/tcastleimagepersistent_persistent_vectors.inc} |
| 398 |
{$undef read_implementation_constructor} |
| 399 |
end; |
| 400 |
|
| 401 |
destructor TCastleImagePersistent.Destroy; |
| 402 |
begin |
| 403 |
FreeDrawableImage; |
| 404 |
FreeAndNil(FProtectedSides); |
| 405 |
|
| 406 |
{$define read_implementation_destructor} |
| 407 |
{$I auto_generated_persistent_vectors/tcastleimagepersistent_persistent_vectors.inc} |
| 408 |
{$undef read_implementation_destructor} |
| 409 |
inherited; |
| 410 |
end; |
| 411 |
|
| 412 |
procedure TCastleImagePersistent.BeforeDestruction; |
| 413 |
begin |
| 414 |
FUrlMonitoring.Finish(FUrl); |
| 415 |
inherited; |
| 416 |
end; |
| 417 |
|
| 418 |
procedure TCastleImagePersistent.ProtectedSidesChange(Sender: TObject); |
| 419 |
begin |
| 420 |
DoChange; |
| 421 |
end; |
| 422 |
|
| 423 |
procedure TCastleImagePersistent.CreateDrawableImage; |
| 424 |
begin |
| 425 |
FDrawableImage := TDrawableImage.Create(nil, FSmoothScaling, |
| 426 |
true { default OwnsImage }); |
| 427 |
FOwnsDrawableImage := true; |
| 428 |
FDrawableImageFromCache := false; |
| 429 |
end; |
| 430 |
|
| 431 |
procedure TCastleImagePersistent.FreeDrawableImage; |
| 432 |
begin |
| 433 |
if OwnsDrawableImage then |
| 434 |
begin |
| 435 |
if FDrawableImageFromCache then |
| 436 |
DrawableImageCache.FreeDrawableImage(FDrawableImage) |
| 437 |
else |
| 438 |
FreeAndNil(FDrawableImage); |
| 439 |
end else |
| 440 |
FDrawableImage := nil; |
| 441 |
end; |
| 442 |
|
| 443 |
procedure TCastleImagePersistent.SetUrl(const Value: String); |
| 444 |
|
| 445 |
function GetScaleFromUrl: Single; |
| 446 |
var |
| 447 |
BaseName: String; |
| 448 |
P: Integer; |
| 449 |
Scale: Single; |
| 450 |
begin |
| 451 |
Result := 1; |
| 452 |
BaseName := DeleteURIExt(ExtractURIName(Url)); |
| 453 |
if (Length(BaseName) >= 4) and // BaseName must look like at least '?@4x' |
| 454 |
(BaseName[Length(BaseName)] = 'x') then |
| 455 |
begin |
| 456 |
P := BackPos('@', BaseName); |
| 457 |
if (P > 1) and |
| 458 |
TryStrToFloatDot(CopyPos(BaseName, P + 1, Length(BaseName) - 1), Scale) and |
| 459 |
(Scale > 0) then |
| 460 |
Result := Scale; |
| 461 |
end; |
| 462 |
end; |
| 463 |
|
| 464 |
begin |
| 465 |
if FUrl <> Value then |
| 466 |
begin |
| 467 |
{ We deliberately change URL even when loading image could fail. |
| 468 |
This way URL property is always correctly deserialized in CGE editor, |
| 469 |
even when loading the image failed. } |
| 470 |
FUrlMonitoring.ChangeUrl(FUrl, Value); |
| 471 |
FScaleFromUrl := GetScaleFromUrl; |
| 472 |
|
| 473 |
FreeDrawableImage; |
| 474 |
|
| 475 |
if Value <> '' then |
| 476 |
begin |
| 477 |
try |
| 478 |
if Cache then |
| 479 |
begin |
| 480 |
FDrawableImage := DrawableImageCache.LoadDrawableImage(Value, FSmoothScaling); |
| 481 |
FDrawableImageFromCache := true; |
| 482 |
FOwnsDrawableImage := true; |
| 483 |
Assert(OwnsImage, 'TDrawableImage from cache should always have OwnsImage = true'); |
| 484 |
end else |
| 485 |
begin |
| 486 |
FDrawableImage := TDrawableImage.Create(Value, FSmoothScaling); |
| 487 |
FDrawableImageFromCache := false; |
| 488 |
FOwnsDrawableImage := true; |
| 489 |
Assert(OwnsImage, 'New TDrawableImage should always have OwnsImage = true'); |
| 490 |
end; |
| 491 |
except |
| 492 |
{ If loading file failed, and we're inside CGE editor, |
| 493 |
merely report a warning. This allows deserializing in CGE editor |
| 494 |
designs with broken URLs. } |
| 495 |
on E: Exception do |
| 496 |
begin |
| 497 |
if CastleDesignMode then |
| 498 |
begin |
| 499 |
WritelnWarning('TCastleImagePersistent', 'Failed to load image "%s": %s', |
| 500 |
[UriDisplay(Value), ExceptMessage(E)]); |
| 501 |
CreateDrawableImage; |
| 502 |
Exit; |
| 503 |
end else |
| 504 |
raise; |
| 505 |
end; |
| 506 |
end; |
| 507 |
end else |
| 508 |
CreateDrawableImage; |
| 509 |
|
| 510 |
Assert(OwnsImage); |
| 511 |
Assert(OwnsDrawableImage); |
| 512 |
DoChange; |
| 513 |
end; |
| 514 |
end; |
| 515 |
|
| 516 |
procedure TCastleImagePersistent.SetCache(const Value: Boolean); |
| 517 |
var |
| 518 |
OldUrl: String; |
| 519 |
begin |
| 520 |
if FCache <> Value then |
| 521 |
begin |
| 522 |
FCache := Value; |
| 523 |
if Url <> '' then |
| 524 |
begin |
| 525 |
// free and reload the image, following new Cache value |
| 526 |
OldUrl := Url; |
| 527 |
Url := ''; |
| 528 |
Url := OldUrl; |
| 529 |
DoChange; // this possibly changed rendered contents |
| 530 |
end; |
| 531 |
end; |
| 532 |
end; |
| 533 |
|
| 534 |
procedure TCastleImagePersistent.SetRegion(AValue: TFloatRectangle); |
| 535 |
|
| 536 |
function RectsEqual(const R1, R2: TFloatRectangle): boolean; |
| 537 |
begin |
| 538 |
Result := |
| 539 |
(R1.Left = R2.Left) and |
| 540 |
(R1.Bottom = R2.Bottom) and |
| 541 |
(R1.Width = R2.Width) and |
| 542 |
(R1.Height = R2.Height); |
| 543 |
end; |
| 544 |
|
| 545 |
begin |
| 546 |
{ Testing using "FRegion.PerfectlyEquals" would result in a bit weird |
| 547 |
behavior: |
| 548 |
|
| 549 |
Since multiple values can satisfy IsEmpty=true, |
| 550 |
so setting some values to Region would be ignored |
| 551 |
when both old and new values are IsEmpty=true. |
| 552 |
Although in principle this is correct (since both old and new values |
| 553 |
mean "empty"), but UX is weird: |
| 554 |
|
| 555 |
- set some non-empty Region |
| 556 |
- then change Width to -1 |
| 557 |
- and now you cannot change Height to -1... |
| 558 |
|
| 559 |
Or: |
| 560 |
|
| 561 |
- set Region to TFloadRectangle.Empty (both width and height = -1) |
| 562 |
- try change Width to 100... and it's ignored, because rectangle is still |
| 563 |
empty, because Height remains -1 |
| 564 |
} |
| 565 |
if not RectsEqual(FRegion, AValue) then |
| 566 |
begin |
| 567 |
FRegion := AValue; |
| 568 |
DoChange; |
| 569 |
end; |
| 570 |
end; |
| 571 |
|
| 572 |
function TCastleImagePersistent.GetImage: TEncodedImage; |
| 573 |
begin |
| 574 |
Result := FDrawableImage.Image; |
| 575 |
end; |
| 576 |
|
| 577 |
procedure TCastleImagePersistent.SetImage(const Value: TEncodedImage); |
| 578 |
begin |
| 579 |
if FDrawableImage.Image <> Value then |
| 580 |
begin |
| 581 |
if FDrawableImageFromCache then |
| 582 |
begin |
| 583 |
FreeDrawableImage; |
| 584 |
CreateDrawableImage; |
| 585 |
end; |
| 586 |
Assert(not FDrawableImageFromCache); |
| 587 |
FDrawableImage.Image := Value; |
| 588 |
DoChange; |
| 589 |
end; |
| 590 |
end; |
| 591 |
|
| 592 |
procedure TCastleImagePersistent.SetDrawableImage(const Value: TDrawableImage); |
| 593 |
begin |
| 594 |
if FDrawableImage <> Value then |
| 595 |
begin |
| 596 |
FreeDrawableImage; |
| 597 |
if Value = nil then |
| 598 |
raise Exception.Create('Cannot assign TCastleImagePersistent.DrawableImage to nil'); |
| 599 |
FDrawableImage := Value; |
| 600 |
FDrawableImageFromCache := false; |
| 601 |
DoChange; |
| 602 |
end; |
| 603 |
end; |
| 604 |
|
| 605 |
function TCastleImagePersistent.GetOwnsImage: boolean; |
| 606 |
begin |
| 607 |
Result := FDrawableImage.OwnsImage; |
| 608 |
end; |
| 609 |
|
| 610 |
procedure TCastleImagePersistent.SetOwnsImage(const Value: boolean); |
| 611 |
begin |
| 612 |
{ For all TDrawableImage in DrawableImageCache, |
| 613 |
we keep their OwnsImage as @true, to keep the memory management simple. |
| 614 |
So when changing this, and previous image was from cache, |
| 615 |
we need to lose previous image. } |
| 616 |
|
| 617 |
if FDrawableImage.OwnsImage <> Value then |
| 618 |
begin |
| 619 |
if FDrawableImageFromCache then |
| 620 |
begin |
| 621 |
FreeDrawableImage; |
| 622 |
CreateDrawableImage; |
| 623 |
DoChange; |
| 624 |
end; |
| 625 |
Assert(not FDrawableImageFromCache); |
| 626 |
FDrawableImage.OwnsImage := Value; |
| 627 |
end; |
| 628 |
end; |
| 629 |
|
| 630 |
procedure TCastleImagePersistent.ImageChanged; |
| 631 |
begin |
| 632 |
FDrawableImage.Load(FDrawableImage.Image); |
| 633 |
DoChange; |
| 634 |
end; |
| 635 |
|
| 636 |
procedure TCastleImagePersistent.DoChange; |
| 637 |
begin |
| 638 |
if Assigned(FOnChange) then |
| 639 |
FOnChange(Self); |
| 640 |
end; |
| 641 |
|
| 642 |
function TCastleImagePersistent.Width: Cardinal; |
| 643 |
begin |
| 644 |
if not FRegion.IsEmpty then |
| 645 |
Result := Min(Floor(FRegion.Width), FDrawableImage.Width) |
| 646 |
else |
| 647 |
Result := FDrawableImage.Width; |
| 648 |
end; |
| 649 |
|
| 650 |
function TCastleImagePersistent.Height: Cardinal; |
| 651 |
begin |
| 652 |
if not FRegion.IsEmpty then |
| 653 |
Result := Min(Floor(FRegion.Height), FDrawableImage.Height) |
| 654 |
else |
| 655 |
Result := FDrawableImage.Height; |
| 656 |
end; |
| 657 |
|
| 658 |
function TCastleImagePersistent.Empty: Boolean; |
| 659 |
begin |
| 660 |
Result := (Width = 0) or (Height = 0); |
| 661 |
end; |
| 662 |
|
| 663 |
procedure TCastleImagePersistent.DrawUiBegin(const NewScaleCorners: Single); |
| 664 |
begin |
| 665 |
FSavedOnChange := OnChange; |
| 666 |
FSavedColor := Color; |
| 667 |
FSavedScaleCorners := ScaleCorners; |
| 668 |
|
| 669 |
OnChange := nil; |
| 670 |
|
| 671 |
ScaleCorners := NewScaleCorners; |
| 672 |
end; |
| 673 |
|
| 674 |
procedure TCastleImagePersistent.DrawUiEnd; |
| 675 |
begin |
| 676 |
Color := FSavedColor; |
| 677 |
ScaleCorners := FSavedScaleCorners; |
| 678 |
|
| 679 |
// restore OnChange last, so that no OnChange is emitted between DrawUiBegin / DrawUiEnd |
| 680 |
OnChange := FSavedOnChange; |
| 681 |
end; |
| 682 |
|
| 683 |
procedure TCastleImagePersistent.Draw( |
| 684 |
const ScreenRectangle: TFloatRectangle); |
| 685 |
|
| 686 |
procedure VisualizeProtectedSides; |
| 687 |
|
| 688 |
{ Make A + B <= Max. |
| 689 |
If A + B are too large, decrease them, but preserving A to B ratio. |
| 690 |
In effect e.g. |
| 691 |
A = 10, B = 100, MaxSize = 1 |
| 692 |
-> |
| 693 |
A := 1/11, B = 10/11 |
| 694 |
|
| 695 |
Copied from FitWithin inside TDrawableImage.Draw3x3, to mirror what it does. |
| 696 |
} |
| 697 |
procedure FitWithin(var A, B: Single; const Max: Single); |
| 698 |
begin |
| 699 |
if A + B > Max then |
| 700 |
begin |
| 701 |
A := Max * A / (A + B); |
| 702 |
B := Max * B / (A + B); |
| 703 |
end; |
| 704 |
end; |
| 705 |
|
| 706 |
var |
| 707 |
L, R, B, T: Single; |
| 708 |
begin |
| 709 |
L := ProtectedSides.TotalLeft * DrawableImage.ScaleCorners; |
| 710 |
R := ProtectedSides.TotalRight * DrawableImage.ScaleCorners; |
| 711 |
B := ProtectedSides.TotalBottom * DrawableImage.ScaleCorners; |
| 712 |
T := ProtectedSides.TotalTop * DrawableImage.ScaleCorners; |
| 713 |
|
| 714 |
FitWithin(L, R, ScreenRectangle.Width); |
| 715 |
FitWithin(B, T, ScreenRectangle.Height); |
| 716 |
|
| 717 |
DrawPrimitive2D(pmLines, [ |
| 718 |
// vertical lines |
| 719 |
Vector2(ScreenRectangle.Left + L, ScreenRectangle.Bottom), |
| 720 |
Vector2(ScreenRectangle.Left + L, ScreenRectangle.Top), |
| 721 |
Vector2(ScreenRectangle.Right - R, ScreenRectangle.Bottom), |
| 722 |
Vector2(ScreenRectangle.Right - R, ScreenRectangle.Top), |
| 723 |
// horizontal lines |
| 724 |
Vector2(ScreenRectangle.Left , ScreenRectangle.Bottom + B), |
| 725 |
Vector2(ScreenRectangle.Right, ScreenRectangle.Bottom + B), |
| 726 |
Vector2(ScreenRectangle.Left , ScreenRectangle.Top - T), |
| 727 |
Vector2(ScreenRectangle.Right, ScreenRectangle.Top - T) |
| 728 |
], Vector4(1, 1, 0, 0.5)); |
| 729 |
end; |
| 730 |
|
| 731 |
function ImageRect: TFloatRectangle; |
| 732 |
begin |
| 733 |
if not FRegion.IsEmpty then |
| 734 |
Result := FRegion |
| 735 |
else |
| 736 |
Result := FloatRectangle(FDrawableImage.Rect); |
| 737 |
end; |
| 738 |
|
| 739 |
var |
| 740 |
ProtectedSidesVector: TVector4; |
| 741 |
begin |
| 742 |
if (DrawableImage.Width = 0) or (DrawableImage.Height = 0) then Exit; |
| 743 |
|
| 744 |
{ All the DrawableImage properties must be set here, |
| 745 |
in case muliple TCastleImagePersistent refer to the same |
| 746 |
DrawableImage instance through the cache. |
| 747 |
Fortunately, almost all TDrawableImage properties have zero cost |
| 748 |
of change. } |
| 749 |
DrawableImage.SmoothScaling := FSmoothScaling; |
| 750 |
DrawableImage.Clip := FClip; |
| 751 |
DrawableImage.ClipLine := FClipLine; |
| 752 |
DrawableImage.Rotation := FRotation; |
| 753 |
DrawableImage.RotationCenter := FRotationCenter; |
| 754 |
DrawableImage.Alpha := FAlphaChannel; |
| 755 |
DrawableImage.Color := FColor; |
| 756 |
DrawableImage.CustomShader := FCustomShader; |
| 757 |
|
| 758 |
case ProtectedSidesScaling of |
| 759 |
pssDefault : DrawableImage.ScaleCorners := FScaleCorners; |
| 760 |
pssMatchHeight: DrawableImage.ScaleCorners := ScreenRectangle.Height / FDrawableImage.Height; |
| 761 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 762 |
else raise EInternalError.Create('ProtectedSidesScaling?'); |
| 763 |
{$endif} |
| 764 |
end; |
| 765 |
|
| 766 |
if not FProtectedSides.Exists then |
| 767 |
begin |
| 768 |
if FlipHorizontal or FlipVertical then |
| 769 |
FDrawableImage.DrawFlipped(ScreenRectangle, ImageRect, FlipHorizontal, FlipVertical) |
| 770 |
else |
| 771 |
FDrawableImage.Draw(ScreenRectangle, ImageRect); |
| 772 |
end else |
| 773 |
begin |
| 774 |
// TODO: FlipHorizontal, FlipVertical ignored in this case |
| 775 |
ProtectedSidesVector := Vector4( |
| 776 |
ProtectedSides.TotalTop, |
| 777 |
ProtectedSides.TotalRight, |
| 778 |
ProtectedSides.TotalBottom, |
| 779 |
ProtectedSides.TotalLeft |
| 780 |
); |
| 781 |
|
| 782 |
FDrawableImage.Draw3x3(ScreenRectangle, ProtectedSidesVector, FRegion); |
| 783 |
|
| 784 |
if CastleDesignMode and InternalVisualizeProtectedSides and ProtectedSides.Exists then |
| 785 |
VisualizeProtectedSides; |
| 786 |
end; |
| 787 |
end; |
| 788 |
|
| 789 |
procedure TCastleImagePersistent.SetSmoothScaling(const Value: boolean); |
| 790 |
begin |
| 791 |
if FSmoothScaling <> Value then |
| 792 |
begin |
| 793 |
FSmoothScaling := Value; |
| 794 |
DrawableImage.SmoothScaling := Value; |
| 795 |
DoChange; |
| 796 |
end; |
| 797 |
end; |
| 798 |
|
| 799 |
procedure TCastleImagePersistent.SetClip(const Value: Boolean); |
| 800 |
begin |
| 801 |
if FClip <> Value then |
| 802 |
begin |
| 803 |
FClip := Value; |
| 804 |
DoChange; |
| 805 |
end; |
| 806 |
end; |
| 807 |
|
| 808 |
procedure TCastleImagePersistent.SetClipLine(const Value: TVector3); |
| 809 |
begin |
| 810 |
if not TVector3.PerfectlyEquals(FClipLine, Value) then |
| 811 |
begin |
| 812 |
FClipLine := Value; |
| 813 |
DoChange; |
| 814 |
end; |
| 815 |
end; |
| 816 |
|
| 817 |
procedure TCastleImagePersistent.SetRotation(const Value: Single); |
| 818 |
begin |
| 819 |
if FRotation <> Value then |
| 820 |
begin |
| 821 |
FRotation := Value; |
| 822 |
DoChange; |
| 823 |
end; |
| 824 |
end; |
| 825 |
|
| 826 |
procedure TCastleImagePersistent.SetRotationCenter(const Value: TVector2); |
| 827 |
begin |
| 828 |
if not TVector2.PerfectlyEquals(FRotationCenter, Value) then |
| 829 |
begin |
| 830 |
FRotationCenter := Value; |
| 831 |
DoChange; |
| 832 |
end; |
| 833 |
end; |
| 834 |
|
| 835 |
procedure TCastleImagePersistent.SetAlphaChannel(const Value: TAutoAlphaChannel); |
| 836 |
begin |
| 837 |
if FAlphaChannel <> Value then |
| 838 |
begin |
| 839 |
FAlphaChannel := Value; |
| 840 |
DoChange; |
| 841 |
end; |
| 842 |
end; |
| 843 |
|
| 844 |
procedure TCastleImagePersistent.SetColor(const Value: TCastleColor); |
| 845 |
begin |
| 846 |
if not TCastleColor.PerfectlyEquals(FColor, Value) then |
| 847 |
begin |
| 848 |
FColor := Value; |
| 849 |
DoChange; |
| 850 |
end; |
| 851 |
end; |
| 852 |
|
| 853 |
procedure TCastleImagePersistent.SetCustomShader(const Value: TGLSLProgram); |
| 854 |
begin |
| 855 |
if FCustomShader <> Value then |
| 856 |
begin |
| 857 |
FCustomShader := Value; |
| 858 |
DoChange; |
| 859 |
end; |
| 860 |
end; |
| 861 |
|
| 862 |
procedure TCastleImagePersistent.SetScaleCorners(const Value: Single); |
| 863 |
begin |
| 864 |
if FScaleCorners <> Value then |
| 865 |
begin |
| 866 |
FScaleCorners := Value; |
| 867 |
DoChange; |
| 868 |
end; |
| 869 |
end; |
| 870 |
|
| 871 |
procedure TCastleImagePersistent.SetFlipVertical(const Value: Boolean); |
| 872 |
begin |
| 873 |
if FFlipVertical <> Value then |
| 874 |
begin |
| 875 |
FFlipVertical := Value; |
| 876 |
DoChange; |
| 877 |
end; |
| 878 |
end; |
| 879 |
|
| 880 |
procedure TCastleImagePersistent.SetFlipHorizontal(const Value: Boolean); |
| 881 |
begin |
| 882 |
if FFlipHorizontal <> Value then |
| 883 |
begin |
| 884 |
FFlipHorizontal := Value; |
| 885 |
DoChange; |
| 886 |
end; |
| 887 |
end; |
| 888 |
|
| 889 |
procedure TCastleImagePersistent.SetProtectedSidesScaling(const Value: TProtectedSidesScaling); |
| 890 |
begin |
| 891 |
if FProtectedSidesScaling <> Value then |
| 892 |
begin |
| 893 |
FProtectedSidesScaling := Value; |
| 894 |
DoChange; |
| 895 |
end; |
| 896 |
end; |
| 897 |
|
| 898 |
procedure TCastleImagePersistent.ReloadUrl; |
| 899 |
var |
| 900 |
SavedUrl: String; |
| 901 |
begin |
| 902 |
SavedUrl := Url; |
| 903 |
Url := ''; |
| 904 |
Url := SavedUrl; |
| 905 |
end; |
| 906 |
|
| 907 |
{$define read_implementation_methods} |
| 908 |
{$I auto_generated_persistent_vectors/tcastleimagepersistent_persistent_vectors.inc} |
| 909 |
{$undef read_implementation_methods} |
| 910 |
|
| 911 |
{$endif read_implementation} |