Skip to content

Commit

Permalink
Add Parallax Origin property to Map Properties
Browse files Browse the repository at this point in the history
  • Loading branch information
krukai committed Dec 15, 2021
1 parent cfd6777 commit 5ab9626
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/libtiled/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class TILEDSHARED_EXPORT Map : public Object
HexSideLengthProperty,
StaggerAxisProperty,
StaggerIndexProperty,
ParallaxOriginProperty,
OrientationProperty,
RenderOrderProperty,
BackgroundColorProperty,
Expand Down Expand Up @@ -159,6 +160,7 @@ class TILEDSHARED_EXPORT Map : public Object
int hexSideLength = 0;
StaggerAxis staggerAxis = StaggerY;
StaggerIndex staggerIndex = StaggerOdd;
QPointF parallaxOrigin;
QColor backgroundColor;
};

Expand Down Expand Up @@ -223,6 +225,9 @@ class TILEDSHARED_EXPORT Map : public Object
void setStaggerIndex(StaggerIndex staggerIndex);
void invertStaggerIndex();

QPointF parallaxOrigin() const;
void setParallaxOrigin(const QPointF &parallaxOrigin);

QMargins drawMargins() const;
void invalidateDrawMargins();

Expand Down Expand Up @@ -493,6 +498,16 @@ inline void Map::invertStaggerIndex()
mParameters.staggerIndex = static_cast<StaggerIndex>(!mParameters.staggerIndex);
}

inline QPointF Map::parallaxOrigin() const
{
return mParameters.parallaxOrigin;
}

inline void Map::setParallaxOrigin(const QPointF &parallaxOrigin)
{
mParameters.parallaxOrigin = parallaxOrigin;
}

inline void Map::invalidateDrawMargins()
{
mDrawMarginsDirty = true;
Expand Down
9 changes: 9 additions & 0 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ std::unique_ptr<Map> MapReaderPrivate::readMap()
mapParameters.staggerAxis = staggerAxisFromString(staggerAxis);
mapParameters.staggerIndex = staggerIndexFromString(staggerIndex);

bool ok;
const qreal parallaxOriginX = atts.value(QLatin1String("parallaxoriginx")).toDouble(&ok);
if (ok)
mapParameters.parallaxOrigin.setX(parallaxOriginX);

const qreal parallaxOriginY = atts.value(QLatin1String("parallaxoriginy")).toDouble(&ok);
if (ok)
mapParameters.parallaxOrigin.setY(parallaxOriginY);

const QString backgroundColor = atts.value(QLatin1String("backgroundcolor")).toString();
if (QColor::isValidColor(backgroundColor))
mapParameters.backgroundColor = QColor(backgroundColor);
Expand Down
5 changes: 5 additions & 0 deletions src/libtiled/mapwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ void MapWriterPrivate::writeMap(QXmlStreamWriter &w, const Map &map)
staggerIndexToString(map.staggerIndex()));
}

if (!map.parallaxOrigin().isNull()) {
w.writeAttribute(QStringLiteral("parallaxoriginx"), QString::number(map.parallaxOrigin().x()));
w.writeAttribute(QStringLiteral("parallaxoriginy"), QString::number(map.parallaxOrigin().y()));
}

if (map.backgroundColor().isValid()) {
w.writeAttribute(QStringLiteral("backgroundcolor"),
colorToString(map.backgroundColor()));
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/lua/luaplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ void LuaWriter::writeMap(const Map *map)
staggerIndexToString(map->staggerIndex()));
}

if (!map->parallaxOrigin().isNull()) {
mWriter.writeStartTable("parallaxorigin");
mWriter.writeKeyAndValue("x", map->parallaxOrigin().x());
mWriter.writeKeyAndValue("y", map->parallaxOrigin().y());
mWriter.writeEndTable();
}

const QColor &backgroundColor = map->backgroundColor();
if (backgroundColor.isValid())
writeColor("backgroundcolor", backgroundColor);
Expand Down
16 changes: 16 additions & 0 deletions src/tiled/changemapproperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument,
{
}

ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument,
const QPointF &parallaxOrigin)
: 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",
Expand Down Expand Up @@ -190,6 +200,12 @@ void ChangeMapProperty::swap()
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);
Expand Down
9 changes: 9 additions & 0 deletions src/tiled/changemapproperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ class ChangeMapProperty : public QUndoCommand
*/
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 &parallaxOrigin);

