Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add Chipset Database Widget #201

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Chipset Widget: Start implementation, including a Tilemap renderer
Ghabry committed Nov 29, 2021
commit 850a99eaf54ec2fe94f6a929a367be5c5a780315
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})

63 changes: 62 additions & 1 deletion src/ui/database/chipset_widget.cpp
Original file line number Diff line number Diff line change
@@ -18,20 +18,81 @@
#include "chipset_widget.h"
#include "ui_chipset_widget.h"

#include "common/lcf_widget_binding.h"
#include "ui/picker/picker_chipset_widget.h"
#include "ui/picker/picker_dialog.h"

#include <QButtonGroup>

ChipsetWidget::ChipsetWidget(ProjectData& project, QWidget *parent) :
QWidget(parent),
ui(new Ui::ChipsetWidget),
m_project(project)
{
ui->setupUi(this);

QListView& list = *ui->listTerrain;
list.setModel(new RpgModel<lcf::rpg::Terrain>(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<int32_t>(this, m_buttonGroupSequence);

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<int32_t>(this, m_buttonGroupSpeed);

ui->graphicsChipsetLower->setProjectData(project);
ui->graphicsChipsetLower->setLayer(ChipsetGraphicsView::Layer::Lower);
ui->graphicsChipsetUpper->setProjectData(project);
ui->graphicsChipsetUpper->setLayer(ChipsetGraphicsView::Layer::Upper);

QObject::connect(ui->pushTileset, &QPushButton::clicked, this, &ChipsetWidget::chipsetClicked);
}

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);

ui->graphicsChipsetLower->setChipset(*chipset);
ui->graphicsChipsetUpper->setChipset(*chipset);

ui->graphicsChipsetLower->refresh();
ui->graphicsChipsetUpper->refresh();

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));
//m_charsetItem->refresh(*m_current);
});
dialog.setDirectoryAndFile(CHIPSET, ToQString(m_current->chipset_name));
dialog.exec();
}
10 changes: 10 additions & 0 deletions src/ui/database/chipset_widget.h
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@ namespace Ui {
class ChipsetWidget;
}

class QButtonGroup;

class ChipsetWidget : public QWidget
{
Q_OBJECT
@@ -39,7 +41,15 @@ class ChipsetWidget : public QWidget
void setData(lcf::rpg::Chipset* chipset);

private:
void chipsetClicked();

lcf::rpg::Chipset dummy;
lcf::rpg::Chipset *m_current = nullptr;

Ui::ChipsetWidget *ui;
ProjectData& m_project;

QButtonGroup* m_buttonGroupSequence = nullptr;
QButtonGroup* m_buttonGroupSpeed = nullptr;
};

928 changes: 214 additions & 714 deletions src/ui/database/chipset_widget.ui

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/ui/picker/picker_chipset_widget.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#include "picker_chipset_widget.h"
#include "ui/viewer/rpg_graphics_view.h"
#include <QGraphicsScene>

void PickerChipsetWidget::imageChanged(QPixmap image) {
if (!m_pixmap) {
m_pixmap = new QGraphicsPixmapItem(image);
}

m_pixmap->setPixmap(image);
m_view->setItem(m_pixmap);
}
35 changes: 35 additions & 0 deletions src/ui/picker/picker_chipset_widget.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <QWidget>
#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;
};
119 changes: 119 additions & 0 deletions src/ui/viewer/chipset_graphics_view.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

#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 <QMouseEvent>

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) {
if (m_chipset == &chipset) {
return;
}

m_chipset = &chipset;
m_chipset_pix = ImageLoader::Load(m_project->project().findFile(CHIPSET, ToQString(chipset.chipset_name), FileFinder::FileType::Image));
}

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::drawGrid() const {
return m_draw_grid;
}

void ChipsetGraphicsView::setDrawGrid(bool draw_grid) {
m_draw_grid = draw_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;

if (layer() == Layer::Lower) {
scene()->clear();
for (int terrain_id = 0; terrain_id < 162; 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();
for (int terrain_id = 0; terrain_id < 144; 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);
}
}
}
89 changes: 89 additions & 0 deletions src/ui/viewer/chipset_graphics_view.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QGraphicsView>
#include <QPainter>
#include <QTimer>
#include <memory>
#include <type_traits>

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 drawGrid() const;
void setDrawGrid(bool draw_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<QTimer> m_timer;
QPixmap m_chipset_pix;

bool m_draw_grid = false;
int m_tiles_per_col = 6;
EditMode m_editmode = EditMode::Terrain;
Layer m_layer = Layer::None;
};
2 changes: 2 additions & 0 deletions src/ui/viewer/rpg_graphics_view.h
Original file line number Diff line number Diff line change
@@ -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<CharSetGraphicsItem>;
using FaceSetGraphicsView = RpgGraphicsView<FaceSetGraphicsItem>;
using TileGraphicsView = RpgGraphicsView<TileGraphicsItem>;
using PixmapGraphicsView = RpgGraphicsView<QGraphicsPixmapItem>;
143 changes: 143 additions & 0 deletions src/ui/viewer/tile_graphics_item.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

#include "tile_graphics_item.h"
#include "core.h"
#include "common/image_loader.h"
#include "common/dbstring.h"
#include "model/project_data.h"

#include <lcf/rpg/actor.h>

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;
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;
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;
}

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;
}
59 changes: 59 additions & 0 deletions src/ui/viewer/tile_graphics_item.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <QGraphicsPixmapItem>
#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, 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);

ChipsetGraphicsView::Layer layer() const;
void setLayer(ChipsetGraphicsView::Layer layer);

ChipsetGraphicsView::EditMode editMode() const;
void setEditMode(ChipsetGraphicsView::EditMode edit_mode);

private:
ProjectData& m_project;
int m_tile_index = 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;
};