Skip to content

Commit

Permalink
Defold plugin: Assign incrementing z values and allow specifying tile…
Browse files Browse the repository at this point in the history
…_set

Probably one should rather use the Defold Collection plugin though, which
was now also extended with a way to set the tilesource path manually.

Closes #3214
  • Loading branch information
bjorn committed Sep 16, 2022
1 parent bb44aed commit 54bb216
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Fixed updating of custom property colors when changing style
* Scripting: Added Tileset.findTile
* AutoMapping: Fixed applying of rule probability (#3425)
* Defold plugin: Assign incrementing z values and allow specifying tile\_set (#3214)

### Tiled 1.9.1 (11 August 2022)

Expand Down
23 changes: 20 additions & 3 deletions docs/manual/export-defold.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@ defold
This plugin exports a map to a `Defold Tile Map <https://www.defold.com/manuals/tilemap/>`__ (\*.tilemap).
It only supports tile layers and only a single tileset may be used.

Upon export, the ``tile_set`` property of the Tile Map is left empty, so it
will need to be set up in Defold after each export.
.. raw:: html

<div class="new">New in Tiled 1.9.2</div>

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

The ``tile_set`` property of the Tile Map can be set by adding a custom
string property to the map named "tile_set" (case sensitive). If left empty,
it will need to be set up in Defold after each export.

A custom float property named "z" can be added to set the ``z`` value for each
tile layer. By default, the layers will be exported with incrementing z values,
so you only need to set this property in case you need to customize the
rendering order.

.. raw:: html

Expand All @@ -31,12 +44,16 @@ It supports:

Upon export:

* The ``Path`` property of each Tileset may need to be set up manually in
* 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
.tilesource file corresponding with the name your Tileset in Tiled in your
project's ``/tilesources/`` directory. If one is found, manual adjustments
won't be necessary.

Alternatively, a custom string property called "tilesource" (case-sensitive)
can be added to the *tileset*, which will then be used instead (since Tiled
1.9.2).

* If you create custom properties on your map called ``x-offset`` and
``y-offset``, these values will be used as coordinates for your top-level
GameObject in the Collection. This is useful when working with :doc:`Worlds
Expand Down
17 changes: 15 additions & 2 deletions src/plugins/defold/defoldplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ static void setCellProperties(QVariantHash &cellHash, const Tiled::Cell &cell)
}
}

template <typename T>
static T optionalProperty(const Tiled::Object *object, const QString &name, const T &def)
{
const QVariant var = object->resolvedProperty(name);
return var.isValid() ? var.value<T>() : def;
}

DefoldPlugin::DefoldPlugin()
{
}
Expand Down Expand Up @@ -113,10 +120,16 @@ bool DefoldPlugin::write(const Tiled::Map *map, const QString &fileName, Options

QString layers;
Tiled::LayerIterator it(map, Tiled::Layer::TileLayerType);
double z = 0.0;

while (auto tileLayer = static_cast<Tiled::TileLayer*>(it.next())) {
// Defold exports the z value to be beteen -1 and 1, so these
// automatic increments should allow up to 10000 layers.
z = optionalProperty(tileLayer, QStringLiteral("z"), z + 0.0001);

QVariantHash layer_h;
layer_h["id"] = tileLayer->name();
layer_h["z"] = 0;
layer_h["z"] = z;
layer_h["is_visible"] = tileLayer->isVisible() ? 1 : 0;
QString cells;

Expand All @@ -138,7 +151,7 @@ bool DefoldPlugin::write(const Tiled::Map *map, const QString &fileName, Options
map_h["layers"] = layers;
map_h["material"] = "/builtins/materials/tile_map.material";
map_h["blend_mode"] = "BLEND_MODE_ALPHA";
map_h["tile_set"] = "";
map_h["tile_set"] = map->property(QStringLiteral("tile_set")).toString();

QString result = replaceTags(QLatin1String(map_t), map_h);
Tiled::SaveFile mapFile(fileName);
Expand Down
28 changes: 20 additions & 8 deletions src/plugins/defoldcollection/defoldcollectionplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ static QString replaceTags(QString context, const QVariantHash &map)
return context;
}

template <typename T>
static T optionalProperty(const Tiled::Object &object, const QString &name, const T &def)
{
const QVariant var = object.resolvedProperty(name);
return var.isValid() ? var.value<T>() : def;
}

static QString tileSource(const Tiled::Tileset &tileset)
{
// Below, we input a value that's not necessarily correct in Defold, but it
// lets the user know what tilesource to link this tilemap with manually.
// However, if the user keeps all tilesources in /tilesources/ and the name
// of the tilesource corresponds with the name of the tileset in Defold,
// the value will be automatically correct.
const QString defaultTileSource = "/tilesources/" + tileset.name() + ".tilesource";
return optionalProperty(tileset, QStringLiteral("tilesource"), defaultTileSource);
}

DefoldCollectionPlugin::DefoldCollectionPlugin()
{
}
Expand Down Expand Up @@ -299,10 +317,7 @@ bool DefoldCollectionPlugin::write(const Tiled::Map *map, const QString &collect
tileMapHash["layers"] = layers;
tileMapHash["material"] = "/builtins/materials/tile_map.material";
tileMapHash["blend_mode"] = "BLEND_MODE_ALPHA";
// Below, we input a value that's not necessarily correct in Defold, but it lets the user know what tilesource to link this tilemap with manually.
// However, if the user keeps all tilesources in /tilesources/ and the name of the tilesource corresponds with the name of the tileset in Defold,
// the value will be automatically correct.
tileMapHash["tile_set"] = "/tilesources/" + tileset->name() + ".tilesource";
tileMapHash["tile_set"] = tileSource(*tileset);

QString result = replaceTags(QLatin1String(tileMapTemplate), tileMapHash);
Tiled::SaveFile mapFile(tilemapFilePath);
Expand Down Expand Up @@ -403,10 +418,7 @@ bool DefoldCollectionPlugin::write(const Tiled::Map *map, const QString &collect
tileMapHash["layers"] = layers;
tileMapHash["material"] = "/builtins/materials/tile_map.material";
tileMapHash["blend_mode"] = "BLEND_MODE_ALPHA";
// Below, we input a value that's not necessarily correct in Defold, but it lets the user know what tilesource to link this tilemap with manually.
// However, if the user keeps all tilesources in /tilesources/ and the name of the tilesource corresponds with the name of the tileset in Defold,
// the value will be automatically correct.
tileMapHash["tile_set"] = "/tilesources/" + tileset->name() + ".tilesource";
tileMapHash["tile_set"] = tileSource(*tileset);

QString result = replaceTags(QLatin1String(tileMapTemplate), tileMapHash);
Tiled::SaveFile mapFile(tilemapFilePath);
Expand Down

0 comments on commit 54bb216

Please sign in to comment.