Files
XCEngine/editor/app/Host/Win32/Resources/Win32EditorResourceService.cpp

140 lines
4.5 KiB
C++

#include "Resources/Win32EditorResourceService.h"
#include "EditorResources.h"
#include <vector>
namespace XCEngine::UI::Editor::Host {
namespace {
int ResolvePngResourceId(EditorHostPngResourceKind kind) {
switch (kind) {
case EditorHostPngResourceKind::FolderIcon:
return IDR_PNG_FOLDER_ICON;
case EditorHostPngResourceKind::GameObjectIcon:
return IDR_PNG_GAMEOBJECT_ICON;
case EditorHostPngResourceKind::SceneIcon:
return IDR_PNG_SCENE_ICON;
case EditorHostPngResourceKind::Logo:
return IDR_PNG_LOGO;
case EditorHostPngResourceKind::LogoIcon:
return IDR_PNG_LOGO_ICON;
case EditorHostPngResourceKind::CameraGizmoIcon:
return IDR_PNG_CAMERA_GIZMO_ICON;
case EditorHostPngResourceKind::DirectionalLightGizmoIcon:
return IDR_PNG_DIRECTIONAL_LIGHT_GIZMO_ICON;
case EditorHostPngResourceKind::PointLightGizmoIcon:
return IDR_PNG_POINT_LIGHT_GIZMO_ICON;
case EditorHostPngResourceKind::SpotLightGizmoIcon:
return IDR_PNG_SPOT_LIGHT_GIZMO_ICON;
case EditorHostPngResourceKind::PlayButtonIcon:
return IDR_PNG_PLAY_BUTTON_ICON;
case EditorHostPngResourceKind::PauseButtonIcon:
return IDR_PNG_PAUSE_BUTTON_ICON;
case EditorHostPngResourceKind::StepButtonIcon:
return IDR_PNG_STEP_BUTTON_ICON;
case EditorHostPngResourceKind::ViewMoveToolIcon:
return IDR_PNG_VIEW_MOVE_TOOL_ICON;
case EditorHostPngResourceKind::ViewMoveToolActiveIcon:
return IDR_PNG_VIEW_MOVE_TOOL_ACTIVE_ICON;
case EditorHostPngResourceKind::MoveToolIcon:
return IDR_PNG_MOVE_TOOL_ICON;
case EditorHostPngResourceKind::MoveToolActiveIcon:
return IDR_PNG_MOVE_TOOL_ACTIVE_ICON;
case EditorHostPngResourceKind::RotateToolIcon:
return IDR_PNG_ROTATE_TOOL_ICON;
case EditorHostPngResourceKind::RotateToolActiveIcon:
return IDR_PNG_ROTATE_TOOL_ACTIVE_ICON;
case EditorHostPngResourceKind::ScaleToolIcon:
return IDR_PNG_SCALE_TOOL_ICON;
case EditorHostPngResourceKind::ScaleToolActiveIcon:
return IDR_PNG_SCALE_TOOL_ACTIVE_ICON;
case EditorHostPngResourceKind::TransformToolIcon:
return IDR_PNG_TRANSFORM_TOOL_ICON;
case EditorHostPngResourceKind::TransformToolActiveIcon:
return IDR_PNG_TRANSFORM_TOOL_ACTIVE_ICON;
default:
return 0;
}
}
} // namespace
Win32EditorResourceService::Win32EditorResourceService(HINSTANCE instance)
: m_instance(instance) {}
bool Win32EditorResourceService::TryLoadPngResource(
EditorHostPngResourceKind kind,
EditorHostResourceBytes& outBytes,
std::string& outError) const {
outBytes = {};
outError.clear();
const int resourceId = ResolvePngResourceId(kind);
if (resourceId == 0) {
outError = "Unknown PNG resource kind.";
return false;
}
HINSTANCE module = m_instance;
if (module == nullptr) {
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;
}
outBytes.data = reinterpret_cast<const std::uint8_t*>(lockedBytes);
outBytes.size = static_cast<std::size_t>(resourceSize);
return true;
}
std::filesystem::path Win32EditorResourceService::GetExecutableDirectory() const {
std::vector<wchar_t> buffer(MAX_PATH);
while (true) {
const DWORD copied = ::GetModuleFileNameW(
nullptr,
buffer.data(),
static_cast<DWORD>(buffer.size()));
if (copied == 0u) {
return std::filesystem::current_path().lexically_normal();
}
if (copied < buffer.size() - 1u) {
return std::filesystem::path(std::wstring(buffer.data(), copied))
.parent_path()
.lexically_normal();
}
buffer.resize(buffer.size() * 2u);
}
}
} // namespace XCEngine::UI::Editor::Host