#include "EterLib/StdAfx.h" #include "EterLib/GrpSubImage.h" #include "EterLib/ResourceManager.h" #include #include #include #include #include #include #include char CGraphicSubImage::m_SearchPath[256] = "D:/Ymir Work/UI/"; auto CGraphicSubImage::Type() -> TType { static TType type = StringToType("CGraphicSubImage"); return type; } CGraphicSubImage::CGraphicSubImage(const char* name) : CGraphicImage(name) {} CGraphicSubImage::~CGraphicSubImage() { m_roImage.Clear(); } bool CGraphicSubImage::CreateDeviceObjects() { if (m_roImage.IsNull()) return false; m_imageTexture.CreateFromTexturePointer(m_roImage->GetTexturePointer()); return true; } void CGraphicSubImage::SetImagePointer(CGraphicImage* image) { m_roImage.SetPointer(image); CreateDeviceObjects(); } bool CGraphicSubImage::SetImageFileName(const char* name) { CResource* resource = CResourceManager::Instance().GetResourcePointer(name); if (!resource || !resource->IsType(CGraphicImage::Type())) return false; SetImagePointer(static_cast(resource)); return true; } void CGraphicSubImage::SetRectPosition(int left, int top, int right, int bottom) { m_rect = {left, top, right, bottom}; } void CGraphicSubImage::SetRectReference(const RECT& rect) { m_rect = rect; } void CGraphicSubImage::SetSearchPath(const char* path) { if (!path) return; std::strncpy(m_SearchPath, path, sizeof(m_SearchPath) - 1); m_SearchPath[sizeof(m_SearchPath) - 1] = 0; } bool CGraphicSubImage::OnLoad(int size, const void* bytes) { if (!bytes || size <= 0) return false; std::istringstream input(std::string(static_cast(bytes), static_cast(size))); std::map tokens; std::string line; while (std::getline(input, line)) { std::istringstream fields(line); std::string key, value; if (!(fields >> key >> value)) continue; if (!value.empty() && value.front() == '"') { value.erase(0, 1); if (!value.empty() && value.back() == '"') value.pop_back(); } std::transform(key.begin(), key.end(), key.begin(), [](unsigned char c) { return std::tolower(c); }); std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c) { return std::tolower(c); }); tokens[key] = value; } if (tokens["title"] != "subimage" || tokens["image"].empty()) return false; std::string image_path; if (tokens["version"] == "2.0") { const std::string parent = GetFileNameString(); const auto slash = parent.find_last_of("\\/"); image_path = slash == std::string::npos ? tokens["image"] : parent.substr(0, slash + 1) + tokens["image"]; } else { image_path = std::string(m_SearchPath) + tokens["image"]; } if (!SetImageFileName(image_path.c_str())) return false; SetRectPosition(std::atoi(tokens["left"].c_str()), std::atoi(tokens["top"].c_str()), std::atoi(tokens["right"].c_str()), std::atoi(tokens["bottom"].c_str())); return true; } void CGraphicSubImage::OnClear() { m_roImage.Clear(); m_imageTexture.Destroy(); m_rect = {0, 0, 0, 0}; } bool CGraphicSubImage::OnIsEmpty() const { return m_roImage.IsNull() || m_roImage->IsEmpty(); } bool CGraphicSubImage::OnIsType(TType type) { return type == Type() || CGraphicImage::OnIsType(type); }