Add skin preview

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97
2025-01-10 00:54:14 +02:00
parent f4f1d5f0d7
commit cd0895b237
15 changed files with 874 additions and 124 deletions

View File

@@ -31,7 +31,7 @@ SkinList::SkinList(QObject* parent, QString path, MinecraftAccountPtr acct) : QA
m_dir.setFilter(QDir::Readable | QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
m_dir.setSorting(QDir::Name | QDir::IgnoreCase | QDir::LocaleAware);
m_watcher.reset(new QFileSystemWatcher(this));
is_watching = false;
m_isWatching = false;
connect(m_watcher.get(), &QFileSystemWatcher::directoryChanged, this, &SkinList::directoryChanged);
connect(m_watcher.get(), &QFileSystemWatcher::fileChanged, this, &SkinList::fileChanged);
directoryChanged(path);
@@ -39,12 +39,12 @@ SkinList::SkinList(QObject* parent, QString path, MinecraftAccountPtr acct) : QA
void SkinList::startWatching()
{
if (is_watching) {
if (m_isWatching) {
return;
}
update();
is_watching = m_watcher->addPath(m_dir.absolutePath());
if (is_watching) {
m_isWatching = m_watcher->addPath(m_dir.absolutePath());
if (m_isWatching) {
qDebug() << "Started watching " << m_dir.absolutePath();
} else {
qDebug() << "Failed to start watching " << m_dir.absolutePath();
@@ -54,11 +54,11 @@ void SkinList::startWatching()
void SkinList::stopWatching()
{
save();
if (!is_watching) {
if (!m_isWatching) {
return;
}
is_watching = !m_watcher->removePath(m_dir.absolutePath());
if (!is_watching) {
m_isWatching = !m_watcher->removePath(m_dir.absolutePath());
if (!m_isWatching) {
qDebug() << "Stopped watching " << m_dir.absolutePath();
} else {
qDebug() << "Failed to stop watching " << m_dir.absolutePath();
@@ -142,7 +142,7 @@ bool SkinList::update()
std::sort(newSkins.begin(), newSkins.end(),
[](const SkinModel& a, const SkinModel& b) { return a.getPath().localeAwareCompare(b.getPath()) < 0; });
beginResetModel();
m_skin_list.swap(newSkins);
m_skinList.swap(newSkins);
endResetModel();
if (needsSave)
save();
@@ -158,7 +158,7 @@ void SkinList::directoryChanged(const QString& path)
if (m_dir.absolutePath() != new_dir.absolutePath()) {
m_dir.setPath(path);
m_dir.refresh();
if (is_watching)
if (m_isWatching)
stopWatching();
startWatching();
}
@@ -172,9 +172,9 @@ void SkinList::fileChanged(const QString& path)
if (!checkfile.exists())
return;
for (int i = 0; i < m_skin_list.count(); i++) {
if (m_skin_list[i].getPath() == checkfile.absoluteFilePath()) {
m_skin_list[i].refresh();
for (int i = 0; i < m_skinList.count(); i++) {
if (m_skinList[i].getPath() == checkfile.absoluteFilePath()) {
m_skinList[i].refresh();
dataChanged(index(i), index(i));
break;
}
@@ -235,9 +235,9 @@ QVariant SkinList::data(const QModelIndex& index, int role) const
int row = index.row();
if (row < 0 || row >= m_skin_list.size())
if (row < 0 || row >= m_skinList.size())
return QVariant();
auto skin = m_skin_list[row];
auto skin = m_skinList[row];
switch (role) {
case Qt::DecorationRole:
return skin.getTexture();
@@ -254,7 +254,7 @@ QVariant SkinList::data(const QModelIndex& index, int role) const
int SkinList::rowCount(const QModelIndex& parent) const
{
return parent.isValid() ? 0 : m_skin_list.size();
return parent.isValid() ? 0 : m_skinList.size();
}
void SkinList::installSkins(const QStringList& iconFiles)
@@ -284,8 +284,8 @@ QString SkinList::installSkin(const QString& file, const QString& name)
int SkinList::getSkinIndex(const QString& key) const
{
for (int i = 0; i < m_skin_list.count(); i++) {
if (m_skin_list[i].name() == key) {
for (int i = 0; i < m_skinList.count(); i++) {
if (m_skinList[i].name() == key) {
return i;
}
}
@@ -297,7 +297,7 @@ const SkinModel* SkinList::skin(const QString& key) const
int idx = getSkinIndex(key);
if (idx == -1)
return nullptr;
return &m_skin_list[idx];
return &m_skinList[idx];
}
SkinModel* SkinList::skin(const QString& key)
@@ -305,22 +305,22 @@ SkinModel* SkinList::skin(const QString& key)
int idx = getSkinIndex(key);
if (idx == -1)
return nullptr;
return &m_skin_list[idx];
return &m_skinList[idx];
}
bool SkinList::deleteSkin(const QString& key, const bool trash)
bool SkinList::deleteSkin(const QString& key, bool trash)
{
int idx = getSkinIndex(key);
if (idx != -1) {
auto s = m_skin_list[idx];
auto s = m_skinList[idx];
if (trash) {
if (FS::trash(s.getPath(), nullptr)) {
m_skin_list.remove(idx);
m_skinList.remove(idx);
save();
return true;
}
} else if (QFile::remove(s.getPath())) {
m_skin_list.remove(idx);
m_skinList.remove(idx);
save();
return true;
}
@@ -332,7 +332,7 @@ void SkinList::save()
{
QJsonObject doc;
QJsonArray arr;
for (auto s : m_skin_list) {
for (auto s : m_skinList) {
arr << s.toJSON();
}
doc["skins"] = arr;
@@ -346,8 +346,8 @@ void SkinList::save()
int SkinList::getSelectedAccountSkin()
{
const auto& skin = m_acct->accountData()->minecraftProfile.skin;
for (int i = 0; i < m_skin_list.count(); i++) {
if (m_skin_list[i].getURL() == skin.url) {
for (int i = 0; i < m_skinList.count(); i++) {
if (m_skinList[i].getURL() == skin.url) {
return i;
}
}
@@ -361,9 +361,9 @@ bool SkinList::setData(const QModelIndex& idx, const QVariant& value, int role)
}
int row = idx.row();
if (row < 0 || row >= m_skin_list.size())
if (row < 0 || row >= m_skinList.size())
return false;
auto& skin = m_skin_list[row];
auto& skin = m_skinList[row];
auto newName = value.toString();
if (skin.name() != newName) {
skin.rename(newName);
@@ -375,18 +375,18 @@ bool SkinList::setData(const QModelIndex& idx, const QVariant& value, int role)
void SkinList::updateSkin(SkinModel* s)
{
auto done = false;
for (auto i = 0; i < m_skin_list.size(); i++) {
if (m_skin_list[i].getPath() == s->getPath()) {
m_skin_list[i].setCapeId(s->getCapeId());
m_skin_list[i].setModel(s->getModel());
m_skin_list[i].setURL(s->getURL());
for (auto i = 0; i < m_skinList.size(); i++) {
if (m_skinList[i].getPath() == s->getPath()) {
m_skinList[i].setCapeId(s->getCapeId());
m_skinList[i].setModel(s->getModel());
m_skinList[i].setURL(s->getURL());
done = true;
break;
}
}
if (!done) {
beginInsertRows(QModelIndex(), m_skin_list.count(), m_skin_list.count() + 1);
m_skin_list.append(*s);
beginInsertRows(QModelIndex(), m_skinList.count(), m_skinList.count() + 1);
m_skinList.append(*s);
endInsertRows();
}
save();

View File

@@ -43,7 +43,7 @@ class SkinList : public QAbstractListModel {
virtual bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) override;
virtual Qt::ItemFlags flags(const QModelIndex& index) const override;
bool deleteSkin(const QString& key, const bool trash);
bool deleteSkin(const QString& key, bool trash);
void installSkins(const QStringList& iconFiles);
QString installSkin(const QString& file, const QString& name = {});
@@ -73,8 +73,8 @@ class SkinList : public QAbstractListModel {
private:
shared_qobject_ptr<QFileSystemWatcher> m_watcher;
bool is_watching;
QVector<SkinModel> m_skin_list;
bool m_isWatching;
QVector<SkinModel> m_skinList;
QDir m_dir;
MinecraftAccountPtr m_acct;
};

View File

@@ -18,17 +18,37 @@
#include "SkinModel.h"
#include <QFileInfo>
#include <QImage>
#include <QPainter>
#include <QTransform>
#include "FileSystem.h"
#include "Json.h"
SkinModel::SkinModel(QString path) : m_path(path), m_texture(path), m_model(Model::CLASSIC) {}
QImage improveSkin(const QImage& skin)
{
if (skin.size() == QSize(64, 32)) { // old format
QImage newSkin = QImage(QSize(64, 64), skin.format());
newSkin.fill(Qt::transparent);
QPainter p(&newSkin);
p.drawImage(QPoint(0, 0), skin.copy(QRect(0, 0, 64, 32))); // copy head
auto leg = skin.copy(QRect(0, 16, 16, 16));
p.drawImage(QPoint(16, 48), leg); // copy leg
auto arm = skin.copy(QRect(40, 16, 16, 16));
p.drawImage(QPoint(32, 48), arm); // copy arm
return newSkin;
}
return skin;
}
QImage getSkin(const QString path)
{
return improveSkin(QImage(path));
}
SkinModel::SkinModel(QString path) : m_path(path), m_texture(getSkin(path)), m_model(Model::CLASSIC) {}
SkinModel::SkinModel(QDir skinDir, QJsonObject obj)
: m_cape_id(Json::ensureString(obj, "capeId")), m_model(Model::CLASSIC), m_url(Json::ensureString(obj, "url"))
: m_capeId(Json::ensureString(obj, "capeId")), m_model(Model::CLASSIC), m_url(Json::ensureString(obj, "url"))
{
auto name = Json::ensureString(obj, "name");
@@ -36,7 +56,7 @@ SkinModel::SkinModel(QDir skinDir, QJsonObject obj)
m_model = Model::SLIM;
}
m_path = skinDir.absoluteFilePath(name) + ".png";
m_texture = QPixmap(m_path);
m_texture = QImage(getSkin(m_path));
}
QString SkinModel::name() const
@@ -55,7 +75,7 @@ QJsonObject SkinModel::toJSON() const
{
QJsonObject obj;
obj["name"] = name();
obj["capeId"] = m_cape_id;
obj["capeId"] = m_capeId;
obj["url"] = m_url;
obj["model"] = getModelString();
return obj;
@@ -76,3 +96,7 @@ bool SkinModel::isValid() const
{
return !m_texture.isNull() && (m_texture.size().height() == 32 || m_texture.size().height() == 64) && m_texture.size().width() == 64;
}
void SkinModel::refresh()
{
m_texture = getSkin(m_path);
}

View File

@@ -19,8 +19,8 @@
#pragma once
#include <QDir>
#include <QImage>
#include <QJsonObject>
#include <QPixmap>
class SkinModel {
public:
@@ -35,23 +35,23 @@ class SkinModel {
QString getModelString() const;
bool isValid() const;
QString getPath() const { return m_path; }
QPixmap getTexture() const { return m_texture; }
QString getCapeId() const { return m_cape_id; }
QImage getTexture() const { return m_texture; }
QString getCapeId() const { return m_capeId; }
Model getModel() const { return m_model; }
QString getURL() const { return m_url; }
bool rename(QString newName);
void setCapeId(QString capeID) { m_cape_id = capeID; }
void setCapeId(QString capeID) { m_capeId = capeID; }
void setModel(Model model) { m_model = model; }
void setURL(QString url) { m_url = url; }
void refresh() { m_texture = QPixmap(m_path); }
void refresh();
QJsonObject toJSON() const;
private:
QString m_path;
QPixmap m_texture;
QString m_cape_id;
QImage m_texture;
QString m_capeId;
Model m_model;
QString m_url;
};