aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2024-04-15 01:13:46 +0200
committer hop311 <hop3114@gmail.com>2024-04-15 01:24:07 +0200
commit083365191bd5f586edd850e0bd6a0190de782ab1 (patch)
treec03b31ed54afbd0cba57b37e47a16fb560131b8f /src
parenta7f125a5f276e2951d1236fe88e32c5c08271504 (diff)
Rework GFX::Actor Attachments and Animations
Diffstat (limited to 'src')
-rw-r--r--src/openvic-simulation/dataloader/Dataloader.cpp3
-rw-r--r--src/openvic-simulation/interface/GFXObject.cpp104
-rw-r--r--src/openvic-simulation/interface/GFXObject.hpp42
-rw-r--r--src/openvic-simulation/interface/UI.cpp6
-rw-r--r--src/openvic-simulation/interface/UI.hpp5
-rw-r--r--src/openvic-simulation/types/IdentifierRegistry.hpp21
6 files changed, 127 insertions, 54 deletions
diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp
index e13bc23..f99417f 100644
--- a/src/openvic-simulation/dataloader/Dataloader.cpp
+++ b/src/openvic-simulation/dataloader/Dataloader.cpp
@@ -280,8 +280,7 @@ bool Dataloader::_load_interface_files(UIManager& ui_manager) const {
return ui_manager.load_gfx_file(parse_defines(file).get_file_node());
}
);
- ui_manager.lock_sprites();
- ui_manager.lock_fonts();
+ ui_manager.lock_gfx_registries();
/* Hard-coded GUI file names, might be replaced with a dynamic system but everything should still be loaded on startup. */
static const std::vector<std::string_view> gui_files {
diff --git a/src/openvic-simulation/interface/GFXObject.cpp b/src/openvic-simulation/interface/GFXObject.cpp
index d873db1..67abc4d 100644
--- a/src/openvic-simulation/interface/GFXObject.cpp
+++ b/src/openvic-simulation/interface/GFXObject.cpp
@@ -101,44 +101,92 @@ node_callback_t Object::expect_objects(length_callback_t length_callback, callba
);
}
-Actor::Attachment::Attachment() : node {}, attach_id { 0 } {}
+Actor::Attachment::Attachment(std::string_view new_actor_name, std::string_view new_attach_node, attach_id_t new_attach_id)
+ : actor_name { new_actor_name }, attach_node { new_attach_node }, attach_id { new_attach_id } {}
-bool Actor::Attachment::_fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) {
- bool ret = Named::_fill_key_map(key_map);
- ret &= add_key_map_entries(key_map,
- "node", ONE_EXACTLY, expect_string(assign_variable_callback_string(node)),
- "attachId", ONE_EXACTLY, expect_uint(assign_variable_callback(attach_id))
- );
- return ret;
-}
+Actor::Animation::Animation(std::string_view new_file, fixed_point_t new_scroll_time)
+ : file { new_file }, scroll_time { new_scroll_time } {}
-Actor::Animation::Animation() : file {}, default_time { 0 } {}
+Actor::Actor() : model_file {}, scale { 1 }, idle_animation {}, move_animation {}, attack_animation {} {}
-bool Actor::Animation::_fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) {
- bool ret = Named::_fill_key_map(key_map);
- ret &= add_key_map_entries(key_map,
- "file", ONE_EXACTLY, expect_string(assign_variable_callback_string(file)),
- "defaultAnimationTime", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(default_time))
- );
- return ret;
-}
+bool Actor::_set_animation(std::string_view name, std::string_view file, fixed_point_t scroll_time) {
+ std::optional<Animation>* animation = nullptr;
+
+ if (name == "idle") {
+ animation = &idle_animation;
+ } else if (name == "move") {
+ animation = &move_animation;
+ } else if (name == "attack") {
+ animation = &attack_animation;
+ } else {
+ Logger::error(
+ "Unknown animation type \"", name, "\" for actor ", get_name(), " (with file ", file, " and scroll time ",
+ scroll_time, ")"
+ );
+ return false;
+ }
+
+ if (animation->has_value()) {
+ Logger::error(
+ "Duplicate ", name, " animation for actor ", get_name(), ": ", file, " with scroll time ",
+ scroll_time, " (already set to ", (*animation)->file, " with scroll time ", (*animation)->scroll_time, ")"
+ );
+ return false;
+ }
-Actor::Actor() : model_file {}, idle_animation_file {}, move_animation_file {}, attack_animation_file {}, scale { 1 } {}
+ /* Don't set static/non-moving animation, to avoid needing an AnimationPlayer for the generated Godot Skeleton3D. */
+ static constexpr std::string_view null_animation = "static.xsm";
+ if (file.ends_with(null_animation)) {
+ return true;
+ }
+
+ animation->emplace(Animation { file, scroll_time });
+ return true;
+}
bool Actor::_fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) {
bool ret = Object::_fill_key_map(key_map);
+
ret &= add_key_map_entries(key_map,
"actorfile", ONE_EXACTLY, expect_string(assign_variable_callback_string(model_file)),
- "idle", ZERO_OR_ONE, expect_string(assign_variable_callback_string(idle_animation_file)),
- "move", ZERO_OR_ONE, expect_string(assign_variable_callback_string(move_animation_file)),
- "attack", ZERO_OR_ONE, expect_string(assign_variable_callback_string(attack_animation_file)),
"scale", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(scale)),
- "attach", ZERO_OR_MORE, Attachment::_expect_value<Attachment>([this](Attachment&& attachment) -> bool {
- return attachments.add_item(std::move(attachment));
- }),
- "animation", ZERO_OR_MORE, Animation::_expect_value<Animation>([this](Animation&& animation) -> bool {
- return animations.add_item(std::move(animation));
- })
+
+ "attach", ZERO_OR_MORE, [this](ast::NodeCPtr node) -> bool {
+ std::string_view actor_name {}, attach_node {};
+ Attachment::attach_id_t attach_id = 0;
+
+ if (!expect_dictionary_keys<StringMapCaseInsensitive>(
+ "name", ONE_EXACTLY, expect_string(assign_variable_callback(actor_name)),
+ "node", ONE_EXACTLY, expect_string(assign_variable_callback(attach_node)),
+ "attachId", ONE_EXACTLY, expect_uint(assign_variable_callback(attach_id))
+ )(node)) {
+ Logger::error("Failed to load attachment for actor ", get_name());
+ return false;
+ }
+
+ attachments.push_back({ actor_name, attach_node, attach_id });
+ return true;
+ },
+
+ "idle", ZERO_OR_ONE, expect_string(std::bind(&Actor::_set_animation, this, "idle", std::placeholders::_1, 0)),
+ "move", ZERO_OR_ONE, expect_string(std::bind(&Actor::_set_animation, this, "move", std::placeholders::_1, 0)),
+ "attack", ZERO_OR_ONE, expect_string(std::bind(&Actor::_set_animation, this, "attack", std::placeholders::_1, 0)),
+ "animation", ZERO_OR_MORE, [this](ast::NodeCPtr node) -> bool {
+ std::string_view name {}, file {};
+ fixed_point_t scroll_time = 0;
+
+ if (!expect_dictionary_keys<StringMapCaseInsensitive>(
+ "name", ONE_EXACTLY, expect_string(assign_variable_callback(name)),
+ "file", ONE_EXACTLY, expect_string(assign_variable_callback(file)),
+ "defaultAnimationTime", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(scroll_time))
+ )(node)) {
+ Logger::error("Failed to load animation for actor ", get_name());
+ return false;
+ }
+
+ return _set_animation(name, file, scroll_time);
+ }
);
+
return ret;
}
diff --git a/src/openvic-simulation/interface/GFXObject.hpp b/src/openvic-simulation/interface/GFXObject.hpp
index 840e4a9..db15096 100644
--- a/src/openvic-simulation/interface/GFXObject.hpp
+++ b/src/openvic-simulation/interface/GFXObject.hpp
@@ -24,48 +24,44 @@ namespace OpenVic::GFX {
friend std::unique_ptr<Actor> std::make_unique<Actor>();
public:
- class Attachment : public Named<> {
- friend class LoadBase;
+ class Attachment {
+ friend class Actor;
- private:
- std::string PROPERTY(node);
- int32_t PROPERTY(attach_id);
+ public:
+ using attach_id_t = uint32_t;
- protected:
- Attachment();
+ private:
+ std::string PROPERTY(actor_name);
+ std::string PROPERTY(attach_node);
+ attach_id_t PROPERTY(attach_id);
- bool _fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) override;
+ Attachment(std::string_view new_actor_name, std::string_view new_attach_node, attach_id_t new_attach_id);
public:
Attachment(Attachment&&) = default;
- virtual ~Attachment() = default;
};
- class Animation : public Named<> {
- friend class LoadBase;
+ class Animation {
+ friend class Actor;
std::string PROPERTY(file);
- fixed_point_t PROPERTY(default_time);
+ fixed_point_t PROPERTY(scroll_time);
- protected:
- Animation();
-
- bool _fill_key_map(NodeTools::case_insensitive_key_map_t& key_map) override;
+ Animation(std::string_view new_file, fixed_point_t new_scroll_time);
public:
Animation(Animation&&) = default;
- virtual ~Animation() = default;
};
private:
- std::string PROPERTY(model_file);
- std::string PROPERTY(idle_animation_file);
- std::string PROPERTY(move_animation_file);
- std::string PROPERTY(attack_animation_file);
fixed_point_t PROPERTY(scale);
+ std::string PROPERTY(model_file);
+ std::optional<Animation> PROPERTY(idle_animation);
+ std::optional<Animation> PROPERTY(move_animation);
+ std::optional<Animation> PROPERTY(attack_animation);
+ std::vector<Attachment> PROPERTY(attachments);
- NamedRegistry<Attachment> IDENTIFIER_REGISTRY(attachment);
- NamedRegistry<Animation> IDENTIFIER_REGISTRY(animation);
+ bool _set_animation(std::string_view name, std::string_view file, fixed_point_t scroll_time);
protected:
Actor();
diff --git a/src/openvic-simulation/interface/UI.cpp b/src/openvic-simulation/interface/UI.cpp
index 479948d..ea871de 100644
--- a/src/openvic-simulation/interface/UI.cpp
+++ b/src/openvic-simulation/interface/UI.cpp
@@ -55,6 +55,12 @@ NodeCallback auto UIManager::_load_fonts(std::string_view font_key) {
);
}
+void UIManager::lock_gfx_registries() {
+ lock_sprites();
+ lock_fonts();
+ lock_objects();
+}
+
bool UIManager::load_gfx_file(ast::NodeCPtr root) {
return expect_dictionary_keys(
"spriteTypes", ZERO_OR_ONE, Sprite::expect_sprites(
diff --git a/src/openvic-simulation/interface/UI.hpp b/src/openvic-simulation/interface/UI.hpp
index ada540a..9aec96c 100644
--- a/src/openvic-simulation/interface/UI.hpp
+++ b/src/openvic-simulation/interface/UI.hpp
@@ -7,10 +7,11 @@ namespace OpenVic {
class UIManager {
NamedInstanceRegistry<GFX::Sprite> IDENTIFIER_REGISTRY(sprite);
- NamedInstanceRegistry<GUI::Scene, UIManager const&> IDENTIFIER_REGISTRY(scene);
IdentifierRegistry<GFX::Font> IDENTIFIER_REGISTRY(font);
NamedInstanceRegistry<GFX::Object> IDENTIFIER_REGISTRY(object);
+ NamedInstanceRegistry<GUI::Scene, UIManager const&> IDENTIFIER_REGISTRY(scene);
+
bool _load_font(ast::NodeCPtr node);
NodeTools::NodeCallback auto _load_fonts(std::string_view font_key);
@@ -20,6 +21,8 @@ namespace OpenVic {
uint32_t height
);
+ void lock_gfx_registries();
+
bool load_gfx_file(ast::NodeCPtr root);
bool load_gui_file(std::string_view scene_name, ast::NodeCPtr root);
};
diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp
index c1fef9d..533273a 100644
--- a/src/openvic-simulation/types/IdentifierRegistry.hpp
+++ b/src/openvic-simulation/types/IdentifierRegistry.hpp
@@ -269,6 +269,23 @@ namespace OpenVic {
} \
return nullptr; \
} \
+ template<std::derived_from<external_value_type> T> \
+ requires requires(external_value_type const& value) { \
+ { value.get_type() } -> std::same_as<std::string_view>; \
+ { T::get_type_static() } -> std::same_as<std::string_view>; \
+ } \
+ constexpr T CONST* get_cast_item_by_identifier(std::string_view identifier) CONST { \
+ external_value_type CONST* item = get_item_by_identifier(identifier); \
+ if (item != nullptr) { \
+ if (item->get_type() == T::get_type_static()) { \
+ return reinterpret_cast<T CONST*>(item); \
+ } \
+ Logger::error( \
+ "Invalid type for item \"", identifier, "\": ", item->get_type(), " (expected ", T::get_type_static(), ")" \
+ ); \
+ } \
+ return nullptr; \
+ } \
constexpr external_value_type CONST* get_item_by_index(std::size_t index) CONST { \
if (index < items.size()) { \
return std::addressof(ValueInfo::get_external_value(ItemInfo::get_value(items[index]))); \
@@ -565,6 +582,10 @@ private:
constexpr decltype(registry)::external_value_type const_kw* get_##singular##_by_identifier(std::string_view identifier) const_kw { \
return registry.get_item_by_identifier(identifier); \
} \
+ template<std::derived_from<decltype(registry)::external_value_type> T> \
+ constexpr T const_kw* get_cast_##singular##_by_identifier(std::string_view identifier) const_kw { \
+ return registry.get_cast_item_by_identifier<T>(identifier); \
+ } \
constexpr decltype(registry)::external_value_type const_kw* get_##singular##_by_index(std::size_t index) const_kw { \
return index >= index_offset ? registry.get_item_by_index(index - index_offset) : nullptr; \
} \