Skip to content

Commit

Permalink
Defold plugin: Allow overriding z value also when exporting to .colle…
Browse files Browse the repository at this point in the history
…ction

See issue #3214
  • Loading branch information
bjorn committed Sep 27, 2022
1 parent 16a90e9 commit 9116f92
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Unreleased

* Defold plugin: Allow overriding z value also when exporting to .collection (#3214)

### Tiled 1.9.2 (16 September 2022)

* Allow adding maps to image collection tilesets (#3447)
Expand Down
20 changes: 14 additions & 6 deletions docs/manual/export-defold.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ It supports:
* Group layers (**only top-level group layers are supported, not nested ones!**)
* Multiple Tilesets per Tilemap

Upon export:
The plugin automatically assigns a Z-index to each layer ranging between 0 and
0.1. It supports the use of 9999 Group Layers and 9999 Tile Layers per Group
Layer.

When any additional information from the map is needed, the map can be
exported in :ref:`Lua format <lua-export>` and loaded as Defold script.

Custom Properties
^^^^^^^^^^^^^^^^^

* The ``tile_set`` property of each tilemap may need to be set up manually in
Defold after each export. However, Tiled will attempt to find the
Expand All @@ -59,9 +67,9 @@ Upon export:
GameObject in the Collection. This is useful when working with :doc:`Worlds
<worlds>`.

All layers of a Tilemap will have Z-index property assigned with values
ranging between 0 and 0.1. The plugin supports the use of 9999 Group Layers
and 9999 Tile Layers per Group Layer.
.. raw:: html

When any additional information from the map is needed, the map can be
exported in :ref:`Lua format <lua-export>` and loaded as Defold script.
<div class="new">New in Tiled 1.9.3</div>

* A custom float property named "z" can be added to tile layers to manually
specify their ``z`` value.
16 changes: 9 additions & 7 deletions src/plugins/defoldcollection/defoldcollectionplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@

#include "layer.h"
#include "map.h"
#include "mapobject.h"
#include "objectgroup.h"
#include "savefile.h"
#include "tile.h"
#include "tilelayer.h"
#include "grouplayer.h"

Expand Down Expand Up @@ -189,10 +186,15 @@ static QString tilesetRelativePath(const QString &filePath)
}

/*
* Returns z-Index for a layer, depending on its order in the map
* Returns z-Index for a layer, depending on its order in the map or a custom
* "z" property.
*/
static float zIndexForLayer(const Tiled::Map &map, const Tiled::Layer &inLayer, bool isTopLayer)
{
bool ok;
if (float z = inLayer.property(QStringLiteral("z")).toFloat(&ok); ok)
return z;

if (isTopLayer) {
int topLayerOrder = 0;
for (auto layer : map.layers()) {
Expand All @@ -202,10 +204,10 @@ static float zIndexForLayer(const Tiled::Map &map, const Tiled::Layer &inLayer,
return qBound(0, topLayerOrder, 9999) * 0.0001f;
topLayerOrder++;
}
} else if (inLayer.parentLayer()) {
float zIndex = zIndexForLayer(map, *inLayer.parentLayer(), true);
} else if (auto parentLayer = inLayer.parentLayer()) {
float zIndex = zIndexForLayer(map, *parentLayer, true);
int subLayerOrder = 0;
for (auto subLayer : inLayer.parentLayer()->layers()) {
for (auto subLayer : parentLayer->layers()) {
if (subLayer == &inLayer) {
zIndex += qBound(0, subLayerOrder, 9999) * 0.00000001f;
return zIndex;
Expand Down

0 comments on commit 9116f92

Please sign in to comment.