| 1 |
{%MainUnit castletiledmap.pas} |
| 2 |
{ |
| 3 |
Copyright 2015-2018 Tomasz Wojtyś, 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 |
{ Display a map created in Tiled (https://www.mapeditor.org/) (TCastleTiledMapControl class). } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ Display a map created in Tiled (https://www.mapeditor.org/). } |
| 23 |
TCastleTiledMapControl = class(TCastleUserInterface) |
| 24 |
strict private |
| 25 |
FMap: TCastleTiledMapData; |
| 26 |
FUrl: String; |
| 27 |
FObjectsVisible: Boolean; |
| 28 |
FOrigin: TVector2; |
| 29 |
FScale: Single; |
| 30 |
FSmoothScaling, FSmoothScalingSafeBorder: Boolean; |
| 31 |
procedure RenderObjects; |
| 32 |
procedure LoadTilesetsImages; |
| 33 |
{ Free images created by LoadTilesetsImages. } |
| 34 |
procedure FreeTilesetsImages; |
| 35 |
{ Free everything created by SetUrl. } |
| 36 |
procedure Close; |
| 37 |
procedure SetUrl(const Value: String); |
| 38 |
procedure SetOrigin(const Value: TVector2); |
| 39 |
procedure SetScale(const Value: Single); |
| 40 |
procedure SetSmoothScaling(const Value: Boolean); |
| 41 |
procedure SetSmoothScalingSafeBorder(const Value: Boolean); |
| 42 |
public |
| 43 |
constructor Create(AOwner: TComponent); override; |
| 44 |
destructor Destroy; override; |
| 45 |
procedure Render; override; |
| 46 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 47 |
|
| 48 |
{ Load given TMX file to memory and creates structures. } |
| 49 |
procedure LoadFromFile(const AUrl: String); deprecated 'set Url instead of calling LoadFromFile'; |
| 50 |
|
| 51 |
{ Origin determines the map position at control's left-bottom corner. |
| 52 |
Increasing Origin.X moves visually the map to the left, |
| 53 |
increasing Origin.Y moves it down. } |
| 54 |
property Origin: TVector2 read FOrigin write SetOrigin; |
| 55 |
|
| 56 |
{ Currently loaded map (from @link(Url)), if any. |
| 57 |
@nil if no map is loaded (when @link(Url) is empty). Read-only. } |
| 58 |
property Map: TCastleTiledMapData read FMap; |
| 59 |
|
| 60 |
{ Detect tile under given position. |
| 61 |
|
| 62 |
The interpretation of Position depends on ContainerCoordinates, |
| 63 |
and is similar to e.g. @link(TCastleViewport.PositionToCameraPlane): |
| 64 |
|
| 65 |
@unorderedList( |
| 66 |
@item(When ContainerCoordinates = @true, |
| 67 |
then Position is relative to the whole container |
| 68 |
(like TCastleWindow or TCastleControl). |
| 69 |
|
| 70 |
And it is expressed in real device coordinates, |
| 71 |
just like @link(TInputPressRelease.Position) |
| 72 |
when mouse is being clicked, or like @link(TInputMotion.Position) |
| 73 |
when mouse is moved. |
| 74 |
) |
| 75 |
|
| 76 |
@item(When ContainerCoordinates = @false, |
| 77 |
then Position is relative to this UI control. |
| 78 |
|
| 79 |
And it is expressed in coordinates after UI scaling. |
| 80 |
IOW, if the size of this control is @link(Width) = 100, |
| 81 |
then Position.X between 0 and 100 reflects the visible range of this control. |
| 82 |
) |
| 83 |
) |
| 84 |
|
| 85 |
Note that you can query the positions that are outside of this UI control, |
| 86 |
or even outside of the container. |
| 87 |
They will return correct map tiles, as if the map was rendered there. |
| 88 |
If you want to avoid this, you can e.g. check whether Position |
| 89 |
is inside the @link(RenderRect) (if it's a position in container coordinates, |
| 90 |
with ContainerCoordinates = @true). |
| 91 |
|
| 92 |
This method returns @false if the position is outside of the map. |
| 93 |
Valid map tiles are from (0, 0) (lower-left) to |
| 94 |
(@link(TCastleTiledMapData.Width) - 1, @link(TCastleTiledMapData.Height) - 1) (upper-right). } |
| 95 |
function PositionToTile(const Position: TVector2; const ContainerCoordinates: Boolean; |
| 96 |
out TilePosition: TVector2Integer): Boolean; |
| 97 |
|
| 98 |
{ Rectangle where the given map tile is rendered. |
| 99 |
Useful to render something over a TCastleTiledMapControl, |
| 100 |
e.g. a pawn in a game. |
| 101 |
|
| 102 |
In some way, this method is an inverse of the @link(PositionToTile) method. |
| 103 |
It converts map (tile) coordinates back into coordinates useful to display |
| 104 |
something. |
| 105 |
The ContainerCoordinates value determines the coordinate system of the resulting |
| 106 |
rectangle. It's meaning is similar to the ContainerCoordinates parameter |
| 107 |
of the @link(PositionToTile) method. |
| 108 |
|
| 109 |
@unorderedList( |
| 110 |
@item(When ContainerCoordinates = @true, |
| 111 |
the resulting rectangle is relative to the whole container |
| 112 |
(like TCastleWindow or TCastleControl). |
| 113 |
And is expressed in real device coordinates. |
| 114 |
|
| 115 |
These are the same coordinates as expressed by @link(RenderRect) |
| 116 |
method. You can pass the resulting rectangle straight |
| 117 |
to @link(DrawRectangle), or @link(TDrawableImage.Draw), or @link(TSprite.Draw), |
| 118 |
to draw something over this tile. |
| 119 |
) |
| 120 |
|
| 121 |
@item(When ContainerCoordinates = @false, |
| 122 |
the resulting rectangle is specified in local coordinates of this |
| 123 |
control, and is expressed in coordinates after UI scaling. |
| 124 |
|
| 125 |
These are the same coordinates as used by children controls (TCastleUserInterface) |
| 126 |
or the TCastleTiledMapControl. |
| 127 |
E.g. you can insert a TCastleImageControl as a child of |
| 128 |
TCastleTiledMapControl, and set it's position/size this way, |
| 129 |
to draw an image over this tile. |
| 130 |
) |
| 131 |
) |
| 132 |
|
| 133 |
This method works for any TilePosition, even outside of valid map positions. |
| 134 |
Returns TFloatRectangle.Empty is @link(Map) not loaded (@link(Url) not set). |
| 135 |
|
| 136 |
Some maps may use tilesets in which image size is different |
| 137 |
than map tile size. That is, TCastleTiledMapData.TTileset.TileWidth may be different |
| 138 |
than TCastleTiledMapData.TileWidth for some tilesets. |
| 139 |
The simpler version of this method, |
| 140 |
without ForceTileWidth/Height, just returns the size of the map tile looking at |
| 141 |
(TCastleTiledMapData.TileWidth, TCastleTiledMapData.TileHeight), since it doesn't know |
| 142 |
the tileset you use. |
| 143 |
|
| 144 |
You can pass ForceTileWidth and ForceTileHeight to set particular size. |
| 145 |
} |
| 146 |
function TileRectangle(const TilePosition: TVector2Integer; |
| 147 |
const ContainerCoordinates: Boolean): TFloatRectangle; overload; |
| 148 |
function TileRectangle(const TilePosition: TVector2Integer; |
| 149 |
const ContainerCoordinates: Boolean; |
| 150 |
const ForceTileWidth, ForceTileHeight: Single): TFloatRectangle; overload; |
| 151 |
published |
| 152 |
property ClipChildren default true; |
| 153 |
|
| 154 |
{ Url (or just filename) of the Tiled map file (TMX). } |
| 155 |
property Url: String read FUrl write SetUrl; |
| 156 |
|
| 157 |
property Scale: Single read FScale write SetScale {$ifdef FPC}default 1{$endif}; |
| 158 |
|
| 159 |
{ Show objects like polylines and points on the map. } |
| 160 |
property ObjectsVisible: Boolean read FObjectsVisible write FObjectsVisible default true; |
| 161 |
|
| 162 |
{ How are images scaled (because of UI scaling, because of @link(Scale)). |
| 163 |
@true means we use nice "bilinear" filter. |
| 164 |
@false means we use pixelated "nearest" filter. |
| 165 |
|
| 166 |
Beware that using @true, and scaling the map, requires preparing |
| 167 |
the tileset (atlas of tile images) in a proper way. The space |
| 168 |
around each tile must have at least 1-pixel margin |
| 169 |
filled with the same color and alpha ("color bleeding"), |
| 170 |
otherwise bad colors will creep in because of bilinear filtering. |
| 171 |
Alternatively, you can use @link(SmoothScalingSafeBorder). |
| 172 |
} |
| 173 |
property SmoothScaling: Boolean read FSmoothScaling write SetSmoothScaling |
| 174 |
default false; |
| 175 |
|
| 176 |
{ In case of @link(SmoothScaling), |
| 177 |
prevent the color outside of rendered frame from creeping in, |
| 178 |
by stretching the rendered frame a bit. |
| 179 |
See @link(TSprite.SmoothScalingSafeBorder). |
| 180 |
|
| 181 |
It is better to prepare the sprite (tileset) images |
| 182 |
with some spacing between tiles, and color bleeding, |
| 183 |
instead of using this property. |
| 184 |
This property workarounds the fact that some sprite (tileset) images |
| 185 |
lack proper spacing and color bleeding, but it distorts the image |
| 186 |
a bit, so it's no longer pixel-perfect. } |
| 187 |
property SmoothScalingSafeBorder: Boolean read FSmoothScalingSafeBorder write SetSmoothScalingSafeBorder |
| 188 |
default false; |
| 189 |
|
| 190 |
{$define read_interface_class} |
| 191 |
{$I auto_generated_persistent_vectors/tcastletiledmapcontrol_persistent_vectors.inc} |
| 192 |
{$undef read_interface_class} |
| 193 |
end {$ifdef FPC}deprecated 'use TCastleTiledMap in a viewport'{$endif}; |
| 194 |
|
| 195 |
{$endif read_interface} |
| 196 |
|
| 197 |
{$ifdef read_implementation} |
| 198 |
|
| 199 |
constructor TCastleTiledMapControl.Create(AOwner: TComponent); |
| 200 |
begin |
| 201 |
inherited Create(AOwner); |
| 202 |
ClipChildren := true; |
| 203 |
FObjectsVisible := true; |
| 204 |
FScale := 1; |
| 205 |
FSmoothScaling := false; |
| 206 |
FSmoothScalingSafeBorder := false; |
| 207 |
|
| 208 |
{$define read_implementation_constructor} |
| 209 |
{$I auto_generated_persistent_vectors/tcastletiledmapcontrol_persistent_vectors.inc} |
| 210 |
{$undef read_implementation_constructor} |
| 211 |
end; |
| 212 |
|
| 213 |
destructor TCastleTiledMapControl.Destroy; |
| 214 |
begin |
| 215 |
Close; |
| 216 |
|
| 217 |
{$define read_implementation_destructor} |
| 218 |
{$I auto_generated_persistent_vectors/tcastletiledmapcontrol_persistent_vectors.inc} |
| 219 |
{$undef read_implementation_destructor} |
| 220 |
inherited; |
| 221 |
end; |
| 222 |
|
| 223 |
procedure TCastleTiledMapControl.RenderObjects; |
| 224 |
var |
| 225 |
Lay: TCastleTiledMapData.TLayer; // ObjectGroupLayer |
| 226 |
TiledObj: TCastleTiledMapData.TTiledObject; // one TiledObject |
| 227 |
TiledObjPoints: array of TVector2; // holds points of TiledObject as array of TVector2 |
| 228 |
|
| 229 |
{ Transform point from Tiled position to our render position. |
| 230 |
This is similar to TCastleTiledMapControl.TileRectangle, but works only for simple point. } |
| 231 |
function RenderPosition(V: TVector2; const ContainerCoordinates: Boolean = true): TVector2; |
| 232 |
begin |
| 233 |
V.Y := Map.Height * Map.TileHeight - V.Y; // convert to be relative to bottom |
| 234 |
// Apply Origin and Scale |
| 235 |
Result := (V - Origin) * Scale; |
| 236 |
if ContainerCoordinates then |
| 237 |
begin |
| 238 |
// Apply UI scaling and anchors to get position on the final container |
| 239 |
Result := LocalToContainerPosition(Result); |
| 240 |
end; |
| 241 |
end; |
| 242 |
|
| 243 |
{ Converts all TVector2's from a TVector2List to and array of TVector2. |
| 244 |
ArrIndex is the index of a TVector2 in the array, ListIndex is the TVector2 |
| 245 |
in the list. } |
| 246 |
procedure ConvertAndScalePoints(const ArrIndex, ListIndex: Cardinal); |
| 247 |
begin |
| 248 |
TiledObjPoints[ArrIndex] := RenderPosition( |
| 249 |
TiledObj.Points.Items[ListIndex] + TiledObj.Position + Lay.Offset); |
| 250 |
end; |
| 251 |
|
| 252 |
{ Prepare and render TiledObject polyline and polygon. } |
| 253 |
procedure RenderPolylinePolygon; |
| 254 |
var |
| 255 |
I: Cardinal; |
| 256 |
begin |
| 257 |
{ Prepare points for DrawPrimitive2D function. Convert from vector list |
| 258 |
to array of vectors. } |
| 259 |
SetLength(TiledObjPoints, TiledObj.Points.Count); |
| 260 |
for I := 0 to High(TiledObjPoints) do |
| 261 |
begin |
| 262 |
ConvertAndScalePoints(I, I); |
| 263 |
end; |
| 264 |
|
| 265 |
{ If TiledObject is a polygon, for rendering of last line, a point has to be |
| 266 |
added manually to close the polygon. } |
| 267 |
if TiledObj.Primitive = topPolygon then |
| 268 |
begin |
| 269 |
SetLength(TiledObjPoints, TiledObj.Points.Count + 1); |
| 270 |
ConvertAndScalePoints(High(TiledObjPoints), 0); |
| 271 |
end; |
| 272 |
|
| 273 |
{ Draw polygon or polyline TiledObject. } |
| 274 |
DrawPrimitive2D(pmLineStrip, TiledObjPoints, Orange, bsSrcAlpha, |
| 275 |
bdOneMinusSrcAlpha, false, 1, 1); |
| 276 |
end; |
| 277 |
|
| 278 |
procedure RenderPoint; |
| 279 |
var |
| 280 |
TiledObjPoint: array [0..0] of TVector2; // array with 1 element |
| 281 |
begin |
| 282 |
{ Prepare point. } |
| 283 |
TiledObjPoint[0] := RenderPosition(Lay.Offset + TiledObj.Position); |
| 284 |
|
| 285 |
{ Draw point TiledObject. } |
| 286 |
DrawPrimitive2D(pmPoints, TiledObjPoint, Orange, bsSrcAlpha, |
| 287 |
bdOneMinusSrcAlpha, false, 1, 1); |
| 288 |
end; |
| 289 |
|
| 290 |
procedure RenderRectangle; |
| 291 |
var |
| 292 |
TiledObjRect: TFloatRectangle; |
| 293 |
LeftBottom, RightTop: TVector2; |
| 294 |
begin |
| 295 |
{ Prepare rectangle. |
| 296 |
The "Lay.Offset + TiledObj.Position" indicates left-top corner of the rectangle in Tiled. } |
| 297 |
LeftBottom := RenderPosition(Lay.Offset + TiledObj.Position + Vector2(0, TiledObj.Height)); |
| 298 |
RightTop := RenderPosition(Lay.Offset + TiledObj.Position + Vector2(TiledObj.Width, 0)); |
| 299 |
TiledObjRect := FloatRectangle( |
| 300 |
LeftBottom.X, |
| 301 |
LeftBottom.Y, |
| 302 |
RightTop.X - LeftBottom.X, |
| 303 |
RightTop.Y - LeftBottom.Y |
| 304 |
); |
| 305 |
|
| 306 |
{ Draw rectangle TiledObject. } |
| 307 |
DrawRectangleOutline(TiledObjRect, Orange, 1, bsSrcAlpha, |
| 308 |
bdOneMinusSrcAlpha, false); |
| 309 |
end; |
| 310 |
|
| 311 |
begin |
| 312 |
{ Object groups. } |
| 313 |
for Lay in Map.Layers do |
| 314 |
begin |
| 315 |
if (Lay is TCastleTiledMapData.TObjectGroupLayer) and Lay.Visible then |
| 316 |
begin |
| 317 |
for TiledObj in (Lay as TCastleTiledMapData.TObjectGroupLayer).Objects do |
| 318 |
begin |
| 319 |
if TiledObj.Visible then |
| 320 |
begin |
| 321 |
case TiledObj.Primitive of |
| 322 |
topPolyline, topPolygon: RenderPolylinePolygon; |
| 323 |
topPoint: RenderPoint; |
| 324 |
topRectangle: RenderRectangle; |
| 325 |
else ; |
| 326 |
// TODO: handle ellipse |
| 327 |
end; |
| 328 |
end; |
| 329 |
end; |
| 330 |
end; |
| 331 |
end; |
| 332 |
// TODO: when to draw TImageLayer? |
| 333 |
end; |
| 334 |
|
| 335 |
procedure TCastleTiledMapControl.LoadTilesetsImages; |
| 336 |
var |
| 337 |
Tileset: TCastleTiledMapData.TTileset; |
| 338 |
{$warnings off} // using deprecated in deprecated |
| 339 |
Spr: TSprite; |
| 340 |
{$warnings on} |
| 341 |
begin |
| 342 |
if Map <> nil then |
| 343 |
for Tileset in Map.Tilesets do |
| 344 |
begin |
| 345 |
if (Tileset.TileCount = 0) or (Tileset.Columns = 0) then |
| 346 |
begin |
| 347 |
WriteLnWarning('Empty Tileset'); |
| 348 |
Continue; |
| 349 |
end; |
| 350 |
{$warnings off} // using deprecated in deprecated |
| 351 |
Spr := TSprite.Create( |
| 352 |
Tileset.Image.Url, Tileset.TileCount, |
| 353 |
Tileset.Columns, Tileset.TileCount div Tileset.Columns, |
| 354 |
SmoothScaling, false); |
| 355 |
{$warnings on} |
| 356 |
{ Note that we use here Tileset.TileWidth/Height, |
| 357 |
which may difer from Map.TileWidth/Height. |
| 358 |
This is important in case of e.g. "perspective_walls.tmx" test |
| 359 |
from https://github.com/bjorn/tiled/tree/master/examples . } |
| 360 |
Spr.DrawingWidth := Tileset.TileWidth; |
| 361 |
Spr.DrawingHeight := Tileset.TileHeight; |
| 362 |
Spr.FrameWidth := Tileset.TileWidth; |
| 363 |
Spr.FrameHeight := Tileset.TileHeight; |
| 364 |
Spr.LeftMargin := Tileset.Margin; |
| 365 |
Spr.TopMargin := Tileset.Margin; |
| 366 |
Spr.HorizontalSpacing := Tileset.Spacing; |
| 367 |
Spr.VerticalSpacing := Tileset.Spacing; |
| 368 |
Spr.VerticalSpacingBottom := true; |
| 369 |
Spr.SmoothScalingSafeBorder := SmoothScalingSafeBorder; |
| 370 |
Tileset.RendererData := Spr; |
| 371 |
end; |
| 372 |
end; |
| 373 |
|
| 374 |
procedure TCastleTiledMapControl.FreeTilesetsImages; |
| 375 |
var |
| 376 |
Tileset: TCastleTiledMapData.TTileset; |
| 377 |
begin |
| 378 |
if Map <> nil then |
| 379 |
for Tileset in Map.Tilesets do |
| 380 |
FreeAndNil(Tileset.RendererData); |
| 381 |
end; |
| 382 |
|
| 383 |
procedure TCastleTiledMapControl.Close; |
| 384 |
begin |
| 385 |
FreeTilesetsImages; |
| 386 |
FreeAndNil(FMap); |
| 387 |
end; |
| 388 |
|
| 389 |
procedure TCastleTiledMapControl.LoadFromFile(const AUrl: String); |
| 390 |
begin |
| 391 |
Url := AUrl; |
| 392 |
end; |
| 393 |
|
| 394 |
procedure TCastleTiledMapControl.SetUrl(const Value: String); |
| 395 |
begin |
| 396 |
if FUrl <> Value then |
| 397 |
begin |
| 398 |
FUrl := Value; |
| 399 |
Close; |
| 400 |
if Value <> '' then |
| 401 |
FMap := TCastleTiledMapData.Create(Value); |
| 402 |
LoadTilesetsImages; |
| 403 |
end; |
| 404 |
end; |
| 405 |
|
| 406 |
procedure TCastleTiledMapControl.Render; |
| 407 |
var |
| 408 |
RR: TFloatRectangle; |
| 409 |
|
| 410 |
{ Return TSprite to render this tile, |
| 411 |
with sprite's Frame, HorizontalFlip, VerticalFlip, DiagonalFlip already set. |
| 412 |
May return @nil if nothing to render. } |
| 413 |
function TileSprite(const Layer: TCastleTiledMapData.TLayer; const X, Y: Integer): TSprite; |
| 414 |
var |
| 415 |
Tileset: TCastleTiledMapData.TTileset; |
| 416 |
Frame: Integer; |
| 417 |
HorizontalFlip, VerticalFlip, DiagonalFlip: Boolean; |
| 418 |
begin |
| 419 |
Result := nil; |
| 420 |
|
| 421 |
if Map.TileRenderData(Vector2Integer(X, Y), Layer, |
| 422 |
Tileset, Frame, HorizontalFlip, VerticalFlip, DiagonalFlip) then |
| 423 |
begin |
| 424 |
{$warnings off} // using deprecated in deprecated |
| 425 |
Result := Tileset.RendererData as TSprite; |
| 426 |
{$warnings on} |
| 427 |
if Result = nil then Exit; |
| 428 |
|
| 429 |
Result.Frame := Frame; |
| 430 |
Result.HorizontalFlip := HorizontalFlip; |
| 431 |
Result.VerticalFlip := VerticalFlip; |
| 432 |
{ TODO: this makes diagonal flip too late, effectively *after* HorizontalFlip and VerticalFlip. |
| 433 |
Tiled expects (https://doc.mapeditor.org/en/stable/reference/tmx-map-format/): |
| 434 |
|
| 435 |
- The diagonal flip (x/y axis swap) is done first, |
| 436 |
- followed by the horizontal |
| 437 |
- and vertical flips. |
| 438 |
} |
| 439 |
if DiagonalFlip then |
| 440 |
Result.Rotation := Pi / 2 |
| 441 |
else |
| 442 |
Result.Rotation := 0; |
| 443 |
end; |
| 444 |
end; |
| 445 |
|
| 446 |
procedure RenderTile(const Layer: TCastleTiledMapData.TLayer; const X, Y: Integer); |
| 447 |
var |
| 448 |
{$warnings off} // using deprecated in deprecated |
| 449 |
Spr: TSprite; |
| 450 |
{$warnings on} |
| 451 |
SpriteRenderRect: TFloatRectangle; |
| 452 |
begin |
| 453 |
Spr := TileSprite(Layer, X, Y); |
| 454 |
if Spr = nil then Exit; |
| 455 |
|
| 456 |
// TODO: use Layer opacity |
| 457 |
// TODO: use Layer offset |
| 458 |
|
| 459 |
SpriteRenderRect := TileRectangle(Vector2Integer(X, Y), true, |
| 460 |
Spr.DrawingWidth, // corresponds to Tileset.TileWidth |
| 461 |
Spr.DrawingHeight // corresponds to Tileset.TileHeight |
| 462 |
); |
| 463 |
|
| 464 |
{ Don't render off-screen images, this makes big difference for huge maps where |
| 465 |
most content if off-screen, |
| 466 |
testcase: examples/tiled/map_viewer/data/maps/desert_big.tmx . |
| 467 |
We rely on ClipChildren = true here: Tiled map rendering is always |
| 468 |
limited to RR. } |
| 469 |
if RR.Collides(SpriteRenderRect) then |
| 470 |
Spr.Draw(SpriteRenderRect); |
| 471 |
end; |
| 472 |
|
| 473 |
var |
| 474 |
X, Y: Integer; |
| 475 |
Layer: TCastleTiledMapData.TLayer; |
| 476 |
begin |
| 477 |
inherited Render; |
| 478 |
if Map = nil then Exit; |
| 479 |
|
| 480 |
{ This routine benefits a lot from TDrawableImage automatic batching, |
| 481 |
testcase: examples/tiled/map_viewer/data/maps/desert_big.tmx } |
| 482 |
TDrawableImage.BatchingBegin; |
| 483 |
try |
| 484 |
RR := RenderRect; |
| 485 |
|
| 486 |
if Map.BackgroundColor[3] <> 0 then |
| 487 |
DrawRectangle(RR, Map.BackgroundColor); |
| 488 |
|
| 489 |
for Layer in Map.Layers do |
| 490 |
begin |
| 491 |
if not Layer.Visible then Continue; |
| 492 |
if (Layer is TCastleTiledMapData.TObjectGroupLayer) or // TODO: See below, it would be better to render TObjectGroupLayer here, instead of special RenderObjects call |
| 493 |
(Layer is TCastleTiledMapData.TImageLayer) then |
| 494 |
Continue; |
| 495 |
|
| 496 |
// TODO: use Map.RenderOrder |
| 497 |
|
| 498 |
for Y := Map.Height - 1 downto 0 do |
| 499 |
for X := 0 to Map.Width - 1 do |
| 500 |
RenderTile(Layer, X, Y); |
| 501 |
end; |
| 502 |
|
| 503 |
{ TODO: This is Redundant? Better to render the layer at cond. above at |
| 504 |
(Layer is TCastleTiledMapData.TObjectGroupLayer). Would make redundant property |
| 505 |
ObjectsVisible obsolete. This property is not found in the tmx tile specs., |
| 506 |
but covered by TLayer.Visible already. See: |
| 507 |
https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#objectgroup } |
| 508 |
if FObjectsVisible then |
| 509 |
RenderObjects; |
| 510 |
|
| 511 |
finally TDrawableImage.BatchingEnd end; |
| 512 |
end; |
| 513 |
|
| 514 |
procedure TCastleTiledMapControl.SetOrigin(const Value: TVector2); |
| 515 |
begin |
| 516 |
if not TVector2.PerfectlyEquals(FOrigin, Value) then |
| 517 |
begin |
| 518 |
FOrigin := Value; |
| 519 |
VisibleChange([chRectangle]); |
| 520 |
end; |
| 521 |
end; |
| 522 |
|
| 523 |
procedure TCastleTiledMapControl.SetScale(const Value: Single); |
| 524 |
begin |
| 525 |
if FScale <> Value then |
| 526 |
begin |
| 527 |
FScale := Value; |
| 528 |
VisibleChange([chRectangle]); |
| 529 |
end; |
| 530 |
end; |
| 531 |
|
| 532 |
procedure TCastleTiledMapControl.SetSmoothScaling(const Value: Boolean); |
| 533 |
|
| 534 |
procedure ChangeExistingImages; |
| 535 |
var |
| 536 |
Tileset: TCastleTiledMapData.TTileset; |
| 537 |
begin |
| 538 |
if Map <> nil then |
| 539 |
for Tileset in Map.Tilesets do |
| 540 |
if Tileset.RendererData <> nil then |
| 541 |
{$warnings off} // using deprecated in deprecated |
| 542 |
(Tileset.RendererData as TSprite).SmoothScaling := SmoothScaling; |
| 543 |
{$warnings on} |
| 544 |
end; |
| 545 |
|
| 546 |
begin |
| 547 |
if FSmoothScaling <> Value then |
| 548 |
begin |
| 549 |
FSmoothScaling := Value; |
| 550 |
ChangeExistingImages; |
| 551 |
VisibleChange([chRectangle]); |
| 552 |
end; |
| 553 |
end; |
| 554 |
|
| 555 |
procedure TCastleTiledMapControl.SetSmoothScalingSafeBorder(const Value: Boolean); |
| 556 |
|
| 557 |
procedure ChangeExistingImages; |
| 558 |
var |
| 559 |
Tileset: TCastleTiledMapData.TTileset; |
| 560 |
begin |
| 561 |
if Map <> nil then |
| 562 |
for Tileset in Map.Tilesets do |
| 563 |
if Tileset.RendererData <> nil then |
| 564 |
{$warnings off} // using deprecated in deprecated |
| 565 |
(Tileset.RendererData as TSprite).SmoothScalingSafeBorder := |
| 566 |
SmoothScalingSafeBorder; |
| 567 |
{$warnings off} // using deprecated in deprecated |
| 568 |
end; |
| 569 |
|
| 570 |
begin |
| 571 |
if FSmoothScalingSafeBorder <> Value then |
| 572 |
begin |
| 573 |
FSmoothScalingSafeBorder := Value; |
| 574 |
ChangeExistingImages; |
| 575 |
VisibleChange([chRectangle]); |
| 576 |
end; |
| 577 |
end; |
| 578 |
|
| 579 |
function TCastleTiledMapControl.PositionToTile(const Position: TVector2; |
| 580 |
const ContainerCoordinates: Boolean; |
| 581 |
out TilePosition: TVector2Integer): Boolean; |
| 582 |
var |
| 583 |
LocalPosition: TVector2; |
| 584 |
begin |
| 585 |
if Map = nil then Exit(false); |
| 586 |
|
| 587 |
if ContainerCoordinates then |
| 588 |
LocalPosition := ContainerToLocalPosition(Position) |
| 589 |
else |
| 590 |
LocalPosition := Position; |
| 591 |
|
| 592 |
Result := Map.PositionToTile((LocalPosition / Scale) + Origin, TilePosition); |
| 593 |
end; |
| 594 |
|
| 595 |
function TCastleTiledMapControl.TileRectangle(const TilePosition: TVector2Integer; |
| 596 |
const ContainerCoordinates: Boolean): TFloatRectangle; |
| 597 |
begin |
| 598 |
if Map = nil then |
| 599 |
Result := TFloatRectangle.Empty |
| 600 |
else |
| 601 |
Result := TileRectangle(TilePosition, ContainerCoordinates, Map.TileWidth, Map.TileHeight); |
| 602 |
end; |
| 603 |
|
| 604 |
function TCastleTiledMapControl.TileRectangle(const TilePosition: TVector2Integer; |
| 605 |
const ContainerCoordinates: Boolean; |
| 606 |
const ForceTileWidth, ForceTileHeight: Single): TFloatRectangle; |
| 607 |
begin |
| 608 |
if Map = nil then |
| 609 |
Result := TFloatRectangle.Empty |
| 610 |
else |
| 611 |
begin |
| 612 |
Result.LeftBottom := Map.TileRenderPosition(TilePosition); |
| 613 |
Result.Width := ForceTileWidth; |
| 614 |
Result.Height := ForceTileHeight; |
| 615 |
Result := Result. |
| 616 |
// Apply Origin and Scale |
| 617 |
Translate(-Origin). |
| 618 |
ScaleAround0(Scale); |
| 619 |
if ContainerCoordinates then |
| 620 |
begin |
| 621 |
Result := Result. |
| 622 |
// Apply UIScale and RenderRect (anchors etc.) |
| 623 |
// to honor standard TCastleUserInterface conventions |
| 624 |
ScaleAround0(UIScale). |
| 625 |
Translate(RenderRect.LeftBottom); |
| 626 |
end; |
| 627 |
end; |
| 628 |
end; |
| 629 |
|
| 630 |
function TCastleTiledMapControl.PropertySections( |
| 631 |
const PropertyName: String): TPropertySections; |
| 632 |
begin |
| 633 |
if (PropertyName = 'Url') or |
| 634 |
(PropertyName = 'SmoothScaling') or |
| 635 |
(PropertyName = 'SmoothScalingSafeBorder') then |
| 636 |
Result := [psBasic] |
| 637 |
else |
| 638 |
Result := inherited PropertySections(PropertyName); |
| 639 |
end; |
| 640 |
|
| 641 |
{$define read_implementation_methods} |
| 642 |
{$I auto_generated_persistent_vectors/tcastletiledmapcontrol_persistent_vectors.inc} |
| 643 |
{$undef read_implementation_methods} |
| 644 |
|
| 645 |
{$endif read_implementation} |