Skip to content

Commit

Permalink
Added the Objects dock and per-MapObject visibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Baker committed May 17, 2012
1 parent 61a9ffa commit 92e49a8
Show file tree
Hide file tree
Showing 21 changed files with 1,176 additions and 37 deletions.
27 changes: 27 additions & 0 deletions src/libtiled/map.cpp
Expand Up @@ -31,6 +31,7 @@
#include "map.h"

#include "layer.h"
#include "objectgroup.h"
#include "tile.h"
#include "tilelayer.h"
#include "tileset.h"
Expand Down Expand Up @@ -82,6 +83,32 @@ int Map::layerCount(Layer::Type type) const
return count;
}

QList<Layer*> Map::layers(Layer::Type type) const
{
QList<Layer*> layers;
foreach (Layer *layer, mLayers)
if (layer->type() == type)
layers.append(layer);
return layers;
}

QList<ObjectGroup*> Map::objectGroups() const
{
QList<ObjectGroup*> layers;
foreach (Layer *layer, mLayers)
if (ObjectGroup *og = layer->asObjectGroup())
layers.append(og);
return layers;
}

QList<TileLayer*> Map::tileLayers() const
{
QList<TileLayer*> layers;
foreach (Layer *layer, mLayers)
if (TileLayer *tl = layer->asTileLayer())
layers.append(tl);
return layers;
}
void Map::addLayer(Layer *layer)
{
adoptLayer(layer);
Expand Down
4 changes: 4 additions & 0 deletions src/libtiled/map.h
Expand Up @@ -175,6 +175,10 @@ class TILEDSHARED_EXPORT Map : public Object
*/
const QList<Layer*> &layers() const { return mLayers; }

QList<Layer*> layers(Layer::Type type) const;
QList<ObjectGroup*> objectGroups() const;
QList<TileLayer*> tileLayers() const;

/**
* Adds a layer to this map.
*/
Expand Down
6 changes: 4 additions & 2 deletions src/libtiled/mapobject.cpp
Expand Up @@ -35,7 +35,8 @@ MapObject::MapObject():
mSize(0, 0),
mShape(Rectangle),
mTile(0),
mObjectGroup(0)
mObjectGroup(0),
mVisible(true)
{
}

Expand All @@ -48,7 +49,8 @@ MapObject::MapObject(const QString &name, const QString &type,
mSize(size),
mShape(Rectangle),
mTile(0),
mObjectGroup(0)
mObjectGroup(0),
mVisible(true)
{
}

Expand Down
4 changes: 4 additions & 0 deletions src/libtiled/mapobject.h
Expand Up @@ -228,6 +228,9 @@ class TILEDSHARED_EXPORT MapObject : public Object
*/
MapObject *clone() const;

bool isVisible() const { return mVisible; }
void setVisible(bool visible) { mVisible = visible; }

private:
QString mName;
QString mType;
Expand All @@ -237,6 +240,7 @@ class TILEDSHARED_EXPORT MapObject : public Object
Shape mShape;
Tile *mTile;
ObjectGroup *mObjectGroup;
bool mVisible;
};

} // namespace Tiled
Expand Down
11 changes: 3 additions & 8 deletions src/tiled/addremovemapobject.cpp
Expand Up @@ -23,6 +23,7 @@
#include "mapdocument.h"
#include "mapobject.h"
#include "objectgroup.h"
#include "mapobjectmodel.h"

#include <QCoreApplication>

Expand Down Expand Up @@ -51,19 +52,13 @@ AddRemoveMapObject::~AddRemoveMapObject()

void AddRemoveMapObject::addObject()
{
if (mIndex == -1)
mObjectGroup->addObject(mMapObject);
else
mObjectGroup->insertObject(mIndex, mMapObject);

mMapDocument->emitObjectAdded(mMapObject);
mMapDocument->mapObjectModel()->insertObject(mObjectGroup, mIndex, mMapObject);
mOwnsObject = false;
}

