Skip to content

Commit

Permalink
Allow to hide and show objects in tmxrasterizer (#3819)
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsLuz committed Sep 19, 2023
1 parent f66c972 commit fb6c1ff
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
### Unreleased

* JSON format: Fixed tile order when loading a tileset using the old format
* tmxrasterizer: Added --hide-object and --show-object arguments (by Lars Luz, #3819)

### Tiled 1.10.2 (4 August 2023)

Expand Down
10 changes: 9 additions & 1 deletion src/tmxrasterizer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ int main(int argc, char *argv[])
QCoreApplication::translate("main", "Don't render image layers.") },
{ QStringLiteral("advance-animations"),
QCoreApplication::translate("main", "If used, tile animations are advanced by the specified duration."),
QCoreApplication::translate("main", "duration") }
QCoreApplication::translate("main", "duration") },
{ QStringLiteral("hide-object"),
QCoreApplication::translate("main", "Specifies a object to omit from the output image. Can be repeated to hide multiple objects. If multiple objects share the specified name they all will be hidden."),
QCoreApplication::translate("main", "name") },
{ QStringLiteral("show-object"),
QCoreApplication::translate("main", "If used only specified objects are shown. Can be repeated to show multiple specified objects only. If multiple objects share the specified name they all will be shown."),
QCoreApplication::translate("main", "name") }
});
parser.addPositionalArgument(QStringLiteral("map|world"), QCoreApplication::translate("main", "Map or world file to render."));
parser.addPositionalArgument(QStringLiteral("image"), QCoreApplication::translate("main", "Image file to output."));
Expand All @@ -112,6 +118,8 @@ int main(int argc, char *argv[])
w.setLayerTypeVisible(Layer::TileLayerType, !parser.isSet(QLatin1String("hide-tile-layers")));
w.setLayerTypeVisible(Layer::ObjectGroupType, !parser.isSet(QLatin1String("hide-object-layers")));
w.setLayerTypeVisible(Layer::ImageLayerType, !parser.isSet(QLatin1String("hide-image-layers")));
w.setObjectsToHide(parser.values(QLatin1String("hide-object")));
w.setObjectsToShow(parser.values(QLatin1String("show-object")));

if (parser.isSet(QLatin1String("size"))) {
bool ok;
Expand Down
16 changes: 15 additions & 1 deletion src/tmxrasterizer/tmxrasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void TmxRasterizer::drawMapLayers(const MapRenderer &renderer,
std::stable_sort(objects.begin(), objects.end(), [](MapObject *a, MapObject *b){return a->y() < b->y();});

for (const MapObject *object : std::as_const(objects)) {
if (object->isVisible()) {
if (shouldDrawObject(object)) {
if (object->rotation() != qreal(0)) {
QPointF origin = renderer.pixelToScreenCoords(object->position());
painter.save();
Expand Down Expand Up @@ -116,6 +116,20 @@ bool TmxRasterizer::shouldDrawLayer(const Layer *layer) const
return !layer->isHidden();
}

bool TmxRasterizer::shouldDrawObject(const MapObject *object) const
{
if (mObjectsToHide.contains(object->name(), Qt::CaseInsensitive))
return false;

if (!mObjectsToShow.empty()) {
if (!mObjectsToShow.contains(object->name(), Qt::CaseInsensitive))
return false;
}

return object->isVisible();
}


int TmxRasterizer::render(const QString &fileName,
const QString &imageFileName)
{
Expand Down
6 changes: 6 additions & 0 deletions src/tmxrasterizer/tmxrasterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class TmxRasterizer
void setLayersToHide(QStringList layersToHide) { mLayersToHide = layersToHide; }
void setLayersToShow(QStringList layersToShow) { mLayersToShow = layersToShow; }

void setObjectsToHide(QStringList objectsToHide) { mObjectsToHide = objectsToHide; }
void setObjectsToShow(QStringList objectsToShow) { mObjectsToShow = objectsToShow; }

void setLayerTypeVisible(Layer::TypeFlag layerType, bool visible);

int render(const QString &fileName, const QString &imageFileName);
Expand All @@ -78,13 +81,16 @@ class TmxRasterizer
bool mIgnoreVisibility = false;
QStringList mLayersToHide;
QStringList mLayersToShow;
QStringList mObjectsToHide;
QStringList mObjectsToShow;
int mLayerTypesToShow = Layer::AnyLayerType & ~Layer::GroupLayerType;

void drawMapLayers(const MapRenderer &renderer, QPainter &painter, QPoint mapOffset = QPoint(0, 0)) const;
int renderMap(const QString &mapFileName, const QString &imageFileName);
int renderWorld(const QString &worldFileName, const QString &imageFileName);
int saveImage(const QString &imageFileName, const QImage &image) const;
bool shouldDrawLayer(const Layer *layer) const;
bool shouldDrawObject(const MapObject *object) const;
};

inline void TmxRasterizer::setLayerTypeVisible(Layer::TypeFlag layerType, bool visible)
Expand Down

0 comments on commit fb6c1ff

Please sign in to comment.