Skip to content

Commit

Permalink
Implemented "Stagger Axis/Index" properties
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn committed Sep 2, 2024
1 parent 596084b commit 7176188
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 80 deletions.
107 changes: 105 additions & 2 deletions src/tiled/propertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "changemapproperty.h"
#include "changeproperties.h"
#include "clipboardmanager.h"
#include "compression.h"
#include "mapdocument.h"
#include "propertybrowser.h"
#include "utils.h"
Expand Down Expand Up @@ -93,6 +94,7 @@ PropertiesWidget::PropertiesWidget(QWidget *parent)
// connect(mPropertyBrowser, &PropertyBrowser::selectedItemsChanged,
// this, &PropertiesWidget::updateActions);

registerEditorFactories();
retranslateUi();
}

Expand Down Expand Up @@ -303,6 +305,30 @@ class MapProperties : public QObject
mMapDocument->undoStack()->push(command);
});

mStaggerAxisProperty = editorFactory->createProperty(
tr("Stagger Axis"),
[this]() {
return QVariant::fromValue(mMapDocument->map()->staggerAxis());
},
[this](const QVariant &value) {
auto command = new ChangeMapProperty(mMapDocument,
Map::StaggerAxisProperty,
value.toInt());
mMapDocument->undoStack()->push(command);
});

mStaggerIndexProperty = editorFactory->createProperty(
tr("Stagger Index"),
[this]() {
return QVariant::fromValue(mMapDocument->map()->staggerIndex());
},
[this](const QVariant &value) {
auto command = new ChangeMapProperty(mMapDocument,
Map::StaggerIndexProperty,
value.toInt());
mMapDocument->undoStack()->push(command);
});

connect(mMapDocument, &MapDocument::changed,
this, &MapProperties::onMapChanged);
}
Expand All @@ -315,8 +341,8 @@ class MapProperties : public QObject
editor->addProperty(mTileSizeProperty);
editor->addProperty(mInfiniteProperty);
editor->addProperty(mHexSideLengthProperty);
// editor->addProperty(mStaggerAxisProperty);
// editor->addProperty(mStaggerIndexProperty);
editor->addProperty(mStaggerAxisProperty);
editor->addProperty(mStaggerIndexProperty);
// editor->addProperty(mParallaxOriginProperty);
// editor->addProperty(mLayerDataFormatProperty);
// editor->addProperty(mChunkSizeProperty);
Expand All @@ -341,8 +367,14 @@ class MapProperties : public QObject
emit mInfiniteProperty->valueChanged();
break;
case Map::HexSideLengthProperty:
emit mHexSideLengthProperty->valueChanged();
break;
case Map::StaggerAxisProperty:
emit mStaggerAxisProperty->valueChanged();
break;
case Map::StaggerIndexProperty:
emit mStaggerIndexProperty->valueChanged();
break;
case Map::ParallaxOriginProperty:
case Map::OrientationProperty:
emit mOrientationProperty->valueChanged();
Expand Down Expand Up @@ -782,6 +814,77 @@ void PropertiesWidget::keyPressEvent(QKeyEvent *event)
}
}

void PropertiesWidget::registerEditorFactories()
{
registerEditorFactory(qMetaTypeId<Alignment>(),
std::make_unique<EnumEditorFactory>(
QStringList {
tr("Unspecified"),
tr("Top Left"),
tr("Top"),
tr("Top Right"),
tr("Left"),
tr("Center"),
tr("Right"),
tr("Bottom Left"),
tr("Bottom"),
tr("Bottom Right"),
}));


registerEditorFactory(qMetaTypeId<Map::StaggerAxis>(),
std::make_unique<EnumEditorFactory>(
QStringList {
tr("X"),
tr("Y"),
}));

registerEditorFactory(qMetaTypeId<Map::StaggerIndex>(),
std::make_unique<EnumEditorFactory>(
QStringList {
tr("Odd"),
tr("Even"),
}));

QStringList layerFormatNames = {
QCoreApplication::translate("PreferencesDialog", "XML (deprecated)"),
QCoreApplication::translate("PreferencesDialog", "Base64 (uncompressed)"),
QCoreApplication::translate("PreferencesDialog", "Base64 (gzip compressed)"),
QCoreApplication::translate("PreferencesDialog", "Base64 (zlib compressed)"),
};
QList<int> layerFormatValues = {
Map::XML,
Map::Base64,
Map::Base64Gzip,
Map::Base64Zlib,
};

if (compressionSupported(Zstandard)) {
layerFormatNames.append(QCoreApplication::translate("PreferencesDialog", "Base64 (Zstandard compressed)"));
layerFormatValues.append(Map::Base64Zstandard);
}

layerFormatNames.append(QCoreApplication::translate("PreferencesDialog", "CSV"));
layerFormatValues.append(Map::CSV);

registerEditorFactory(qMetaTypeId<Map::LayerDataFormat>(),
std::make_unique<EnumEditorFactory>(layerFormatNames, layerFormatValues));

registerEditorFactory(qMetaTypeId<Map::RenderOrder>(),
std::make_unique<EnumEditorFactory>(
QStringList {
tr("Right Down"),
tr("Right Up"),
tr("Left Down"),
tr("Left Up"),
}));
}

void PropertiesWidget::registerEditorFactory(int type, std::unique_ptr<EditorFactory> factory)
{
mDefaultEditorFactory->registerEditorFactory(type, std::move(factory));
}

