Permalink
Browse files

GmxPlugin: Added support for setting scale and origin for instances

The scale of instances was already determined automatically for tile
objects, but now it can also be manually specified or overridden using
the custom properties 'scaleX' and 'scaleY'.

It is now also possible to specify the origin of the object using custom
properties 'originX' and 'originY'. These will affect the position of the
exported instance, taking into account the rotation of the object as
well as its flipped state.

Closes #1427
  • Loading branch information...
bjorn committed Sep 20, 2017
1 parent 24af1f9 commit 9e1023eabb6add3989ce21ee148c6ef46fc50abd
Showing with 19 additions and 10 deletions.
  1. +19 −10 src/plugins/gmx/gmxplugin.cpp
@@ -220,15 +220,10 @@ bool GmxPlugin::write(const Map *map, const QString &fileName)
qreal scaleX = 1; qreal scaleX = 1;
qreal scaleY = 1; qreal scaleY = 1;
if (!object->cell().isEmpty()) { QPointF origin(optionalProperty(object, "originX", 0.0),
// Tile objects have bottom-left origin in Tiled, so the optionalProperty(object, "originY", 0.0));
// position needs to be translated for top-left origin in
// GameMaker, taking into account the rotation.
QTransform transform;
transform.rotate(object->rotation());
pos += transform.map(QPointF(0, -object->height()));
if (!object->cell().isEmpty()) {
// For tile objects we can support scaling and flipping, though // For tile objects we can support scaling and flipping, though
// flipping in combination with rotation doesn't work in GameMaker. // flipping in combination with rotation doesn't work in GameMaker.
if (auto tile = object->cell().tile()) { if (auto tile = object->cell().tile()) {
@@ -238,15 +233,29 @@ bool GmxPlugin::write(const Map *map, const QString &fileName)
if (object->cell().flippedHorizontally()) { if (object->cell().flippedHorizontally()) {
scaleX *= -1; scaleX *= -1;
pos += transform.map(QPointF(object->width(), 0)); origin += QPointF(object->width() - 2 * origin.x(), 0);
} }
if (object->cell().flippedVertically()) { if (object->cell().flippedVertically()) {
scaleY *= -1; scaleY *= -1;
pos += transform.map(QPointF(0, object->height())); origin += QPointF(0, object->height() - 2 * origin.y());
} }
} }
// Tile objects have bottom-left origin in Tiled, so the
// position needs to be translated for top-left origin in
// GameMaker, taking into account the rotation.
origin += QPointF(0, -object->height());
} }
// Allow overriding the scale using custom properties
scaleX = optionalProperty(object, "scaleX", scaleX);
scaleY = optionalProperty(object, "scaleY", scaleY);
// Adjust the position based on the origin
QTransform transform;
transform.rotate(object->rotation());
pos += transform.map(origin);
stream.writeAttribute("x", QString::number(qRound(pos.x()))); stream.writeAttribute("x", QString::number(qRound(pos.x())));
stream.writeAttribute("y", QString::number(qRound(pos.y()))); stream.writeAttribute("y", QString::number(qRound(pos.y())));

0 comments on commit 9e1023e

Please sign in to comment.