Skip to content

Commit

Permalink
Refactor ChangeImageLayerProperties
Browse files Browse the repository at this point in the history
Use the same pattern as ChangeMapProperty. Renaming the class
accordingly will happen in a future commit.
  • Loading branch information
krukai committed Dec 9, 2021
1 parent f0cfc5e commit bd583f3
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 116 deletions.
100 changes: 67 additions & 33 deletions src/tiled/changeimagelayerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,53 +32,87 @@ using namespace Tiled;
ChangeImageLayerProperties::ChangeImageLayerProperties(
MapDocument *mapDocument,
ImageLayer *imageLayer,
const QColor &color,
const QUrl &newSource,
bool newRepeatX,
bool newRepeatY)
: QUndoCommand(
QCoreApplication::translate(
"Undo Commands", "Change Image Layer Properties"))
const QColor transparentColor)
: QUndoCommand(QCoreApplication::translate("Undo Commands",
"Change Image Layer Transparent Color"))
, mMapDocument(mapDocument)
, mImageLayer(imageLayer)
, mUndoColor(imageLayer->transparentColor())
, mRedoColor(color)
, mUndoSource(imageLayer->imageSource())
, mRedoSource(newSource)
, mUndoRepeatX(imageLayer->repeatX())
, mRedoRepeatX(newRepeatX)
, mUndoRepeatY(imageLayer->repeatY())
, mRedoRepeatY(newRepeatY)
, mProperty(TransparentColorProperty)
, mTransparentColor(transparentColor)
{
}

void ChangeImageLayerProperties::redo()
ChangeImageLayerProperties::ChangeImageLayerProperties(
MapDocument *mapDocument,
ImageLayer *imageLayer,
const QUrl imageSource)
: QUndoCommand(QCoreApplication::translate("Undo Commands",
"Change Image Layer Image Source"))
, mMapDocument(mapDocument)
, mImageLayer(imageLayer)
, mProperty(ImageSourceProperty)
, mImageSource(imageSource)
{
mImageLayer->setTransparentColor(mRedoColor);

if (mRedoSource.isEmpty())
mImageLayer->resetImage();
else
mImageLayer->loadFromImage(mRedoSource);
}

mImageLayer->setRepeatX(mRedoRepeatX);
mImageLayer->setRepeatY(mRedoRepeatY);
ChangeImageLayerProperties::ChangeImageLayerProperties(
MapDocument *mapDocument,
ImageLayer *imageLayer,
ChangeImageLayerProperties::Property property,
bool repeat)
: QUndoCommand(QCoreApplication::translate("Undo Commands",
"Change Image Layer Repeat Property"))
, mMapDocument(mapDocument)
, mImageLayer(imageLayer)
, mProperty(property)
, mRepeat(repeat)
{
}

emit mMapDocument->imageLayerChanged(mImageLayer);
void ChangeImageLayerProperties::redo()
{
swap();
}

void ChangeImageLayerProperties::undo()
{
mImageLayer->setTransparentColor(mUndoColor);
swap();
}

if (mUndoSource.isEmpty())
mImageLayer->resetImage();
else
mImageLayer->loadFromImage(mUndoSource);
void ChangeImageLayerProperties::swap()
{
switch (mProperty) {
case TransparentColorProperty: {
const QColor color = mImageLayer->transparentColor();
mImageLayer->setTransparentColor(mTransparentColor);
mTransparentColor = color;
break;
}
case ImageSourceProperty: {
const QUrl source = mImageLayer->imageSource();
mImageLayer->setSource(mImageSource);
mImageSource = source;

mImageLayer->setRepeatX(mUndoRepeatX);
mImageLayer->setRepeatY(mUndoRepeatY);
if (mImageSource.isEmpty())
mImageLayer->resetImage();
else
mImageLayer->loadFromImage(mImageSource);

break;
}
case RepeatXProperty: {
const bool repeatX = mImageLayer->repeatX();
mImageLayer->setRepeatX(mRepeat);
mRepeat = repeatX;
break;
}
case RepeatYProperty: {
const bool repeatY = mImageLayer->repeatY();
mImageLayer->setRepeatY(mRepeat);
mRepeat = repeatY;
break;
}
}

emit mMapDocument->imageLayerChanged(mImageLayer);
}

65 changes: 46 additions & 19 deletions src/tiled/changeimagelayerproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,64 @@ class MapDocument;
class ChangeImageLayerProperties : public QUndoCommand
{
public:
enum Property {
TransparentColorProperty,
ImageSourceProperty,
RepeatXProperty,
RepeatYProperty
};

/**
* Constructs a command that changes the transparent color.
*
* @param mapDocument the map document of the image layer's map
* @param imageLayer the image layer to modify
* @param transparentColor the new transparent color to apply
*/
ChangeImageLayerProperties(MapDocument *mapDocument,
ImageLayer *imageLayer,
const QColor transparentColor);

/**
* Constructs a command that changes the image source.
*
* @param mapDocument the map document of the image layer's map
* @param imageLayer the image layer to modify
* @param repeat the new image source to apply
*/
ChangeImageLayerProperties(MapDocument *mapDocument,
ImageLayer *imageLayer,
const QUrl imageSource);

/**
* Constructs a new 'Change Image Layer Properties' command.
* Constructs a command that changes the repetition along an axis.
*
* @param mapDocument the map document of the layer's map
* Can only be used for the properties RepeatXProperty and RepeatYProperty.
*
* @param mapDocument the map document of the image layer's map
* @param imageLayer the image layer to modify
* @param newColor the new transparent color to apply
* @param newSource the new image source to apply
* @param newRepeatX the new repetition along the X axis to apply
* @param newRepeatY the new repetition along the Y axis to apply
* @param property the image layer property to modify
* @param repeat the new repetition along an axis to apply
*/
ChangeImageLayerProperties(MapDocument *mapDocument,
ImageLayer *imageLayer,
const QColor &newColor,
const QUrl &newSource,
bool newRepeatX,
bool newRepeatY);
ChangeImageLayerProperties::Property property,
bool repeat);