/**
* Constructs a command that changes the map orientation.
*
Expand Down Expand Up @@ -112,6 +120,7 @@ class ChangeMapProperty : public QUndoCommand
int mIntValue;
Map::StaggerAxis mStaggerAxis;
Map::StaggerIndex mStaggerIndex;
QPointF mParallaxOrigin;
Map::Orientation mOrientation;
Map::RenderOrder mRenderOrder;
Map::LayerDataFormat mLayerDataFormat;
Expand Down
8 changes: 8 additions & 0 deletions src/tiled/editablemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,14 @@ void EditableMap::setStaggerIndex(StaggerIndex value)
map()->setStaggerIndex(static_cast<Map::StaggerIndex>(value));
}

void EditableMap::setParallaxOrigin(const QPointF &parallaxOrigin)
{
if (auto doc = mapDocument())
push(new ChangeMapProperty(doc, parallaxOrigin));
else if (!checkReadOnly())
map()->setParallaxOrigin(parallaxOrigin);
}

void EditableMap::setOrientation(Orientation value)
{
if (auto doc = mapDocument()) {
Expand Down
8 changes: 8 additions & 0 deletions src/tiled/editablemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class EditableMap : public EditableAsset
Q_PROPERTY(int hexSideLength READ hexSideLength WRITE setHexSideLength)
Q_PROPERTY(StaggerAxis staggerAxis READ staggerAxis WRITE setStaggerAxis)
Q_PROPERTY(StaggerIndex staggerIndex READ staggerIndex WRITE setStaggerIndex)
Q_PROPERTY(QPointF parallaxOrigin READ parallaxOrigin WRITE setParallaxOrigin)
Q_PROPERTY(Orientation orientation READ orientation WRITE setOrientation)
Q_PROPERTY(RenderOrder renderOrder READ renderOrder WRITE setRenderOrder)
Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
Expand Down Expand Up @@ -120,6 +121,7 @@ class EditableMap : public EditableAsset
int hexSideLength() const;
StaggerAxis staggerAxis() const;
StaggerIndex staggerIndex() const;
QPointF parallaxOrigin() const;
Orientation orientation() const;
RenderOrder renderOrder() const;
QColor backgroundColor() const;
Expand Down Expand Up @@ -179,6 +181,7 @@ class EditableMap : public EditableAsset
void setHexSideLength(int value);
void setStaggerAxis(StaggerAxis value);
void setStaggerIndex(StaggerIndex value);
void setParallaxOrigin(const QPointF &parallaxOrigin);
void setOrientation(Orientation value);
void setRenderOrder(RenderOrder value);
void setBackgroundColor(const QColor &value);
Expand Down Expand Up @@ -266,6 +269,11 @@ inline EditableMap::StaggerIndex EditableMap::staggerIndex() const
return static_cast<StaggerIndex>(map()->staggerIndex());
}

inline QPointF EditableMap::parallaxOrigin() const
{
return map()->parallaxOrigin();
}

inline EditableMap::Orientation EditableMap::orientation() const
{
return static_cast<Orientation>(map()->orientation());
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/mapscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ QPointF MapScene::parallaxOffset(const Layer &layer) const
return {};

const QPointF parallaxFactor = layer.effectiveParallaxFactor();
const QPointF viewCenter = mViewRect.center();
const QPointF viewCenter = mViewRect.center() + mapDocument()->map()->parallaxOrigin();
return QPointF((1.0 - parallaxFactor.x()) * viewCenter.x(),
(1.0 - parallaxFactor.y()) * viewCenter.y());
}
Expand Down
7 changes: 7 additions & 0 deletions src/tiled/propertybrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ void PropertyBrowser::addMapProperties()

staggerIndexProperty->setAttribute(QLatin1String("enumNames"), mStaggerIndexNames);

addProperty(ParallaxOriginProperty, QMetaType::QPointF, tr("Parallax Origin"), groupProperty);

QtVariantProperty *layerFormatProperty =
addProperty(LayerFormatProperty,
QtVariantPropertyManager::enumTypeId(),
Expand Down Expand Up @@ -1025,6 +1027,10 @@ void PropertyBrowser::applyMapValue(PropertyId id, const QVariant &val)
command = new ChangeMapProperty(mMapDocument, staggerIndex);
break;
}
case ParallaxOriginProperty: {
command = new ChangeMapProperty(mMapDocument, val.value<QPointF>());
break;
}
case LayerFormatProperty: {
Map::LayerDataFormat format = mLayerFormatValues.at(val.toInt());
command = new ChangeMapProperty(mMapDocument, format);
Expand Down Expand Up @@ -1679,6 +1685,7 @@ void PropertyBrowser::updateProperties()
mIdToProperty[HexSideLengthProperty]->setValue(map->hexSideLength());
mIdToProperty[StaggerAxisProperty]->setValue(map->staggerAxis());
mIdToProperty[StaggerIndexProperty]->setValue(map->staggerIndex());
mIdToProperty[ParallaxOriginProperty]->setValue(map->parallaxOrigin());
mIdToProperty[LayerFormatProperty]->setValue(mLayerFormatValues.indexOf(map->layerDataFormat()));
mIdToProperty[CompressionLevelProperty]->setValue(map->compressionLevel());
mIdToProperty[RenderOrderProperty]->setValue(map->renderOrder());
Expand Down
1 change: 1 addition & 0 deletions src/tiled/propertybrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class PropertyBrowser : public QtTreePropertyBrowser
HexSideLengthProperty,
StaggerAxisProperty,
StaggerIndexProperty,
ParallaxOriginProperty,
RenderOrderProperty,
LayerFormatProperty,
ImageSourceProperty,
Expand Down

0 comments on commit 5ab9626

Please sign in to comment.