Skip to content

Commit

Permalink
Implemented "Infinite" and "Hex Side Length" properties
Browse files Browse the repository at this point in the history
* Introduced MapProperties class to make it easier to emit valueChanged
  for all the map's properties, as well to move their creation out of
  PropertiesWidget::currentObjectChanged.

* Added GetSetProperty that makes it easier to define a property in
  terms of a given getter and setter function, since it avoids the need
  for a specific Property subclass.

* Finished implementation of the BoolEditorFactory (signals connected).

* Moved property edit widgets to their own file.
  • Loading branch information
bjorn committed Sep 2, 2024
1 parent 17d8ad9 commit 596084b
Show file tree
Hide file tree
Showing 7 changed files with 523 additions and 313 deletions.
2 changes: 2 additions & 0 deletions src/tiled/libtilededitor.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ DynamicLibrary {
"propertieswidget.h",
"propertybrowser.cpp",
"propertybrowser.h",
"propertyeditorwidgets.cpp",
"propertyeditorwidgets.h",
"propertytypeseditor.cpp",
"propertytypeseditor.h",
"propertytypeseditor.ui",
Expand Down
146 changes: 114 additions & 32 deletions src/tiled/propertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ namespace Tiled {

PropertiesWidget::PropertiesWidget(QWidget *parent)
: QWidget{parent}
, mDocument(nullptr)
, mPropertyBrowser(new VariantEditor(this))
, mDefaultEditorFactory(std::make_unique<ValueTypeEditorFactory>())
{
Expand Down Expand Up @@ -150,8 +149,8 @@ class MapOrientationProperty : public EnumProperty
Q_OBJECT

public:
MapOrientationProperty(MapDocument *mapDocument)
: EnumProperty(tr("Orientation"))
MapOrientationProperty(MapDocument *mapDocument, QObject *parent = nullptr)
: EnumProperty(tr("Orientation"), parent)
, mMapDocument(mapDocument)
{
setEnumNames({
Expand All @@ -166,9 +165,6 @@ class MapOrientationProperty : public EnumProperty
Map::Staggered,
Map::Hexagonal,
});

connect(mMapDocument, &MapDocument::mapChanged,
this, &Property::valueChanged);
}

QVariant value() const override
Expand All @@ -192,8 +188,9 @@ class MapSizeProperty : public AbstractProperty
Q_OBJECT

public:
MapSizeProperty(MapDocument *mapDocument, EditorFactory *editorFactory)
: AbstractProperty(tr("Map Size"), editorFactory)
MapSizeProperty(MapDocument *mapDocument, EditorFactory *editorFactory,
QObject *parent = nullptr)
: AbstractProperty(tr("Map Size"), editorFactory, parent)
, mMapDocument(mapDocument)
{
connect(mMapDocument, &MapDocument::mapChanged,
Expand Down Expand Up @@ -231,12 +228,12 @@ class TileSizeProperty : public AbstractProperty
Q_OBJECT

public:
TileSizeProperty(MapDocument *mapDocument, EditorFactory *editorFactory)
: AbstractProperty(tr("Tile Size"), editorFactory)
TileSizeProperty(MapDocument *mapDocument,
EditorFactory *editorFactory,
QObject *parent = nullptr)
: AbstractProperty(tr("Tile Size"), editorFactory, parent)
, mMapDocument(mapDocument)
{
connect(mMapDocument, &Document::changed,
this, &TileSizeProperty::onChanged);
}

QVariant value() const override
Expand Down Expand Up @@ -265,49 +262,134 @@ class TileSizeProperty : public AbstractProperty
};

private:
void onChanged(const ChangeEvent &event)
MapDocument *mMapDocument;
};

class MapProperties : public QObject
{
Q_OBJECT

public:
MapProperties(MapDocument *mapDocument,
ValueTypeEditorFactory *editorFactory,
QObject *parent = nullptr)
: QObject(parent)
, mMapDocument(mapDocument)
, mOrientationProperty(new MapOrientationProperty(mapDocument, this))
, mSizeProperty(new MapSizeProperty(mapDocument, editorFactory, this))
, mTileSizeProperty(new TileSizeProperty(mapDocument, editorFactory, this))
{
mInfiniteProperty = editorFactory->createProperty(
tr("Infinite"),
[this]() {
return mMapDocument->map()->infinite();
},
[this](const QVariant &value) {
auto command = new ChangeMapProperty(mMapDocument,
Map::InfiniteProperty,
value.toBool());
mMapDocument->undoStack()->push(command);
});

mHexSideLengthProperty = editorFactory->createProperty(
tr("Hex Side Length"),
[this]() {
return mMapDocument->map()->hexSideLength();
},
[this](const QVariant &value) {
auto command = new ChangeMapProperty(mMapDocument,
Map::HexSideLengthProperty,
value.toInt());
mMapDocument->undoStack()->push(command);
});

connect(mMapDocument, &MapDocument::changed,
this, &MapProperties::onMapChanged);
}

void populateEditor(VariantEditor *editor)
{
editor->addHeader(tr("Map"));
editor->addProperty(mOrientationProperty);
editor->addProperty(mSizeProperty);
editor->addProperty(mTileSizeProperty);
editor->addProperty(mInfiniteProperty);
editor->addProperty(mHexSideLengthProperty);
// editor->addProperty(mStaggerAxisProperty);
// editor->addProperty(mStaggerIndexProperty);
// editor->addProperty(mParallaxOriginProperty);
// editor->addProperty(mLayerDataFormatProperty);
// editor->addProperty(mChunkSizeProperty);
// editor->addProperty(mTileRenderOrderProperty);
// editor->addProperty(mCompressionLevelProperty);
// editor->addProperty(mBackgroundColorProperty);
}

private:
void onMapChanged(const ChangeEvent &event)
{
if (event.type != ChangeEvent::MapChanged)
return;

const auto property = static_cast<const MapChangeEvent&>(event).property;
if (property == Map::TileWidthProperty || property == Map::TileHeightProperty)
emit valueChanged();
switch (property) {
case Map::TileWidthProperty:
case Map::TileHeightProperty:
emit mTileSizeProperty->valueChanged();
break;
case Map::InfiniteProperty:
emit mInfiniteProperty->valueChanged();
break;
case Map::HexSideLengthProperty:
case Map::StaggerAxisProperty:
case Map::StaggerIndexProperty:
case Map::ParallaxOriginProperty:
case Map::OrientationProperty:
emit mOrientationProperty->valueChanged();
break;
case Map::RenderOrderProperty:
case Map::BackgroundColorProperty:
case Map::LayerDataFormatProperty:
case Map::CompressionLevelProperty:
case Map::ChunkSizeProperty:
break;
}
}

MapDocument *mMapDocument;
Property *mOrientationProperty;
Property *mSizeProperty;
Property *mTileSizeProperty;
Property *mInfiniteProperty;
Property *mHexSideLengthProperty;
Property *mStaggerAxisProperty;
Property *mStaggerIndexProperty;
Property *mParallaxOriginProperty;
Property *mLayerDataFormatProperty;
Property *mChunkSizeProperty;
Property *mTileRenderOrderProperty;
Property *mCompressionLevelProperty;
Property *mBackgroundColorProperty;
};


void PropertiesWidget::currentObjectChanged(Object *object)
{
// mPropertyBrowser->setObject(object);
mPropertyBrowser->clear();
delete mPropertiesObject;
mPropertiesObject = nullptr;

if (object) {
switch (object->typeId()) {
case Object::LayerType:
case Object::MapObjectType:
break;
case Object::MapType: {
Map *map = static_cast<Map*>(object);
auto mapDocument = static_cast<MapDocument*>(mDocument);

mPropertyBrowser->addHeader(tr("Map"));
mPropertyBrowser->addProperty(new MapOrientationProperty(mapDocument));
mPropertyBrowser->addProperty(new MapSizeProperty(mapDocument, mDefaultEditorFactory.get()));
mPropertyBrowser->addProperty(new TileSizeProperty(mapDocument, mDefaultEditorFactory.get()));
// todo: infinite
// todo: hex side length
// todo: stagger axis
// todo: stagger index
// todo: parallax origin
// todo: layer data format
// todo: chunk size
// todo: tile render order
// todo: compression level
// todo: background color

auto properties = new MapProperties(mapDocument, mDefaultEditorFactory.get(), this);
properties->populateEditor(mPropertyBrowser);
mPropertiesObject = properties;
break;
}
case Object::TilesetType:
Expand Down
3 changes: 2 additions & 1 deletion src/tiled/propertieswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public slots:

void retranslateUi();

Document *mDocument;
Document *mDocument = nullptr;
QObject *mPropertiesObject = nullptr;
VariantEditor *mPropertyBrowser;
std::unique_ptr<ValueTypeEditorFactory> mDefaultEditorFactory;
QAction *mActionAddProperty;
Expand Down
Loading

0 comments on commit 596084b

Please sign in to comment.