109
launcher/ui/themes/CatPack.cpp
Normal file
109
launcher/ui/themes/CatPack.cpp
Normal file
@ -0,0 +1,109 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
/*
|
||||
* Prism Launcher - Minecraft Launcher
|
||||
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
|
||||
*
|
||||
* This program 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, version 3.
|
||||
*
|
||||
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and
|
||||
* permission notice:
|
||||
*
|
||||
* Copyright 2013-2021 MultiMC Contributors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "ui/themes/CatPack.h"
|
||||
#include <qdatetime.h>
|
||||
#include <qjsonarray.h>
|
||||
#include <qjsonobject.h>
|
||||
#include <qobject.h>
|
||||
#include <QDateTime>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include "FileSystem.h"
|
||||
#include "Json.h"
|
||||
#include "ui/themes/ThemeManager.h"
|
||||
|
||||
QString BasicCatPack::path()
|
||||
{
|
||||
const QDateTime now = QDateTime::currentDateTime();
|
||||
const QDateTime birthday(QDate(now.date().year(), 11, 30), QTime(0, 0));
|
||||
const QDateTime xmas(QDate(now.date().year(), 12, 25), QTime(0, 0));
|
||||
const QDateTime halloween(QDate(now.date().year(), 10, 31), QTime(0, 0));
|
||||
|
||||
QString cat = QString(":/backgrounds/%1").arg(m_id);
|
||||
if (std::abs(now.daysTo(xmas)) <= 4) {
|
||||
cat += "-xmas";
|
||||
} else if (std::abs(now.daysTo(halloween)) <= 4) {
|
||||
cat += "-spooky";
|
||||
} else if (std::abs(now.daysTo(birthday)) <= 12) {
|
||||
cat += "-bday";
|
||||
}
|
||||
return cat;
|
||||
}
|
||||
|
||||
JsonCatPack::JsonCatPack(QFileInfo& manifestInfo) : BasicCatPack(manifestInfo.dir().dirName())
|
||||
{
|
||||
QString path = FS::PathCombine("catpacks", m_id);
|
||||
|
||||
if (!FS::ensureFolderPathExists(path)) {
|
||||
themeWarningLog() << "couldn't create folder for catpack!";
|
||||
return;
|
||||
}
|
||||
|
||||
if (manifestInfo.exists() && manifestInfo.isFile()) {
|
||||
try {
|
||||
auto doc = Json::requireDocument(manifestInfo.absoluteFilePath(), "CatPack JSON file");
|
||||
const auto root = doc.object();
|
||||
m_name = Json::requireString(root, "name", "Catpack name");
|
||||
m_id = Json::requireString(root, "id", "Catpack ID");
|
||||
m_defaultPath = FS::PathCombine(path, Json::requireString(root, "default", "Deafult Cat"));
|
||||
auto variants = Json::ensureArray(root, "variants", QJsonArray(), "Catpack Variants");
|
||||
for (auto v : variants) {
|
||||
auto variant = Json::ensureObject(v, QJsonObject(), "Cat variant");
|
||||
m_variants << Variant{ FS::PathCombine(path, Json::requireString(variant, "path", "Variant path")),
|
||||
date(Json::requireString(variant, "startTime", "Variant startTime")),
|
||||
date(Json::requireString(variant, "endTime", "Variant endTime")) };
|
||||
}
|
||||
|
||||
} catch (const Exception& e) {
|
||||
themeWarningLog() << "Couldn't load catpack json: " << e.cause();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
themeDebugLog() << "No catpack json present.";
|
||||
}
|
||||
}
|
||||
|
||||
QString JsonCatPack::path()
|
||||
{
|
||||
const QDateTime now = QDateTime::currentDateTime();
|
||||
for (auto var : m_variants) {
|
||||
QDateTime startDate(QDate(now.date().year(), var.startTime.mounth, var.startTime.day), QTime(0, 0));
|
||||
QDateTime endDate(QDate(now.date().year(), var.endTime.mounth, var.endTime.day), QTime(0, 0));
|
||||
if (startDate.daysTo(now) > 0 && now.daysTo(endDate) > 0)
|
||||
return var.path;
|
||||
}
|
||||
return m_defaultPath;
|
||||
}
|
98
launcher/ui/themes/CatPack.h
Normal file
98
launcher/ui/themes/CatPack.h
Normal file
@ -0,0 +1,98 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
/*
|
||||
* Prism Launcher - Minecraft Launcher
|
||||
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
|
||||
*
|
||||
* This program 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, version 3.
|
||||
*
|
||||
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and
|
||||
* permission notice:
|
||||
*
|
||||
* Copyright 2013-2021 MultiMC Contributors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QFileInfo>
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
|
||||
class CatPack {
|
||||
public:
|
||||
virtual ~CatPack() {}
|
||||
virtual QString id() = 0;
|
||||
virtual QString name() = 0;
|
||||
virtual QString path() = 0;
|
||||
};
|
||||
|
||||
class BasicCatPack : public CatPack {
|
||||
public:
|
||||
BasicCatPack(QString id, QString name) : m_id(id), m_name(name) {}
|
||||
BasicCatPack(QString id) : BasicCatPack(id, id) {}
|
||||
virtual QString id() { return m_id; };
|
||||
virtual QString name() { return m_name; };
|
||||
virtual QString path();
|
||||
|
||||
protected:
|
||||
QString m_id;
|
||||
QString m_name;
|
||||
};
|
||||
|
||||
class FileCatPack : public BasicCatPack {
|
||||
public:
|
||||
FileCatPack(QString id, QFileInfo& fileInfo) : BasicCatPack(id), m_path(fileInfo.absoluteFilePath()) {}
|
||||
FileCatPack(QFileInfo& fileInfo) : FileCatPack(fileInfo.baseName(), fileInfo) {}
|
||||
virtual QString path() { return m_path; }
|
||||
|
||||
private:
|
||||
QString m_path;
|
||||
};
|
||||
|
||||
class JsonCatPack : public BasicCatPack {
|
||||
public:
|
||||
struct date {
|
||||
date(QString d)
|
||||
{
|
||||
auto sp = d.split("-");
|
||||
day = sp[0].toInt();
|
||||
if (sp.length() >= 2)
|
||||
mounth = sp[1].length();
|
||||
}
|
||||
int mounth;
|
||||
int day;
|
||||
};
|
||||
struct Variant {
|
||||
QString path;
|
||||
date startTime;
|
||||
date endTime;
|
||||
};
|
||||
JsonCatPack(QFileInfo& manifestInfo);
|
||||
virtual QString path();
|
||||
|
||||
private:
|
||||
QString m_defaultPath;
|
||||
QList<Variant> m_variants;
|
||||
};
|
@ -22,6 +22,7 @@
|
||||
#include <QDirIterator>
|
||||
#include <QIcon>
|
||||
#include "ui/themes/BrightTheme.h"
|
||||
#include "ui/themes/CatPack.h"
|
||||
#include "ui/themes/CustomTheme.h"
|
||||
#include "ui/themes/DarkTheme.h"
|
||||
#include "ui/themes/SystemTheme.h"
|
||||
@ -32,6 +33,7 @@ ThemeManager::ThemeManager(MainWindow* mainWindow)
|
||||
{
|
||||
m_mainWindow = mainWindow;
|
||||
initializeThemes();
|
||||
initializeCatPacks();
|
||||
}
|
||||
|
||||
/// @brief Adds the Theme to the list of themes
|
||||
@ -111,6 +113,16 @@ QList<ITheme*> ThemeManager::getValidApplicationThemes()
|
||||
return ret;
|
||||
}
|
||||
|
||||
QList<CatPack*> ThemeManager::getValidCatPacks()
|
||||
{
|
||||
QList<CatPack*> ret;
|
||||
ret.reserve(m_catPacks.size());
|
||||
for (auto&& [id, theme] : m_catPacks) {
|
||||
ret.append(theme.get());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ThemeManager::setIconTheme(const QString& name)
|
||||
{
|
||||
QIcon::setThemeName(name);
|
||||
@ -137,19 +149,63 @@ void ThemeManager::setApplicationTheme(const QString& name, bool initial)
|
||||
}
|
||||
}
|
||||
|
||||
QString ThemeManager::getCatImage(QString catName)
|
||||
QString ThemeManager::getCatPack(QString catName)
|
||||
{
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
QDateTime birthday(QDate(now.date().year(), 11, 30), QTime(0, 0));
|
||||
QDateTime xmas(QDate(now.date().year(), 12, 25), QTime(0, 0));
|
||||
QDateTime halloween(QDate(now.date().year(), 10, 31), QTime(0, 0));
|
||||
QString cat = !catName.isEmpty() ? catName : APPLICATION->settings()->get("BackgroundCat").toString();
|
||||
if (std::abs(now.daysTo(xmas)) <= 4) {
|
||||
cat += "-xmas";
|
||||
} else if (std::abs(now.daysTo(halloween)) <= 4) {
|
||||
cat += "-spooky";
|
||||
} else if (std::abs(now.daysTo(birthday)) <= 12) {
|
||||
cat += "-bday";
|
||||
auto catIter = m_catPacks.find(!catName.isEmpty() ? catName : APPLICATION->settings()->get("BackgroundCat").toString());
|
||||
if (catIter != m_catPacks.end()) {
|
||||
auto& catPack = catIter->second;
|
||||
themeDebugLog() << "applying catpack" << catPack->id();
|
||||
return catPack->path();
|
||||
} else {
|
||||
themeWarningLog() << "Tried to get invalid catPack:" << catName;
|
||||
}
|
||||
|
||||
return m_catPacks.begin()->second->path();
|
||||
}
|
||||
|
||||
QString ThemeManager::addCatPack(std::unique_ptr<CatPack> catPack)
|
||||
{
|
||||
QString id = catPack->id();
|
||||
m_catPacks.emplace(id, std::move(catPack));
|
||||
return id;
|
||||
}
|
||||
|
||||
void ThemeManager::initializeCatPacks()
|
||||
{
|
||||
QList<std::pair<QString, QString>> defaultCats{ { "kitteh", QObject::tr("Background Cat (from MultiMC)") },
|
||||
{ "rory", QObject::tr("Rory ID 11 (drawn by Ashtaka)") },
|
||||
{ "rory-flat", QObject::tr("Rory ID 11 (flat edition, drawn by Ashtaka)") },
|
||||
{ "teawie", QObject::tr("Teawie (drawn by SympathyTea)") } };
|
||||
for (auto [id, name] : defaultCats) {
|
||||
addCatPack(std::unique_ptr<CatPack>(new BasicCatPack(id, name)));
|
||||
}
|
||||
QDir catpacksDir("./catpacks/");
|
||||
QString catpacksFolder = catpacksDir.absoluteFilePath("");
|
||||
themeDebugLog() << "CatPacks Folder Path: " << catpacksFolder;
|
||||
|
||||
auto loadFiles = [this](QDir dir) {
|
||||
// Load image files directly
|
||||
QDirIterator ImageFileIterator(dir.absoluteFilePath(""), { "*.png", "*.gif", "*.jpg", "*.apng", "*.jxl", "*.avif" }, QDir::Files);
|
||||
while (ImageFileIterator.hasNext()) {
|
||||
QFile customCatFile(ImageFileIterator.next());
|
||||
QFileInfo customCatFileInfo(customCatFile);
|
||||
themeDebugLog() << "Loading QSS Theme from:" << customCatFileInfo.absoluteFilePath();
|
||||
addCatPack(std::unique_ptr<CatPack>(new FileCatPack(customCatFileInfo)));
|
||||
}
|
||||
};
|
||||
|
||||
loadFiles(catpacksDir);
|
||||
|
||||
QDirIterator directoryIterator(catpacksFolder, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
|
||||
while (directoryIterator.hasNext()) {
|
||||
QDir dir(directoryIterator.next());
|
||||
QFileInfo manifest(dir.absoluteFilePath("catpack.json"));
|
||||
if (manifest.exists()) {
|
||||
// Load background manifest
|
||||
themeDebugLog() << "Loading background manifest from:" << manifest.absoluteFilePath();
|
||||
addCatPack(std::unique_ptr<CatPack>(new JsonCatPack(manifest)));
|
||||
} else {
|
||||
loadFiles(dir);
|
||||
}
|
||||
}
|
||||
return cat;
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <QString>
|
||||
|
||||
#include "ui/MainWindow.h"
|
||||
#include "ui/themes/CatPack.h"
|
||||
#include "ui/themes/ITheme.h"
|
||||
|
||||
inline auto themeDebugLog()
|
||||
@ -40,18 +41,20 @@ class ThemeManager {
|
||||
void applyCurrentlySelectedTheme(bool initial = false);
|
||||
void setApplicationTheme(const QString& name, bool initial = false);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the cat based on selected cat and with events (Birthday, XMas, etc.)
|
||||
/// </summary>
|
||||
/// <param name="catName">Optional, if you need a specific cat.</param>
|
||||
/// <returns></returns>
|
||||
static QString getCatImage(QString catName = "");
|
||||
/// @brief Returns the background based on selected and with events (Birthday, XMas, etc.)
|
||||
/// @param catName Optional, if you need a specific background.
|
||||
/// @return
|
||||
QString getCatPack(QString catName = "");
|
||||
QList<CatPack*> getValidCatPacks();
|
||||
|
||||
private:
|
||||
std::map<QString, std::unique_ptr<ITheme>> m_themes;
|
||||
std::map<QString, std::unique_ptr<CatPack>> m_catPacks;
|
||||
MainWindow* m_mainWindow;
|
||||
|
||||
void initializeThemes();
|
||||
void initializeCatPacks();
|
||||
QString addTheme(std::unique_ptr<ITheme> theme);
|
||||
ITheme* getTheme(QString themeId);
|
||||
QString addCatPack(std::unique_ptr<CatPack> catPack);
|
||||
};
|
||||
|
Reference in New Issue
Block a user