From 4c2e744185675050f49b294a69ba3e3b895c03af Mon Sep 17 00:00:00 2001 From: ravi688 Date: Thu, 10 Oct 2024 19:12:19 +0530 Subject: [PATCH] [SUTK] Added font loading/unloading interface in UIDriver, though implementation is to be defined. --- sutk/include/sutk/IGfxDriver.hpp | 2 ++ sutk/include/sutk/SGEGfxDriver.hpp | 4 +++ sutk/include/sutk/ThemeManager.hpp | 22 ++++++++++++++--- sutk/include/sutk/UIDriver.hpp | 11 +++++++-- sutk/source/SGEGfxDriver.cpp | 18 +++++++++++--- sutk/source/ThemeManager.cpp | 39 ++++++++++++++++++++++++++++++ sutk/source/UIDriver.cpp | 10 ++++++++ 7 files changed, 98 insertions(+), 8 deletions(-) diff --git a/sutk/include/sutk/IGfxDriver.hpp b/sutk/include/sutk/IGfxDriver.hpp index c6e73f91..60db4d27 100644 --- a/sutk/include/sutk/IGfxDriver.hpp +++ b/sutk/include/sutk/IGfxDriver.hpp @@ -71,6 +71,8 @@ namespace SUTK virtual GfxDriverObjectHandleType loadTexture(std::string_view str) = 0; virtual void getTextureAttributes(GfxDriverObjectHandleType texture, TextureAttributes& out) = 0; virtual void unloadTexture(GfxDriverObjectHandleType handle) = 0; + virtual GfxDriverObjectHandleType loadFont(std::string_view str) = 0; + virtual void unloadFont(GfxDriverObjectHandleType handle) = 0; virtual GfxDriverObjectHandleType compileGeometry(const Geometry& geometryDsc, GfxDriverObjectHandleType previous = GFX_DRIVER_OBJECT_NULL_HANDLE) = 0; virtual void destroyGeometry(GfxDriverObjectHandleType geometry) = 0; virtual GfxDriverObjectHandleType getGeometryObject(GfxDriverObjectHandleType geometry) = 0; diff --git a/sutk/include/sutk/SGEGfxDriver.hpp b/sutk/include/sutk/SGEGfxDriver.hpp index 6470dddb..877ae0e8 100644 --- a/sutk/include/sutk/SGEGfxDriver.hpp +++ b/sutk/include/sutk/SGEGfxDriver.hpp @@ -62,6 +62,7 @@ namespace SUTK std::unordered_map m_bitmapTextStringMappings; std::unordered_map m_renderObjectMappings; com::IDMap> m_textureData; + com::IDMap> m_fontData; std::unordered_map m_typeTable; template struct CallbackHandlerData @@ -158,6 +159,9 @@ namespace SUTK virtual void getTextureAttributes(GfxDriverObjectHandleType texture, TextureAttributes& out) override; virtual void unloadTexture(GfxDriverObjectHandleType handle) override; + virtual GfxDriverObjectHandleType loadFont(std::string_view str) override; + virtual void unloadFont(GfxDriverObjectHandleType handle) override; + // compiles SUTK::Geometry description into SGE objects which can eventually be renderered in SGE virtual GfxDriverObjectHandleType compileGeometry(const Geometry& geometryDsc, GfxDriverObjectHandleType previous = GFX_DRIVER_OBJECT_NULL_HANDLE) override; virtual void destroyGeometry(GfxDriverObjectHandleType geometry) override; diff --git a/sutk/include/sutk/ThemeManager.hpp b/sutk/include/sutk/ThemeManager.hpp index 2724e033..7d04420d 100644 --- a/sutk/include/sutk/ThemeManager.hpp +++ b/sutk/include/sutk/ThemeManager.hpp @@ -63,7 +63,7 @@ namespace SUTK private: com::unordered_map m_colors; com::unordered_map m_images; - // com::unordered_map m_fonts; + com::unordered_map m_fonts; ThemeInterfaceType* m_interface; public: Theme(UIDriver& driver, ThemeInterfaceType* interface) noexcept : UIDriverObject(driver), m_interface(interface) { } @@ -87,17 +87,27 @@ namespace SUTK else m_images.insert({ KeyType { key }, std::forward(value) }); } + else if constexpr(std::is_same_v) + { + if(m_interface->getType(key) != ThemeInterfaceType::Type::Font) + DEBUG_LOG_ERROR("Type mismatch"); + else + m_fonts.insert({ KeyType { key }, std::forward(value) }); + } else static_assert(false, "Type not supported"); } template void add(const KeyViewType& key, std::string_view view) noexcept { - bool result = view.ends_with(".png") || view.ends_with(".jpg") || view.ends_with(".bmp"); - if(result) + if(view.ends_with(".png") || view.ends_with(".jpg") || view.ends_with(".bmp")) { UIDriver::ImageReference image = getUIDriver().loadImage(view); m_images.insert({ KeyType { key }, image }); + } else if(view.ends_with(".ttf")) + { + UIDriver::FontReference font = getUIDriver().loadFont(view); + m_fonts.insert({ KeyType { key }, font }); } else DEBUG_LOG_ERROR("Unrecognized image file extension in path: %.*s", view.length(), view.data()); @@ -110,6 +120,8 @@ namespace SUTK result = com::find_erase(m_colors, key); else if constexpr(std::is_same_v) result = com::find_erase(m_images, key); + else if constexpr(std::is_same_v) + result = com::find_erase(m_fonts, key); else static_assert(false, "Type not supported"); _com_assert(result); @@ -121,6 +133,8 @@ namespace SUTK return com::find_value(m_colors, key); else if constexpr(std::is_same_v) return com::find_value(m_images, key); + else if constexpr(std::is_same_v) + return com::find_value(m_fonts, key); else static_assert(false, "Type not supported"); } @@ -136,6 +150,8 @@ namespace SUTK m_defs.insert({ KeyType { key }, { Type::Color, { } } }); else if constexpr(std::is_same_v) m_defs.insert({ KeyType { key }, { Type::Image, { } } }); + else if constexpr(std::is_same_v) + m_defs.insert({ KeyType { key }, { Type::Font, { } } }); else static_assert(false, "Type not supported"); } diff --git a/sutk/include/sutk/UIDriver.hpp b/sutk/include/sutk/UIDriver.hpp index f08feab3..18fab7ca 100644 --- a/sutk/include/sutk/UIDriver.hpp +++ b/sutk/include/sutk/UIDriver.hpp @@ -3,6 +3,7 @@ #include #include // for SUTK::IGfxDriver::getSizeInCentimeters() +#include // for com::Reference #include // for std::vector #include // for std::forward @@ -29,9 +30,13 @@ namespace SUTK class UIDriver { public: - typedef GfxDriverObjectHandleType ImageReference; - static constexpr ImageReference InvalidImage = GFX_DRIVER_OBJECT_NULL_HANDLE; + struct AuxImage { }; + typedef com::Reference ImageReference; + static constexpr ImageReference InvalidImage = ImageReference::Null; typedef TextureAttributes ImageAttributes; + struct AuxFont { }; + typedef com::Reference FontReference; + static constexpr FontReference InvalidFont = FontReference::Null; private: IGfxDriver& m_gfxDriver; IInputDriver* m_inputDriver; @@ -61,6 +66,8 @@ namespace SUTK ImageReference loadImage(std::string_view path) noexcept; void unloadImage(ImageReference id) noexcept; ImageAttributes getImageAttributes(ImageReference id) noexcept; + FontReference loadFont(std::string_view path) noexcept; + void unloadFont(FontReference id) noexcept; Container* getDebugRootContainer() noexcept { return m_debugRootContainer; } diff --git a/sutk/source/SGEGfxDriver.cpp b/sutk/source/SGEGfxDriver.cpp index 9617ec59..b06d5050 100644 --- a/sutk/source/SGEGfxDriver.cpp +++ b/sutk/source/SGEGfxDriver.cpp @@ -805,7 +805,7 @@ namespace SUTK { // if there is no vertex buffer then create one with binding equal to zero SGE::Buffer vertexBuffer = mesh.getVertexBuffer(createInfo.binding); - if(static_cast(vertexBuffer) == NULL) + if(!vertexBuffer) mesh.createAndAddVertexBuffer(com::cast_away_const(createInfo)); else { @@ -849,9 +849,21 @@ namespace SUTK // TODO : unload the texture from SGE Driver's side } + GfxDriverObjectHandleType SGEGfxDriver::loadFont(std::string_view str) + { + // TODO: Load the font from another thread + return m_fontData.add({ std::string(str), { } }); + } + + void SGEGfxDriver::unloadFont(GfxDriverObjectHandleType handle) + { + m_fontData.remove(handle); + // TODO : unload the font from SGE Driver's side + } + SGE::Texture SGEGfxDriver::getTexture(GfxDriverObjectHandleType handle) noexcept { - if(handle == UIDriver::InvalidImage) + if(handle == GFX_DRIVER_OBJECT_NULL_HANDLE) return getDefaultTexture(); std::pair& pair = m_textureData.get(handle); @@ -1011,7 +1023,7 @@ namespace SUTK if(vertexBuffer || createInfo.count) { // if there is no vertex buffer then create one with binding equal to 5 - if(static_cast(vertexBuffer) == NULL) + if(!vertexBuffer) mesh.createAndAddVertexBuffer(createInfo); else { diff --git a/sutk/source/ThemeManager.cpp b/sutk/source/ThemeManager.cpp index 52012c10..864cd1d3 100644 --- a/sutk/source/ThemeManager.cpp +++ b/sutk/source/ThemeManager.cpp @@ -80,6 +80,12 @@ namespace SUTK interface->define(nameSV); continue; } + typeAttr = node_find_attribute(child, str, "Font"); + if(typeAttr) + { + interface->define(nameSV); + continue; + } DEBUG_LOG_WARNING("Type either isn't recognized or not given, skipping \"%.*s\"", nameSV.length(), nameSV.data()); } ppsr_v3d_generic_parse_result_destroy(NULL, result); @@ -231,6 +237,33 @@ namespace SUTK return image; } + template<> + template<> + UIDriver::FontReference ThemeManager::deriveValue(v3d_generic_node_t* node, const char* str) noexcept + { + // Followings are the possibilies: + // 1. "path/to/a/file.ttf" + // 2. default + UIDriver::FontReference font = UIDriver::InvalidFont; + if(node->qualifier_count == 1) + { + u32_pair_t pair = node->qualifiers[0]; + if(com_safe_strncmp(pair.start + str, "default", U32_PAIR_DIFF(pair)) == 0) + { + // The Gfx Driver will automatically load its default font + font = UIDriver::InvalidFont; + } + else + { + auto filePath = std::string_view { pair.start + str, U32_PAIR_DIFF(pair) }; + font = getUIDriver().loadFont(filePath); + } + } + else + DEBUG_LOG_ERROR("Neither file path nor \"default\" is provided"); + return font; + } + template<> typename ThemeManager::ThemeType* ThemeManager::loadTheme(const std::string_view filePath) noexcept { @@ -334,6 +367,12 @@ namespace SUTK theme->add(nameSV, std::move(image)); break; } + case ThemeInterfaceType::Type::Font: + { + UIDriver::FontReference font = deriveValue(child->value, str); + theme->add(nameSV, std::move(font)); + break; + } default: { DEBUG_LOG_WARNING("Type either isn't recognized or not given, skipping \"%.*s\"", nameSV.length(), nameSV.data()); diff --git a/sutk/source/UIDriver.cpp b/sutk/source/UIDriver.cpp index 59cc731a..3e6c3296 100644 --- a/sutk/source/UIDriver.cpp +++ b/sutk/source/UIDriver.cpp @@ -104,6 +104,16 @@ namespace SUTK getGfxDriver().unloadTexture(id); } + UIDriver::FontReference UIDriver::loadFont(std::string_view path) noexcept + { + return getGfxDriver().loadFont(path); + } + + void UIDriver::unloadFont(FontReference id) noexcept + { + getGfxDriver().unloadFont(id); + } + UIDriver::ImageAttributes UIDriver::getImageAttributes(ImageReference id) noexcept { TextureAttributes attr;