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

Allow to change atlas per theme #15464

Merged
merged 1 commit into from
Apr 23, 2022
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
11 changes: 7 additions & 4 deletions Common/UI/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "Common/UI/Context.h"
#include "Common/Render/DrawBuffer.h"
#include "Common/Render/Text/draw_text.h"

#include "Common/Log.h"
#include "UI/TextureUtil.h"

Expand All @@ -36,10 +35,14 @@ void UIContext::Init(Draw::DrawContext *thin3d, Draw::Pipeline *uipipe, Draw::Pi
textDrawer_ = TextDrawer::Create(thin3d); // May return nullptr if no implementation is available for this platform.
}

void UIContext::setUIAtlas(const std::string &name) {
UIAtlas_ = name;
}

void UIContext::BeginFrame() {
if (!uitexture_) {
uitexture_ = CreateTextureFromFile(draw_, "ui_atlas.zim", ImageFileType::ZIM, false);
_dbg_assert_msg_(uitexture_, "Failed to load ui_atlas.zim.\n\nPlace it in the directory \"assets\" under your PPSSPP directory.");
if (!uitexture_ || UIAtlas_ != lastUIAtlas_) {
uitexture_ = CreateTextureFromFile(draw_, UIAtlas_.c_str(), ImageFileType::ZIM, false);
lastUIAtlas_ = UIAtlas_;
if (!fontTexture_) {
#if PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(ANDROID)
// Don't bother with loading font_atlas.zim
Expand Down
6 changes: 6 additions & 0 deletions Common/UI/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>
#include <vector>
#include <cstdint>
#include <string>

#include "Common/Math/geom2d.h"
#include "Common/Math/lin/vec3.h"
Expand Down Expand Up @@ -100,6 +101,8 @@ class UIContext {
void PopTransform();
Bounds TransformBounds(const Bounds &bounds);

void setUIAtlas(const std::string &name);

private:
Draw::DrawContext *draw_ = nullptr;
Bounds bounds_;
Expand All @@ -120,4 +123,7 @@ class UIContext {

std::vector<Bounds> scissorStack_;
std::vector<UITransform> transformStack_;

std::string lastUIAtlas_;
std::string UIAtlas_ = "ui_atlas.zim";
};
4 changes: 2 additions & 2 deletions UI/GameSettingsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,9 @@ void GameSettingsScreen::CreateViews() {
backgroundChoice_->OnClick.Handle(this, &GameSettingsScreen::OnChangeBackground);
}

PopupMultiChoiceDynamic *theme = systemSettings->Add(new PopupMultiChoiceDynamic(&g_Config.sThemeName, sy->T("Color Theme"), GetThemeInfoNames(), th->GetName(), screenManager()));
PopupMultiChoiceDynamic *theme = systemSettings->Add(new PopupMultiChoiceDynamic(&g_Config.sThemeName, sy->T("Theme"), GetThemeInfoNames(), th->GetName(), screenManager()));
theme->OnChoice.Add([=](EventParams &e) {
UpdateTheme();
UpdateTheme(screenManager()->getUIContext());

return UI::EVENT_CONTINUE;
});
Expand Down
39 changes: 5 additions & 34 deletions UI/NativeApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@
#include "android/jni/app-android.h"
#endif

// The new UI framework, for initialization

static Atlas g_ui_atlas;
static Atlas g_font_atlas;

#if PPSSPP_ARCH(ARM) && defined(__ANDROID__)
#include "../../android/jni/ArmEmitterTest.h"
#elif PPSSPP_ARCH(ARM64) && defined(__ANDROID__)
Expand Down Expand Up @@ -876,22 +871,6 @@ void NativeInit(int argc, const char *argv[], const char *savegame_dir, const ch
void RenderOverlays(UIContext *dc, void *userdata);
bool CreateGlobalPipelines();

static void LoadAtlasMetadata(Atlas &metadata, const char *filename, bool required) {
size_t atlas_data_size = 0;
if (!metadata.IsMetadataLoaded()) {
const uint8_t *atlas_data = VFSReadFile(filename, &atlas_data_size);
bool load_success = atlas_data != nullptr && metadata.Load(atlas_data, atlas_data_size);
if (!load_success) {
if (required)
ERROR_LOG(G3D, "Failed to load %s - graphics will be broken", filename);
else
WARN_LOG(G3D, "Failed to load %s", filename);
// Stumble along with broken visuals instead of dying...
}
delete[] atlas_data;
}
}

bool NativeInitGraphics(GraphicsContext *graphicsContext) {
INFO_LOG(SYSTEM, "NativeInitGraphics");

Expand All @@ -906,22 +885,14 @@ bool NativeInitGraphics(GraphicsContext *graphicsContext) {
return false;
}

// Load any missing atlas metadata (the images are loaded from UIContext).
LoadAtlasMetadata(g_ui_atlas, "ui_atlas.meta", true);
#if !(PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(ANDROID))
LoadAtlasMetadata(g_font_atlas, "font_atlas.meta", g_ui_atlas.num_fonts == 0);
#else
LoadAtlasMetadata(g_font_atlas, "asciifont_atlas.meta", g_ui_atlas.num_fonts == 0);
#endif

ui_draw2d.SetAtlas(&g_ui_atlas);
ui_draw2d.SetFontAtlas(&g_font_atlas);
ui_draw2d_front.SetAtlas(&g_ui_atlas);
ui_draw2d_front.SetFontAtlas(&g_font_atlas);
ui_draw2d.SetAtlas(GetUIAtlas());
ui_draw2d.SetFontAtlas(GetFontAtlas());
ui_draw2d_front.SetAtlas(GetUIAtlas());
ui_draw2d_front.SetFontAtlas(GetFontAtlas());

UpdateTheme();
uiContext = new UIContext();
uiContext->theme = GetTheme();
UpdateTheme(uiContext);

ui_draw2d.Init(g_draw, texColorPipeline);
ui_draw2d_front.Init(g_draw, texColorPipeline);
Expand Down
58 changes: 57 additions & 1 deletion UI/Theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Common/File/VFS/VFS.h"
#include "Common/Data/Format/IniFile.h"
#include "Common/File/DirListing.h"
#include "Common/LogManager.h"

#include "Core/Config.h"

Expand Down Expand Up @@ -52,6 +53,8 @@ struct ThemeInfo {
uint32_t uPopupStyleBg = 0xFF303030;
uint32_t uBackgroundColor = 0xFF754D24;

std::string UIAtlas;

bool operator == (const std::string &other) {
return name == other;
}
Expand All @@ -63,6 +66,9 @@ struct ThemeInfo {
static UI::Theme ui_theme;
static std::vector<ThemeInfo> themeInfos;

static Atlas ui_atlas;
static Atlas font_atlas;

static void LoadThemeInfo(const std::vector<Path> &directories) {
themeInfos.clear();
ThemeInfo def{};
Expand Down Expand Up @@ -128,6 +134,22 @@ static void LoadThemeInfo(const std::vector<Path> &directories) {
section.Get("PopupStyleBg", &info.uPopupStyleBg, info.uPopupStyleBg);
section.Get("BackgroundColor", &info.uBackgroundColor, info.uBackgroundColor);

std::string tmpPath;
section.Get("UIAtlas", &tmpPath, "");
if (tmpPath != "") {
tmpPath = (path / tmpPath).ToString();

File::FileInfo tmpInfo;
if (VFSGetFileInfo((tmpPath+".meta").c_str(), &tmpInfo) && VFSGetFileInfo((tmpPath+".zim").c_str(), &tmpInfo)) {
info.UIAtlas = tmpPath;
} else {
// Files not found, fallback to default
info.UIAtlas = "ui_atlas";
}
} else {
info.UIAtlas = "ui_atlas";
}

appendTheme(info);
}
}
Expand All @@ -141,7 +163,23 @@ static UI::Style MakeStyle(uint32_t fg, uint32_t bg) {
return s;
}

void UpdateTheme() {
static void LoadAtlasMetadata(Atlas &metadata, const char *filename, bool required) {
size_t atlas_data_size = 0;
if (!metadata.IsMetadataLoaded()) {
const uint8_t *atlas_data = VFSReadFile(filename, &atlas_data_size);
bool load_success = atlas_data != nullptr && metadata.Load(atlas_data, atlas_data_size);
if (!load_success) {
if (required)
ERROR_LOG(G3D, "Failed to load %s - graphics will be broken", filename);
else
WARN_LOG(G3D, "Failed to load %s", filename);
// Stumble along with broken visuals instead of dying...
}
delete[] atlas_data;
}
}

void UpdateTheme(UIContext *ctx) {
// First run, get the default in at least
if (themeInfos.empty()) {
ReloadAllThemeInfo();
Expand Down Expand Up @@ -188,12 +226,30 @@ void UpdateTheme() {
ui_theme.popupTitle.fgColor = themeInfos[i].uPopupTitleStyleFg;
ui_theme.popupStyle = MakeStyle(themeInfos[i].uPopupStyleFg, themeInfos[i].uPopupStyleBg);
ui_theme.backgroundColor = themeInfos[i].uBackgroundColor;

// Load any missing atlas metadata (the images are loaded from UIContext).
LoadAtlasMetadata(ui_atlas, (themeInfos[i].UIAtlas+".meta").c_str(), true);
#if !(PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(ANDROID))
LoadAtlasMetadata(font_atlas, "font_atlas.meta", ui_atlas.num_fonts == 0);
#else
LoadAtlasMetadata(font_atlas, "asciifont_atlas.meta", ui_atlas.num_fonts == 0);
#endif

ctx->setUIAtlas(themeInfos[i].UIAtlas+".zim");
}

UI::Theme *GetTheme() {
return &ui_theme;
}

Atlas *GetFontAtlas() {
return &font_atlas;
}

Atlas *GetUIAtlas() {
return &ui_atlas;
}

void ReloadAllThemeInfo() {
std::vector<Path> directories;
directories.push_back(Path("themes")); // For VFS
Expand Down
5 changes: 4 additions & 1 deletion UI/Theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
#include <vector>

#include "Common/UI/Context.h"
#include "Common/Render/TextureAtlas.h"

void ReloadAllThemeInfo();

std::vector<std::string> GetThemeInfoNames();
void UpdateTheme();
void UpdateTheme(UIContext *ctx);
Atlas *GetFontAtlas();
Atlas *GetUIAtlas();
UI::Theme *GetTheme();