void PropertiesWidget::retranslateUi()
{
mActionAddProperty->setText(QCoreApplication::translate("Tiled::PropertiesDock", "Add Property"));
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/propertieswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Tiled {
class Object;

class Document;
class EditorFactory;
class ValueTypeEditorFactory;
class VariantEditor;

Expand Down Expand Up @@ -61,6 +62,9 @@ public slots:
void keyPressEvent(QKeyEvent *event) override;

private:
void registerEditorFactories();
void registerEditorFactory(int type, std::unique_ptr<EditorFactory> factory);

void currentObjectChanged(Object *object);
void updateActions();

Expand Down
91 changes: 13 additions & 78 deletions src/tiled/varianteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ AbstractProperty::AbstractProperty(const QString &name,

QWidget *AbstractProperty::createEditor(QWidget *parent)
{
return m_editorFactory->createEditor(this, parent);
return m_editorFactory ? m_editorFactory->createEditor(this, parent)
: nullptr;
}


Expand Down Expand Up @@ -316,7 +317,7 @@ VariantEditor::VariantEditor(QWidget *parent)
m_gridLayout = new QGridLayout;
verticalLayout->addLayout(m_gridLayout);
verticalLayout->addStretch();
verticalLayout->setContentsMargins(QMargins());
verticalLayout->setContentsMargins(0, 0, 0, Utils::dpiScaled(6));

setWidget(m_widget);
setWidgetResizable(true);
Expand All @@ -330,71 +331,6 @@ VariantEditor::VariantEditor(QWidget *parent)
m_gridLayout->setColumnMinimumWidth(MiddleSpacing, Utils::dpiScaled(2));
m_gridLayout->setColumnMinimumWidth(RightSpacing, Utils::dpiScaled(3));

// auto alignmentEditorFactory = std::make_unique<EnumEditorFactory>();
// alignmentEditorFactory->setEnumNames({
// tr("Unspecified"),
// tr("Top Left"),
// tr("Top"),
// tr("Top Right"),
// tr("Left"),
// tr("Center"),
// tr("Right"),
// tr("Bottom Left"),
// tr("Bottom"),
// tr("Bottom Right"),
// });
// registerEditorFactory(qMetaTypeId<Alignment>(), std::move(alignmentEditorFactory));


// auto staggerAxisEditorFactory = std::make_unique<EnumEditorFactory>();
// staggerAxisEditorFactory->setEnumNames({
// tr("X"),
// tr("Y"),
// });
// registerEditorFactory(qMetaTypeId<Map::StaggerAxis>(), std::move(staggerAxisEditorFactory));

// auto staggerIndexEditorFactory = std::make_unique<EnumEditorFactory>();
// staggerIndexEditorFactory->setEnumNames({
// tr("Odd"),
// tr("Even"),
// });
// registerEditorFactory(qMetaTypeId<Map::StaggerIndex>(), std::move(staggerIndexEditorFactory));

QStringList layerFormatNames = {
QCoreApplication::translate("PreferencesDialog", "XML (deprecated)"),
QCoreApplication::translate("PreferencesDialog", "Base64 (uncompressed)"),
QCoreApplication::translate("PreferencesDialog", "Base64 (gzip compressed)"),
QCoreApplication::translate("PreferencesDialog", "Base64 (zlib compressed)"),
};
QList<int> layerFormatValues = {
Map::XML,
Map::Base64,
Map::Base64Gzip,
Map::Base64Zlib,
};

if (compressionSupported(Zstandard)) {
layerFormatNames.append(QCoreApplication::translate("PreferencesDialog", "Base64 (Zstandard compressed)"));
layerFormatValues.append(Map::Base64Zstandard);
}

layerFormatNames.append(QCoreApplication::translate("PreferencesDialog", "CSV"));
layerFormatValues.append(Map::CSV);

// auto layerFormatEditorFactory = std::make_unique<EnumEditorFactory>();
// layerFormatEditorFactory->setEnumNames(layerFormatNames);
// layerFormatEditorFactory->setEnumValues(layerFormatValues);
// registerEditorFactory(qMetaTypeId<Map::LayerDataFormat>(), std::move(layerFormatEditorFactory));

// auto renderOrderEditorFactory = std::make_unique<EnumEditorFactory>();
// renderOrderEditorFactory->setEnumNames({
// tr("Right Down"),
// tr("Right Up"),
// tr("Left Down"),
// tr("Left Up"),
// });
// registerEditorFactory(qMetaTypeId<Map::RenderOrder>(), std::move(renderOrderEditorFactory));

// setValue(QVariantMap {
// { QStringLiteral("Name"), QVariant(QLatin1String("Hello")) },
// { QStringLiteral("Position"), QVariant(QPoint(15, 50)) },
Expand Down Expand Up @@ -607,24 +543,23 @@ QObjectProperty *ValueTypeEditorFactory::createQObjectProperty(QObject *qObject,
this);
}

ValueProperty *ValueTypeEditorFactory::createProperty(const QString &name, const QVariant &value)
ValueProperty *ValueTypeEditorFactory::createProperty(const QString &name,
const QVariant &value)
{
const int type = value.userType();
auto factory = m_factories.find(type);
if (factory != m_factories.end())
return new ValueProperty(name, value, factory->second.get());
return nullptr;
auto f = m_factories.find(value.userType());
return new ValueProperty(name, value,
f != m_factories.end() ? f->second.get()
: nullptr);
}

AbstractProperty *ValueTypeEditorFactory::createProperty(const QString &name,
std::function<QVariant ()> get,
std::function<void (const QVariant &)> set)
{
const int type = get().userType();
auto factory = m_factories.find(type);
if (factory != m_factories.end())
return new GetSetProperty(name, get, set, factory->second.get());
return nullptr;
auto f = m_factories.find(get().userType());
return new GetSetProperty(name, get, set,
f != m_factories.end() ? f->second.get()
: nullptr);
}

QWidget *ValueTypeEditorFactory::createEditor(Property *property, QWidget *parent)
Expand Down

0 comments on commit 7176188

Please sign in to comment.