diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ac79748..a9bfaf46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -299,6 +299,8 @@ set(EDITOR_SOURCES src/ui/picker/picker_child_widget.h src/ui/picker/picker_charset_widget.cpp src/ui/picker/picker_charset_widget.h + src/ui/picker/picker_chipset_widget.cpp + src/ui/picker/picker_chipset_widget.h src/ui/picker/picker_faceset_widget.cpp src/ui/picker/picker_faceset_widget.h src/ui/picker/picker_dialog.cpp @@ -308,12 +310,16 @@ set(EDITOR_SOURCES src/ui/viewer/battle_animation_graphics_item.h src/ui/viewer/charset_graphics_item.cpp src/ui/viewer/charset_graphics_item.h + src/ui/viewer/chipset_graphics_view.cpp + src/ui/viewer/chipset_graphics_view.h src/ui/viewer/faceset_graphics_item.cpp src/ui/viewer/faceset_graphics_item.h src/ui/viewer/rpg_graphics_view.cpp src/ui/viewer/rpg_graphics_view.h src/ui/viewer/stat_curve_graphics_item.cpp src/ui/viewer/stat_curve_graphics_item.h + src/ui/viewer/tile_graphics_item.cpp + src/ui/viewer/tile_graphics_item.cpp ) add_library(${PROJECT_NAME} STATIC ${EDITOR_SOURCES}) diff --git a/src/ui/database/chipset_widget.cpp b/src/ui/database/chipset_widget.cpp index 01243eef..6fd6e0e3 100644 --- a/src/ui/database/chipset_widget.cpp +++ b/src/ui/database/chipset_widget.cpp @@ -18,20 +18,108 @@ #include "chipset_widget.h" #include "ui_chipset_widget.h" -ChipSetWidget::ChipSetWidget(ProjectData& project, QWidget *parent) : +#include "common/lcf_widget_binding.h" +#include "ui/picker/picker_chipset_widget.h" +#include "ui/picker/picker_dialog.h" + +#include + +ChipsetWidget::ChipsetWidget(ProjectData& project, QWidget *parent) : QWidget(parent), - ui(new Ui::ChipSetWidget), + ui(new Ui::ChipsetWidget), m_project(project) { ui->setupUi(this); + + QListView& list = *ui->listTerrain; + list.setModel(new RpgModel(project, project.database().terrains, parent)); + + LcfWidgetBinding::connect(this, ui->lineName); + + m_buttonGroupSequence = new QButtonGroup(this); + m_buttonGroupSequence->addButton(ui->radioLowerAnim1232); + m_buttonGroupSequence->setId(ui->radioLowerAnim1232, 0); + m_buttonGroupSequence->addButton(ui->radioLowerAnim123); + m_buttonGroupSequence->setId(ui->radioLowerAnim123, 1); + LcfWidgetBinding::connect(this, m_buttonGroupSequence); + connect(m_buttonGroupSequence, QOverload::of(&QButtonGroup::buttonClicked), + [=](QAbstractButton*){ + if (m_water_tile) { + m_water_tile->setAnimType(m_buttonGroupSequence->checkedId()); + } + }); + + m_buttonGroupSpeed = new QButtonGroup(this); + m_buttonGroupSpeed->addButton(ui->radioLowerAnimSlow); + m_buttonGroupSpeed->setId(ui->radioLowerAnimSlow, 0); + m_buttonGroupSpeed->addButton(ui->radioLowerAnimFast); + m_buttonGroupSpeed->setId(ui->radioLowerAnimFast, 1); + LcfWidgetBinding::connect(this, m_buttonGroupSpeed); + connect(m_buttonGroupSpeed, QOverload::of(&QButtonGroup::buttonClicked), + [=](QAbstractButton*){ + if (m_water_tile) { + m_water_tile->setAnimSpeed(m_buttonGroupSpeed->checkedId()); + } + }); + + ui->graphicsChipsetLower->setProjectData(project); + ui->graphicsChipsetLower->setLayer(ChipsetGraphicsView::Layer::Lower); + ui->graphicsChipsetUpper->setProjectData(project); + ui->graphicsChipsetUpper->setLayer(ChipsetGraphicsView::Layer::Upper); + + ui->viewWaterAnim->scale(2., 2.); + ui->viewWaterAnim->enableTimer(); + + QObject::connect(ui->pushTileset, &QPushButton::clicked, this, &ChipsetWidget::chipsetClicked); } -ChipSetWidget::~ChipSetWidget() +ChipsetWidget::~ChipsetWidget() { delete ui; } -void ChipSetWidget::setData(lcf::rpg::Chipset* /* chipset */) +void ChipsetWidget::setData(lcf::rpg::Chipset* chipset) { + if (!chipset) { + chipset = &dummy; + } + m_current = chipset; + + LcfWidgetBinding::bind(ui->lineName, chipset->name); + LcfWidgetBinding::bind(m_buttonGroupSpeed, chipset->animation_speed); + LcfWidgetBinding::bind(m_buttonGroupSequence, chipset->animation_type); + + for (auto* view: {ui->graphicsChipsetLower, ui->graphicsChipsetUpper}) { + view->setChipset(*chipset); + view->setShowGrid(true); + view->refresh(); + } + + QString file = m_project.project().findFile(CHIPSET, ToQString(m_current->chipset_name), FileFinder::FileType::Image); + if (!file.isEmpty()) { + m_water_tile = new TileGraphicsItem(m_project, nullptr, ImageLoader::Load(file)); + m_water_tile->setLayer(ChipsetGraphicsView::Layer::Lower); + m_water_tile->setTileIndex(2); + m_water_tile->setAnimType(m_current->animation_type); + m_water_tile->setAnimSpeed(m_current->animation_speed); + ui->viewWaterAnim->setItem(m_water_tile); + } else { + ui->viewWaterAnim->setItem(nullptr); + } + + ui->pushTileset->setText(ToQString(chipset->chipset_name)); + + this->setEnabled(chipset != &dummy); +} +void ChipsetWidget::chipsetClicked() { + auto* widget = new PickerChipsetWidget(this); + PickerDialog dialog(m_project, FileFinder::FileType::Image, widget, this); + QObject::connect(&dialog, &PickerDialog::fileSelected, [&](const QString& baseName) { + m_current->chipset_name = ToDBString(baseName); + ui->pushTileset->setText(ToQString(m_current->chipset_name)); + setData(m_current); + }); + dialog.setDirectoryAndFile(CHIPSET, ToQString(m_current->chipset_name)); + dialog.exec(); } diff --git a/src/ui/database/chipset_widget.h b/src/ui/database/chipset_widget.h index d60458d5..c46d0ed1 100644 --- a/src/ui/database/chipset_widget.h +++ b/src/ui/database/chipset_widget.h @@ -23,23 +23,35 @@ class ProjectData; namespace Ui { -class ChipSetWidget; +class ChipsetWidget; } -class ChipSetWidget : public QWidget +class QButtonGroup; +class TileGraphicsItem; + +class ChipsetWidget : public QWidget { Q_OBJECT public: using value_type = lcf::rpg::Chipset; - explicit ChipSetWidget(ProjectData& project, QWidget *parent = nullptr); - ~ChipSetWidget(); + explicit ChipsetWidget(ProjectData& project, QWidget *parent = nullptr); + ~ChipsetWidget(); void setData(lcf::rpg::Chipset* chipset); private: - Ui::ChipSetWidget *ui; + void chipsetClicked(); + + lcf::rpg::Chipset dummy; + lcf::rpg::Chipset *m_current = nullptr; + + Ui::ChipsetWidget *ui; ProjectData& m_project; + + TileGraphicsItem* m_water_tile = nullptr; + QButtonGroup* m_buttonGroupSequence = nullptr; + QButtonGroup* m_buttonGroupSpeed = nullptr; }; diff --git a/src/ui/database/chipset_widget.ui b/src/ui/database/chipset_widget.ui index f1f8ac2f..bc3f4be5 100644 --- a/src/ui/database/chipset_widget.ui +++ b/src/ui/database/chipset_widget.ui @@ -1,769 +1,309 @@ - ChipSetWidget - + ChipsetWidget + 0 0 - 690 - 557 + 929 + 605 Form - - - - 10 - 10 - 151 - 23 - - - - - - - - - 255 - 255 - 255 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 255 - 255 - 255 - - - - - - - 255 - 255 - 255 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 220 - - - - - - - 0 - 0 - 0 - - - - - - - - - 255 - 255 - 255 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 255 - 255 - 255 - - - - - - - 255 - 255 - 255 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 220 - - - - - - - 0 - 0 - 0 - - - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 255 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 0 - 0 - 0 - - - - - - - 255 - 255 - 220 - - - - - - - 0 - 0 - 0 - - - - - - - - - 10 - 75 - true - - - - true - - - Titleset - - - Qt::AlignCenter - - - - - - 10 - 60 - 150 - 465 - - - - - 150 - 16777215 - - - - true - - - - - - 180 - 10 - 491 - 501 - - - - - 1 - 0 - - - - QFrame::WinPanel - - - QFrame::Sunken - - - - - - - - Name - - - - 5 - - - 5 - - - 5 - - - 5 - - - - - - 0 - 0 - + + + + + + 4 + 0 + + + + 0 + + + + Lower Layer + + + + + + + + Editing Mode + + + + + + Terrain + + + groupLowerEditMode + + + + + + + Passability + + + groupLowerEditMode + + + + + + + Passage + + + groupLowerEditMode + + + + + + + + + + Set uniform Terrain - - + + - Title File + Water Animation - - - 3 - - - 5 - - - 5 - - - 5 - - - 5 - + - + + + + 0 + 0 + + + - - - ... + + + Sequence + + + + + + 1-2-3-2 + + + + + + + 1-2-3 + + + + + + + + + + Speed + + + + + Slow + + + + + + + Fast + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + - - - - - - - - GroupBox - + + + + + + 0 + 0 + + + + + - - - - - 0 - - - - LowerLayer - - - - - 180 - 20 - 201 - 341 - - - - - - - 10 - 20 - 151 - 111 - - - - Editing Mode - - - - - 0 - 20 - 151 - 100 - + + + Upper Layer + + + + + + + 0 + 0 + - - - + + Editing Mode + + + + - Terrain + Counter + + groupUpperEditMode + - - + + Passability + + groupUpperEditMode + - - + + - Directional Pass + Passage + + groupUpperEditMode + - - - - - 20 - 140 - 141 - 21 - - - - Set uniform Terrain - - - - - - 20 - 200 - 141 - 161 - - - - Water Animation - - - - - 10 - 40 - 121 - 51 - + + + + + + + + Qt::Vertical - - Sequence + + + 20 + 315 + + + + + + + + + + + + + 1 + 0 + + + + + + + + + + + + Name: - - - - 20 - 10 - 82 - 17 - - - - 1-2-3-2 - - - - - - 20 - 30 - 82 - 17 - - - - 1-2-3 - - - - - - 10 - 100 - 121 - 51 - + + + + + + + + + + + + Tileset: - - Speed + + + + + + - - - - 20 - 10 - 82 - 17 - - - - Slow - - - - - - 20 - 30 - 82 - 17 - - - - Fast - - - - - - - UpperLayer - - - - - - - - - - 10 - 40 - 150 - 20 - - - - Filter - - + + + + + + + + + TileGraphicsView + QGraphicsView +
ui/viewer/rpg_graphics_view.h
+
+ + ChipsetGraphicsView + QGraphicsView +
ui/viewer/chipset_graphics_view.h
+
+
+ + + +
diff --git a/src/ui/database/database_dialog.cpp b/src/ui/database/database_dialog.cpp index bb727031..190fffaa 100644 --- a/src/ui/database/database_dialog.cpp +++ b/src/ui/database/database_dialog.cpp @@ -46,7 +46,7 @@ DatabaseDialog::DatabaseDialog(ProjectData& project, QWidget *parent) : pageBattleAnimations2 = new DatabaseSplitWidget(m_projectDataCopy, this); pageBattleScreen = new BattleScreenWidget(m_projectDataCopy, this); pageTerrain = new DatabaseSplitWidget(m_projectDataCopy, this); - // FIXME: This fails to compile pageChipset = new DatabaseSplitWidget(m_projectDataCopy, this); + pageChipset = new DatabaseSplitWidget(m_projectDataCopy, this); pageCommonevents = new DatabaseSplitWidget(m_projectDataCopy, this); pageSwitches = new DatabaseSplitWidget(m_projectDataCopy, this); pageVariables = new DatabaseSplitWidget(m_projectDataCopy, this); @@ -66,7 +66,7 @@ DatabaseDialog::DatabaseDialog(ProjectData& project, QWidget *parent) : ui->tabOld_Pages->insertTab(9, pageBattleAnimations2, tr("Battle Animation 2")); ui->tabOld_Pages->insertTab(10, pageBattleScreen, tr("Battle screen")); ui->tabOld_Pages->insertTab(11, pageTerrain, tr("Terrain")); - // FIXME ui->tabOld_Pages->insertTab(12, pageChipset, tr("Chipset")); + ui->tabOld_Pages->insertTab(12, pageChipset, tr("Chipset")); ui->tabOld_Pages->insertTab(13, pageVocabulary, tr("Vocabulary")); ui->tabOld_Pages->insertTab(14, pageSystem, tr("System")); ui->tabOld_Pages->insertTab(15, pageSystem2, tr("System")); diff --git a/src/ui/picker/picker_chipset_widget.cpp b/src/ui/picker/picker_chipset_widget.cpp new file mode 100644 index 00000000..00fd07e8 --- /dev/null +++ b/src/ui/picker/picker_chipset_widget.cpp @@ -0,0 +1,29 @@ +/* + * This file is part of EasyRPG Editor. + * + * EasyRPG Editor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EasyRPG Editor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EasyRPG Editor. If not, see . + */ + +#include "picker_chipset_widget.h" +#include "ui/viewer/rpg_graphics_view.h" +#include + +void PickerChipsetWidget::imageChanged(QPixmap image) { + if (!m_pixmap) { + m_pixmap = new QGraphicsPixmapItem(image); + } + + m_pixmap->setPixmap(image); + m_view->setItem(m_pixmap); +} diff --git a/src/ui/picker/picker_chipset_widget.h b/src/ui/picker/picker_chipset_widget.h new file mode 100644 index 00000000..79ead99c --- /dev/null +++ b/src/ui/picker/picker_chipset_widget.h @@ -0,0 +1,35 @@ +/* + * This file is part of EasyRPG Editor. + * + * EasyRPG Editor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EasyRPG Editor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EasyRPG Editor. If not, see . + */ + +#pragma once + +#include +#include "picker_child_widget.h" + +class QGraphicsScene; +class QGraphicsPixmapItem; + +class PickerChipsetWidget : public PickerChildWidget { + Q_OBJECT +public: + PickerChipsetWidget(QWidget* parent = nullptr) : PickerChildWidget(parent) {} + + void imageChanged(QPixmap image) override; + +private: + QGraphicsPixmapItem* m_pixmap = nullptr; +}; diff --git a/src/ui/viewer/chipset_graphics_view.cpp b/src/ui/viewer/chipset_graphics_view.cpp new file mode 100644 index 00000000..abfcd63b --- /dev/null +++ b/src/ui/viewer/chipset_graphics_view.cpp @@ -0,0 +1,138 @@ +/* + * This file is part of EasyRPG Editor. + * + * EasyRPG Editor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EasyRPG Editor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EasyRPG Editor. If not, see . + */ + +#include "chipset_graphics_view.h" +#include "model/project.h" +#include "common/dbstring.h" +#include "common/image_loader.h" +#include "defines.h" +#include "tile_graphics_item.h" +#include + +ChipsetGraphicsView::ChipsetGraphicsView(QWidget* parent) : QGraphicsView(parent) { + setScene(new QGraphicsScene(this)); + scale(2, 2); +} + +void ChipsetGraphicsView::setProjectData(ProjectData& project) { + m_project = &project; +} + +void ChipsetGraphicsView::setChipset(lcf::rpg::Chipset& chipset) { + m_chipset = &chipset; + QString file = m_project->project().findFile(CHIPSET, ToQString(chipset.chipset_name), FileFinder::FileType::Image); + if (!file.isEmpty()) { + m_chipset_pix = ImageLoader::Load(file); + } +} + +void ChipsetGraphicsView::enableTimer() { + /*m_timer.reset(new QTimer()); + QObject::connect(m_timer.get(), &QTimer::timeout, scene(), &QGraphicsScene::advance); + m_timer->start(1000 / 33);*/ +} + +void ChipsetGraphicsView::mousePressEvent(QMouseEvent* event) { + /*if (event->button() == Qt::LeftButton && m_item) { + const auto& p = mapToScene(event->pos()); + if (m_item->boundingRect().contains(p)) { + emit clicked(p); + } + } + QGraphicsView::mousePressEvent(event);*/ +} + +bool ChipsetGraphicsView::showGrid() const { + return m_show_grid; +} + +void ChipsetGraphicsView::setShowGrid(bool show_grid) { + m_show_grid = show_grid; +} + +int ChipsetGraphicsView::tilesPerCol() const { + return m_tiles_per_col; +} + +void ChipsetGraphicsView::setTilesPerCol(int tiles_per_col) { + m_tiles_per_col = tiles_per_col; +} + +ChipsetGraphicsView::Layer ChipsetGraphicsView::layer() const { + return m_layer; +} + +void ChipsetGraphicsView::setLayer(ChipsetGraphicsView::Layer layer) { + m_layer = layer; +} + +ChipsetGraphicsView::EditMode ChipsetGraphicsView::editMode() const { + return m_editmode; +} + +void ChipsetGraphicsView::setEditMode(ChipsetGraphicsView::EditMode edit_mode) { + m_editmode = edit_mode; +} + +void ChipsetGraphicsView::refresh() { + assert(m_project); + assert(m_chipset); + + const int TILE_SIZE = 16; + + int num_tiles = 0; + + if (layer() == Layer::Lower) { + scene()->clear(); + num_tiles = 162; + for (int terrain_id = 0; terrain_id < num_tiles; terrain_id++) { + auto* tile = new TileGraphicsItem(*m_project, m_chipset, m_chipset_pix); + tile->setTileIndex(terrain_id); + tile->setLayer(Layer::Lower); + tile->setEditMode(m_editmode); + tile->setPos(QPointF(terrain_id * TILE_SIZE % (TILE_SIZE * m_tiles_per_col), terrain_id / m_tiles_per_col * TILE_SIZE)); + scene()->addItem(tile); + } + } else if (layer() == Layer::Upper) { + scene()->clear(); + num_tiles = 144; + for (int terrain_id = 0; terrain_id < num_tiles; terrain_id++) { + auto* tile = new TileGraphicsItem(*m_project, m_chipset, m_chipset_pix); + tile->setTileIndex(terrain_id); + tile->setLayer(Layer::Upper); + tile->setEditMode(m_editmode); + tile->setPos(QPointF(terrain_id * TILE_SIZE % (TILE_SIZE * m_tiles_per_col), terrain_id / m_tiles_per_col * TILE_SIZE)); + scene()->addItem(tile); + } + } + + if (showGrid() && num_tiles > 0) { + // Draw X Grid + for (int i = 1; i < num_tiles - 1; ++i) { + if (i % m_tiles_per_col == 0) { + int y = i / m_tiles_per_col * TILE_SIZE; + scene()->addLine(0, y, m_tiles_per_col * TILE_SIZE, y); + } + } + + // Draw Y Grid + for (int i = 1; i < m_tiles_per_col; ++i) { + int x = i * TILE_SIZE; + scene()->addLine(x, 0, x, num_tiles / m_tiles_per_col * TILE_SIZE); + } + } +} diff --git a/src/ui/viewer/chipset_graphics_view.h b/src/ui/viewer/chipset_graphics_view.h new file mode 100644 index 00000000..8afc07f0 --- /dev/null +++ b/src/ui/viewer/chipset_graphics_view.h @@ -0,0 +1,89 @@ +/* + * This file is part of EasyRPG Editor. + * + * EasyRPG Editor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EasyRPG Editor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EasyRPG Editor. If not, see . + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +class ProjectData; + +namespace lcf::rpg { + class Chipset; +} + +class ChipsetGraphicsView : public QGraphicsView { +Q_OBJECT + +public: + enum class Layer { + None, + Lower, + Upper + }; + + enum class EditMode { + None, + Terrain, + Passability, + Passage, + Counter + }; + + explicit ChipsetGraphicsView(QWidget* parent); + + void setProjectData(ProjectData& project); + + void setChipset(lcf::rpg::Chipset& chipset); + + void enableTimer(); + + bool showGrid() const; + void setShowGrid(bool show_grid); + + int tilesPerCol() const; + void setTilesPerCol(int tiles_per_col); + + Layer layer() const; + void setLayer(Layer layer); + + EditMode editMode() const; + void setEditMode(EditMode edit_mode); + + void refresh(); + +signals: + void clicked(const QPointF&); + +protected: + void mousePressEvent(QMouseEvent* event) override; + + ProjectData* m_project = nullptr; + lcf::rpg::Chipset* m_chipset = nullptr; + std::unique_ptr m_timer; + QPixmap m_chipset_pix; + + bool m_show_grid = false; + int m_tiles_per_col = 6; + EditMode m_editmode = EditMode::Terrain; + Layer m_layer = Layer::None; +}; diff --git a/src/ui/viewer/rpg_graphics_view.cpp b/src/ui/viewer/rpg_graphics_view.cpp index c3f709d7..41fda574 100644 --- a/src/ui/viewer/rpg_graphics_view.cpp +++ b/src/ui/viewer/rpg_graphics_view.cpp @@ -17,13 +17,14 @@ #include "rpg_graphics_view.h" #include +#include RpgGraphicsViewBase::RpgGraphicsViewBase(QWidget* parent) : QGraphicsView(parent) { setScene(new QGraphicsScene(this)); } void RpgGraphicsViewBase::enableTimer() { - m_timer.reset(new QTimer()); + m_timer = std::make_unique(); QObject::connect(m_timer.get(), &QTimer::timeout, scene(), &QGraphicsScene::advance); m_timer->start(1000 / 33); } diff --git a/src/ui/viewer/rpg_graphics_view.h b/src/ui/viewer/rpg_graphics_view.h index 1b3d3bf0..f665fcba 100644 --- a/src/ui/viewer/rpg_graphics_view.h +++ b/src/ui/viewer/rpg_graphics_view.h @@ -27,6 +27,7 @@ #include "charset_graphics_item.h" #include "faceset_graphics_item.h" +#include "tile_graphics_item.h" class RpgGraphicsViewBase : public QGraphicsView { Q_OBJECT @@ -79,4 +80,5 @@ class RpgGraphicsView : public RpgGraphicsViewBase { using CharSetGraphicsView = RpgGraphicsView; using FaceSetGraphicsView = RpgGraphicsView; +using TileGraphicsView = RpgGraphicsView; using PixmapGraphicsView = RpgGraphicsView; diff --git a/src/ui/viewer/tile_graphics_item.cpp b/src/ui/viewer/tile_graphics_item.cpp new file mode 100644 index 00000000..2e3e4b02 --- /dev/null +++ b/src/ui/viewer/tile_graphics_item.cpp @@ -0,0 +1,178 @@ +/* + * This file is part of EasyRPG Editor. + * + * EasyRPG Editor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EasyRPG Editor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EasyRPG Editor. If not, see . + */ + +#include "tile_graphics_item.h" +#include "core.h" +#include "common/image_loader.h" +#include "common/dbstring.h" +#include "model/project_data.h" + +#include + +TileGraphicsItem::TileGraphicsItem(ProjectData& project, lcf::rpg::Chipset* chipset, const QPixmap pix) : + QGraphicsItem(), m_project(project), m_chipset(chipset), m_image(pix) { +} + +QRectF TileGraphicsItem::tileRect() const { + const int TILE_SIZE = 16; + + int sub_tile_id = 0; + int x; + int y; + + if (layer() == ChipsetGraphicsView::Layer::Lower) { + if (m_tile_index > 0 && m_tile_index < 3) { + x = m_tile_index % 2 * TILE_SIZE * 3; + y = m_tile_index / 2 * TILE_SIZE * 4; + } else if (m_tile_index >= 3 && m_tile_index < 6) { + sub_tile_id = m_tile_index - 3; + x = 3 * TILE_SIZE; + y = 4 * TILE_SIZE; + } else if (m_tile_index >= 6 && m_tile_index < 10) { + x = m_tile_index % 2 * TILE_SIZE * 3; + y = 128 + (m_tile_index - 6) / 2 * TILE_SIZE * 4; + } else if (m_tile_index >= 10 && m_tile_index < 18) { + x = m_tile_index % 2 * TILE_SIZE * 3 + 6 * TILE_SIZE; + y = (m_tile_index - 10) / 2 * TILE_SIZE * 4; + } else if (m_tile_index >= 18 && m_tile_index < 114) { + sub_tile_id = m_tile_index - 18; + x = 192; + y = 0; + } else if (m_tile_index >= 114 && m_tile_index < 170) { + sub_tile_id = m_tile_index - 114; + x = 288; + y = 0; + } else { // Invalid -> Use water tile (first one) + x = 0; + y = 0; + } + + x += sub_tile_id % 6 * 16 + (m_anim_step * TILE_SIZE); + y += sub_tile_id / 6 * 16; + + return QRectF(x, y, TILE_SIZE, TILE_SIZE); + } else if (layer() == ChipsetGraphicsView::Layer::Upper) { + if (m_tile_index > 0 && m_tile_index < 48) { + sub_tile_id = m_tile_index; + x = 288; + y = 128; + } else if (m_tile_index >= 48 && m_tile_index < 96) { + sub_tile_id = m_tile_index - 48; + x = 384; + y = 0; + } else if (m_tile_index >= 96 && m_tile_index < 144) { + sub_tile_id = m_tile_index - 96; + x = 384; + y = 128; + } else { // Invalid -> Use empty tile (first one) + x = 288; + y = 128; + } + + x += sub_tile_id % 6 * 16 + (m_anim_step * TILE_SIZE); + y += sub_tile_id / 6 * 16; + + return QRectF(x, y, TILE_SIZE, TILE_SIZE); + } else { + assert(false); + } + + return QRectF(); +} + +QRectF TileGraphicsItem::boundingRect() const { + return {0., 0., 16., 16.}; +} + +void TileGraphicsItem::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) { + painter->drawPixmap(boundingRect(), m_image, tileRect()); +} + +void TileGraphicsItem::refresh(const lcf::rpg::Chipset& chipset) { + refresh(ToQString(chipset.chipset_name)); +} + +void TileGraphicsItem::refresh(const QString& filename) { + if (m_filename != filename) { + m_filename = filename; + m_image = ImageLoader::Load(m_project.project().findFile(CHIPSET, filename, FileFinder::FileType::Image)); + } + update(); +} + +void TileGraphicsItem::clicked() { + // todo +} + +int TileGraphicsItem::tileIndex() const { + return m_tile_index; +} + +void TileGraphicsItem::setTileIndex(int index) { + m_tile_index = index; +} + +int TileGraphicsItem::animType() const { + return m_anim_type; +} + +void TileGraphicsItem::setAnimType(int anim_type) { + m_anim_type = anim_type; +} + +int TileGraphicsItem::animSpeed() const { + return m_anim_speed; +} + +void TileGraphicsItem::setAnimSpeed(int anim_speed) { + m_anim_speed = (anim_speed + 1) * 12; +} + +ChipsetGraphicsView::Layer TileGraphicsItem::layer() const { + return m_layer; +} + +void TileGraphicsItem::setLayer(ChipsetGraphicsView::Layer layer) { + m_layer = layer; +} + +ChipsetGraphicsView::EditMode TileGraphicsItem::editMode() const { + return m_editmode; +} + +void TileGraphicsItem::setEditMode(ChipsetGraphicsView::EditMode edit_mode) { + m_editmode = edit_mode; +} + +void TileGraphicsItem::advance(int phase) { + if (!phase) { + m_anim_step = frames / animSpeed(); + if (animType() == lcf::rpg::Chipset::AnimType_reciprocating) { + m_anim_step %= 3; + } else if (animType() == lcf::rpg::Chipset::AnimType_cyclic) { + m_anim_step %= 4; + if (m_anim_step == 3) { + m_anim_step = 1; + } + } else { + return; + } + ++frames; + } else { + update(); + } +} diff --git a/src/ui/viewer/tile_graphics_item.h b/src/ui/viewer/tile_graphics_item.h new file mode 100644 index 00000000..3fc4261e --- /dev/null +++ b/src/ui/viewer/tile_graphics_item.h @@ -0,0 +1,72 @@ +/* + * This file is part of EasyRPG Editor. + * + * EasyRPG Editor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EasyRPG Editor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EasyRPG Editor. If not, see . + */ + +#pragma once + +#include +#include "chipset_graphics_view.h" + +namespace lcf::rpg { + class Chipset; +} + +class ProjectData; + +class TileGraphicsItem : public QGraphicsItem { +public: + explicit TileGraphicsItem(ProjectData& project, lcf::rpg::Chipset* chipset = nullptr, const QPixmap pix = QPixmap(16,16)); + + QRectF boundingRect() const override; + QRectF tileRect() const; + void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) override; + + void refresh(const lcf::rpg::Chipset& chipset); + void refresh(const QString& filename); + + void clicked(); + + int tileIndex() const; + void setTileIndex(int index); + + int animType() const; + void setAnimType(int anim_type); + + int animSpeed() const; + void setAnimSpeed(int anim_speed); + + ChipsetGraphicsView::Layer layer() const; + void setLayer(ChipsetGraphicsView::Layer layer); + + ChipsetGraphicsView::EditMode editMode() const; + void setEditMode(ChipsetGraphicsView::EditMode edit_mode); + +protected: + void advance(int phase) override; + +private: + ProjectData& m_project; + int m_tile_index = 0; + int m_anim_type = -1; + int m_anim_speed = 12; + int m_anim_step = 0; + ChipsetGraphicsView::Layer m_layer = ChipsetGraphicsView::Layer::None; + ChipsetGraphicsView::EditMode m_editmode = ChipsetGraphicsView::EditMode::None; + QString m_filename; + lcf::rpg::Chipset* m_chipset = nullptr; + QPixmap m_image; + int frames = 0; +};