diff options
author | Hop311 <Hop3114@gmail.com> | 2024-08-30 23:29:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-30 23:29:57 +0200 |
commit | f54e454afb90f8868e7c62529e2a388fdaadf20b (patch) | |
tree | f19dbcdfe613397e86dc52cc34e0a443bd0f3e96 /extension/src/openvic-extension/singletons | |
parent | 855e5b087459da19caf230cf22d99462680b268e (diff) | |
parent | d7672f406406eea46625bc725690651f28211e19 (diff) |
Merge pull request #251 from OpenVicProject/gui-text-label
Add GUILabel (colour code + currency icon support)
Diffstat (limited to 'extension/src/openvic-extension/singletons')
3 files changed, 63 insertions, 6 deletions
diff --git a/extension/src/openvic-extension/singletons/AssetManager.cpp b/extension/src/openvic-extension/singletons/AssetManager.cpp index eec1ada..d17edd0 100644 --- a/extension/src/openvic-extension/singletons/AssetManager.cpp +++ b/extension/src/openvic-extension/singletons/AssetManager.cpp @@ -4,6 +4,7 @@ #include "openvic-extension/singletons/GameSingleton.hpp" #include "openvic-extension/utility/ClassBindings.hpp" +#include "openvic-extension/utility/UITools.hpp" #include "openvic-extension/utility/Utilities.hpp" using namespace godot; @@ -119,7 +120,7 @@ Ref<ImageTexture> AssetManager::get_texture(StringName const& path, LoadFlags lo } } -Ref<Font> AssetManager::get_font(StringName const& name) { +Ref<FontFile> AssetManager::get_font(StringName const& name) { const font_map_t::const_iterator it = fonts.find(name); if (it != fonts.end()) { ERR_FAIL_NULL_V_MSG(it->second, nullptr, vformat("Failed to load font previously: %s", name)); @@ -152,7 +153,7 @@ Ref<Font> AssetManager::get_font(StringName const& name) { ERR_FAIL_V_MSG(nullptr, vformat("Failed to look up font: %s", font_path)); } - const Ref<Font> font = Utilities::load_godot_font(lookedup_font_path, image); + const Ref<FontFile> font = Utilities::load_godot_font(lookedup_font_path, image); if (font.is_null()) { fonts.emplace(name, nullptr); @@ -165,3 +166,40 @@ Ref<Font> AssetManager::get_font(StringName const& name) { fonts.emplace(name, font); return font; } + +Error AssetManager::preload_textures() { + static const String currency_sprite_big = "GFX_tooltip_money_big"; + static const String currency_sprite_medium = "GFX_tooltip_money_small"; + static const String currency_sprite_small = "GFX_tooltip_money"; + + constexpr auto load = [](String const& sprite_name, Ref<GFXSpriteTexture>& texture) -> bool { + GFX::Sprite const* sprite = UITools::get_gfx_sprite(sprite_name); + ERR_FAIL_NULL_V(sprite, false); + + GFX::IconTextureSprite const* icon_sprite = sprite->cast_to<GFX::IconTextureSprite>(); + ERR_FAIL_NULL_V(icon_sprite, false); + + texture = GFXSpriteTexture::make_gfx_sprite_texture(icon_sprite); + ERR_FAIL_NULL_V(texture, false); + + return true; + }; + + bool ret = true; + + ret &= load(currency_sprite_big, currency_texture_big); + ret &= load(currency_sprite_medium, currency_texture_medium); + ret &= load(currency_sprite_small, currency_texture_small); + + return ERR(ret); +} + +Ref<GFXSpriteTexture> const& AssetManager::get_currency_texture(real_t height) const { + if (height > currency_texture_big->get_height()) { + return currency_texture_big; + } else if (height > currency_texture_medium->get_height()) { + return currency_texture_medium; + } else { + return currency_texture_small; + } +} diff --git a/extension/src/openvic-extension/singletons/AssetManager.hpp b/extension/src/openvic-extension/singletons/AssetManager.hpp index 0856d05..deca309 100644 --- a/extension/src/openvic-extension/singletons/AssetManager.hpp +++ b/extension/src/openvic-extension/singletons/AssetManager.hpp @@ -1,12 +1,14 @@ #pragma once #include <godot_cpp/classes/atlas_texture.hpp> -#include <godot_cpp/classes/font.hpp> +#include <godot_cpp/classes/font_file.hpp> #include <godot_cpp/classes/image_texture.hpp> #include <godot_cpp/core/class_db.hpp> #include <openvic-simulation/interface/GFXSprite.hpp> +#include "openvic-extension/classes/GFXSpriteTexture.hpp" + namespace OpenVic { class AssetManager : public godot::Object { GDCLASS(AssetManager, godot::Object) @@ -32,7 +34,7 @@ namespace OpenVic { }; /* deque_ordered_map to avoid the need to reallocate. */ using image_asset_map_t = deque_ordered_map<godot::StringName, image_asset_t>; - using font_map_t = deque_ordered_map<godot::StringName, godot::Ref<godot::Font>>; + using font_map_t = deque_ordered_map<godot::StringName, godot::Ref<godot::FontFile>>; image_asset_map_t image_assets; font_map_t fonts; @@ -68,7 +70,18 @@ namespace OpenVic { /* Search for and load a font with the specified name from the game defines' font directory, first checking the * AssetManager's font cache in case it has already been loaded, and returning nullptr if font loading fails. */ - godot::Ref<godot::Font> get_font(godot::StringName const& name); + godot::Ref<godot::FontFile> get_font(godot::StringName const& name); + + private: + godot::Ref<GFXSpriteTexture> PROPERTY(currency_texture_big); // 32x32 + godot::Ref<GFXSpriteTexture> PROPERTY(currency_texture_medium); // 24x24 + godot::Ref<GFXSpriteTexture> PROPERTY(currency_texture_small); // 16x16 + + public: + godot::Error preload_textures(); + + /* Get the largest currency texture with height less than the specified font height. */ + godot::Ref<GFXSpriteTexture> const& get_currency_texture(real_t height) const; }; } diff --git a/extension/src/openvic-extension/singletons/GameSingleton.cpp b/extension/src/openvic-extension/singletons/GameSingleton.cpp index 5268789..13324d0 100644 --- a/extension/src/openvic-extension/singletons/GameSingleton.cpp +++ b/extension/src/openvic-extension/singletons/GameSingleton.cpp @@ -597,7 +597,7 @@ Error GameSingleton::set_compatibility_mode_roots(PackedStringArray const& file_ Error GameSingleton::load_defines_compatibility_mode() { Error err = OK; auto add_message = std::bind_front(&LoadLocalisation::add_message, LoadLocalisation::get_singleton()); - + if (!game_manager.load_definitions(add_message)) { UtilityFunctions::push_error("Failed to load defines!"); err = FAILED; @@ -616,6 +616,12 @@ Error GameSingleton::load_defines_compatibility_mode() { err = FAILED; } + AssetManager* asset_manager = AssetManager::get_singleton(); + if (asset_manager == nullptr || asset_manager->preload_textures() != OK) { + UtilityFunctions::push_error("Failed to preload assets!"); + err = FAILED; + } + return err; } |