ImGuiManager: Localization support

This commit is contained in:
Stenzek 2023-06-19 21:42:52 +10:00 committed by Connor McLaughlin
parent c4c7d26bb3
commit c359223fd4
3 changed files with 28 additions and 4 deletions

View File

@ -31,6 +31,7 @@
#include "Recording/InputRecording.h" #include "Recording/InputRecording.h"
#include "VMManager.h" #include "VMManager.h"
#include "common/FileSystem.h"
#include "common/StringUtil.h" #include "common/StringUtil.h"
#include "common/Timer.h" #include "common/Timer.h"
@ -60,6 +61,9 @@ namespace ImGuiManager
static float s_global_scale = 1.0f; static float s_global_scale = 1.0f;
static std::string s_font_path;
static const ImWchar* s_font_range = nullptr;
static ImFont* s_standard_font; static ImFont* s_standard_font;
static ImFont* s_fixed_font; static ImFont* s_fixed_font;
static ImFont* s_medium_font; static ImFont* s_medium_font;
@ -82,6 +86,18 @@ static std::unordered_map<u32, ImGuiKey> s_imgui_key_map;
// need to keep track of this, so we can reinitialize on renderer switch // need to keep track of this, so we can reinitialize on renderer switch
static bool s_fullscreen_ui_was_initialized = false; static bool s_fullscreen_ui_was_initialized = false;
void ImGuiManager::SetFontPath(std::string path)
{
s_font_path = std::move(path);
s_standard_font_data = {};
}
void ImGuiManager::SetFontRange(const u16* range)
{
s_font_range = range;
s_standard_font_data = {};
}
bool ImGuiManager::Initialize() bool ImGuiManager::Initialize()
{ {
if (!LoadFontData()) if (!LoadFontData())
@ -333,7 +349,9 @@ bool ImGuiManager::LoadFontData()
{ {
if (s_standard_font_data.empty()) if (s_standard_font_data.empty())
{ {
std::optional<std::vector<u8>> font_data = Host::ReadResourceFile("fonts/Roboto-Regular.ttf"); std::optional<std::vector<u8>> font_data = s_font_path.empty() ?
Host::ReadResourceFile("fonts/Roboto-Regular.ttf") :
FileSystem::ReadBinaryFile(s_font_path.c_str());
if (!font_data.has_value()) if (!font_data.has_value())
return false; return false;
@ -393,7 +411,7 @@ ImFont* ImGuiManager::AddTextFont(float size)
ImFontConfig cfg; ImFontConfig cfg;
cfg.FontDataOwnedByAtlas = false; cfg.FontDataOwnedByAtlas = false;
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF( return ImGui::GetIO().Fonts->AddFontFromMemoryTTF(
s_standard_font_data.data(), static_cast<int>(s_standard_font_data.size()), size, &cfg, default_ranges); s_standard_font_data.data(), static_cast<int>(s_standard_font_data.size()), size, &cfg, s_font_range ? s_font_range : default_ranges);
} }
ImFont* ImGuiManager::AddFixedFont(float size) ImFont* ImGuiManager::AddFixedFont(float size)

View File

@ -26,6 +26,12 @@ enum class GenericInputBinding : u8;
namespace ImGuiManager namespace ImGuiManager
{ {
/// Sets the path to the font to use. Empty string means to use the default.
void SetFontPath(std::string path);
/// Sets the glyph range to use when loading fonts.
void SetFontRange(const u16* range);
/// Initializes ImGui, creates fonts, etc. /// Initializes ImGui, creates fonts, etc.
bool Initialize(); bool Initialize();
@ -106,4 +112,4 @@ namespace Host
/// Called by ImGuiManager when the cursor leaves a text field. /// Called by ImGuiManager when the cursor leaves a text field.
void EndTextInput(); void EndTextInput();
} } // namespace Host

View File

@ -42,7 +42,7 @@
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\libpng</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\libpng</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\libchdr\libchdr\include</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\libchdr\libchdr\include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\cubeb\include</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\cubeb\include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\imgui\include;$(SolutionDir)3rdparty\imgui\include</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\imgui\include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\simpleini\include</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\simpleini\include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\sdl2\include;$(SolutionDir)3rdparty\sdl2\SDL\include</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\sdl2\include;$(SolutionDir)3rdparty\sdl2\SDL\include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\libzip;$(SolutionDir)3rdparty\libzip\libzip\lib</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(SolutionDir)3rdparty\libzip;$(SolutionDir)3rdparty\libzip\libzip\lib</AdditionalIncludeDirectories>