From 7ca34da4170fb011f447e9312f0d6dd59946dc24 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 25 Feb 2020 20:26:35 -0600 Subject: [PATCH] [nostalgia/core/studio] Replace TileSheetEditor ScrollView scrolling with manual version --- src/nostalgia/core/studio/TileSheetEditor.qml | 52 ++++++++++++++----- src/nostalgia/core/studio/tilesheeteditor.cpp | 12 +++-- src/nostalgia/core/studio/tilesheeteditor.hpp | 5 +- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/nostalgia/core/studio/TileSheetEditor.qml b/src/nostalgia/core/studio/TileSheetEditor.qml index 58d24894..ff735b01 100644 --- a/src/nostalgia/core/studio/TileSheetEditor.qml +++ b/src/nostalgia/core/studio/TileSheetEditor.qml @@ -10,18 +10,10 @@ import QtQuick 2.0 import QtQuick.Controls 2.14 import 'qrc:/qml/' -ScrollView { +Rectangle { id: tileSheetEditor - ScrollBar.horizontal.policy: ScrollBar.AsNeeded - ScrollBar.vertical.policy: ScrollBar.AsNeeded - contentWidth: tileGrid.width - contentHeight: tileGrid.height clip: true - focusPolicy: Qt.WheelFocus - - background: Rectangle { - color: '#717d7e' - } + color: '#717d7e' MouseArea { id: mouseArea @@ -45,10 +37,42 @@ ScrollView { } else if (tileGrid.scaleFactor > 0.9) { tileGrid.scaleFactor -= mod; } - wheel.accepted = true; + + + if (tileGrid.width <= this.width) { + tileGrid.x = this.width / 2 - tileGrid.width / 2; + } + if (tileGrid.height <= this.height) { + tileGrid.y = this.height / 2 - tileGrid.height / 2; + } } else { - wheel.accepted = false; + const mod = 15; + if (tileGrid.width > this.width) { + if (wheel.angleDelta.x > 0) { + if (tileGrid.x < tileGrid.width / 2) { + tileGrid.x += mod; + } + } else if (wheel.angleDelta.x < 0) { + let x2 = tileGrid.x + tileGrid.width; + if (x2 > this.width / 2) { + tileGrid.x -= mod; + } + } + } + if (tileGrid.height > this.height) { + if (wheel.angleDelta.y > 0) { + if (tileGrid.y < this.height / 2) { + tileGrid.y += mod; + } + } else if (wheel.angleDelta.y < 0) { + let y2 = tileGrid.y + tileGrid.height; + if (y2 > this.height / 2) { + tileGrid.y -= mod; + } + } + } } + wheel.accepted = true; } onPositionChanged: sheetData.updatePixel(pixelAt(mouseX, mouseY)) @@ -74,10 +98,10 @@ ScrollView { id: tileGrid property double scaleFactor: 0.9 property int baseTileSize: Math.min(2000 / tileGrid.columns, 1000 / tileGrid.rows) + x: tileSheetEditor.width / 2 - this.width / 2 + y: tileSheetEditor.height / 2 - this.height / 2 width: tileGrid.columns * tileGrid.baseTileSize * tileGrid.scaleFactor height: tileGrid.rows * tileGrid.baseTileSize * tileGrid.scaleFactor - //anchors.horizontalCenter: parent.horizontalCenter - //anchors.verticalCenter: parent.verticalCenter rows: sheetData ? sheetData.rows : 1 columns: sheetData ? sheetData.columns : 1 Repeater { diff --git a/src/nostalgia/core/studio/tilesheeteditor.cpp b/src/nostalgia/core/studio/tilesheeteditor.cpp index 4fd08fa3..4d1b0377 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor.cpp @@ -224,17 +224,17 @@ TileSheetEditor::TileSheetEditor(QString path, const studio::Context *ctx, QWidg m_splitter = new QSplitter(this); auto canvasParent = new QWidget(m_splitter); auto canvasLyt = new QVBoxLayout(canvasParent); - auto canvas = new QQuickWidget(canvasParent); - canvasLyt->addWidget(canvas); + m_canvas = new QQuickWidget(canvasParent); + canvasLyt->addWidget(m_canvas); canvasLyt->setMenuBar(setupToolBar()); lyt->addWidget(m_splitter); m_splitter->addWidget(canvasParent); m_splitter->addWidget(setupColorPicker(m_splitter)); m_splitter->setStretchFactor(0, 1); m_sheetData.updatePixels(m_ctx, path); - canvas->rootContext()->setContextProperty("sheetData", &m_sheetData); - canvas->setSource(QUrl::fromLocalFile(":/qml/TileSheetEditor.qml")); - canvas->setResizeMode(QQuickWidget::SizeRootObjectToView); + m_canvas->rootContext()->setContextProperty("sheetData", &m_sheetData); + m_canvas->setSource(QUrl::fromLocalFile(":/qml/TileSheetEditor.qml")); + m_canvas->setResizeMode(QQuickWidget::SizeRootObjectToView); setColorTable(m_sheetData.palette()); restoreState(); } @@ -304,6 +304,7 @@ void TileSheetEditor::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.setValue("m_colorPicker.colorTable/geometry", m_colorPicker.colorTable->horizontalHeader()->saveState()); settings.endGroup(); } @@ -313,6 +314,7 @@ void TileSheetEditor::restoreState() { 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()); + m_colorPicker.colorTable->horizontalHeader()->restoreState(settings.value("m_colorPicker.colorTable/geometry", m_colorPicker.colorTable->horizontalHeader()->saveState()).toByteArray()); settings.endGroup(); } diff --git a/src/nostalgia/core/studio/tilesheeteditor.hpp b/src/nostalgia/core/studio/tilesheeteditor.hpp index 776851de..349a0b02 100644 --- a/src/nostalgia/core/studio/tilesheeteditor.hpp +++ b/src/nostalgia/core/studio/tilesheeteditor.hpp @@ -88,8 +88,9 @@ class TileSheetEditor: public studio::Editor { const studio::Context *m_ctx = nullptr; SheetData m_sheetData; QSplitter *m_splitter = nullptr; - class LabeledSpinner *m_tilesX = nullptr; - class LabeledSpinner *m_tilesY = nullptr; + struct LabeledSpinner *m_tilesX = nullptr; + struct LabeledSpinner *m_tilesY = nullptr; + class QQuickWidget* m_canvas = nullptr; struct { QComboBox *palette = nullptr; QTableWidget *colorTable = nullptr;