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

Add support for importing symbols from fonts as a spritesheet #149

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(PROJECT_SOURCES
source/d1formats/d1til.cpp
source/d1formats/d1trn.cpp
source/dialogs/exportdialog.cpp
source/dialogs/importdialog.cpp
source/views/view.cpp
source/views/levelcelview.cpp
source/widgets/leveltabframewidget.cpp
Expand Down
120 changes: 120 additions & 0 deletions source/dialogs/importdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include "importdialog.h"

#include <QColorDialog>
#include <QFileDialog>
#include <QFontDatabase>
#include <QImageWriter>
#include <QMessageBox>
#include <QPainter>
#include <algorithm>

#include "mainwindow.h"
#include "ui_importdialog.h"

ImportDialog::ImportDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::ImportDialog())
{
ui->setupUi(this);
}

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

void ImportDialog::initialize()
{
// - remember the last selected type
QComboBox *typeBox = this->ui->typeComboBox;
QString lastFmt = typeBox->currentText();
if (lastFmt.isEmpty()) {
lastFmt = "Font";
}
typeBox->clear();
typeBox->addItem("Font");
typeBox->setCurrentIndex(typeBox->findText(lastFmt));

setRenderColor(this->renderColor);
}

void ImportDialog::setRenderColor(QColor color)
{
QString red = QString::number(color.red());
QString green = QString::number(color.green());
QString blue = QString::number(color.blue());
QString styleSheet = QString("background: rgb(") + red + QString(",") + green + QString(",") + blue + QString(")");
ui->fontColorButton->setStyleSheet(styleSheet);
this->renderColor = color;
}

QString ImportDialog::getFileFormatExtension()
{
return "." + this->ui->typeComboBox->currentText().toLower();
}

void ImportDialog::on_inputFileBrowseButton_clicked()
{
QString selectedDirectory = QFileDialog::getOpenFileName(
this, "Select Font File", QString(), "Fonts (*.ttf *.otf)");

if (selectedDirectory.isEmpty())
return;

ui->inputFileEdit->setText(selectedDirectory);
}

void ImportDialog::on_fontSymbolsEdit_textChanged(const QString &text)
{
bool ok = false;
uint test = text.toUInt(&ok, 16);
if (!ok) {
ui->fontSymbolsRangeLabel->setText("Error");
return;
}

QString pad = text.toLower();
while (pad.size() < 2)
pad = "0" + pad;

QString start = "U+" + pad + "00";
QString end = "U+" + pad + "ff";
ui->fontSymbolsRangeLabel->setText(start + " - " + end);
}

void ImportDialog::on_fontColorButton_clicked()
{
QColor color = QColorDialog::getColor();
setRenderColor(color);
}

void ImportDialog::on_importButton_clicked()
{
if (ui->inputFileEdit->text() == "") {
QMessageBox::warning(this, "Warning", "Input file is missing, please choose an input folder.");
return;
}

try {
MainWindow *mainWindow = dynamic_cast<MainWindow *>(this->parent());
if (mainWindow == nullptr) {
QMessageBox::critical(this, "Error", "Window not found.");
return;
}

QString filePath = ui->inputFileEdit->text();
int pointSize = ui->fontSizeEdit->text().toInt();
uint symbolPrefix = ui->fontSymbolsEdit->text().toUInt() << 8;
mainWindow->openFontFile(filePath, this->renderColor, pointSize, symbolPrefix);
} catch (...) {
QMessageBox::critical(this, "Error", "Import Failed.");
return;
}

this->close();
}

void ImportDialog::on_importCancelButton_clicked()
{
this->close();
}
44 changes: 44 additions & 0 deletions source/dialogs/importdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <QDialog>
#include <QProgressDialog>

#include "d1formats/d1amp.h"
#include "d1formats/d1gfx.h"
#include "d1formats/d1min.h"
#include "d1formats/d1sol.h"
#include "d1formats/d1til.h"

namespace Ui {
class ImportDialog;
}

// subtiles per line if the output is groupped, an odd number to ensure it is not recognized as a flat tile
#define EXPORT_SUBTILES_PER_LINE 15

// frames per line if the output of a tileset-frames is groupped, an odd number to ensure it is not recognized as a flat tile or as subtiles
#define EXPORT_LVLFRAMES_PER_LINE 31

class ImportDialog : public QDialog {
Q_OBJECT

public:
explicit ImportDialog(QWidget *parent = nullptr);
~ImportDialog();

void initialize();

private slots:
void on_inputFileBrowseButton_clicked();
void on_fontSymbolsEdit_textChanged(const QString &text);
void on_fontColorButton_clicked();
void on_importButton_clicked();
void on_importCancelButton_clicked();

private:
void setRenderColor(QColor color);
QString getFileFormatExtension();

Ui::ImportDialog *ui;
QColor renderColor = QColor::fromRgb(204, 183, 117);
};
Loading
Loading