Skip to content

Commit

Permalink
Scripting: Added a few new ways to interact with the UI
Browse files Browse the repository at this point in the history
    MapEditor.currentMapView
    MapView.scale
    MapView.centerOn(x, y)
    TileCollisionEditor.view
  • Loading branch information
bjorn committed Sep 24, 2019
1 parent 0d40a35 commit bb85b7e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
25 changes: 24 additions & 1 deletion docs/reference/scripting.rst
Expand Up @@ -1183,8 +1183,30 @@ Properties
.. csv-table::
:widths: 1, 2

**tilesetsView** : :ref:`script-tilesetsview` |ro|, "Access the Tilesets view."
**currentBrush** : :ref:`script-map`, "Get or set the currently used tile brush."
**currentMapView** : :ref:`script-mapview` |ro|, "Access the current map view."
**tilesetsView** : :ref:`script-tilesetsview` |ro|, "Access the Tilesets view."

.. _script-mapview:

Map View
^^^^^^^^

The view displaying the map.

Properties
~~~~~~~~~~

.. csv-table::
:widths: 1, 2

**scale** : number, "Get or set the scale of the view."

Functions
~~~~~~~~~

MapView.centerOn(x : number, y : number) : void
Centers the view at the given location in screen coordinates.

.. _script-tilesetsview:

Expand Down Expand Up @@ -1224,6 +1246,7 @@ Properties
:widths: 1, 2

**selectedObjects** : [:ref:`script-mapobject`], Selected objects.
**view** : [:ref:`script-mapview`], The map view used by the Collision Editor.

Functions
~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions src/tiled/mapeditor.h
Expand Up @@ -76,6 +76,7 @@ class MapEditor : public Editor

Q_PROPERTY(Tiled::TilesetDock *tilesetsView READ tilesetDock)
Q_PROPERTY(Tiled::EditableMap *currentBrush READ currentBrush WRITE setCurrentBrush)
Q_PROPERTY(Tiled::MapView *currentMapView READ currentMapView)

public:
explicit MapEditor(QObject *parent = nullptr);
Expand Down
14 changes: 12 additions & 2 deletions src/tiled/mapview.cpp
Expand Up @@ -111,6 +111,16 @@ MapScene *MapView::mapScene() const
return static_cast<MapScene*>(scene());
}

qreal MapView::scale() const
{
return mZoomable->scale();
}

void MapView::setScale(qreal scale)
{
mZoomable->setScale(scale);
}

void MapView::adjustScale(qreal scale)
{
const QTransform newTransform = QTransform::fromScale(scale, scale);
Expand Down Expand Up @@ -232,7 +242,7 @@ void MapView::forceCenterOn(const QPointF &pos)
{
// This is only to make it update QGraphicsViewPrivate::lastCenterPoint,
// just in case this is important.
centerOn(pos);
QGraphicsView::centerOn(pos);

auto hBar = static_cast<FlexibleScrollBar*>(horizontalScrollBar());
auto vBar = static_cast<FlexibleScrollBar*>(verticalScrollBar());
Expand Down Expand Up @@ -447,5 +457,5 @@ void MapView::adjustCenterFromMousePosition(QPoint mousePos)
QPointF viewCenterScenePos = mapToScene(view->rect().center());
QPointF mouseScenePos = mapToScene(view->mapFromGlobal(mousePos));
QPointF diff = viewCenterScenePos - mouseScenePos;
centerOn(mLastMouseScenePos + diff);
QGraphicsView::centerOn(mLastMouseScenePos + diff);
}
10 changes: 10 additions & 0 deletions src/tiled/mapview.h
Expand Up @@ -42,6 +42,8 @@ class MapView : public QGraphicsView
{
Q_OBJECT

Q_PROPERTY(qreal scale READ scale WRITE setScale)

public:
/**
* Using Qt::WA_StaticContents gives a performance boost in certain
Expand All @@ -64,9 +66,15 @@ class MapView : public QGraphicsView

Zoomable *zoomable() const { return mZoomable; }

qreal scale() const;
void setScale(qreal scale);

bool handScrolling() const { return mHandScrolling; }
void setHandScrolling(bool handScrolling);

using QGraphicsView::centerOn;
Q_INVOKABLE void centerOn(qreal x, qreal y) { forceCenterOn(QPointF(x, y)); };

void forceCenterOn(const QPointF &pos);

protected:
Expand Down Expand Up @@ -110,3 +118,5 @@ class MapView : public QGraphicsView
};

} // namespace Tiled

Q_DECLARE_METATYPE(Tiled::MapView*)
4 changes: 3 additions & 1 deletion src/tiled/scriptmanager.cpp
Expand Up @@ -31,6 +31,7 @@
#include "editabletileset.h"
#include "logginginterface.h"
#include "mapeditor.h"
#include "mapview.h"
#include "regionvaluetype.h"
#include "scriptedaction.h"
#include "scriptedtool.h"
Expand Down Expand Up @@ -91,13 +92,14 @@ ScriptManager::ScriptManager(QObject *parent)
qRegisterMetaType<EditableTileset*>();
qRegisterMetaType<Font>();
qRegisterMetaType<MapEditor*>();
qRegisterMetaType<MapView*>();
qRegisterMetaType<RegionValueType>();
qRegisterMetaType<ScriptedAction*>();
qRegisterMetaType<ScriptedTool*>();
qRegisterMetaType<TileCollisionDock*>();
qRegisterMetaType<TileLayerEdit*>();
qRegisterMetaType<TilesetEditor*>();
qRegisterMetaType<TilesetDock*>();
qRegisterMetaType<TilesetEditor*>();

connect(&mWatcher, &FileSystemWatcher::filesChanged,
this, &ScriptManager::scriptFilesChanged);
Expand Down
7 changes: 7 additions & 0 deletions src/tiled/tilecollisiondock.h
Expand Up @@ -46,6 +46,7 @@ class TileCollisionDock : public QDockWidget
Q_OBJECT

Q_PROPERTY(QList<QObject*> selectedObjects READ selectedObjectsForScript WRITE setSelectedObjectsFromScript)
Q_PROPERTY(Tiled::MapView *view READ mapView)

public:
enum Operation {
Expand All @@ -69,6 +70,7 @@ class TileCollisionDock : public QDockWidget
void setTilesetDocument(TilesetDocument *tilesetDocument);

MapDocument *dummyMapDocument() const;
MapView *mapView() const;

ToolManager *toolManager() const;

Expand Down Expand Up @@ -148,6 +150,11 @@ inline MapDocument *TileCollisionDock::dummyMapDocument() const
return mDummyMapDocument.data();
}

inline MapView *TileCollisionDock::mapView() const
{
return mMapView;
}

inline ToolManager *TileCollisionDock::toolManager() const
{
return mToolManager;
Expand Down

0 comments on commit bb85b7e

Please sign in to comment.