void undo() override;
void redo() override;

private:
void swap();

MapDocument *mMapDocument;
ImageLayer *mImageLayer;
const QColor mUndoColor;
const QColor mRedoColor;
const QUrl mUndoSource;
const QUrl mRedoSource;
const bool mUndoRepeatX;
const bool mRedoRepeatX;
const bool mUndoRepeatY;
const bool mRedoRepeatY;
ImageLayer *mImageLayer;
Property mProperty;
QUrl mImageSource;
union {
QColor mTransparentColor;
bool mRepeat;
};
};

} // namespace Tiled
20 changes: 5 additions & 15 deletions src/tiled/editableimagelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ void EditableImageLayer::setTransparentColor(const QColor &transparentColor)
if (auto doc = mapDocument()) {
asset()->push(new ChangeImageLayerProperties(doc,
imageLayer(),
transparentColor,
imageSource(),
repeatX(),
repeatY()));
transparentColor));
} else {
imageLayer()->setTransparentColor(transparentColor);
if (!imageSource().isEmpty())
Expand All @@ -57,10 +54,7 @@ void EditableImageLayer::setImageSource(const QUrl &imageSource)
if (auto doc = mapDocument()) {
asset()->push(new ChangeImageLayerProperties(doc,
imageLayer(),
transparentColor(),
imageSource,
repeatX(),
repeatY()));
imageSource));
} else {
if (imageSource.isEmpty())
imageLayer()->resetImage();
Expand All @@ -80,10 +74,8 @@ void EditableImageLayer::setRepeatX(bool repeatX)
if (auto doc = mapDocument()) {
asset()->push(new ChangeImageLayerProperties(doc,
imageLayer(),
transparentColor(),
imageSource(),
repeatX,
repeatY()));
ChangeImageLayerProperties::RepeatXProperty,
repeatX));
} else {
imageLayer()->setRepeatX(repeatX);
}
Expand All @@ -94,9 +86,7 @@ void EditableImageLayer::setRepeatY(bool repeatY)
if (auto doc = mapDocument()) {
asset()->push(new ChangeImageLayerProperties(doc,
imageLayer(),
transparentColor(),
imageSource(),
repeatX(),
ChangeImageLayerProperties::RepeatYProperty,
repeatY));
} else {
imageLayer()->setRepeatY(repeatY);
Expand Down
64 changes: 15 additions & 49 deletions src/tiled/propertybrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,66 +1275,32 @@ QUndoCommand *PropertyBrowser::applyObjectGroupValueTo(PropertyId id, const QVar

QUndoCommand *PropertyBrowser::applyImageLayerValueTo(PropertyId id, const QVariant &val, ImageLayer *imageLayer)
{
QUndoCommand *command = nullptr;

switch (id) {
case ImageSourceProperty: {
const FilePath imageSource = val.value<FilePath>();
const QColor &color = imageLayer->transparentColor();
const bool repeatX = imageLayer->repeatX();
const bool repeatY = imageLayer->repeatY();
command = new ChangeImageLayerProperties(mMapDocument,
imageLayer,
color,
imageSource.url,
repeatX,
repeatY);
break;
return new ChangeImageLayerProperties(mMapDocument,
imageLayer,
val.value<FilePath>().url);
}
case ColorProperty: {
const QColor color = val.value<QColor>();
const QUrl &imageSource = imageLayer->imageSource();
const bool repeatX = imageLayer->repeatX();
const bool repeatY = imageLayer->repeatY();
command = new ChangeImageLayerProperties(mMapDocument,
imageLayer,
color,
imageSource,
repeatX,
repeatY);
break;
return new ChangeImageLayerProperties(mMapDocument,
imageLayer,
val.value<QColor>());
}
case RepeatXProperty: {
const bool repeatX = val.toBool();
const QUrl &imageSource = imageLayer->imageSource();
const QColor &color = imageLayer->transparentColor();
const bool repeatY = imageLayer->repeatY();
command = new ChangeImageLayerProperties(mMapDocument,
imageLayer,
color,
imageSource,
repeatX,
repeatY);
break;
return new ChangeImageLayerProperties(mMapDocument,
imageLayer,
ChangeImageLayerProperties::RepeatXProperty,
val.toBool());
}
case RepeatYProperty: {
const bool repeatY = val.toBool();
const QUrl &imageSource = imageLayer->imageSource();
const QColor &color = imageLayer->transparentColor();
const bool repeatX = imageLayer->repeatX();
command = new ChangeImageLayerProperties(mMapDocument,
imageLayer,
color,
imageSource,
repeatX,
repeatY);
break;
return new ChangeImageLayerProperties(mMapDocument,
imageLayer,
ChangeImageLayerProperties::RepeatYProperty,
val.toBool());
}
default:
break;
return nullptr;
}

return command;
}

QUndoCommand *PropertyBrowser::applyGroupLayerValueTo(PropertyId id, const QVariant &val, GroupLayer *groupLayer)
Expand Down

0 comments on commit bd583f3

Please sign in to comment.