void AddRemoveMapObject::removeObject()
{
mIndex = mObjectGroup->removeObject(mMapObject);
mMapDocument->emitObjectRemoved(mMapObject);
mIndex = mMapDocument->mapObjectModel()->removeObject(mObjectGroup, mMapObject);
mOwnsObject = true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/tiled/changemapobject.cpp
Expand Up @@ -22,6 +22,7 @@

#include "mapdocument.h"
#include "mapobject.h"
#include "mapobjectmodel.h"

#include <QCoreApplication>

Expand Down Expand Up @@ -56,9 +57,8 @@ void ChangeMapObject::swap()
const QString name = mMapObject->name();
const QString type = mMapObject->type();

mMapObject->setName(mName);
mMapObject->setType(mType);
mMapDocument->emitObjectChanged(mMapObject);
mMapDocument->mapObjectModel()->setObjectName(mMapObject, mName);
mMapDocument->mapObjectModel()->setObjectType(mMapObject, mType);

mName = name;
mType = type;
Expand Down
6 changes: 4 additions & 2 deletions src/tiled/changeobjectgroupproperties.cpp
Expand Up @@ -23,6 +23,7 @@

#include "mapdocument.h"
#include "objectgroup.h"
#include "mapobjectmodel.h"

#include <QCoreApplication>

Expand All @@ -46,11 +47,12 @@ ChangeObjectGroupProperties::ChangeObjectGroupProperties(
void ChangeObjectGroupProperties::redo()
{
mObjectGroup->setColor(mRedoColor);
mMapDocument->emitObjectsChanged(mObjectGroup->objects());
mMapDocument->mapObjectModel()->emitObjectsChanged(mObjectGroup->objects());

}

void ChangeObjectGroupProperties::undo()
{
mObjectGroup->setColor(mUndoColor);
mMapDocument->emitObjectsChanged(mObjectGroup->objects());
mMapDocument->mapObjectModel()->emitObjectsChanged(mObjectGroup->objects());
}
7 changes: 3 additions & 4 deletions src/tiled/changepolygon.cpp
Expand Up @@ -22,6 +22,7 @@

#include "mapdocument.h"
#include "mapobject.h"
#include "mapobjectmodel.h"

#include <QCoreApplication>

Expand All @@ -41,12 +42,10 @@ ChangePolygon::ChangePolygon(MapDocument *mapDocument,

void ChangePolygon::undo()
{
mMapObject->setPolygon(mOldPolygon);
mMapDocument->emitObjectChanged(mMapObject);
mMapDocument->mapObjectModel()->setObjectPolygon(mMapObject, mOldPolygon);
}

void ChangePolygon::redo()
{
mMapObject->setPolygon(mNewPolygon);
mMapDocument->emitObjectChanged(mMapObject);
mMapDocument->mapObjectModel()->setObjectPolygon(mMapObject, mNewPolygon);
}
6 changes: 6 additions & 0 deletions src/tiled/mainwindow.cpp
Expand Up @@ -74,6 +74,7 @@
#include "utils.h"
#include "zoomable.h"
#include "commandbutton.h"
#include "objectsdock.h"

#ifdef Q_WS_MAC
#include "macsupport.h"
Expand Down Expand Up @@ -105,6 +106,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
, mMapDocument(0)
, mActionHandler(new MapDocumentActionHandler(this))
, mLayerDock(new LayerDock(this))
, mObjectsDock(new ObjectsDock())
, mTilesetDock(new TilesetDock(this))
, mCurrentLayerLabel(new QLabel)
, mZoomable(0)
Expand Down Expand Up @@ -161,6 +163,8 @@ MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
addDockWidget(Qt::RightDockWidgetArea, mLayerDock);
addDockWidget(Qt::RightDockWidgetArea, undoDock);
tabifyDockWidget(undoDock, mLayerDock);
addDockWidget(Qt::RightDockWidgetArea, mObjectsDock);
tabifyDockWidget(mLayerDock, mObjectsDock);
addDockWidget(Qt::RightDockWidgetArea, mTilesetDock);

statusBar()->addPermanentWidget(mZoomComboBox);
Expand Down Expand Up @@ -365,6 +369,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
mUi->menuView->addAction(mTilesetDock->toggleViewAction());
mUi->menuView->addAction(mLayerDock->toggleViewAction());
mUi->menuView->addAction(undoDock->toggleViewAction());
mUi->menuView->addAction(mObjectsDock->toggleViewAction());

connect(mClipboardManager, SIGNAL(hasMapChanged()), SLOT(updateActions()));

Expand Down Expand Up @@ -1440,6 +1445,7 @@ void MainWindow::mapDocumentChanged(MapDocument *mapDocument)

mActionHandler->setMapDocument(mMapDocument);
mLayerDock->setMapDocument(mMapDocument);
mObjectsDock->setMapDocument(mMapDocument);
mTilesetDock->setMapDocument(mMapDocument);
AutomappingManager::instance()->setMapDocument(mMapDocument);
QuickStampManager::instance()->setMapDocument(mMapDocument);
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/mainwindow.h
Expand Up @@ -55,6 +55,7 @@ class BucketFillTool;
class TilesetDock;
class MapView;
class CommandButton;
class ObjectsDock;
class Zoomable;

/**
Expand Down Expand Up @@ -202,6 +203,7 @@ public slots:
MapDocument *mMapDocument;
MapDocumentActionHandler *mActionHandler;
LayerDock *mLayerDock;
ObjectsDock *mObjectsDock;
TilesetDock *mTilesetDock;
QLabel *mCurrentLayerLabel;
Zoomable *mZoomable;
Expand Down
33 changes: 33 additions & 0 deletions src/tiled/mapdocument.cpp
Expand Up @@ -29,6 +29,7 @@
#include "imagelayer.h"
#include "isometricrenderer.h"
#include "layermodel.h"
#include "mapobjectmodel.h"
#include "map.h"
#include "mapobject.h"
#include "movelayer.h"
Expand Down Expand Up @@ -56,6 +57,7 @@ MapDocument::MapDocument(Map *map, const QString &fileName):
mFileName(fileName),
mMap(map),
mLayerModel(new LayerModel(this)),
mMapObjectModel(new MapObjectModel(this)),
mUndoStack(new QUndoStack(this))
{
switch (map->orientation()) {
Expand All @@ -80,6 +82,11 @@ MapDocument::MapDocument(Map *map, const QString &fileName):
connect(mLayerModel, SIGNAL(layerRemoved(int)), SLOT(onLayerRemoved(int)));
connect(mLayerModel, SIGNAL(layerChanged(int)), SIGNAL(layerChanged(int)));

mMapObjectModel->setMapDocument(this);
connect(mMapObjectModel, SIGNAL(objectsAdded(QList<MapObject*>)), SLOT(onObjectsAdded(QList<MapObject*>)));
connect(mMapObjectModel, SIGNAL(objectsChanged(QList<MapObject*>)), SLOT(onObjectsChanged(QList<MapObject*>)));
connect(mMapObjectModel, SIGNAL(objectsAboutToBeRemoved(QList<MapObject*>)), SLOT(onObjectsAboutToBeRemoved(QList<MapObject*>)));
connect(mMapObjectModel, SIGNAL(objectsRemoved(QList<MapObject*>)), SLOT(onObjectsRemoved(QList<MapObject*>)));
connect(mUndoStack, SIGNAL(cleanChanged(bool)), SIGNAL(modifiedChanged()));

// Register tileset references
Expand Down Expand Up @@ -476,6 +483,11 @@ void MapDocument::emitObjectsAdded(const QList<MapObject*> &objects)
emit objectsAdded(objects);
}

void MapDocument::emitObjectsAboutToBeRemoved(const QList<MapObject*> &objects)
{
emit objectsAboutToBeRemoved(objects);
}

/**
* Emits the objects removed signal with the specified list of objects.
* This will cause the scene to remove the related items.
Expand All @@ -499,6 +511,26 @@ void MapDocument::emitObjectsChanged(const QList<MapObject*> &objects)
emit objectsChanged(objects);
}

void MapDocument::onObjectsAdded(const QList<MapObject*> &objects)
{
emitObjectsAdded(objects);
}

void MapDocument::onObjectsChanged(const QList<MapObject*> &objects)
{
emitObjectsChanged(objects);
}

void MapDocument::onObjectsAboutToBeRemoved(const QList<MapObject*> &objects)
{
emitObjectsAboutToBeRemoved(objects);
}

void MapDocument::onObjectsRemoved(const QList<MapObject*> &objects)
{
emitObjectsRemoved(objects);
}

void MapDocument::onLayerAdded(int index)
{
emit layerAdded(index);
Expand All @@ -513,6 +545,7 @@ void MapDocument::onLayerAboutToBeRemoved(int index)
// Deselect any objects on this layer when necessary
if (ObjectGroup *og = dynamic_cast<ObjectGroup*>(mMap->layerAt(index)))
deselectObjects(og->objects());
emit layerAboutToBeRemoved(index);
}

void MapDocument::onLayerRemoved(int index)
Expand Down
13 changes: 13 additions & 0 deletions src/tiled/mapdocument.h
Expand Up @@ -46,6 +46,7 @@ namespace Internal {

class LayerModel;
class TileSelectionModel;
class MapObjectModel;

/**
* Represents an editable map. The purpose of this class is to make sure that
Expand Down Expand Up @@ -155,6 +156,8 @@ class MapDocument : public QObject
*/
LayerModel *layerModel() const { return mLayerModel; }

MapObjectModel *mapObjectModel() const { return mMapObjectModel; }

/**
* Returns the map renderer.
*/
Expand Down Expand Up @@ -217,6 +220,7 @@ class MapDocument : public QObject
void emitRegionEdited(const QRegion &region, Layer *layer);

void emitObjectsAdded(const QList<MapObject*> &objects);
void emitObjectsAboutToBeRemoved(const QList<MapObject*> &objects);
void emitObjectsRemoved(const QList<MapObject*> &objects);
void emitObjectsChanged(const QList<MapObject*> &objects);

Expand Down Expand Up @@ -257,6 +261,8 @@ class MapDocument : public QObject
void mapChanged();

void layerAdded(int index);
void layerAboutToBeRemoved(int index);
void layerRenamed(int index);
void layerRemoved(int index);
void layerChanged(int index);

Expand Down Expand Up @@ -291,10 +297,16 @@ class MapDocument : public QObject
void tilesetNameChanged(Tileset *tileset);

void objectsAdded(const QList<MapObject*> &objects);
void objectsAboutToBeRemoved(const QList<MapObject*> &objects);
void objectsRemoved(const QList<MapObject*> &objects);
void objectsChanged(const QList<MapObject*> &objects);

private slots:
void onObjectsAdded(const QList<MapObject*> &objects);
void onObjectsChanged(const QList<MapObject*> &objects);
void onObjectsAboutToBeRemoved(const QList<MapObject*> &objects);
void onObjectsRemoved(const QList<MapObject*> &objects);

void onLayerAdded(int index);
void onLayerAboutToBeRemoved(int index);
void onLayerRemoved(int index);
Expand All @@ -309,6 +321,7 @@ private slots:
QList<MapObject*> mSelectedObjects;
MapRenderer *mRenderer;
int mCurrentLayerIndex;
MapObjectModel *mMapObjectModel;
QUndoStack *mUndoStack;
};

Expand Down
2 changes: 2 additions & 0 deletions src/tiled/mapobjectitem.cpp
Expand Up @@ -226,6 +226,8 @@ void MapObjectItem::syncWithMapObject()
}

mSyncing = false;

setVisible(mObject->isVisible());
}

void MapObjectItem::setEditable(bool editable)
Expand Down

0 comments on commit 92e49a8

Please sign in to comment.