-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI element generator + very basic province panel
- Loading branch information
Showing
17 changed files
with
852 additions
and
339 deletions.
There are no files selected for viewing
Submodule openvic-simulation
updated
14 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#include "AssetManager.hpp" | ||
|
||
#include <godot_cpp/variant/utility_functions.hpp> | ||
|
||
#include "openvic-extension/Utilities.hpp" | ||
|
||
using namespace godot; | ||
using namespace OpenVic; | ||
|
||
using OpenVic::Utilities::std_to_godot_string; | ||
using OpenVic::Utilities::godot_to_std_string; | ||
|
||
AssetManager::AssetManager(Dataloader const& new_dataloader) : dataloader { new_dataloader } {} | ||
|
||
AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName path) { | ||
const image_asset_map_t::iterator it = image_assets.find(path); | ||
if (it != image_assets.end()) { | ||
return it; | ||
} | ||
const String lookedup_path = std_to_godot_string(dataloader.lookup_image_file(godot_to_std_string(path)).string()); | ||
if (lookedup_path.is_empty()) { | ||
UtilityFunctions::push_error("Failed to look up image: ", path); | ||
return image_assets.end(); | ||
} | ||
const Ref<Image> image = Utilities::load_godot_image(lookedup_path); | ||
if (image.is_null() || image->is_empty()) { | ||
UtilityFunctions::push_error("Failed to load image: ", lookedup_path, " (looked up from ", path, ")"); | ||
return image_assets.end(); | ||
} | ||
return image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first; | ||
} | ||
|
||
Ref<Image> AssetManager::get_image(StringName path) { | ||
const image_asset_map_t::const_iterator it = _get_image_asset(path); | ||
if (it != image_assets.end()) { | ||
return it->second.image; | ||
} else { | ||
return nullptr; | ||
} | ||
} | ||
|
||
Ref<ImageTexture> AssetManager::get_texture(StringName path) { | ||
const image_asset_map_t::iterator it = _get_image_asset(path); | ||
if (it != image_assets.end()) { | ||
if (it->second.texture.is_null()) { | ||
it->second.texture = ImageTexture::create_from_image(it->second.image); | ||
if (it->second.texture.is_null()) { | ||
UtilityFunctions::push_error("Failed to turn image into texture: ", path); | ||
} | ||
} | ||
return it->second.texture; | ||
} else { | ||
return nullptr; | ||
} | ||
} | ||
|
||
Rect2i AssetManager::get_frame_region(GFX::frame_t frame, GFX::frame_t frame_count, Vector2i size) { | ||
if (frame_count <= GFX::NO_FRAMES) { | ||
UtilityFunctions::push_warning("No frames!"); | ||
frame_count = 1; | ||
} | ||
if (frame <= GFX::NO_FRAMES || frame > frame_count) { | ||
UtilityFunctions::push_warning("Invalid frame index ", frame, " out of count ", frame_count); | ||
frame = frame_count; | ||
} | ||
frame--; | ||
return { frame * size.x / frame_count, 0, size.x / frame_count, size.y }; | ||
} | ||
|
||
Ref<AtlasTexture> AssetManager::make_atlas_texture(Ref<Texture2D> texture, Rect2i region) { | ||
ERR_FAIL_NULL_V(texture, nullptr); | ||
Ref<AtlasTexture> atlas; | ||
atlas.instantiate(); | ||
ERR_FAIL_NULL_V(atlas, nullptr); | ||
atlas->set_atlas(texture); | ||
atlas->set_region(region); | ||
return atlas; | ||
} | ||
|
||
Ref<AtlasTexture> AssetManager::get_atlas_texture(StringName path, GFX::frame_t frame, GFX::frame_t frame_count) { | ||
Ref<ImageTexture> texture = get_texture(path); | ||
ERR_FAIL_NULL_V(texture, nullptr); | ||
return make_atlas_texture(texture, get_frame_region(frame, frame_count, texture->get_size())); | ||
} | ||
|
||
Ref<Texture2D> AssetManager::get_texture(StringName path, GFX::frame_t frame, GFX::frame_t frame_count) { | ||
if (frame_count < 2) { | ||
if (frame_count == 1) { | ||
if (frame != 1) { | ||
UtilityFunctions::push_warning("Invalid frame index ", frame, " out of count ", frame_count); | ||
} | ||
} else { | ||
if (frame_count > GFX::NO_FRAMES) { | ||
UtilityFunctions::push_warning("Invalid frame index ", frame, " out of no frames "); | ||
} | ||
} | ||
return get_texture(path); | ||
} else { | ||
if (frame <= GFX::NO_FRAMES || frame > frame_count) { | ||
UtilityFunctions::push_warning("Invalid frame index ", frame, " out of count ", frame_count); | ||
frame = frame_count; | ||
} | ||
return get_atlas_texture(path, frame, frame_count); | ||
} | ||
} | ||
|
||
Ref<Font> AssetManager::get_font(StringName name) { | ||
const font_map_t::const_iterator it = fonts.find(name); | ||
if (it != fonts.end()) { | ||
return it->second; | ||
} | ||
|
||
static const String font_dir = "gfx/fonts/"; | ||
static const String font_ext = ".fnt"; | ||
static const String image_ext = ".tga"; | ||
|
||
const String image_path = font_dir + name + image_ext; | ||
const Ref<Image> image = get_image(image_path); | ||
if (image.is_null()) { | ||
UtilityFunctions::push_error("Failed to load font image: ", image_path, " for the font named ", name); | ||
return nullptr; | ||
} | ||
const String lookedup_font_path = | ||
std_to_godot_string(dataloader.lookup_file(godot_to_std_string(font_dir + name + font_ext)).string()); | ||
const Ref<Font> font = Utilities::load_godot_font(lookedup_font_path, image); | ||
if (font.is_null()) { | ||
UtilityFunctions::push_error("Failed to load font file ", lookedup_font_path, " for the font named ", name); | ||
return nullptr; | ||
} | ||
fonts.emplace(std::move(name), font); | ||
return font; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <godot_cpp/classes/atlas_texture.hpp> | ||
#include <godot_cpp/classes/font.hpp> | ||
#include <godot_cpp/classes/image_texture.hpp> | ||
|
||
#include <openvic-simulation/dataloader/Dataloader.hpp> | ||
#include <openvic-simulation/interface/GFX.hpp> | ||
|
||
namespace OpenVic { | ||
|
||
class AssetManager { | ||
struct image_asset_t { | ||
godot::Ref<godot::Image> image; | ||
godot::Ref<godot::ImageTexture> texture; | ||
}; | ||
using image_asset_map_t = std::map<godot::StringName, image_asset_t>; | ||
using font_map_t = std::map<godot::StringName, godot::Ref<godot::Font>>; | ||
|
||
Dataloader const& dataloader; | ||
image_asset_map_t image_assets; | ||
font_map_t fonts; | ||
|
||
image_asset_map_t::iterator _get_image_asset(godot::StringName path); | ||
|
||
public: | ||
AssetManager(Dataloader const& new_dataloader); | ||
|
||
godot::Ref<godot::Image> get_image(godot::StringName path); | ||
godot::Ref<godot::ImageTexture> get_texture(godot::StringName path); | ||
|
||
static godot::Rect2i get_frame_region(GFX::frame_t frame, GFX::frame_t frame_count, godot::Vector2i size); | ||
static godot::Ref<godot::AtlasTexture> make_atlas_texture(godot::Ref<godot::Texture2D> texture, godot::Rect2i region); | ||
godot::Ref<godot::AtlasTexture> get_atlas_texture(godot::StringName path, GFX::frame_t frame, GFX::frame_t frame_count); | ||
|
||
godot::Ref<godot::Texture2D> get_texture(godot::StringName path, GFX::frame_t frame, GFX::frame_t frame_count); | ||
|
||
godot::Ref<godot::Font> get_font(godot::StringName name); | ||
}; | ||
} |
Oops, something went wrong.