Skip to content

Commit

Permalink
Made parallax factor scriptable and documented it
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn committed Dec 15, 2020
1 parent 16140e1 commit cfe6f74
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
36 changes: 33 additions & 3 deletions docs/manual/layers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ foreground or background graphics. The order of the layers determines
the rendering order of your content.

Layers can be hidden, made only partially visible and can be locked. Layers
also have an offset, which can be used to position them independently of each
other, for example to fake depth. Finally their contents can be tinted by
multiplying with a custom :ref:`tint color <tint-color>`.
also have an offset and a :ref:`parallax scrolling factor <parallax-factor>`,
which can be used to position them independently of each other, for example to
fake depth. Finally their contents can be tinted by multiplying with a custom
:ref:`tint color <tint-color>`.

.. figure:: images/layers/lock-visibility-toggle.png
:alt: Layers View
Expand Down Expand Up @@ -125,6 +126,35 @@ Layers can be easily dragged in and out of groups with the mouse. The
Raise Layer / Lower Layer actions also allow moving layers in and out of
groups.

.. raw:: html

<div class="new">New in Tiled 1.5</div>

.. _parallax-factor:

Parallax Scrolling Factor
-------------------------

The parallax scrolling factor determines the amount by which the layer moves in
relation to the camera.

By default its value is 1, which means its position on the screen changes at
the same rate as the position of the camera (in opposite direction). A lower
value makes it move slower, simulating a layer that is further away, whereas
a higher value makes it move faster, simulating a layer positioned in between
the screen and the camera.

A value of 0 makes the layer not move at all, which can be useful to include
some pieces of your ingame UI or to mark its general viewport boundaries.

Negative values make the layer move in opposite direction, though this is rarely
useful.

When the parallax scrolling factor is set on a group layer, it applies to all
its child layers. The effective parallax scrolling factor of a layer is
determined by multiplying the parallax scrolling factor by the scrolling
factors of all parent layers.

.. raw:: html

<div class="new">New in Tiled 1.4</div>
Expand Down
4 changes: 3 additions & 1 deletion docs/reference/json-map-format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ Layer
offsetx, double, "Horizontal layer offset in pixels (default: 0)"
offsety, double, "Vertical layer offset in pixels (default: 0)"
opacity, double, "Value between 0 and 1"
parallaxx, double, "Horizontal :ref:`parallax factor <parallax-factor>` for this layer (default: 1). (since Tiled 1.5)"
parallaxy, double, "Vertical :ref:`parallax factor <parallax-factor>` for this layer (default: 1). (since Tiled 1.5)"
properties, array, "Array of :ref:`Properties <json-property>`"
startx, int, "X coordinate where layer content starts (for infinite maps)"
starty, int, "Y coordinate where layer content starts (for infinite maps)"
tintcolor, string, "Hex-formatted color (#RRGGBB or #AARRGGBB) that is multiplied with any graphics drawn by this layer or any child layers (optional)."
tintcolor, string, "Hex-formatted :ref:`tint color <tint-color>` (#RRGGBB or #AARRGGBB) that is multiplied with any graphics drawn by this layer or any child layers (optional)."
transparentcolor, string, "Hex-formatted color (#RRGGBB) (optional). ``imagelayer`` only."
type, string, "``tilelayer``, ``objectgroup``, ``imagelayer`` or ``group``"
visible, bool, "Whether layer is shown or hidden in editor"
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/scripting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,8 @@ Properties
**visible** : bool, Whether the layer is visible (affects child layer visibility for group layers).
**locked** : bool, Whether the layer is locked (affects whether child layers are locked for group layers).
**offset** : :ref:`script-point`, Offset in pixels that is applied when this layer is rendered.
**map** : :ref:`script-map`, Map that this layer is part of (or ``null`` in case of a standalone layer).
**parallaxFactor** : :ref:`script-point`, The parallax factor of the layer (since Tiled 1.5).
**map** : :ref:`script-map` |ro|, Map that this layer is part of (or ``null`` in case of a standalone layer).
**selected** : bool, Whether the layer is selected.
**isTileLayer** : bool |ro|, Whether this layer is a :ref:`script-tilelayer`.
**isObjectLayer** : bool |ro|, Whether this layer is an :ref:`script-objectgroup`.
Expand Down
4 changes: 3 additions & 1 deletion docs/reference/tmx-map-format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,13 @@ tiles.
- **height:** The height of the layer in tiles. Always the same as the map height for fixed-size maps.
- **opacity:** The opacity of the layer as a value from 0 to 1. Defaults to 1.
- **visible:** Whether the layer is shown (1) or hidden (0). Defaults to 1.
- **tintcolor:** A color that is multiplied with any tiles drawn by this layer in ``#AARRGGBB`` or ``#RRGGBB`` format (optional).
- **tintcolor:** A :ref:`tint color <tint-color>` that is multiplied with any tiles drawn by this layer in ``#AARRGGBB`` or ``#RRGGBB`` format (optional).
- **offsetx:** Horizontal offset for this layer in pixels. Defaults to 0.
(since 0.14)
- **offsety:** Vertical offset for this layer in pixels. Defaults to 0.
(since 0.14)
- **parallaxx:** Horizontal :ref:`parallax factor <parallax-factor>` for this layer. Defaults to 1. (since 1.5)
- **parallaxy:** Vertical :ref:`parallax factor <parallax-factor>` for this layer. Defaults to 1. (since 1.5)

Can contain at most one: :ref:`tmx-properties`, :ref:`tmx-data`

Expand Down
8 changes: 8 additions & 0 deletions src/tiled/editablelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ void EditableLayer::setOffset(QPointF offset)
layer()->setOffset(offset);
}

void EditableLayer::setParallaxFactor(QPointF factor)
{
if (auto doc = document())
asset()->push(new SetLayerParallaxFactor(doc, layer(), factor));
else if (!checkReadOnly())
layer()->setParallaxFactor(factor);
}

void EditableLayer::setSelected(bool selected)
{
auto document = mapDocument();
Expand Down
8 changes: 8 additions & 0 deletions src/tiled/editablelayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class EditableLayer : public EditableObject
Q_PROPERTY(bool visible READ isVisible WRITE setVisible)
Q_PROPERTY(bool locked READ isLocked WRITE setLocked)
Q_PROPERTY(QPointF offset READ offset WRITE setOffset)
Q_PROPERTY(QPointF parallaxFactor READ parallaxFactor WRITE setParallaxFactor)
Q_PROPERTY(Tiled::EditableMap *map READ map)
Q_PROPERTY(bool selected READ isSelected WRITE setSelected)
Q_PROPERTY(bool isTileLayer READ isTileLayer CONSTANT)
Expand All @@ -62,6 +63,7 @@ class EditableLayer : public EditableObject
bool isVisible() const;
bool isLocked() const;
QPointF offset() const;
QPointF parallaxFactor() const;
EditableMap *map() const;
bool isSelected() const;
bool isTileLayer() const;
Expand All @@ -83,6 +85,7 @@ public slots:
void setVisible(bool visible);
void setLocked(bool locked);
void setOffset(QPointF offset);
void setParallaxFactor(QPointF factor);
void setSelected(bool selected);

protected:
Expand Down Expand Up @@ -123,6 +126,11 @@ inline QPointF EditableLayer::offset() const
return layer()->offset();
}

inline QPointF EditableLayer::parallaxFactor() const
{
return layer()->parallaxFactor();
}

inline bool EditableLayer::isTileLayer() const
{
return layer()->isTileLayer();
Expand Down

0 comments on commit cfe6f74

Please sign in to comment.