Refine new editor shell host and embedded icons

This commit is contained in:
2026-04-14 14:41:45 +08:00
parent 4ee1bcc599
commit 91c62c6b14
27 changed files with 768 additions and 124 deletions

View File

@@ -1,4 +1,5 @@
#include "ProductBuiltInIcons.h"
#include "EditorResources.h"
#include <sstream>
@@ -20,31 +21,96 @@ void AppendLoadError(
stream << label << ": " << error;
}
bool LoadEmbeddedPngBytes(
UINT resourceId,
const std::uint8_t*& outData,
std::size_t& outSize,
std::string& outError) {
outData = nullptr;
outSize = 0u;
outError.clear();
HMODULE module = GetModuleHandleW(nullptr);
if (module == nullptr) {
outError = "GetModuleHandleW(nullptr) returned null.";
return false;
}
HRSRC resource = FindResourceW(module, MAKEINTRESOURCEW(resourceId), L"PNG");
if (resource == nullptr) {
outError = "FindResourceW failed.";
return false;
}
HGLOBAL resourceData = LoadResource(module, resource);
if (resourceData == nullptr) {
outError = "LoadResource failed.";
return false;
}
const DWORD resourceSize = SizeofResource(module, resource);
if (resourceSize == 0u) {
outError = "SizeofResource returned zero.";
return false;
}
const void* lockedBytes = LockResource(resourceData);
if (lockedBytes == nullptr) {
outError = "LockResource failed.";
return false;
}
outData = reinterpret_cast<const std::uint8_t*>(lockedBytes);
outSize = static_cast<std::size_t>(resourceSize);
return true;
}
void LoadEmbeddedPngTexture(
Host::NativeRenderer& renderer,
UINT resourceId,
std::string_view label,
::XCEngine::UI::UITextureHandle& outTexture,
std::ostringstream& errorStream) {
const std::uint8_t* bytes = nullptr;
std::size_t byteCount = 0u;
std::string error = {};
if (!LoadEmbeddedPngBytes(resourceId, bytes, byteCount, error)) {
AppendLoadError(errorStream, label, error);
return;
}
error.clear();
if (!renderer.LoadTextureFromMemory(bytes, byteCount, outTexture, error)) {
AppendLoadError(errorStream, label, error);
}
}
} // namespace
void ProductBuiltInIcons::Initialize(
Host::NativeRenderer& renderer,
const std::filesystem::path& repoRoot) {
void ProductBuiltInIcons::Initialize(Host::NativeRenderer& renderer) {
Shutdown();
m_renderer = &renderer;
m_repoRoot = repoRoot.lexically_normal();
std::ostringstream errorStream = {};
std::string error = {};
if (!m_renderer->LoadTextureFromFile(ResolveFolderIconPath(), m_folderIcon, error)) {
AppendLoadError(errorStream, "folder_icon.png", error);
}
error.clear();
if (!m_renderer->LoadTextureFromFile(ResolveGameObjectIconPath(), m_gameObjectIcon, error)) {
AppendLoadError(errorStream, "gameobject_icon.png", error);
}
error.clear();
if (!m_renderer->LoadTextureFromFile(ResolveSceneIconPath(), m_sceneIcon, error)) {
AppendLoadError(errorStream, "scene_icon.png", error);
}
LoadEmbeddedPngTexture(
renderer,
IDR_PNG_FOLDER_ICON,
"folder_icon.png",
m_folderIcon,
errorStream);
LoadEmbeddedPngTexture(
renderer,
IDR_PNG_GAMEOBJECT_ICON,
"gameobject_icon.png",
m_gameObjectIcon,
errorStream);
LoadEmbeddedPngTexture(
renderer,
IDR_PNG_SCENE_ICON,
"scene_icon.png",
m_sceneIcon,
errorStream);
m_lastError = errorStream.str();
}
@@ -57,7 +123,6 @@ void ProductBuiltInIcons::Shutdown() {
}
m_renderer = nullptr;
m_repoRoot.clear();
m_lastError.clear();
}
@@ -79,16 +144,4 @@ const std::string& ProductBuiltInIcons::GetLastError() const {
return m_lastError;
}
std::filesystem::path ProductBuiltInIcons::ResolveFolderIconPath() const {
return (m_repoRoot / "editor/resources/Icons/folder_icon.png").lexically_normal();
}
std::filesystem::path ProductBuiltInIcons::ResolveGameObjectIconPath() const {
return (m_repoRoot / "editor/resources/Icons/gameobject_icon.png").lexically_normal();
}
std::filesystem::path ProductBuiltInIcons::ResolveSceneIconPath() const {
return (m_repoRoot / "editor/resources/Icons/scene_icon.png").lexically_normal();
}
} // namespace XCEngine::UI::Editor::App