2026-03-26 22:10:43 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "BaseTheme.h"
|
|
|
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace Editor {
|
|
|
|
|
namespace UI {
|
|
|
|
|
|
|
|
|
|
class ImGuiSession {
|
|
|
|
|
public:
|
2026-03-28 15:07:19 +08:00
|
|
|
void Initialize(const std::string& projectPath, float mainDpiScale = 1.0f) {
|
2026-03-26 22:10:43 +08:00
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
|
ImGui::CreateContext();
|
|
|
|
|
|
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
2026-03-28 15:07:19 +08:00
|
|
|
io.ConfigDpiScaleFonts = true;
|
|
|
|
|
io.ConfigDpiScaleViewports = true;
|
2026-03-26 22:10:43 +08:00
|
|
|
|
|
|
|
|
ConfigureIniFile(projectPath, io);
|
2026-03-28 15:07:19 +08:00
|
|
|
ConfigureStyle(ImGui::GetStyle(), mainDpiScale);
|
2026-03-26 22:10:43 +08:00
|
|
|
ConfigureFonts(io);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Shutdown() {
|
|
|
|
|
SaveSettings();
|
|
|
|
|
if (ImGui::GetCurrentContext() != nullptr) {
|
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_iniPath.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SaveSettings() const {
|
|
|
|
|
if (m_iniPath.empty() || ImGui::GetCurrentContext() == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImGui::SaveIniSettingsToDisk(m_iniPath.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string& GetIniPath() const {
|
|
|
|
|
return m_iniPath;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 16:19:15 +08:00
|
|
|
void SetProjectPath(const std::string& projectPath) {
|
|
|
|
|
if (ImGui::GetCurrentContext() == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SaveSettings();
|
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
|
ConfigureIniFile(projectPath, io);
|
|
|
|
|
ImGui::LoadIniSettingsFromDisk(m_iniPath.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 22:10:43 +08:00
|
|
|
private:
|
2026-03-28 15:07:19 +08:00
|
|
|
static constexpr float kUiFontSize = 18.0f;
|
|
|
|
|
static constexpr const char* kPrimaryUiFontPath = "C:/Windows/Fonts/segoeui.ttf";
|
|
|
|
|
static constexpr const char* kChineseFallbackFontPath = "C:/Windows/Fonts/msyh.ttc";
|
|
|
|
|
|
2026-03-26 22:10:43 +08:00
|
|
|
void ConfigureIniFile(const std::string& projectPath, ImGuiIO& io) {
|
|
|
|
|
const std::filesystem::path configDir = std::filesystem::path(projectPath) / ".xceditor";
|
|
|
|
|
std::error_code ec;
|
|
|
|
|
std::filesystem::create_directories(configDir, ec);
|
|
|
|
|
|
|
|
|
|
m_iniPath = (configDir / "imgui_layout.ini").string();
|
|
|
|
|
io.IniFilename = m_iniPath.c_str();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 15:07:19 +08:00
|
|
|
void ConfigureStyle(ImGuiStyle& style, float mainDpiScale) const {
|
|
|
|
|
ApplyBaseTheme(style);
|
|
|
|
|
|
|
|
|
|
const float dpiScale = mainDpiScale < 1.0f ? 1.0f : (mainDpiScale > 4.0f ? 4.0f : mainDpiScale);
|
|
|
|
|
style.ScaleAllSizes(dpiScale);
|
|
|
|
|
style.FontScaleDpi = dpiScale;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 22:10:43 +08:00
|
|
|
void ConfigureFonts(ImGuiIO& io) const {
|
2026-03-28 15:07:19 +08:00
|
|
|
ImFontAtlas* atlas = io.Fonts;
|
|
|
|
|
atlas->Clear();
|
|
|
|
|
|
|
|
|
|
ImFontConfig baseConfig;
|
|
|
|
|
baseConfig.OversampleH = 2;
|
|
|
|
|
baseConfig.OversampleV = 1;
|
|
|
|
|
baseConfig.PixelSnapH = true;
|
|
|
|
|
|
|
|
|
|
ImFont* uiFont = atlas->AddFontFromFileTTF(
|
|
|
|
|
kPrimaryUiFontPath,
|
|
|
|
|
kUiFontSize,
|
|
|
|
|
&baseConfig,
|
|
|
|
|
atlas->GetGlyphRangesDefault());
|
|
|
|
|
|
|
|
|
|
if (uiFont) {
|
|
|
|
|
ImFontConfig mergeConfig = baseConfig;
|
|
|
|
|
mergeConfig.MergeMode = true;
|
|
|
|
|
mergeConfig.PixelSnapH = true;
|
|
|
|
|
atlas->AddFontFromFileTTF(
|
|
|
|
|
kChineseFallbackFontPath,
|
|
|
|
|
kUiFontSize,
|
|
|
|
|
&mergeConfig,
|
|
|
|
|
atlas->GetGlyphRangesChineseSimplifiedCommon());
|
2026-03-26 22:10:43 +08:00
|
|
|
} else {
|
2026-03-28 15:07:19 +08:00
|
|
|
uiFont = atlas->AddFontFromFileTTF(
|
|
|
|
|
kChineseFallbackFontPath,
|
|
|
|
|
kUiFontSize,
|
|
|
|
|
&baseConfig,
|
|
|
|
|
atlas->GetGlyphRangesChineseSimplifiedCommon());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!uiFont) {
|
|
|
|
|
ImFontConfig fallbackConfig = baseConfig;
|
|
|
|
|
fallbackConfig.SizePixels = kUiFontSize;
|
|
|
|
|
uiFont = atlas->AddFontDefault(&fallbackConfig);
|
2026-03-26 22:10:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-28 15:07:19 +08:00
|
|
|
io.FontDefault = uiFont;
|
2026-03-26 22:10:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string m_iniPath;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace UI
|
|
|
|
|
} // namespace Editor
|
|
|
|
|
} // namespace XCEngine
|