From 2cd4cb933436c32981a4641823197ae90f8dfedb Mon Sep 17 00:00:00 2001 From: GriffinR Date: Fri, 27 Sep 2024 22:19:52 -0400 Subject: [PATCH] Add styles to grid settings --- forms/gridsettingsdialog.ui | 78 ++++++++++++++++++++++++--------- include/editor.h | 1 - include/settings.h | 9 ++++ include/ui/gridsettingsdialog.h | 14 ++---- src/editor.cpp | 18 ++++++-- src/ui/gridsettingsdialog.cpp | 41 ++++++++++++----- 6 files changed, 113 insertions(+), 48 deletions(-) diff --git a/forms/gridsettingsdialog.ui b/forms/gridsettingsdialog.ui index 855b098b..c5aa299d 100644 --- a/forms/gridsettingsdialog.ui +++ b/forms/gridsettingsdialog.ui @@ -6,8 +6,8 @@ 0 0 - 416 - 350 + 423 + 375 @@ -24,15 +24,15 @@ 0 0 - 390 - 284 + 397 + 309 - Offset (in metatiles) + Offset (in pixels) @@ -50,14 +50,27 @@ - + - + + + + + + 0 + 0 + + + + Color + + + @@ -74,7 +87,7 @@ - Dimensions (in metatiles) + Dimensions (in pixels) @@ -92,14 +105,14 @@ - + 1 - + 1 @@ -109,21 +122,31 @@ - - - - - - - 0 - 0 - + + + false - - Color + + QComboBox::SizeAdjustPolicy::AdjustToContentsOnFirstShow + + + 0 + + + + Qt::Orientation::Vertical + + + + 20 + 40 + + + + @@ -140,5 +163,18 @@ + + + NoScrollSpinBox + QSpinBox +
noscrollspinbox.h
+
+ + NoScrollComboBox + QComboBox +
noscrollcombobox.h
+
+
+ diff --git a/include/editor.h b/include/editor.h index 9e8c7d28..671c70d0 100644 --- a/include/editor.h +++ b/include/editor.h @@ -23,7 +23,6 @@ #include "collisionpixmapitem.h" #include "mappixmapitem.h" #include "settings.h" -#include "gridsettingsdialog.h" #include "movablerect.h" #include "cursortilerect.h" #include "mapruler.h" diff --git a/include/settings.h b/include/settings.h index d37283cd..0e0e8df5 100644 --- a/include/settings.h +++ b/include/settings.h @@ -4,6 +4,15 @@ #include +struct GridSettings { + uint width = 16; + uint height = 16; + int offsetX = 0; + int offsetY = 0; + Qt::PenStyle style = Qt::SolidLine; + QColor color = Qt::black; +}; + class Settings { public: diff --git a/include/ui/gridsettingsdialog.h b/include/ui/gridsettingsdialog.h index 806608a0..957edb43 100644 --- a/include/ui/gridsettingsdialog.h +++ b/include/ui/gridsettingsdialog.h @@ -4,20 +4,12 @@ #include #include +#include "settings.h" + namespace Ui { class GridSettingsDialog; } -struct GridSettings { - uint width = 16; - uint height = 16; - int offsetX = 0; - int offsetY = 0; - QString style; - QColor color; -}; - - class GridSettingsDialog : public QDialog { Q_OBJECT @@ -41,7 +33,7 @@ private slots: void on_spinBox_Height_valueChanged(int value); void on_spinBox_X_valueChanged(int value); void on_spinBox_Y_valueChanged(int value); - void on_comboBox_Style_currentTextChanged(QString style); + void on_comboBox_Style_currentIndexChanged(int index); }; inline bool operator==(const struct GridSettings &a, const struct GridSettings &b) { diff --git a/src/editor.cpp b/src/editor.cpp index dc15b059..99e18c3e 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -1878,15 +1878,25 @@ void Editor::displayMapGrid() { const uint pixelMapWidth = map->getWidth() * 16; const uint pixelMapHeight = map->getHeight() * 16; + QPen pen; + pen.setStyle(this->gridSettings.style); + pen.setColor(this->gridSettings.color); + // Create vertical lines int offset = this->gridSettings.offsetX % this->gridSettings.width; - for (uint i = offset; i <= pixelMapWidth; i += this->gridSettings.width) - this->mapGrid->addToGroup(new QGraphicsLineItem(i, 0, i, pixelMapHeight)); + for (uint i = offset; i <= pixelMapWidth; i += this->gridSettings.width) { + auto line = new QGraphicsLineItem(i, 0, i, pixelMapHeight); + line->setPen(pen); + this->mapGrid->addToGroup(line); + } // Create horizontal lines offset = this->gridSettings.offsetY % this->gridSettings.height; - for (uint i = offset; i <= pixelMapHeight; i += this->gridSettings.height) - this->mapGrid->addToGroup(new QGraphicsLineItem(0, i, pixelMapWidth, i)); + for (uint i = offset; i <= pixelMapHeight; i += this->gridSettings.height) { + auto line = new QGraphicsLineItem(0, i, pixelMapWidth, i); + line->setPen(pen); + this->mapGrid->addToGroup(line); + } this->mapGrid->setVisible(porymapConfig.showGrid); } diff --git a/src/ui/gridsettingsdialog.cpp b/src/ui/gridsettingsdialog.cpp index 14a0d8b3..10934196 100644 --- a/src/ui/gridsettingsdialog.cpp +++ b/src/ui/gridsettingsdialog.cpp @@ -2,14 +2,19 @@ #include "gridsettingsdialog.h" // TODO: Add color picker -// TODO: Add styles -// TODO: Update units in UI // TODO: Add linking chain button to width/height // TODO: Add "snap to metatile" check box? // TODO: Save settings in config // TODO: Look into custom painting to improve performance // TODO: Add tooltips +const QList> penStyleMap = { + {"Solid", Qt::SolidLine}, + {"Large Dashes", Qt::DashLine}, + {"Small Dashes", Qt::DotLine}, + {"Dots", Qt::CustomDashLine}, // TODO: Implement a custom pattern for this +}; + GridSettingsDialog::GridSettingsDialog(GridSettings *settings, QWidget *parent) : QDialog(parent), ui(new Ui::GridSettingsDialog), @@ -18,14 +23,16 @@ GridSettingsDialog::GridSettingsDialog(GridSettings *settings, QWidget *parent) ui->setupUi(this); setAttribute(Qt::WA_DeleteOnClose); - // TODO: Populate comboBox_Style + // Populate the styles combo box + for (const auto &pair : penStyleMap) + ui->comboBox_Style->addItem(pair.first, static_cast(pair.second)); ui->spinBox_Width->setMaximum(INT_MAX); ui->spinBox_Height->setMaximum(INT_MAX); ui->spinBox_X->setMaximum(INT_MAX); ui->spinBox_Y->setMaximum(INT_MAX); - // Initialize UI values + // Initialize the settings if (!this->settings) this->settings = new GridSettings; // TODO: Don't leak this this->originalSettings = *this->settings; @@ -37,6 +44,10 @@ GridSettingsDialog::GridSettingsDialog(GridSettings *settings, QWidget *parent) // connect(ui->, &, this, &GridSettingsDialog::changedGridSettings); } +GridSettingsDialog::~GridSettingsDialog() { + delete ui; +} + void GridSettingsDialog::reset(bool force) { if (!force && *this->settings == this->originalSettings) return; @@ -47,12 +58,21 @@ void GridSettingsDialog::reset(bool force) { const QSignalBlocker b_Height(ui->spinBox_Height); const QSignalBlocker b_X(ui->spinBox_X); const QSignalBlocker b_Y(ui->spinBox_Y); + const QSignalBlocker b_Style(ui->comboBox_Style); ui->spinBox_Width->setValue(this->settings->width); ui->spinBox_Height->setValue(this->settings->height); ui->spinBox_X->setValue(this->settings->offsetX); ui->spinBox_Y->setValue(this->settings->offsetY); - // TODO: Initialize comboBox_Style with settings->style + + // TODO: Debug + //ui->comboBox_Style->setCurrentIndex(ui->comboBox_Style->findData(static_cast(this->settings->style))); + for (const auto &pair : penStyleMap) { + if (pair.second == this->settings->style) { + ui->comboBox_Style->setCurrentText(pair.first); + break; + } + } // TODO: Initialize color with settings-color emit changedGridSettings(); @@ -78,8 +98,11 @@ void GridSettingsDialog::on_spinBox_Y_valueChanged(int value) { emit changedGridSettings(); } -void GridSettingsDialog::on_comboBox_Style_currentTextChanged(QString text) { - this->settings->style = text; +void GridSettingsDialog::on_comboBox_Style_currentIndexChanged(int index) { + if (index < 0 || index >= penStyleMap.length()) + return; + + this->settings->style = penStyleMap.at(index).second; emit changedGridSettings(); } @@ -94,7 +117,3 @@ void GridSettingsDialog::dialogButtonClicked(QAbstractButton *button) { reset(); } } - -GridSettingsDialog::~GridSettingsDialog() { - delete ui; -}