diff --git a/src/libtiled/map.h b/src/libtiled/map.h index b7e58117fd..f353ad72f8 100644 --- a/src/libtiled/map.h +++ b/src/libtiled/map.h @@ -78,8 +78,7 @@ class TILEDSHARED_EXPORT Map : public Object QString exportFormat; enum Property { - TileWidthProperty, - TileHeightProperty, + TileSizeProperty, InfiniteProperty, HexSideLengthProperty, StaggerAxisProperty, @@ -212,6 +211,7 @@ class TILEDSHARED_EXPORT Map : public Object void setTileHeight(int height); QSize tileSize() const; + void setTileSize(QSize size); bool infinite() const; void setInfinite(bool infinite); @@ -455,6 +455,12 @@ inline QSize Map::tileSize() const return QSize(mParameters.tileWidth, mParameters.tileHeight); } +inline void Map::setTileSize(QSize size) +{ + mParameters.tileWidth = size.width(); + mParameters.tileHeight = size.height(); +} + inline bool Map::infinite() const { return mParameters.infinite; diff --git a/src/tiled/changelayer.cpp b/src/tiled/changelayer.cpp index 93ce88f176..39427fb664 100644 --- a/src/tiled/changelayer.cpp +++ b/src/tiled/changelayer.cpp @@ -23,7 +23,6 @@ #include "changeevents.h" #include "document.h" #include "layer.h" -#include "map.h" #include diff --git a/src/tiled/changemapproperty.cpp b/src/tiled/changemapproperty.cpp index c52d4052d0..6a03a2fc0f 100644 --- a/src/tiled/changemapproperty.cpp +++ b/src/tiled/changemapproperty.cpp @@ -21,221 +21,235 @@ #include "changemapproperty.h" #include "changeevents.h" -#include "map.h" #include "mapdocument.h" #include using namespace Tiled; -ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument, - Map::Property property, - int value) - : mMapDocument(mapDocument) - , mProperty(property) - , mIntValue(value) -{ - switch (property) { - case Map::TileWidthProperty: - setText(QCoreApplication::translate("Undo Commands", - "Change Tile Width")); - break; - case Map::TileHeightProperty: - setText(QCoreApplication::translate("Undo Commands", - "Change Tile Height")); - break; - case Map::InfiniteProperty: - setText(QCoreApplication::translate("Undo Commands", - "Change Infinite Property")); - break; - case Map::HexSideLengthProperty: - setText(QCoreApplication::translate("Undo Commands", - "Change Hex Side Length")); - break; - case Map::CompressionLevelProperty: - setText(QCoreApplication::translate("Undo Commands", - "Change Compression Level")); - break; - default: - break; - } +ChangeMapBackgroundColor::ChangeMapBackgroundColor(MapDocument *document, const QColor &backgroundColor) + : ChangeValue(document, { document->map() }, backgroundColor) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Background Color")); +} + +QColor ChangeMapBackgroundColor::getValue(const Map *map) const +{ + return map->backgroundColor(); +} + +void ChangeMapBackgroundColor::setValue(Map *map, const QColor &value) const +{ + map->setBackgroundColor(value); + emit document()->changed(MapChangeEvent(Map::BackgroundColorProperty)); +} + + +ChangeMapChunkSize::ChangeMapChunkSize(MapDocument *document, const QSize &chunkSize) + : ChangeValue(document, { document->map() }, chunkSize) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Chunk Size")); +} + +QSize ChangeMapChunkSize::getValue(const Map *map) const +{ + return map->chunkSize(); +} + +void ChangeMapChunkSize::setValue(Map *map, const QSize &value) const +{ + map->setChunkSize(value); + emit document()->changed(MapChangeEvent(Map::ChunkSizeProperty)); +} + + +ChangeMapStaggerAxis::ChangeMapStaggerAxis(MapDocument *document, Map::StaggerAxis staggerAxis) + : ChangeValue(document, { document->map() }, staggerAxis) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Stagger Axis")); +} + +Map::StaggerAxis ChangeMapStaggerAxis::getValue(const Map *map) const +{ + return map->staggerAxis(); +} + +void ChangeMapStaggerAxis::setValue(Map *map, const Map::StaggerAxis &value) const +{ + map->setStaggerAxis(value); + emit document()->changed(MapChangeEvent(Map::StaggerAxisProperty)); +} + + +ChangeMapStaggerIndex::ChangeMapStaggerIndex(MapDocument *document, Map::StaggerIndex staggerIndex) + : ChangeValue(document, { document->map() }, staggerIndex) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Stagger Index")); +} + +Map::StaggerIndex ChangeMapStaggerIndex::getValue(const Map *map) const +{ + return map->staggerIndex(); +} + +void ChangeMapStaggerIndex::setValue(Map *map, const Map::StaggerIndex &value) const +{ + map->setStaggerIndex(value); + emit document()->changed(MapChangeEvent(Map::StaggerIndexProperty)); +} + + +ChangeMapParallaxOrigin::ChangeMapParallaxOrigin(MapDocument *document, const QPointF ¶llaxOrigin) + : ChangeValue(document, { document->map() }, parallaxOrigin) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Parallax Origin")); +} + +QPointF ChangeMapParallaxOrigin::getValue(const Map *map) const +{ + return map->parallaxOrigin(); +} + +void ChangeMapParallaxOrigin::setValue(Map *map, const QPointF &value) const +{ + map->setParallaxOrigin(value); + emit document()->changed(MapChangeEvent(Map::ParallaxOriginProperty)); +} + + +ChangeMapOrientation::ChangeMapOrientation(MapDocument *document, Map::Orientation orientation) + : ChangeValue(document, { document->map() }, orientation) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Orientation")); +} + +Map::Orientation ChangeMapOrientation::getValue(const Map *map) const +{ + return map->orientation(); +} + +void ChangeMapOrientation::setValue(Map *map, const Map::Orientation &value) const +{ + map->setOrientation(value); + emit document()->changed(MapChangeEvent(Map::OrientationProperty)); +} + + +ChangeMapRenderOrder::ChangeMapRenderOrder(MapDocument *document, Map::RenderOrder renderOrder) + : ChangeValue(document, { document->map() }, renderOrder) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Render Order")); +} + +Map::RenderOrder ChangeMapRenderOrder::getValue(const Map *map) const +{ + return map->renderOrder(); +} + +void ChangeMapRenderOrder::setValue(Map *map, const Map::RenderOrder &value) const +{ + map->setRenderOrder(value); + emit document()->changed(MapChangeEvent(Map::RenderOrderProperty)); +} + + +ChangeMapLayerDataFormat::ChangeMapLayerDataFormat(MapDocument *document, Map::LayerDataFormat layerDataFormat) + : ChangeValue(document, { document->map() }, layerDataFormat) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Layer Data Format")); +} + +Map::LayerDataFormat ChangeMapLayerDataFormat::getValue(const Map *map) const +{ + return map->layerDataFormat(); +} + +void ChangeMapLayerDataFormat::setValue(Map *map, const Map::LayerDataFormat &value) const +{ + map->setLayerDataFormat(value); + emit document()->changed(MapChangeEvent(Map::LayerDataFormatProperty)); +} + + +ChangeMapTileSize::ChangeMapTileSize(MapDocument *document, const QSize &size) + : ChangeValue(document, { document->map() }, size) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Tile Size")); +} + +QSize ChangeMapTileSize::getValue(const Map *map) const +{ + return map->tileSize(); } -ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument, - const QColor &backgroundColor) - : QUndoCommand(QCoreApplication::translate("Undo Commands", - "Change Background Color")) - , mMapDocument(mapDocument) - , mProperty(Map::BackgroundColorProperty) - , mBackgroundColor(backgroundColor) +void ChangeMapTileSize::setValue(Map *map, const QSize &value) const { + map->setTileSize(value); + emit document()->changed(MapChangeEvent(Map::TileSizeProperty)); } -ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument, - QSize chunkSize) - : QUndoCommand(QCoreApplication::translate("Undo Commands", - "Change Chunk Size")) - , mMapDocument(mapDocument) - , mProperty(Map::ChunkSizeProperty) - , mChunkSize(chunkSize) -{ -} - -ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument, - Map::StaggerAxis staggerAxis) - : QUndoCommand(QCoreApplication::translate("Undo Commands", - "Change Stagger Axis")) - , mMapDocument(mapDocument) - , mProperty(Map::StaggerAxisProperty) - , mStaggerAxis(staggerAxis) -{ -} - -ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument, - Map::StaggerIndex staggerIndex) - : QUndoCommand(QCoreApplication::translate("Undo Commands", - "Change Stagger Index")) - , mMapDocument(mapDocument) - , mProperty(Map::StaggerIndexProperty) - , mStaggerIndex(staggerIndex) -{ -} - -ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument, - const QPointF ¶llaxOrigin) - : QUndoCommand(QCoreApplication::translate("Undo Commands", - "Change Parallax Origin")) - , mMapDocument(mapDocument) - , mProperty(Map::ParallaxOriginProperty) - , mParallaxOrigin(parallaxOrigin) -{ -} - -ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument, - Map::Orientation orientation) - : QUndoCommand(QCoreApplication::translate("Undo Commands", - "Change Orientation")) - , mMapDocument(mapDocument) - , mProperty(Map::OrientationProperty) - , mOrientation(orientation) -{ -} - -ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument, - Map::RenderOrder renderOrder) - : QUndoCommand(QCoreApplication::translate("Undo Commands", - "Change Render Order")) - , mMapDocument(mapDocument) - , mProperty(Map::RenderOrderProperty) - , mRenderOrder(renderOrder) -{ -} - -ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument, - Map::LayerDataFormat layerDataFormat) - : QUndoCommand(QCoreApplication::translate("Undo Commands", - "Change Layer Data Format")) - , mMapDocument(mapDocument) - , mProperty(Map::LayerDataFormatProperty) - , mLayerDataFormat(layerDataFormat) -{ -} - -void ChangeMapProperty::redo() -{ - swap(); -} - -void ChangeMapProperty::undo() -{ - swap(); -} - -void ChangeMapProperty::swap() -{ - Map *map = mMapDocument->map(); - - switch (mProperty) { - case Map::TileWidthProperty: { - const int tileWidth = map->tileWidth(); - map->setTileWidth(mIntValue); - mIntValue = tileWidth; - break; - } - case Map::TileHeightProperty: { - const int tileHeight = map->tileHeight(); - map->setTileHeight(mIntValue); - mIntValue = tileHeight; - break; - } - case Map::InfiniteProperty: { - const int infinite = map->infinite(); - map->setInfinite(mIntValue); - mIntValue = infinite; - break; - } - case Map::OrientationProperty: { - const Map::Orientation orientation = map->orientation(); - map->setOrientation(mOrientation); - mOrientation = orientation; - break; - } - case Map::HexSideLengthProperty: { - const int hexSideLength = map->hexSideLength(); - map->setHexSideLength(mIntValue); - mIntValue = hexSideLength; - break; - } - case Map::StaggerAxisProperty: { - const Map::StaggerAxis staggerAxis = map->staggerAxis(); - map->setStaggerAxis(mStaggerAxis); - mStaggerAxis = staggerAxis; - break; - } - case Map::StaggerIndexProperty: { - const Map::StaggerIndex staggerIndex = map->staggerIndex(); - map->setStaggerIndex(mStaggerIndex); - mStaggerIndex = staggerIndex; - break; - } - case Map::ParallaxOriginProperty: { - const QPointF parallaxOrigin = map->parallaxOrigin(); - map->setParallaxOrigin(mParallaxOrigin); - mParallaxOrigin = parallaxOrigin; - break; - } - case Map::RenderOrderProperty: { - const Map::RenderOrder renderOrder = map->renderOrder(); - map->setRenderOrder(mRenderOrder); - mRenderOrder = renderOrder; - break; - } - case Map::BackgroundColorProperty: { - const QColor backgroundColor = map->backgroundColor(); - map->setBackgroundColor(mBackgroundColor); - mBackgroundColor = backgroundColor; - break; - } - case Map::LayerDataFormatProperty: { - const Map::LayerDataFormat layerDataFormat = map->layerDataFormat(); - map->setLayerDataFormat(mLayerDataFormat); - mLayerDataFormat = layerDataFormat; - break; - } - case Map::CompressionLevelProperty: { - const int compressionLevel = map->compressionLevel(); - map->setCompressionLevel(mIntValue); - mIntValue = compressionLevel; - break; - } - case Map::ChunkSizeProperty: { - const QSize chunkSize = map->chunkSize(); - map->setChunkSize(mChunkSize); - mChunkSize = chunkSize; - break; - } - } - - emit mMapDocument->changed(MapChangeEvent(mProperty)); - emit mMapDocument->mapChanged(); + +ChangeMapInfinite::ChangeMapInfinite(MapDocument *document, bool infinite) + : ChangeValue(document, { document->map() }, infinite) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Infinite Property")); +} + +bool ChangeMapInfinite::getValue(const Map *map) const +{ + return map->infinite(); +} + +void ChangeMapInfinite::setValue(Map *map, const bool &value) const +{ + map->setInfinite(value); + emit document()->changed(MapChangeEvent(Map::InfiniteProperty)); +} + + +ChangeMapHexSideLength::ChangeMapHexSideLength(MapDocument *document, int hexSideLength) + : ChangeValue(document, { document->map() }, hexSideLength) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Hex Side Length")); +} + +int ChangeMapHexSideLength::getValue(const Map *map) const +{ + return map->hexSideLength(); +} + +void ChangeMapHexSideLength::setValue(Map *map, const int &value) const +{ + map->setHexSideLength(value); + emit document()->changed(MapChangeEvent(Map::HexSideLengthProperty)); +} + + +ChangeMapCompressionLevel::ChangeMapCompressionLevel(MapDocument *document, int compressionLevel) + : ChangeValue(document, { document->map() }, compressionLevel) +{ + setText(QCoreApplication::translate("Undo Commands", + "Change Compression Level")); +} + +int ChangeMapCompressionLevel::getValue(const Map *map) const +{ + return map->compressionLevel(); +} + +void ChangeMapCompressionLevel::setValue(Map *map, const int &value) const +{ + map->setCompressionLevel(value); + emit document()->changed(MapChangeEvent(Map::CompressionLevelProperty)); } diff --git a/src/tiled/changemapproperty.h b/src/tiled/changemapproperty.h index 03e93dd403..7c87becebf 100644 --- a/src/tiled/changemapproperty.h +++ b/src/tiled/changemapproperty.h @@ -20,111 +20,170 @@ #pragma once +#include "changeevents.h" +#include "changevalue.h" #include "map.h" +#include "mapdocument.h" #include -#include namespace Tiled { class MapDocument; -class ChangeMapProperty : public QUndoCommand +class ChangeMapBackgroundColor : public ChangeValue { public: - /** - * Constructs a command that changes the value of the given property. - * - * Can only be used for the HexSideLength property. - * - * @param mapDocument the map document of the map - * @param backgroundColor the new color to apply for the background - */ - ChangeMapProperty(MapDocument *mapDocument, Map::Property property, int value); - - /** - * Constructs a command that changes the map background color. - * - * @param mapDocument the map document of the map - * @param backgroundColor the new color to apply for the background - */ - ChangeMapProperty(MapDocument *mapDocument, const QColor &backgroundColor); - - /** - * Constructs a command that changes the chunk size. - * - * @param mapDocument the map document of the map - * @param chunkSize the new chunk size to use for tile layers - */ - ChangeMapProperty(MapDocument *mapDocument, QSize chunkSize); - - /** - * Constructs a command that changes the map stagger axis. - * - * @param mapDocument the map document of the map - * @param staggerAxis the new map stagger axis - */ - ChangeMapProperty(MapDocument *mapDocument, Map::StaggerAxis staggerAxis); - - /** - * Constructs a command that changes the map stagger index. - * - * @param mapDocument the map document of the map - * @param staggerIndex the new map stagger index - */ - ChangeMapProperty(MapDocument *mapDocument, Map::StaggerIndex staggerIndex); - - /** - * Constructs a command that changes the parallax origin. - * - * @param mapDocument the map document of the map - * @param parallaxOrigin the new parallax origin - */ - ChangeMapProperty(MapDocument *mapDocument, const QPointF ¶llaxOrigin); - - /** - * Constructs a command that changes the map orientation. - * - * @param mapDocument the map document of the map - * @param orientation the new map orientation - */ - ChangeMapProperty(MapDocument *mapDocument, Map::Orientation orientation); - - /** - * Constructs a command that changes the render order. - * - * @param mapDocument the map document of the map - * @param renderOrder the new map render order - */ - ChangeMapProperty(MapDocument *mapDocument, Map::RenderOrder renderOrder); - - /** - * Constructs a command that changes the layer data format. - * - * @param mapDocument the map document of the map - * @param layerDataFormat the new layer data format - */ - ChangeMapProperty(MapDocument *mapDocument, Map::LayerDataFormat layerDataFormat); - - void undo() override; - void redo() override; + ChangeMapBackgroundColor(MapDocument *document, const QColor &backgroundColor); + + int id() const override { return Cmd_ChangeMapBackgroundColor; } + +private: + QColor getValue(const Map *map) const override; + void setValue(Map *map, const QColor &value) const override; +}; + + +class ChangeMapChunkSize : public ChangeValue +{ +public: + ChangeMapChunkSize(MapDocument *document, const QSize &chunkSize); + + int id() const override { return Cmd_ChangeMapChunkSize; } + +private: + QSize getValue(const Map *map) const override; + void setValue(Map *map, const QSize &value) const override; +}; + + +class ChangeMapStaggerAxis : public ChangeValue +{ +public: + ChangeMapStaggerAxis(MapDocument *document, Map::StaggerAxis staggerAxis); + + int id() const override { return Cmd_ChangeMapStaggerAxis; } + +private: + Map::StaggerAxis getValue(const Map *map) const override; + void setValue(Map *map, const Map::StaggerAxis &value) const override; +}; + + +class ChangeMapStaggerIndex : public ChangeValue +{ +public: + ChangeMapStaggerIndex(MapDocument *document, Map::StaggerIndex staggerIndex); + + int id() const override { return Cmd_ChangeMapStaggerIndex; } + +private: + Map::StaggerIndex getValue(const Map *map) const override; + void setValue(Map *map, const Map::StaggerIndex &value) const override; +}; + + +class ChangeMapParallaxOrigin : public ChangeValue +{ +public: + ChangeMapParallaxOrigin(MapDocument *document, const QPointF ¶llaxOrigin); + + int id() const override { return Cmd_ChangeMapParallaxOrigin; } + +private: + QPointF getValue(const Map *map) const override; + void setValue(Map *map, const QPointF &value) const override; +}; + + +class ChangeMapOrientation : public ChangeValue +{ +public: + ChangeMapOrientation(MapDocument *document, Map::Orientation orientation); + + int id() const override { return Cmd_ChangeMapOrientation; } + +private: + Map::Orientation getValue(const Map *map) const override; + void setValue(Map *map, const Map::Orientation &value) const override; +}; + + +class ChangeMapRenderOrder : public ChangeValue +{ +public: + ChangeMapRenderOrder(MapDocument *document, Map::RenderOrder renderOrder); + + int id() const override { return Cmd_ChangeMapRenderOrder; } + +private: + Map::RenderOrder getValue(const Map *map) const override; + void setValue(Map *map, const Map::RenderOrder &value) const override; +}; + + +class ChangeMapLayerDataFormat : public ChangeValue +{ +public: + ChangeMapLayerDataFormat(MapDocument *document, Map::LayerDataFormat layerDataFormat); + + int id() const override { return Cmd_ChangeMapLayerDataFormat; } + +private: + Map::LayerDataFormat getValue(const Map *map) const override; + void setValue(Map *map, const Map::LayerDataFormat &value) const override; +}; + + +class ChangeMapTileSize : public ChangeValue +{ +public: + ChangeMapTileSize(MapDocument *document, const QSize &tileSize); + + int id() const override { return Cmd_ChangeMapTileSize; } + +private: + QSize getValue(const Map *map) const override; + void setValue(Map *map, const QSize &value) const override; +}; + + +class ChangeMapInfinite : public ChangeValue +{ +public: + ChangeMapInfinite(MapDocument *document, bool infinite); + + int id() const override { return Cmd_ChangeMapInfinite; } + +private: + bool getValue(const Map *map) const override; + void setValue(Map *map, const bool &value) const override; +}; + + +class ChangeMapHexSideLength : public ChangeValue +{ +public: + ChangeMapHexSideLength(MapDocument *document, int hexSideLength); + + int id() const override { return Cmd_ChangeMapHexSideLength; } + +private: + int getValue(const Map *map) const override; + void setValue(Map *map, const int &value) const override; +}; + + +class ChangeMapCompressionLevel : public ChangeValue +{ +public: + ChangeMapCompressionLevel(MapDocument *document, int compressionLevel); + + int id() const override { return Cmd_ChangeMapCompressionLevel; } private: - void swap(); - - MapDocument *mMapDocument; - Map::Property mProperty; - QColor mBackgroundColor; - QSize mChunkSize; - union { - int mIntValue; - Map::StaggerAxis mStaggerAxis; - Map::StaggerIndex mStaggerIndex; - QPointF mParallaxOrigin; - Map::Orientation mOrientation; - Map::RenderOrder mRenderOrder; - Map::LayerDataFormat mLayerDataFormat; - }; + int getValue(const Map *map) const override; + void setValue(Map *map, const int &value) const override; }; } // namespace Tiled diff --git a/src/tiled/editablemap.cpp b/src/tiled/editablemap.cpp index d0f3a2878a..02c0b97572 100644 --- a/src/tiled/editablemap.cpp +++ b/src/tiled/editablemap.cpp @@ -495,7 +495,7 @@ void EditableMap::setSize(int width, int height) void EditableMap::setTileWidth(int value) { if (auto doc = mapDocument()) - push(new ChangeMapProperty(doc, Map::TileWidthProperty, value)); + push(new ChangeMapTileSize(doc, QSize(value, tileHeight()))); else if (!checkReadOnly()) map()->setTileWidth(value); } @@ -503,7 +503,7 @@ void EditableMap::setTileWidth(int value) void EditableMap::setTileHeight(int value) { if (auto doc = mapDocument()) - push(new ChangeMapProperty(doc, Map::TileHeightProperty, value)); + push(new ChangeMapTileSize(doc, QSize(tileWidth(), value))); else if (!checkReadOnly()) map()->setTileHeight(value); } @@ -528,7 +528,7 @@ void EditableMap::setTileSize(int width, int height) void EditableMap::setInfinite(bool value) { if (auto doc = mapDocument()) - push(new ChangeMapProperty(doc, Map::InfiniteProperty, value)); + push(new ChangeMapInfinite(doc, value)); else if (!checkReadOnly()) map()->setInfinite(value); } @@ -536,7 +536,7 @@ void EditableMap::setInfinite(bool value) void EditableMap::setHexSideLength(int value) { if (auto doc = mapDocument()) - push(new ChangeMapProperty(doc, Map::HexSideLengthProperty, value)); + push(new ChangeMapHexSideLength(doc, value)); else if (!checkReadOnly()) map()->setHexSideLength(value); } @@ -544,7 +544,7 @@ void EditableMap::setHexSideLength(int value) void EditableMap::setStaggerAxis(StaggerAxis value) { if (auto doc = mapDocument()) - push(new ChangeMapProperty(doc, static_cast(value))); + push(new ChangeMapStaggerAxis(doc, static_cast(value))); else if (!checkReadOnly()) map()->setStaggerAxis(static_cast(value)); } @@ -552,7 +552,7 @@ void EditableMap::setStaggerAxis(StaggerAxis value) void EditableMap::setStaggerIndex(StaggerIndex value) { if (auto doc = mapDocument()) - push(new ChangeMapProperty(doc, static_cast(value))); + push(new ChangeMapStaggerIndex(doc, static_cast(value))); else if (!checkReadOnly()) map()->setStaggerIndex(static_cast(value)); } @@ -560,7 +560,7 @@ void EditableMap::setStaggerIndex(StaggerIndex value) void EditableMap::setParallaxOrigin(const QPointF ¶llaxOrigin) { if (auto doc = mapDocument()) - push(new ChangeMapProperty(doc, parallaxOrigin)); + push(new ChangeMapParallaxOrigin(doc, parallaxOrigin)); else if (!checkReadOnly()) map()->setParallaxOrigin(parallaxOrigin); } @@ -568,7 +568,7 @@ void EditableMap::setParallaxOrigin(const QPointF ¶llaxOrigin) void EditableMap::setOrientation(Orientation value) { if (auto doc = mapDocument()) { - push(new ChangeMapProperty(doc, static_cast(value))); + push(new ChangeMapOrientation(doc, static_cast(value))); } else if (!checkReadOnly()) { map()->setOrientation(static_cast(value)); mRenderer.reset(); @@ -578,7 +578,7 @@ void EditableMap::setOrientation(Orientation value) void EditableMap::setRenderOrder(RenderOrder value) { if (auto doc = mapDocument()) - push(new ChangeMapProperty(doc, static_cast(value))); + push(new ChangeMapRenderOrder(doc, static_cast(value))); else if (!checkReadOnly()) map()->setRenderOrder(static_cast(value)); } @@ -586,7 +586,7 @@ void EditableMap::setRenderOrder(RenderOrder value) void EditableMap::setBackgroundColor(const QColor &value) { if (auto doc = mapDocument()) - push(new ChangeMapProperty(doc, value)); + push(new ChangeMapBackgroundColor(doc, value)); else if (!checkReadOnly()) map()->setBackgroundColor(value); } @@ -594,7 +594,7 @@ void EditableMap::setBackgroundColor(const QColor &value) void EditableMap::setLayerDataFormat(LayerDataFormat value) { if (auto doc = mapDocument()) - push(new ChangeMapProperty(doc, static_cast(value))); + push(new ChangeMapLayerDataFormat(doc, static_cast(value))); else if (!checkReadOnly()) map()->setLayerDataFormat(static_cast(value)); } diff --git a/src/tiled/mapscene.cpp b/src/tiled/mapscene.cpp index c87faf96b2..d9609b1d80 100644 --- a/src/tiled/mapscene.cpp +++ b/src/tiled/mapscene.cpp @@ -102,8 +102,6 @@ void MapScene::setMapDocument(MapDocument *mapDocument) if (mMapDocument) { connect(mMapDocument, &MapDocument::changed, this, &MapScene::changeEvent); - connect(mMapDocument, &MapDocument::mapChanged, - this, &MapScene::mapChanged); connect(mMapDocument, &MapDocument::tilesetTilePositioningChanged, this, [this] { update(); }); connect(mMapDocument, &MapDocument::tileImageSourceChanged, @@ -405,10 +403,19 @@ MapItem *MapScene::takeOrCreateMapItem(const MapDocumentPtr &mapDocument, MapIte void MapScene::changeEvent(const ChangeEvent &change) { switch (change.type) { - case ChangeEvent::MapChanged: - if (static_cast(change).property == Map::ParallaxOriginProperty) + case ChangeEvent::MapChanged: { + switch (static_cast(change).property) { + case Map::ParallaxOriginProperty: emit parallaxParametersChanged(); + break; + case Map::BackgroundColorProperty: + updateBackgroundColor(); + break; + default: + break; + } break; + } case ChangeEvent::TilesetChanged:{ auto &tilesetChange = static_cast(change); switch (tilesetChange.property) { @@ -424,14 +431,6 @@ void MapScene::changeEvent(const ChangeEvent &change) } } -/** - * Updates the possibly changed background color. - */ -void MapScene::mapChanged() -{ - updateBackgroundColor(); -} - void MapScene::repaintTileset(Tileset *tileset) { for (MapItem *mapItem : std::as_const(mMapItems)) { diff --git a/src/tiled/mapscene.h b/src/tiled/mapscene.h index 7eacf13290..3e81127b9d 100644 --- a/src/tiled/mapscene.h +++ b/src/tiled/mapscene.h @@ -112,7 +112,6 @@ class MapScene : public QGraphicsScene void refreshScene(); void changeEvent(const ChangeEvent &change); - void mapChanged(); void repaintTileset(Tileset *tileset); void tilesetReplaced(int index, Tileset *tileset, Tileset *oldTileset); diff --git a/src/tiled/propertieswidget.cpp b/src/tiled/propertieswidget.cpp index 43fa0c6d16..4ec7c3d11e 100644 --- a/src/tiled/propertieswidget.cpp +++ b/src/tiled/propertieswidget.cpp @@ -896,7 +896,7 @@ class MapProperties : public ObjectProperties return map()->orientation(); }, [this](Map::Orientation value) { - push(new ChangeMapProperty(mapDocument(), value)); + push(new ChangeMapOrientation(mapDocument(), value)); }); mSizeProperty = new MapSizeProperty(mapDocument(), this); @@ -907,19 +907,7 @@ class MapProperties : public ObjectProperties return mapDocument()->map()->tileSize(); }, [this](const QSize &newSize) { - const auto oldSize = mapDocument()->map()->tileSize(); - - if (oldSize.width() != newSize.width()) { - push(new ChangeMapProperty(mapDocument(), - Map::TileWidthProperty, - newSize.width())); - } - - if (oldSize.height() != newSize.height()) { - push(new ChangeMapProperty(mapDocument(), - Map::TileHeightProperty, - newSize.height())); - } + push(new ChangeMapTileSize(mapDocument(), newSize)); }, this); mTileSizeProperty->setMinimum(1); @@ -931,9 +919,7 @@ class MapProperties : public ObjectProperties return map()->infinite(); }, [this](const bool &value) { - push(new ChangeMapProperty(mapDocument(), - Map::InfiniteProperty, - value ? 1 : 0)); + push(new ChangeMapInfinite(mapDocument(), value)); }); mInfiniteProperty->setNameOnCheckBox(true); @@ -942,10 +928,8 @@ class MapProperties : public ObjectProperties [this] { return map()->hexSideLength(); }, - [this](const QVariant &value) { - push(new ChangeMapProperty(mapDocument(), - Map::HexSideLengthProperty, - value.toInt())); + [this](const int &value) { + push(new ChangeMapHexSideLength(mapDocument(), value)); }); mHexSideLengthProperty->setSuffix(tr(" px")); @@ -955,7 +939,7 @@ class MapProperties : public ObjectProperties return map()->staggerAxis(); }, [this](Map::StaggerAxis value) { - push(new ChangeMapProperty(mapDocument(), value)); + push(new ChangeMapStaggerAxis(mapDocument(), value)); }); mStaggerIndexProperty = new EnumProperty( @@ -964,7 +948,7 @@ class MapProperties : public ObjectProperties return map()->staggerIndex(); }, [this](Map::StaggerIndex value) { - push(new ChangeMapProperty(mapDocument(), value)); + push(new ChangeMapStaggerIndex(mapDocument(), value)); }); mParallaxOriginProperty = new PointFProperty( @@ -973,7 +957,7 @@ class MapProperties : public ObjectProperties return map()->parallaxOrigin(); }, [this](const QPointF &value) { - push(new ChangeMapProperty(mapDocument(), value)); + push(new ChangeMapParallaxOrigin(mapDocument(), value)); }); mLayerDataFormatProperty = new EnumProperty( @@ -982,7 +966,16 @@ class MapProperties : public ObjectProperties return map()->layerDataFormat(); }, [this](Map::LayerDataFormat value) { - push(new ChangeMapProperty(mapDocument(), value)); + push(new ChangeMapLayerDataFormat(mapDocument(), value)); + }); + + mCompressionLevelProperty = new IntProperty( + tr("Compression Level"), + [this] { + return map()->compressionLevel(); + }, + [this](const int &value) { + push(new ChangeMapCompressionLevel(mapDocument(), value)); }); mChunkSizeProperty = new SizeProperty( @@ -991,7 +984,7 @@ class MapProperties : public ObjectProperties return map()->chunkSize(); }, [this](const QSize &value) { - push(new ChangeMapProperty(mapDocument(), value)); + push(new ChangeMapChunkSize(mapDocument(), value)); }); mChunkSizeProperty->setMinimum(CHUNK_SIZE_MIN); @@ -1001,18 +994,7 @@ class MapProperties : public ObjectProperties return map()->renderOrder(); }, [this](Map::RenderOrder value) { - push(new ChangeMapProperty(mapDocument(), value)); - }); - - mCompressionLevelProperty = new IntProperty( - tr("Compression Level"), - [this] { - return map()->compressionLevel(); - }, - [this](const int &value) { - push(new ChangeMapProperty(mapDocument(), - Map::CompressionLevelProperty, - value)); + push(new ChangeMapRenderOrder(mapDocument(), value)); }); mBackgroundColorProperty = new ColorProperty( @@ -1021,7 +1003,7 @@ class MapProperties : public ObjectProperties return map()->backgroundColor(); }, [this](const QColor &value) { - push(new ChangeMapProperty(mapDocument(), value)); + push(new ChangeMapBackgroundColor(mapDocument(), value)); }); mMapProperties = new GroupProperty(tr("Map")); @@ -1038,8 +1020,8 @@ class MapProperties : public ObjectProperties mMapProperties->addProperty(mParallaxOriginProperty); mMapProperties->addSeparator(); mMapProperties->addProperty(mLayerDataFormatProperty); - mMapProperties->addProperty(mChunkSizeProperty); mMapProperties->addProperty(mCompressionLevelProperty); + mMapProperties->addProperty(mChunkSizeProperty); mMapProperties->addSeparator(); mMapProperties->addProperty(mRenderOrderProperty); mMapProperties->addProperty(mBackgroundColorProperty); @@ -1062,8 +1044,7 @@ class MapProperties : public ObjectProperties const auto property = static_cast(event).property; switch (property) { - case Map::TileWidthProperty: - case Map::TileHeightProperty: + case Map::TileSizeProperty: emit mTileSizeProperty->valueChanged(); break; case Map::InfiniteProperty: @@ -1149,9 +1130,9 @@ class MapProperties : public ObjectProperties Property *mStaggerIndexProperty; Property *mParallaxOriginProperty; Property *mLayerDataFormatProperty; + Property *mCompressionLevelProperty; SizeProperty *mChunkSizeProperty; Property *mRenderOrderProperty; - Property *mCompressionLevelProperty; Property *mBackgroundColorProperty; }; diff --git a/src/tiled/propertybrowser.cpp b/src/tiled/propertybrowser.cpp index 511a33eee1..13f31df60b 100644 --- a/src/tiled/propertybrowser.cpp +++ b/src/tiled/propertybrowser.cpp @@ -1147,18 +1147,16 @@ void PropertyBrowser::applyMapValue(PropertyId id, const QVariant &val) switch (id) { case TileWidthProperty: - command = new ChangeMapProperty(mMapDocument, Map::TileWidthProperty, - val.toInt()); + command = new ChangeMapTileSize(mMapDocument, QSize(val.toInt(), + mMapDocument->map()->tileHeight())); break; case TileHeightProperty: - command = new ChangeMapProperty(mMapDocument, Map::TileHeightProperty, - val.toInt()); + command = new ChangeMapTileSize(mMapDocument, QSize(mMapDocument->map()->tileWidth(), + val.toInt())); break; case InfiniteProperty: { - bool infinite = val.toInt(); - - auto changePropertyCommand = new ChangeMapProperty(mMapDocument, Map::InfiniteProperty, - val.toInt()); + const bool infinite = val.toInt(); + auto changePropertyCommand = new ChangeMapInfinite(mMapDocument, infinite); QUndoStack *undoStack = mDocument->undoStack(); undoStack->beginMacro(changePropertyCommand->text()); @@ -1184,54 +1182,53 @@ void PropertyBrowser::applyMapValue(PropertyId id, const QVariant &val) } case OrientationProperty: { Map::Orientation orientation = static_cast(val.toInt() + 1); - command = new ChangeMapProperty(mMapDocument, orientation); + command = new ChangeMapOrientation(mMapDocument, orientation); break; } case HexSideLengthProperty: { - command = new ChangeMapProperty(mMapDocument, Map::HexSideLengthProperty, - val.toInt()); + command = new ChangeMapHexSideLength(mMapDocument, val.toInt()); break; } case StaggerAxisProperty: { Map::StaggerAxis staggerAxis = static_cast(val.toInt()); - command = new ChangeMapProperty(mMapDocument, staggerAxis); + command = new ChangeMapStaggerAxis(mMapDocument, staggerAxis); break; } case StaggerIndexProperty: { Map::StaggerIndex staggerIndex = static_cast(val.toInt()); - command = new ChangeMapProperty(mMapDocument, staggerIndex); + command = new ChangeMapStaggerIndex(mMapDocument, staggerIndex); break; } case ParallaxOriginProperty: { - command = new ChangeMapProperty(mMapDocument, val.value()); + command = new ChangeMapParallaxOrigin(mMapDocument, val.value()); break; } case LayerFormatProperty: { Map::LayerDataFormat format = mLayerFormatValues.at(val.toInt()); - command = new ChangeMapProperty(mMapDocument, format); + command = new ChangeMapLayerDataFormat(mMapDocument, format); break; } case RenderOrderProperty: { Map::RenderOrder renderOrder = static_cast(val.toInt()); - command = new ChangeMapProperty(mMapDocument, renderOrder); + command = new ChangeMapRenderOrder(mMapDocument, renderOrder); break; } case BackgroundColorProperty: - command = new ChangeMapProperty(mMapDocument, val.value()); + command = new ChangeMapBackgroundColor(mMapDocument, val.value()); break; case CompressionLevelProperty: - command = new ChangeMapProperty(mMapDocument, Map::CompressionLevelProperty, val.toInt()); + command = new ChangeMapCompressionLevel(mMapDocument, val.toInt()); break; case ChunkWidthProperty: { QSize chunkSize = mMapDocument->map()->chunkSize(); chunkSize.setWidth(val.toInt()); - command = new ChangeMapProperty(mMapDocument, chunkSize); + command = new ChangeMapChunkSize(mMapDocument, chunkSize); break; } case ChunkHeightProperty: { QSize chunkSize = mMapDocument->map()->chunkSize(); chunkSize.setHeight(val.toInt()); - command = new ChangeMapProperty(mMapDocument, chunkSize); + command = new ChangeMapChunkSize(mMapDocument, chunkSize); break; } default: diff --git a/src/tiled/undocommands.h b/src/tiled/undocommands.h index 3af2985d35..2627662510 100644 --- a/src/tiled/undocommands.h +++ b/src/tiled/undocommands.h @@ -39,8 +39,20 @@ enum UndoCommands { Cmd_ChangeLayerParallaxFactor, Cmd_ChangeLayerTintColor, Cmd_ChangeLayerVisible, + Cmd_ChangeMapBackgroundColor, + Cmd_ChangeMapChunkSize, + Cmd_ChangeMapCompressionLevel, + Cmd_ChangeMapHexSideLength, + Cmd_ChangeMapInfinite, + Cmd_ChangeMapLayerDataFormat, Cmd_ChangeMapObject, Cmd_ChangeMapObjectTransform, + Cmd_ChangeMapOrientation, + Cmd_ChangeMapParallaxOrigin, + Cmd_ChangeMapRenderOrder, + Cmd_ChangeMapStaggerAxis, + Cmd_ChangeMapStaggerIndex, + Cmd_ChangeMapTileSize, Cmd_ChangeSelectedArea, Cmd_ChangeTileImageRect, Cmd_ChangeTileProbability,