[nostalgia/core/studio] Add changing of column/row display configuration

This commit is contained in:
Gary Talent 2020-01-04 19:54:33 -06:00
parent 91b959ff9d
commit 5f4cff3c00
2 changed files with 71 additions and 22 deletions

View File

@ -6,14 +6,17 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include <QHBoxLayout>
#include <QHeaderView> #include <QHeaderView>
#include <QPointer> #include <QPointer>
#include <QQmlContext> #include <QQmlContext>
#include <QQuickWidget> #include <QQuickWidget>
#include <QSet> #include <QSet>
#include <QSettings> #include <QSettings>
#include <QSpinBox>
#include <QSplitter> #include <QSplitter>
#include <QUndoCommand> #include <QUndoCommand>
#include <QToolBar>
#include <QVBoxLayout> #include <QVBoxLayout>
#include "consts.hpp" #include "consts.hpp"
@ -30,6 +33,26 @@ QColor toQColor(Color16 nc) {
} }
struct LabeledSpinner: public QWidget {
QSpinBox *const spinBox = new QSpinBox(this);
LabeledSpinner(QString name, int minimum, int value) {
auto lyt = new QHBoxLayout;
setLayout(lyt);
auto lbl = new QLabel(name, this);
lbl->setBuddy(spinBox);
lyt->addWidget(lbl);
lyt->addWidget(spinBox);
spinBox->setMinimum(minimum);
spinBox->setValue(value);
}
virtual ~LabeledSpinner() = default;
};
class UpdatePixelsCommand: public QUndoCommand { class UpdatePixelsCommand: public QUndoCommand {
private: private:
struct PixelUpdate { struct PixelUpdate {
@ -109,20 +132,10 @@ int SheetData::columns() {
return m_columns; return m_columns;
} }
void SheetData::setColumns(int columns) {
m_columns = columns;
emit columnsChanged();
}
int SheetData::rows() { int SheetData::rows() {
return m_rows; return m_rows;
} }
void SheetData::setRows(int rows) {
m_rows = rows;
emit rowsChanged();
}
const QVector<int> &SheetData::pixels() { const QVector<int> &SheetData::pixels() {
return m_pixels; return m_pixels;
} }
@ -154,6 +167,16 @@ QUndoStack *SheetData::undoStack() {
return &m_cmdStack; return &m_cmdStack;
} }
void SheetData::setColumns(int columns) {
m_columns = columns;
emit columnsChanged(columns);
}
void SheetData::setRows(int rows) {
m_rows = rows;
emit rowsChanged(rows);
}
void SheetData::updatePixels(const NostalgiaGraphic *ng, const NostalgiaPalette *npal) { void SheetData::updatePixels(const NostalgiaGraphic *ng, const NostalgiaPalette *npal) {
if (!npal) { if (!npal) {
npal = &ng->pal; npal = &ng->pal;
@ -199,9 +222,13 @@ TileSheetEditor::TileSheetEditor(QString path, const studio::Context *ctx, QWidg
m_itemName = path.mid(path.lastIndexOf('/')); m_itemName = path.mid(path.lastIndexOf('/'));
auto lyt = new QVBoxLayout(this); auto lyt = new QVBoxLayout(this);
m_splitter = new QSplitter(this); m_splitter = new QSplitter(this);
auto canvas = new QQuickWidget(m_splitter); auto canvasParent = new QWidget(m_splitter);
auto canvasLyt = new QVBoxLayout(canvasParent);
auto canvas = new QQuickWidget(canvasParent);
canvasLyt->addWidget(canvas);
canvasLyt->setMenuBar(setupToolBar());
lyt->addWidget(m_splitter); lyt->addWidget(m_splitter);
m_splitter->addWidget(canvas); m_splitter->addWidget(canvasParent);
m_splitter->addWidget(setupColorPicker(m_splitter)); m_splitter->addWidget(setupColorPicker(m_splitter));
m_splitter->setStretchFactor(0, 1); m_splitter->setStretchFactor(0, 1);
m_sheetData.updatePixels(m_ctx, path); m_sheetData.updatePixels(m_ctx, path);
@ -227,6 +254,19 @@ QUndoStack *TileSheetEditor::undoStack() {
return m_sheetData.undoStack(); return m_sheetData.undoStack();
} }
QWidget *TileSheetEditor::setupToolBar() {
auto tb = new QToolBar(tr("Tile Sheet Options"));
m_tilesX = new LabeledSpinner(tr("Tiles &X:"), 1, m_sheetData.columns());
m_tilesY = new LabeledSpinner(tr("Tiles &Y:"), 1, m_sheetData.rows());
tb->addWidget(m_tilesX);
tb->addWidget(m_tilesY);
connect(&m_sheetData, &SheetData::columnsChanged, m_tilesX->spinBox, &QSpinBox::setValue);
connect(&m_sheetData, &SheetData::rowsChanged, m_tilesY->spinBox, &QSpinBox::setValue);
connect(m_tilesX->spinBox, QOverload<int>::of(&QSpinBox::valueChanged), &m_sheetData, &SheetData::setColumns);
connect(m_tilesY->spinBox, QOverload<int>::of(&QSpinBox::valueChanged), &m_sheetData, &SheetData::setRows);
return tb;
}
QWidget *TileSheetEditor::setupColorPicker(QWidget *parent) { QWidget *TileSheetEditor::setupColorPicker(QWidget *parent) {
auto colorPicker = new QWidget(parent); auto colorPicker = new QWidget(parent);
auto lyt = new QVBoxLayout(colorPicker); auto lyt = new QVBoxLayout(colorPicker);
@ -260,15 +300,19 @@ void TileSheetEditor::setColorTable(QStringList hexColors) {
void TileSheetEditor::saveState() { void TileSheetEditor::saveState() {
QSettings settings(m_ctx->orgName, PluginName); QSettings settings(m_ctx->orgName, PluginName);
settings.beginGroup("TileSheetEditor"); settings.beginGroup("TileSheetEditor/" + m_itemName);
settings.setValue("m_splitter/state", m_splitter->saveState()); settings.setValue("m_splitter/state", m_splitter->saveState());
settings.setValue("m_sheetData/tileRows", m_sheetData.rows());
settings.setValue("m_sheetData/tileColumns", m_sheetData.columns());
settings.endGroup(); settings.endGroup();
} }
void TileSheetEditor::restoreState() { void TileSheetEditor::restoreState() {
QSettings settings(m_ctx->orgName, PluginName); QSettings settings(m_ctx->orgName, PluginName);
settings.beginGroup("TileSheetEditor"); settings.beginGroup("TileSheetEditor/" + m_itemName);
m_splitter->restoreState(settings.value("m_splitter/state", m_splitter->saveState()).toByteArray()); m_splitter->restoreState(settings.value("m_splitter/state", m_splitter->saveState()).toByteArray());
m_sheetData.setRows(settings.value("m_sheetData/tileRows", 1).toInt());
m_sheetData.setColumns(settings.value("m_sheetData/tileColumns", 1).toInt());
settings.endGroup(); settings.endGroup();
} }

View File

@ -35,8 +35,8 @@ class SheetData: public QObject {
QUndoStack m_cmdStack; QUndoStack m_cmdStack;
QStringList m_palette; QStringList m_palette;
QVector<int> m_pixels; QVector<int> m_pixels;
int m_columns = 2; int m_columns = 1;
int m_rows = 2; int m_rows = 1;
int m_selectedColor = 0; int m_selectedColor = 0;
public: public:
@ -48,12 +48,8 @@ class SheetData: public QObject {
int columns(); int columns();
void setColumns(int columns);
int rows(); int rows();
void setRows(int rows);
const QVector<int> &pixels(); const QVector<int> &pixels();
QStringList palette(); QStringList palette();
@ -64,13 +60,18 @@ class SheetData: public QObject {
QUndoStack *undoStack(); QUndoStack *undoStack();
public slots:
void setColumns(int columns);
void setRows(int rows);
private: private:
void updatePixels(const NostalgiaGraphic *ng, const NostalgiaPalette *npal); void updatePixels(const NostalgiaGraphic *ng, const NostalgiaPalette *npal);
signals: signals:
void columnsChanged(); void columnsChanged(int);
void rowsChanged(); void rowsChanged(int);
void pixelsChanged(); void pixelsChanged();
@ -87,6 +88,8 @@ class TileSheetEditor: public studio::Editor {
const studio::Context *m_ctx = nullptr; const studio::Context *m_ctx = nullptr;
SheetData m_sheetData; SheetData m_sheetData;
QSplitter *m_splitter = nullptr; QSplitter *m_splitter = nullptr;
class LabeledSpinner *m_tilesX = nullptr;
class LabeledSpinner *m_tilesY = nullptr;
struct { struct {
QComboBox *palette = nullptr; QComboBox *palette = nullptr;
QTableWidget *colorTable = nullptr; QTableWidget *colorTable = nullptr;
@ -104,6 +107,8 @@ class TileSheetEditor: public studio::Editor {
QUndoStack *undoStack() override; QUndoStack *undoStack() override;
private: private:
QWidget *setupToolBar();
QWidget *setupColorPicker(QWidget *widget); QWidget *setupColorPicker(QWidget *widget);
void setColorTable(QStringList hexColors); void setColorTable(QStringList hexColors);