[nostalgia/core] Add basic color selection and basic tilesheet editing

This commit is contained in:
2019-12-01 00:52:00 -06:00
parent 2e5263764c
commit e407ad7246
9 changed files with 171 additions and 26 deletions
+1
View File
@@ -14,6 +14,7 @@ target_link_libraries(
Qt5::Widgets
Qt5::QuickWidgets
NostalgiaStudio
NostalgiaCore-Qt
NostalgiaCore
OxFS
OxStd
+6
View File
@@ -26,4 +26,10 @@ Rectangle {
anchors.horizontalCenter: pixel.horizontalCenter
anchors.bottom: pixel.bottom
}
MouseArea {
anchors.fill: parent
onClicked: sheetData.updatePixels([pixel])
}
}
@@ -16,12 +16,12 @@ Rectangle {
Grid {
id: tileGrid
property int baseTileSize: Math.min(parent.width / tileGrid.columns, parent.height / tileGrid.rows)
width: tileGrid.columns * tileGrid.baseTileSize * 0.85
height: tileGrid.rows * tileGrid.baseTileSize * 0.85
width: tileGrid.columns * tileGrid.baseTileSize * 0.90
height: tileGrid.rows * tileGrid.baseTileSize * 0.90
anchors.horizontalCenter: tileSheetEditor.horizontalCenter
anchors.verticalCenter: tileSheetEditor.verticalCenter
rows: 2
columns: 2
rows: sheetData.rows
columns: sheetData.columns
Repeater {
model: tileGrid.rows * tileGrid.columns
Tile {
+89 -19
View File
@@ -6,7 +6,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <QHeaderView>
#include <QQmlContext>
#include <QQuickItem>
#include <QQuickWidget>
#include <QSettings>
#include <QSplitter>
@@ -17,11 +19,68 @@
namespace nostalgia::core {
QColor toQColor(Color16 nc) {
const auto r = red32(nc);
const auto g = green32(nc);
const auto b = blue32(nc);
const auto a = 255;
return QColor(r, g, b, a);
}
QString SheetData::pixel(int index) {
return m_palette[m_pixels[index]];
}
void SheetData::updatePixels(QVariantList pixels) {
for (auto pi : pixels) {
// TODO: replace with push to QUndoStack
auto p = qobject_cast<QQuickItem*>(pi.value<QObject*>());
p->setProperty("color", m_palette[m_selectedColor]);
}
}
int SheetData::columns() {
return m_columns;
}
void SheetData::setColumns(int columns) {
m_columns = columns;
emit columnsChanged();
}
int SheetData::rows() {
return m_rows;
}
void SheetData::setRows(int rows) {
m_rows = rows;
emit rowsChanged();
}
QStringList SheetData::palette() {
return m_palette;
}
void SheetData::updatePixels(const studio::Context *ctx, QString ngPath, QString palPath) {
auto ng = ctx->project->loadObj<NostalgiaGraphic>(ngPath);
std::unique_ptr<NostalgiaPalette> npal;
if (palPath == "" && ng->defaultPalette.type() == ox::FileAddressType::Path) {
palPath = ng->defaultPalette.getPath().value;
}
try {
npal = ctx->project->loadObj<NostalgiaPalette>(palPath);
qInfo() << "Opened palette" << palPath;
} catch (ox::Error) {
qWarning() << "Could not open palette" << palPath;
}
updatePixels(ng.get(), npal.get());
}
void SheetData::setSelectedColor(int index) {
m_selectedColor = index;
}
void SheetData::updatePixels(const NostalgiaGraphic *ng, const NostalgiaPalette *npal) {
if (!npal) {
npal = &ng->pal;
@@ -29,8 +88,8 @@ void SheetData::updatePixels(const NostalgiaGraphic *ng, const NostalgiaPalette
// load palette
for (std::size_t i = 0; i < npal->colors.size(); i++) {
auto c = toColor32(npal->colors[i]);
auto color = "#" + QString("%1").arg(QString::number(c, 16), 8, '0');
const auto c = toQColor(npal->colors[i]);
const auto color = c.name(QColor::HexArgb);
m_palette.append(color);
}
@@ -51,21 +110,6 @@ void SheetData::updatePixels(const NostalgiaGraphic *ng, const NostalgiaPalette
}
}
void SheetData::updatePixels(const studio::Context *ctx, QString ngPath, QString palPath) {
auto ng = ctx->project->loadObj<NostalgiaGraphic>(ngPath);
std::unique_ptr<NostalgiaPalette> npal;
if (palPath == "" && ng->defaultPalette.type() == ox::FileAddressType::Path) {
palPath = ng->defaultPalette.getPath().value;
}
try {
npal = ctx->project->loadObj<NostalgiaPalette>(palPath);
qInfo() << "Opened palette" << palPath;
} catch (ox::Error) {
qWarning() << "Could not open palette" << palPath;
}
return updatePixels(ng.get(), npal.get());
}
TileSheetEditor::TileSheetEditor(QString path, const studio::Context *ctx, QWidget *parent): QWidget(parent) {
m_ctx = ctx;
@@ -80,6 +124,7 @@ TileSheetEditor::TileSheetEditor(QString path, const studio::Context *ctx, QWidg
canvas->rootContext()->setContextProperty("sheetData", &m_sheetData);
canvas->setSource(QUrl::fromLocalFile(":/qml/TileSheetEditor.qml"));
canvas->setResizeMode(QQuickWidget::SizeRootObjectToView);
setColorTable(m_sheetData.palette());
restoreState();
}
@@ -92,23 +137,48 @@ QWidget *TileSheetEditor::setupColorPicker(QWidget *parent) {
auto lyt = new QVBoxLayout(colorPicker);
m_colorPicker.palette = new QComboBox(colorPicker);
m_colorPicker.colorTable = new QTableWidget(colorPicker);
m_colorPicker.colorTable->setColumnCount(2);
m_colorPicker.colorTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_colorPicker.colorTable->setSelectionMode(QAbstractItemView::SingleSelection);
m_colorPicker.colorTable->setHorizontalHeaderLabels(QStringList() << tr("Hex Code") << tr("Color"));
m_colorPicker.colorTable->horizontalHeader()->setStretchLastSection(true);
m_colorPicker.colorTable->verticalHeader()->hide();
connect(m_colorPicker.colorTable, &QTableWidget::itemSelectionChanged, this, &TileSheetEditor::colorSelected);
lyt->addWidget(m_colorPicker.palette);
lyt->addWidget(m_colorPicker.colorTable);
return colorPicker;
}
void TileSheetEditor::setColorTable(QStringList hexColors) {
auto tbl = m_colorPicker.colorTable;
tbl->setRowCount(hexColors.size());
for (int i = 0; i < hexColors.size(); i++) {
auto hexCode = new QTableWidgetItem;
hexCode->setText(hexColors[i]);
hexCode->setFont(QFont("monospace"));
tbl->setItem(i, 0, hexCode);
auto color = new QTableWidgetItem;
color->setBackground(QColor(hexColors[i]));
tbl->setItem(i, 1, color);
}
}
void TileSheetEditor::saveState() {
QSettings settings(m_ctx->orgName, PluginName);
settings.beginGroup("TileSheetEditor/state");
settings.beginGroup("TileSheetEditor");
settings.setValue("m_splitter/state", m_splitter->saveState());
settings.endGroup();
}
void TileSheetEditor::restoreState() {
QSettings settings(m_ctx->orgName, PluginName);
settings.beginGroup("TileSheetEditor/state");
settings.beginGroup("TileSheetEditor");
m_splitter->restoreState(settings.value("m_splitter/state", m_splitter->saveState()).toByteArray());
settings.endGroup();
}
void TileSheetEditor::colorSelected() {
m_sheetData.setSelectedColor(m_colorPicker.colorTable->currentRow());
}
}
+30 -2
View File
@@ -12,6 +12,7 @@
#include <QStringList>
#include <QStringView>
#include <QTableWidget>
#include <QVariant>
#include <QWidget>
#include <nostalgia/core/gfx.hpp>
@@ -21,20 +22,42 @@ namespace nostalgia::core {
class SheetData: public QObject {
Q_OBJECT
Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged)
Q_PROPERTY(int rows READ rows WRITE setRows NOTIFY rowsChanged)
private:
QStringList m_palette;
QVector<uint8_t> m_pixels;
int m_columns = 2;
int m_rows = 2;
int m_selectedColor = 0;
public:
Q_INVOKABLE QString pixel(int index);
void updatePixels(const NostalgiaGraphic *ng, const NostalgiaPalette *npal);
Q_INVOKABLE void updatePixels(QVariantList pixels);
int columns();
void setColumns(int columns);
int rows();
void setRows(int rows);
QStringList palette();
void updatePixels(const studio::Context *ctx, QString ngPath, QString palPath = "");
void setSelectedColor(int index);
private:
void updatePixels(const NostalgiaGraphic *ng, const NostalgiaPalette *npal);
signals:
void refreshTileSheet();
void columnsChanged();
void rowsChanged();
};
@@ -59,10 +82,15 @@ class TileSheetEditor: public QWidget {
private:
QWidget *setupColorPicker(QWidget *widget);
void setColorTable(QStringList hexColors);
void saveState();
void restoreState();
public slots:
void colorSelected();
};
}