[nostalgia/core/studio] Replace TileSheetEditor ScrollView scrolling with manual version

This commit is contained in:
Gary Talent 2020-02-25 20:26:35 -06:00
parent f762e658de
commit 7ca34da417
3 changed files with 48 additions and 21 deletions

View File

@ -10,18 +10,10 @@ import QtQuick 2.0
import QtQuick.Controls 2.14 import QtQuick.Controls 2.14
import 'qrc:/qml/' import 'qrc:/qml/'
ScrollView { Rectangle {
id: tileSheetEditor id: tileSheetEditor
ScrollBar.horizontal.policy: ScrollBar.AsNeeded
ScrollBar.vertical.policy: ScrollBar.AsNeeded
contentWidth: tileGrid.width
contentHeight: tileGrid.height
clip: true clip: true
focusPolicy: Qt.WheelFocus color: '#717d7e'
background: Rectangle {
color: '#717d7e'
}
MouseArea { MouseArea {
id: mouseArea id: mouseArea
@ -45,10 +37,42 @@ ScrollView {
} else if (tileGrid.scaleFactor > 0.9) { } else if (tileGrid.scaleFactor > 0.9) {
tileGrid.scaleFactor -= mod; 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 { } 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)) onPositionChanged: sheetData.updatePixel(pixelAt(mouseX, mouseY))
@ -74,10 +98,10 @@ ScrollView {
id: tileGrid id: tileGrid
property double scaleFactor: 0.9 property double scaleFactor: 0.9
property int baseTileSize: Math.min(2000 / tileGrid.columns, 1000 / tileGrid.rows) 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 width: tileGrid.columns * tileGrid.baseTileSize * tileGrid.scaleFactor
height: tileGrid.rows * tileGrid.baseTileSize * tileGrid.scaleFactor height: tileGrid.rows * tileGrid.baseTileSize * tileGrid.scaleFactor
//anchors.horizontalCenter: parent.horizontalCenter
//anchors.verticalCenter: parent.verticalCenter
rows: sheetData ? sheetData.rows : 1 rows: sheetData ? sheetData.rows : 1
columns: sheetData ? sheetData.columns : 1 columns: sheetData ? sheetData.columns : 1
Repeater { Repeater {

View File

@ -224,17 +224,17 @@ TileSheetEditor::TileSheetEditor(QString path, const studio::Context *ctx, QWidg
m_splitter = new QSplitter(this); m_splitter = new QSplitter(this);
auto canvasParent = new QWidget(m_splitter); auto canvasParent = new QWidget(m_splitter);
auto canvasLyt = new QVBoxLayout(canvasParent); auto canvasLyt = new QVBoxLayout(canvasParent);
auto canvas = new QQuickWidget(canvasParent); m_canvas = new QQuickWidget(canvasParent);
canvasLyt->addWidget(canvas); canvasLyt->addWidget(m_canvas);
canvasLyt->setMenuBar(setupToolBar()); canvasLyt->setMenuBar(setupToolBar());
lyt->addWidget(m_splitter); lyt->addWidget(m_splitter);
m_splitter->addWidget(canvasParent); 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);
canvas->rootContext()->setContextProperty("sheetData", &m_sheetData); m_canvas->rootContext()->setContextProperty("sheetData", &m_sheetData);
canvas->setSource(QUrl::fromLocalFile(":/qml/TileSheetEditor.qml")); m_canvas->setSource(QUrl::fromLocalFile(":/qml/TileSheetEditor.qml"));
canvas->setResizeMode(QQuickWidget::SizeRootObjectToView); m_canvas->setResizeMode(QQuickWidget::SizeRootObjectToView);
setColorTable(m_sheetData.palette()); setColorTable(m_sheetData.palette());
restoreState(); restoreState();
} }
@ -304,6 +304,7 @@ void TileSheetEditor::saveState() {
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/tileRows", m_sheetData.rows());
settings.setValue("m_sheetData/tileColumns", m_sheetData.columns()); settings.setValue("m_sheetData/tileColumns", m_sheetData.columns());
settings.setValue("m_colorPicker.colorTable/geometry", m_colorPicker.colorTable->horizontalHeader()->saveState());
settings.endGroup(); settings.endGroup();
} }
@ -313,6 +314,7 @@ void TileSheetEditor::restoreState() {
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.setRows(settings.value("m_sheetData/tileRows", 1).toInt());
m_sheetData.setColumns(settings.value("m_sheetData/tileColumns", 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(); settings.endGroup();
} }

View File

@ -88,8 +88,9 @@ 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; struct LabeledSpinner *m_tilesX = nullptr;
class LabeledSpinner *m_tilesY = nullptr; struct LabeledSpinner *m_tilesY = nullptr;
class QQuickWidget* m_canvas = nullptr;
struct { struct {
QComboBox *palette = nullptr; QComboBox *palette = nullptr;
QTableWidget *colorTable = nullptr; QTableWidget *colorTable = nullptr;