Skip to content

Commit

Permalink
better
Browse files Browse the repository at this point in the history
  • Loading branch information
fundies committed Jan 5, 2020
1 parent 3f388b2 commit 939388b
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 56 deletions.
66 changes: 62 additions & 4 deletions Editors/PathEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

#include "ui_PathEditor.h"

#include <QLineEdit>
#include <QEvent>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QSpinBox>
#include <QToolButton>

Expand All @@ -26,12 +27,14 @@ PathEditor::PathEditor(ProtoModelPtr model, QWidget* parent) : BaseEditor(model,

QSpinBox* xSnap(new QSpinBox(this));
ui->mainToolBar->addWidget(xSnap);
xSnap->setRange(1, 99999999);

QLabel* ySnapLabel(new QLabel(tr("Y Snap"), this));
ui->mainToolBar->addWidget(ySnapLabel);

QSpinBox* ySnap(new QSpinBox(this));
ui->mainToolBar->addWidget(ySnap);
ySnap->setRange(1, 99999999);

ui->mainToolBar->addSeparator();

Expand All @@ -40,11 +43,16 @@ PathEditor::PathEditor(ProtoModelPtr model, QWidget* parent) : BaseEditor(model,
roomButton->setIcon(ArtManager::GetIcon(":/resources/room.png"));
ui->mainToolBar->addWidget(roomButton);

QLineEdit* roomLineEdit(new QLineEdit(this));
roomLineEdit = new QLineEdit(this);
roomLineEdit->setReadOnly(true);
roomLineEdit->setMaximumWidth(150);
ui->mainToolBar->addWidget(roomLineEdit);

connect(roomButton, &QToolButton::pressed, [=]() {
roomLineEdit->setText("");
ui->roomView->SetResourceModel(nullptr);
});

connect(roomButton->menu, &QMenu::triggered, this, [=](QAction* action) {
roomLineEdit->setText(action->text());
ui->roomView->SetResourceModel(MainWindow::resourceMap->GetResourceByName(TypeCase::kRoom, action->text())
Expand All @@ -71,21 +79,71 @@ PathEditor::PathEditor(ProtoModelPtr model, QWidget* parent) : BaseEditor(model,
connect(zoomOut, &QToolButton::pressed, ui->pathPreviewBackground,
[=]() { ui->pathPreviewBackground->SetZoom(ui->pathPreviewBackground->GetZoom() / 2); });

connect(ui->actionShowGrid, &QAction::triggered, [=]() {
ui->roomView->GetGrid().show = !ui->roomView->GetGrid().show;
ui->pathPreviewBackground->update();
});

connect(ui->actionSnaptoGrid, &QAction::triggered, [this]() { this->snapToGrid = !this->snapToGrid; });

cursorPositionLabel = new QLabel();
connect(ui->pathPreviewBackground, &AssetScrollAreaBackground::MouseMoved, [=](int x, int y) {
const GridDimensions g = ui->roomView->GetGrid();
QPoint mousePos(RoundNum(x, g.horSpacing), RoundNum(y, g.vertSpacing));
QPoint mousePos(x, y);
if (snapToGrid) mousePos = QPoint(RoundNum(x, g.horSpacing), RoundNum(y, g.vertSpacing));
ui->roomView->mousePos = mousePos;
cursorPositionLabel->setText(tr("X %0, Y %1").arg(mousePos.x()).arg(mousePos.y()));
ui->pathPreviewBackground->update();
});
ui->statusBar->addWidget(cursorPositionLabel);

ui->pointsTableView->installEventFilter(this);

connect(xSnap, QOverload<int>::of(&QSpinBox::valueChanged),
[this](int val) { ui->roomView->GetGrid().horSpacing = val; });

connect(ySnap, QOverload<int>::of(&QSpinBox::valueChanged),
[this](int val) { ui->roomView->GetGrid().vertSpacing = val; });

nodeMapper->addMapping(ui->nameEdit, TreeNode::kNameFieldNumber);

resMapper->addMapping(ui->smoothCheckBox, Path::kSmoothFieldNumber);
resMapper->addMapping(ui->closedCheckBox, Path::kClosedFieldNumber);
resMapper->addMapping(ui->precisionSpinBox, Path::kPrecisionFieldNumber);
resMapper->addMapping(xSnap, Path::kHsnapFieldNumber);
resMapper->addMapping(ySnap, Path::kVsnapFieldNumber);

RebindSubModels();
}

PathEditor::~PathEditor() { delete ui; }

void PathEditor::RebindSubModels() {
ProtoModelPtr pathModel = _model->GetSubModel(TreeNode::kPathFieldNumber);

connect(pathModel, &ProtoModel::dataChanged, [=]() { ui->pathPreviewBackground->update(); });

ui->roomView->SetPathModel(pathModel);
RepeatedProtoModelPtr pointsModel = pathModel->GetRepeatedSubModel(Path::kPointsFieldNumber);
ui->pointsTableView->setModel(pointsModel);
ui->pointsTableView->hideColumn(0);

QString roomName = pathModel->data(Path::kBackgroundRoomNameFieldNumber).toString();
if (roomName != "") {
ui->roomView->SetResourceModel(
MainWindow::resourceMap->GetResourceByName(TypeCase::kRoom, roomName)->GetSubModel(TreeNode::kRoomFieldNumber));
}
roomLineEdit->setText(roomName);

BaseEditor::RebindSubModels();
}

PathEditor::~PathEditor() { delete ui; }
bool PathEditor::eventFilter(QObject* obj, QEvent* event) {
if (obj == ui->pointsTableView && event->type() == QEvent::Resize) {
QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
for (int c = 0; c < 3; ++c) {
ui->pointsTableView->setColumnWidth(c, (resizeEvent->size().width() - 40) / 3);
}
}
return QWidget::eventFilter(obj, event);
}
5 changes: 5 additions & 0 deletions Editors/PathEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Components/ArtManager.h"

#include <QLabel>
#include <QLineEdit>

namespace Ui {
class PathEditor;
Expand All @@ -16,10 +17,14 @@ class PathEditor : public BaseEditor {
public:
explicit PathEditor(ProtoModelPtr model, QWidget* parent);
~PathEditor() override;
void RebindSubModels() override;
bool eventFilter(QObject* obj, QEvent* event) override;

private:
Ui::PathEditor* ui;
QLabel* cursorPositionLabel;
QLineEdit* roomLineEdit;
bool snapToGrid = true;
};

#endif // PATHEDITOR_H
30 changes: 30 additions & 0 deletions Editors/PathEditor.ui
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="value">
<number>4</number>
</property>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -318,6 +321,12 @@
<height>0</height>
</size>
</property>
<property name="minimum">
<number>-999999999</number>
</property>
<property name="maximum">
<number>999999999</number>
</property>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -365,6 +374,12 @@
<height>0</height>
</size>
</property>
<property name="minimum">
<number>-999999999</number>
</property>
<property name="maximum">
<number>999999999</number>
</property>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -418,6 +433,15 @@
<height>0</height>
</size>
</property>
<property name="minimum">
<number>-999999999</number>
</property>
<property name="maximum">
<number>999999999</number>
</property>
<property name="value">
<number>100</number>
</property>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -629,6 +653,9 @@
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/actions/snap-to-grid.png</normaloff>:/actions/snap-to-grid.png</iconset>
Expand All @@ -650,6 +677,9 @@
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/actions/grid.png</normaloff>:/actions/grid.png</iconset>
Expand Down
2 changes: 2 additions & 0 deletions Models/ProtoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ bool ProtoModel::setData(const QModelIndex &index, const QVariant &value, int ro
SetDirty(true);
emit dataChanged(index, index, oldValue);

if (GetParentModel() != nullptr) emit GetParentModel()->dataChanged(QModelIndex(), QModelIndex());

return true;
}

Expand Down
1 change: 1 addition & 0 deletions Models/RepeatedProtoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ bool RepeatedProtoModel::setData(const QModelIndex &index, const QVariant &value
const QVariant oldValue = this->data(index, role);
if (models[index.row()]->setData(models[index.row()]->index(index.column(), 0), value, role)) {
GetParentModel()->SetDirty(true);
emit GetParentModel()->dataChanged(QModelIndex(), QModelIndex());
emit dataChanged(index, index, oldValue);
return true;
}
Expand Down
18 changes: 12 additions & 6 deletions Models/ResourceModelMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ void ResourceModelMap::recursiveBindRes(buffers::TreeNode* node, QObject* parent
}

void ResourceModelMap::AddResource(buffers::TreeNode* child, QObject* parent) {
ProtoModelPtr model = child->has_folder() ? nullptr : ProtoModelPtr(new ProtoModel(child, this));
_resources[child->type_case()][QString::fromStdString(child->name())] =
QPair<buffers::TreeNode*, ProtoModelPtr>(child, child->has_folder() ? nullptr : ProtoModelPtr(new ProtoModel(child, this)));
QPair<buffers::TreeNode*, ProtoModelPtr>(child, model);

if (model != nullptr) {
connect(model, &ProtoModel::dataChanged, [this]() { emit dataChanged(); });
}
}

void ResourceModelMap::RemoveResource(TypeCase type, const QString& name) {
Expand Down Expand Up @@ -60,8 +65,7 @@ void ResourceModelMap::RemoveResource(TypeCase type, const QString& name) {
RepeatedProtoModel::RowRemovalOperation remover(tilesModel);

for (int row = 0; row < tilesModel->rowCount(); ++row) {
if (tilesModel->data(row, Room::Instance::kObjectTypeFieldNumber).toString() == name)
remover.RemoveRow(row);
if (tilesModel->data(row, Room::Instance::kObjectTypeFieldNumber).toString() == name) remover.RemoveRow(row);
}

// Only models in use in open editors should have backup models
Expand All @@ -86,6 +90,7 @@ void ResourceModelMap::RemoveResource(TypeCase type, const QString& name) {
}

_resources[type].remove(name);
emit dataChanged();
}

QString ResourceModelMap::CreateResourceName(TreeNode* node) {
Expand Down Expand Up @@ -117,18 +122,19 @@ ProtoModelPtr ResourceModelMap::GetResourceByName(int type, const std::string& n
return GetResourceByName(type, QString::fromStdString(name));
}


void ResourceModelMap::ResourceRenamed(TypeCase type, const QString& oldName, const QString& newName) {
if (oldName == newName || !_resources[type].contains(oldName)) return;
_resources[type][newName] = _resources[type][oldName];

for (auto res : _resources) {
for (auto model : res) {
UpdateReferences(model.second, ResTypeAsString(type), oldName, newName);
}
}

_resources[type].remove(oldName);

emit dataChanged();
}

const ProtoModelPtr GetObjectSprite(const std::string& objName) {
Expand Down
3 changes: 3 additions & 0 deletions Models/ResourceModelMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class ResourceModelMap : public QObject {
public slots:
void ResourceRenamed(TypeCase type, const QString& oldName, const QString& newName);

signals:
void dataChanged();

protected:
void recursiveBindRes(buffers::TreeNode* node, QObject* parent);
QHash<int, QHash<QString, QPair<buffers::TreeNode*, ProtoModelPtr>>> _resources;
Expand Down
17 changes: 11 additions & 6 deletions Widgets/AssetScrollAreaBackground.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "AssetScrollAreaBackground.h"
#include "Components/ArtManager.h"
#include "MainWindow.h"
#include "Widgets/RoomView.h"

#include <QEvent>
Expand All @@ -10,6 +11,8 @@
AssetScrollAreaBackground::AssetScrollAreaBackground(AssetScrollArea* parent) : QWidget(parent) {
installEventFilter(this);
setMouseTracking(true);
// Redraw on an model changes
connect(MainWindow::resourceMap.get(), &ResourceModelMap::dataChanged, [this]() { this->update(); });
}

void AssetScrollAreaBackground::SetAssetView(AssetView* asset) {
Expand Down Expand Up @@ -126,12 +129,14 @@ void AssetScrollAreaBackground::paintEvent(QPaintEvent* /* event */) {
painter.restore();

GridDimensions g = assetView->GetGrid();
if (g.type == GridType::Complex)
paintGrid(painter, g.width * zoom, g.height * zoom, g.horSpacing * zoom, g.vertSpacing * zoom,
(g.horOff * zoom) + offset.x(), (g.vertOff * zoom) + offset.y(), g.cellWidth * zoom,
g.cellHeight * zoom);
else if (g.type == GridType::Standard)
paintGrid(painter, g.horSpacing * zoom, g.vertSpacing * zoom, offset.x(), offset.y());
if (g.show) {
if (g.type == GridType::Complex)
paintGrid(painter, g.width * zoom, g.height * zoom, g.horSpacing * zoom, g.vertSpacing * zoom,
(g.horOff * zoom) + offset.x(), (g.vertOff * zoom) + offset.y(), g.cellWidth * zoom,
g.cellHeight * zoom);
else if (g.type == GridType::Standard)
paintGrid(painter, g.horSpacing * zoom, g.vertSpacing * zoom, offset.x(), offset.y());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Widgets/AssetView.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ enum GridType {
Standard, // Simple x by x Grid
Complex, // Grid with offset and spacing between squares
Isometric, // TODO: Implement this
None, // Don't draw the grid
};

struct GridDimensions {
GridType type = GridType::Standard;
bool show = true;
int horSpacing = 16;
int vertSpacing = 16;
int horOff = 0;
Expand Down
6 changes: 3 additions & 3 deletions Widgets/BackgroundView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ QSize BackgroundView::sizeHint() const { return QSize(pixmap.width(), pixmap.hei

void BackgroundView::Paint(QPainter &painter) {
if (!model) {
grid.type = GridType::None;
grid.show = false;
return;
}

Expand All @@ -65,7 +65,7 @@ void BackgroundView::Paint(QPainter &painter) {
painter.drawPixmap(0, 0, (transparent) ? transparentPixmap : pixmap);

if (model->data(Background::kUseAsTilesetFieldNumber).toBool()) {
grid.type = GridType::Complex;
grid.show = true;
grid.horSpacing = model->data(Background::kHorizontalSpacingFieldNumber).toInt();
grid.vertSpacing = model->data(Background::kVerticalSpacingFieldNumber).toInt();
grid.horOff = model->data(Background::kHorizontalOffsetFieldNumber).toInt();
Expand All @@ -75,6 +75,6 @@ void BackgroundView::Paint(QPainter &painter) {
grid.width = pixmap.width();
grid.height = pixmap.height();
} else {
grid.type = GridType::None;
grid.show = false;
}
}

0 comments on commit 939388b

Please sign in to comment.