diff --git a/src/anm2/animation.cpp b/src/anm2/animation.cpp index 7f43bc7..cbce76b 100644 --- a/src/anm2/animation.cpp +++ b/src/anm2/animation.cpp @@ -16,7 +16,7 @@ namespace anm2ed auto spritesheet = element_get(ElementType::SPRITESHEET, layer->spritesheetId); if (!spritesheet) return resolved; - auto region = element_child_id_get(*spritesheet, ElementType::REGION, frame.regionId); + auto region = child_id_get(*spritesheet, ElementType::REGION, frame.regionId); if (!region) return resolved; resolved.crop = region->crop; @@ -89,86 +89,34 @@ namespace anm2ed return {minX, minY, maxX - minX, maxY - minY}; } - Element* animation_container_get(Element& animation, ElementType type) + int Anm2::item_add(ItemType type, int animationIndex, const Element& item, int insertBeforeId, + types::destination::Type destination) { - if (auto container = child_first_get(animation, type)) return container; - animation.children.push_back(element_make(type)); - return &animation.children.back(); - } + auto row = track_container_get(type); + auto elements = row ? element_get(row->elements) : nullptr; + if (!elements) return -1; - int Anm2::layer_animation_add(int animationIndex, int id, int insertBeforeId, std::string name, int spritesheetId, - types::destination::Type destination) - { - auto layers = element_get(ElementType::LAYERS); - if (!layers) return -1; - - id = id == -1 ? element_child_next_id_get(*layers, ElementType::LAYER_ELEMENT) : id; - auto layer = element_child_id_get(*layers, ElementType::LAYER_ELEMENT, id); - if (!layer) + auto id = item.id == -1 ? element_child_next_id_get(*elements, row->element) : item.id; + auto element = child_id_get(*elements, row->element, id); + if (!element) { - layers->children.push_back(element_make(ElementType::LAYER_ELEMENT)); - layer = &layers->children.back(); - layer->id = id; + element = &elements->children.emplace_back(element_make(row->element)); + element->id = id; } - if (!name.empty()) layer->name = name; - layer->spritesheetId = element_get(ElementType::SPRITESHEET, spritesheetId) ? spritesheetId : 0; + if (!item.name.empty()) element->name = item.name; + element->spritesheetId = element_get(ElementType::SPRITESHEET, item.spritesheetId) ? item.spritesheetId : 0; + element->isShowRect = item.isShowRect; auto add = [&](Element& animation) { - if (animation_item_get(animation, ItemType::LAYER, id)) return; - auto layerAnimations = animation_container_get(animation, ElementType::LAYER_ANIMATIONS); - auto item = element_make(ElementType::LAYER_ANIMATION); - item.layerId = id; - - if (insertBeforeId != -1) - for (auto it = layerAnimations->children.begin(); it != layerAnimations->children.end(); ++it) - if (it->type == ElementType::LAYER_ANIMATION && it->layerId == insertBeforeId) - { - layerAnimations->children.insert(it, item); - return; - } - - layerAnimations->children.push_back(item); - }; - - if (destination == types::destination::ALL) - { - if (auto animations = element_get(ElementType::ANIMATIONS)) - for (auto& animation : animations->children) - if (animation.type == ElementType::ANIMATION) add(animation); - } - else if (auto animation = element_get(ElementType::ANIMATION, animationIndex)) - add(*animation); - - return id; - } - - int Anm2::null_animation_add(int animationIndex, int id, std::string name, bool isShowRect, - types::destination::Type destination) - { - auto nulls = element_get(ElementType::NULLS); - if (!nulls) return -1; - - id = id == -1 ? element_child_next_id_get(*nulls, ElementType::NULL_ELEMENT) : id; - auto null = element_child_id_get(*nulls, ElementType::NULL_ELEMENT, id); - if (!null) - { - nulls->children.push_back(element_make(ElementType::NULL_ELEMENT)); - null = &nulls->children.back(); - null->id = id; - } - - if (!name.empty()) null->name = name; - null->isShowRect = isShowRect; - - auto add = [&](Element& animation) - { - if (animation_item_get(animation, ItemType::NULL_, id)) return; - auto nullAnimations = animation_container_get(animation, ElementType::NULL_ANIMATIONS); - auto item = element_make(ElementType::NULL_ANIMATION); - item.nullId = id; - nullAnimations->children.push_back(item); + if (animation_item_get(animation, type, id)) return; + auto& tracks = child_ensure(animation, row->container); + auto track = element_make(row->track); + track.*(row->id) = id; + auto before = std::ranges::find_if(tracks.children, [&](const Element& child) + { return child.type == row->track && track_id_get(child) == insertBeforeId; }); + tracks.children.insert(insertBeforeId == -1 ? tracks.children.end() : before, track); }; if (destination == types::destination::ALL) diff --git a/src/anm2/anm2.cpp b/src/anm2/anm2.cpp index 0e47bc3..ca2f227 100644 --- a/src/anm2/anm2.cpp +++ b/src/anm2/anm2.cpp @@ -5,6 +5,8 @@ using namespace tinyxml2; namespace anm2ed { + constexpr const char* CREATED_ON_FORMAT = "%m/%d/%Y %I:%M:%S %p"; + int animations_count_get(const Element& animations) { int count{}; @@ -17,30 +19,14 @@ namespace anm2ed { int current{}; for (int i = 0; i < (int)animations.children.size(); ++i) - { - if (animations.children[i].type != ElementType::ANIMATION) continue; - if (current == animationIndex) return i; - ++current; - } + if (animations.children[i].type == ElementType::ANIMATION && current++ == animationIndex) return i; return -1; } int animations_child_insert_index_get(const Element& animations, int animationIndex) { - if (animationIndex <= 0) - { - for (int i = 0; i < (int)animations.children.size(); ++i) - if (animations.children[i].type == ElementType::ANIMATION) return i; - return (int)animations.children.size(); - } - int current{}; - for (int i = 0; i < (int)animations.children.size(); ++i) - { - if (animations.children[i].type != ElementType::ANIMATION) continue; - if (current == animationIndex) return i; - ++current; - } - return (int)animations.children.size(); + auto childIndex = animations_child_index_get(animations, std::max(animationIndex, 0)); + return childIndex == -1 ? (int)animations.children.size() : childIndex; } bool is_source_document_tag(std::string_view tag) { return tag == SOURCE_DOCUMENT_TAG; } @@ -89,14 +75,12 @@ namespace anm2ed root = element_make(ElementType::ANIMATED_ACTOR); auto info = element_make(ElementType::INFO); - info.createdOn = time::get("%m/%d/%Y %I:%M:%S %p"); + info.createdOn = time::get(CREATED_ON_FORMAT); auto content = element_make(ElementType::CONTENT); - content.children.push_back(element_make(ElementType::SPRITESHEETS)); - content.children.push_back(element_make(ElementType::SHADERS)); - content.children.push_back(element_make(ElementType::LAYERS)); - content.children.push_back(element_make(ElementType::NULLS)); - content.children.push_back(element_make(ElementType::EVENTS)); + for (auto type : {ElementType::SPRITESHEETS, ElementType::SHADERS, ElementType::LAYERS, ElementType::NULLS, + ElementType::EVENTS}) + content.children.push_back(element_make(type)); root.children.push_back(std::move(info)); root.children.push_back(std::move(content)); @@ -169,23 +153,19 @@ namespace anm2ed XMLElement* Anm2::to_element(XMLDocument& document, Options options) const { - if (options.isExtendedFormat) + auto serialize = [&](Flags flags) { - auto editor = normalized_for_serialize(SERIALIZE_ANM2ED_DEFAULT); - editor.region_frames_sync(true); - return element_to_xml(document, editor.root, ElementType::UNKNOWN, SERIALIZE_ANM2ED_DEFAULT); - } + auto normalized = normalized_for_serialize(flags); + normalized.region_frames_sync(true); + return element_to_xml(document, normalized.root, ElementType::UNKNOWN, flags); + }; - auto isaac = normalized_for_serialize(SERIALIZE_ISAAC_DEFAULT); - isaac.region_frames_sync(true); - auto out = element_to_xml(document, isaac.root, ElementType::UNKNOWN, SERIALIZE_ISAAC_DEFAULT); - - auto editor = normalized_for_serialize(SERIALIZE_ANM2ED_DEFAULT); - editor.region_frames_sync(true); - auto extension = element_to_xml(document, editor.root, ElementType::UNKNOWN, SERIALIZE_ANM2ED_DEFAULT); - extension->SetName(SOURCE_DOCUMENT_TAG.data()); - out->InsertEndChild(extension); + auto editor = serialize(SERIALIZE_ANM2ED_DEFAULT); + if (options.isExtendedFormat) return editor; + auto out = serialize(SERIALIZE_ISAAC_DEFAULT); + editor->SetName(SOURCE_DOCUMENT_TAG.data()); + out->InsertEndChild(editor); return out; } diff --git a/src/anm2/anm2.hpp b/src/anm2/anm2.hpp index b7d52fd..b74d9ec 100644 --- a/src/anm2/anm2.hpp +++ b/src/anm2/anm2.hpp @@ -6,7 +6,9 @@ #include #include #include +#include #include +#include #include #include @@ -16,49 +18,55 @@ #include "types.hpp" #define ANM2_ELEMENT_TYPES \ - X(UNKNOWN, "") \ - X(ANIMATED_ACTOR, "AnimatedActor") \ - X(INFO, "Info") \ - X(CONTENT, "Content") \ - X(SPRITESHEETS, "Spritesheets") \ - X(SPRITESHEET, "Spritesheet") \ - X(SHADERS, "Shaders") \ - X(SHADER, "Shader") \ - X(UNIFORM, "Uniform") \ - X(COMPONENT, "Component") \ - X(REGION, "Region") \ - X(LAYERS, "Layers") \ - X(LAYER_ELEMENT, "Layer") \ - X(NULLS, "Nulls") \ - X(NULL_ELEMENT, "Null") \ - X(EVENTS, "Events") \ - X(EVENT_ELEMENT, "Event") \ - X(SOUNDS, "Sounds") \ - X(SOUND_ELEMENT, "Sound") \ - X(ANIMATIONS, "Animations") \ - X(ANIMATION, "Animation") \ - X(ROOT_ANIMATION, "RootAnimation") \ - X(LAYER_ANIMATIONS, "LayerAnimations") \ - X(LAYER_ANIMATION_GROUPS, "LayerAnimationGroups") \ - X(LAYER_ANIMATION, "LayerAnimation") \ - X(NULL_ANIMATIONS, "NullAnimations") \ - X(NULL_ANIMATION_GROUPS, "NullAnimationGroups") \ - X(NULL_ANIMATION, "NullAnimation") \ - X(GROUP, "Group") \ - X(TRIGGERS, "Triggers") \ - X(FRAME, "Frame") \ - X(TRIGGER, "Trigger") + X(UNKNOWN, "", UNKNOWN) \ + X(ANIMATED_ACTOR, "AnimatedActor", UNKNOWN) \ + X(INFO, "Info", UNKNOWN) \ + X(CONTENT, "Content", UNKNOWN) \ + X(SPRITESHEETS, "Spritesheets", UNKNOWN) \ + X(SPRITESHEET, "Spritesheet", SPRITESHEETS) \ + X(SHADERS, "Shaders", UNKNOWN) \ + X(SHADER, "Shader", SHADERS) \ + X(UNIFORM, "Uniform", UNKNOWN) \ + X(COMPONENT, "Component", UNKNOWN) \ + X(REGION, "Region", UNKNOWN) \ + X(LAYERS, "Layers", UNKNOWN) \ + X(LAYER_ELEMENT, "Layer", LAYERS) \ + X(NULLS, "Nulls", UNKNOWN) \ + X(NULL_ELEMENT, "Null", NULLS) \ + X(EVENTS, "Events", UNKNOWN) \ + X(EVENT_ELEMENT, "Event", EVENTS) \ + X(SOUNDS, "Sounds", UNKNOWN) \ + X(SOUND_ELEMENT, "Sound", SOUNDS) \ + X(ANIMATIONS, "Animations", UNKNOWN) \ + X(ANIMATION, "Animation", ANIMATIONS) \ + X(ROOT_ANIMATION, "RootAnimation", UNKNOWN) \ + X(LAYER_ANIMATIONS, "LayerAnimations", UNKNOWN) \ + X(LAYER_ANIMATION_GROUPS, "LayerAnimationGroups", UNKNOWN) \ + X(LAYER_ANIMATION, "LayerAnimation", UNKNOWN) \ + X(NULL_ANIMATIONS, "NullAnimations", UNKNOWN) \ + X(NULL_ANIMATION_GROUPS, "NullAnimationGroups", UNKNOWN) \ + X(NULL_ANIMATION, "NullAnimation", UNKNOWN) \ + X(GROUP, "Group", UNKNOWN) \ + X(TRIGGERS, "Triggers", UNKNOWN) \ + X(FRAME, "Frame", UNKNOWN) \ + X(TRIGGER, "Trigger", UNKNOWN) namespace anm2ed { enum class ElementType { -#define X(symbol, tag) symbol, +#define X(symbol, tag, container) symbol, ANM2_ELEMENT_TYPES #undef X COUNT }; + inline constexpr ElementType ELEMENT_CONTAINERS[] = { +#define X(symbol, tag, container) ElementType::container, + ANM2_ELEMENT_TYPES +#undef X + }; + enum class Interpolation { NONE, @@ -127,47 +135,60 @@ namespace anm2ed inline const glm::vec4 TRIGGER_COLOR_HOVERED = glm::vec4(0.950f, 0.330f, 0.490f, 1.000f); #define ANM2_ITEM_TYPES \ - X(NONE, STRING_UNDEFINED, "", resource::icon::NONE, glm::vec4(), glm::vec4(), glm::vec4()) \ - X(ROOT, BASIC_ROOT, "RootAnimation", resource::icon::ROOT, ROOT_COLOR, ROOT_COLOR_ACTIVE, ROOT_COLOR_HOVERED) \ + X(NONE, STRING_UNDEFINED, "", resource::icon::NONE, glm::vec4(), glm::vec4(), glm::vec4(), UNKNOWN, UNKNOWN) \ + X(ROOT, BASIC_ROOT, "RootAnimation", resource::icon::ROOT, ROOT_COLOR, ROOT_COLOR_ACTIVE, ROOT_COLOR_HOVERED, \ + ROOT_ANIMATION, UNKNOWN) \ X(LAYER, BASIC_LAYER_ANIMATION, "LayerAnimation", resource::icon::LAYER, LAYER_COLOR, LAYER_COLOR_ACTIVE, \ - LAYER_COLOR_HOVERED) \ + LAYER_COLOR_HOVERED, LAYER_ANIMATION, LAYER_ANIMATIONS) \ X(NULL_, BASIC_NULL_ANIMATION, "NullAnimation", resource::icon::NULL_, NULL_COLOR, NULL_COLOR_ACTIVE, \ - NULL_COLOR_HOVERED) \ + NULL_COLOR_HOVERED, NULL_ANIMATION, NULL_ANIMATIONS) \ X(TRIGGER, BASIC_TRIGGERS, "Triggers", resource::icon::TRIGGERS, TRIGGER_COLOR, TRIGGER_COLOR_ACTIVE, \ - TRIGGER_COLOR_HOVERED) + TRIGGER_COLOR_HOVERED, TRIGGERS, UNKNOWN) constexpr StringType TYPE_STRINGS[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered) string, +#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) string, ANM2_ITEM_TYPES #undef X }; constexpr const char* TYPE_ITEM_STRINGS[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered) itemString, +#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) itemString, ANM2_ITEM_TYPES #undef X }; constexpr resource::icon::Type TYPE_ICONS[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered) icon, +#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) icon, ANM2_ITEM_TYPES #undef X }; inline const glm::vec4 TYPE_COLOR[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered) color, +#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) color, ANM2_ITEM_TYPES #undef X }; inline const glm::vec4 TYPE_COLOR_ACTIVE[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered) colorActive, +#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) colorActive, ANM2_ITEM_TYPES #undef X }; inline const glm::vec4 TYPE_COLOR_HOVERED[] = { -#define X(symbol, string, itemString, icon, color, colorActive, colorHovered) colorHovered, +#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) colorHovered, + ANM2_ITEM_TYPES +#undef X + }; + + inline constexpr ElementType TYPE_TRACKS[] = { +#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) ElementType::track, + ANM2_ITEM_TYPES +#undef X + }; + + inline constexpr ElementType TYPE_CONTAINERS[] = { +#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) ElementType::container, ANM2_ITEM_TYPES #undef X }; @@ -302,6 +323,166 @@ namespace anm2ed auto operator<=>(const Reference&) const = default; }; + struct TrackContainer + { + ItemType itemType; + ElementType container; + ElementType track; + ElementType groups; + ElementType elements; + ElementType element; + int Element::* id; + }; + + inline constexpr TrackContainer TRACK_CONTAINERS[] = { + {ItemType::LAYER, ElementType::LAYER_ANIMATIONS, ElementType::LAYER_ANIMATION, + ElementType::LAYER_ANIMATION_GROUPS, ElementType::LAYERS, ElementType::LAYER_ELEMENT, &Element::layerId}, + {ItemType::NULL_, ElementType::NULL_ANIMATIONS, ElementType::NULL_ANIMATION, ElementType::NULL_ANIMATION_GROUPS, + ElementType::NULLS, ElementType::NULL_ELEMENT, &Element::nullId}}; + + inline constexpr int GROUP_ANY = -2; + + template using ElementPointer = std::conditional_t, const Element*, Element*>; + + constexpr const TrackContainer* track_container_get(ElementType type) + { + for (const auto& row : TRACK_CONTAINERS) + if (type == row.container || type == row.track || type == row.groups || type == row.elements || + type == row.element) + return &row; + return nullptr; + } + + constexpr const TrackContainer* track_container_get(ItemType type) + { + for (const auto& row : TRACK_CONTAINERS) + if (type == row.itemType) return &row; + return nullptr; + } + + constexpr bool is_track(const Element& element) + { + return element.type != ElementType::UNKNOWN && std::ranges::contains(TYPE_TRACKS, element.type); + } + + constexpr ElementType track_frame_type_get(const Element& track) + { + return track.type == ElementType::TRIGGERS ? ElementType::TRIGGER : ElementType::FRAME; + } + + constexpr int track_id_get(const Element& track) + { + auto row = track_container_get(track.type); + return row && row->track == track.type ? track.*(row->id) : -1; + } + + template ElementPointer child_find(E& element, Predicate&& isMatch) + { + for (auto& child : element.children) + if (isMatch(child)) return &child; + return nullptr; + } + + template ElementPointer child_first_get(E& element, ElementType type) + { + return child_find(element, [&](const Element& child) { return child.type == type; }); + } + + template ElementPointer child_id_get(E& element, ElementType type, int id) + { + return child_find(element, [&](const Element& child) { return child.type == type && child.id == id; }); + } + + template ElementPointer shader_uniform_get(E& shader, std::string_view name) + { + return child_find(shader, + [&](const Element& child) { return child.type == ElementType::UNIFORM && child.name == name; }); + } + + template ElementPointer shader_uniform_component_get(E& uniform, int index) + { + return child_find( + uniform, [&](const Element& child) { return child.type == ElementType::COMPONENT && child.index == index; }); + } + + template ElementPointer element_first_get(E& element, ElementType type) + { + if (element.type == type) return &element; + for (auto& child : element.children) + if (auto result = element_first_get(child, type)) return result; + return nullptr; + } + + template ElementPointer track_find(E& parent, ElementType trackType, int id, int groupId = GROUP_ANY) + { + for (auto& child : parent.children) + { + if (child.type == trackType && track_id_get(child) == id && (groupId == GROUP_ANY || child.groupId == groupId)) + return &child; + if (child.type == ElementType::GROUP) + if (auto found = track_find(child, trackType, id, groupId)) return found; + } + return nullptr; + } + + template ElementPointer animation_group_get(E& animation, int groupType, int groupId) + { + auto container = groupId < 0 ? nullptr : child_first_get(animation, TYPE_CONTAINERS[groupType]); + return container ? child_id_get(*container, ElementType::GROUP, groupId) : nullptr; + } + + template + ElementPointer animation_item_get(E& animation, ItemType type, int id = -1, int groupType = NONE, + int groupId = -1) + { + auto trackType = TYPE_TRACKS[(int)type]; + auto isGrouped = groupType != NONE && groupId != -1; + if (type == ItemType::ROOT && isGrouped) + { + auto group = animation_group_get(animation, groupType, groupId); + return group ? child_first_get(*group, ElementType::ROOT_ANIMATION) : nullptr; + } + if (type == ItemType::ROOT || type == ItemType::TRIGGER) return child_first_get(animation, trackType); + + auto container = child_first_get(animation, TYPE_CONTAINERS[(int)type]); + return container ? track_find(*container, trackType, id, isGrouped ? groupId : GROUP_ANY) : nullptr; + } + + int track_frame_child_index_get(const Element&, int); + int track_frame_insert_child_index_get(const Element&, int); + + template ElementPointer track_frame_get(E& track, int index) + { + auto childIndex = track_frame_child_index_get(track, index); + return childIndex == -1 ? nullptr : &track.children[childIndex]; + } + + template void tracks_each(E& parent, ElementType trackType, Callback&& callback) + { + for (auto& child : parent.children) + if (child.type == trackType) + callback(child); + else if (child.type == ElementType::GROUP) + tracks_each(child, trackType, callback); + } + + template void animations_tracks_each(E& root, ElementType trackType, Callback&& callback) + { + auto animations = element_first_get(root, ElementType::ANIMATIONS); + auto row = track_container_get(trackType); + if (!animations || !row) return; + for (auto& animation : animations->children) + if (animation.type == ElementType::ANIMATION) + if (auto container = child_first_get(animation, row->container)) tracks_each(*container, trackType, callback); + } + + template void element_each(E& element, Callback&& callback) + { + callback(element); + for (auto& child : element.children) + element_each(child, callback); + } + Element element_make(ElementType); Element element_read(const tinyxml2::XMLElement*); std::string element_to_string(const Element&, Flags = SERIALIZE_EDITOR_DEFAULT); @@ -309,31 +490,13 @@ namespace anm2ed tinyxml2::XMLElement* element_to_xml(tinyxml2::XMLDocument&, const Element&, Flags = SERIALIZE_EDITOR_DEFAULT); tinyxml2::XMLElement* element_to_xml(tinyxml2::XMLDocument&, const Element&, ElementType, Flags = SERIALIZE_EDITOR_DEFAULT); - Element* element_child_first_get(Element&, ElementType); - const Element* element_child_first_get(const Element&, ElementType); - Element* shader_uniform_get(Element&, std::string_view, bool = false); - const Element* shader_uniform_get(const Element&, std::string_view); - Element* shader_uniform_component_get(Element&, int, bool = false); - const Element* shader_uniform_component_get(const Element&, int); - Element* element_child_id_get(Element&, ElementType, int); - const Element* element_child_id_get(const Element&, ElementType, int); + Element& child_ensure(Element&, ElementType); int element_child_next_id_get(const Element&, ElementType); int element_child_max_id_get(const Element&, ElementType); bool element_child_id_erase(Element&, ElementType, int); - Element* element_first_get(Element&, ElementType); - const Element* element_first_get(const Element&, ElementType); - Element* animation_item_get(Element&, ItemType, int = -1); - const Element* animation_item_get(const Element&, ItemType, int = -1); - Element* animation_item_get(Element&, ItemType, int, int, int); - const Element* animation_item_get(const Element&, ItemType, int, int, int); - Element* animation_group_get(Element&, int, int); - const Element* animation_group_get(const Element&, int, int); - Element* animation_group_root_get(Element&, int, int); - const Element* animation_group_root_get(const Element&, int, int); - Element* animation_group_root_ensure(Element&, int, int); - ElementType item_type_to_track_type_get(ItemType); - Element* track_frame_get(Element&, int); - const Element* track_frame_get(const Element&, int); + int track_frames_count_get(const Element&); + float interpolation_factor(Interpolation, float); + void frame_mix(Element&, const Element&, float); Element frame_generate(const Element&, float); int frame_index_from_at_frame_get(const Element&, int); int frame_index_from_time_get(const Element&, float); @@ -366,29 +529,16 @@ namespace anm2ed void region_frames_sync(bool); Anm2 normalized_for_serialize(Flags = SERIALIZE_DEFAULT) const; - Element* element_get(ElementType); - const Element* element_get(ElementType) const; - Element* element_get(ElementType, int); - const Element* element_get(ElementType, int) const; - Element* element_get(int, ItemType, int = -1); - const Element* element_get(int, ItemType, int = -1) const; - Element* element_get(int, ItemType, int, int, int); - const Element* element_get(int, ItemType, int, int, int) const; - Element* element_get(int, ItemType, int, int); - const Element* element_get(int, ItemType, int, int) const; - Element* element_get(Reference); - const Element* element_get(Reference) const; - std::set element_unused(ElementType) const; - std::set element_unused(ElementType, const Element&) const; - std::set element_unused(ElementType, int) const; - bool deserialize(ElementType, const std::string&, bool, std::string* = nullptr, const std::filesystem::path& = {}); - bool regions_deserialize(int, const std::string&, bool, std::string* = nullptr); + template ElementPointer element_get(this Self&, ElementType); + template ElementPointer element_get(this Self&, ElementType, int); + template ElementPointer element_get(this Self&, Reference); + std::set element_unused(ElementType, const Element* = nullptr) const; + std::set region_unused(int) const; + bool deserialize(ElementType, const std::string&, bool, std::string* = nullptr, const std::filesystem::path& = {}, + int = -1); Element frame_effective(int, const Element&) const; glm::vec4 animation_rect(const Element&, bool) const; - int layer_animation_add(int, int = -1, int = -1, std::string = {}, int = 0, - types::destination::Type = types::destination::ALL); - int null_animation_add(int, int = -1, std::string = {}, bool = false, - types::destination::Type = types::destination::ALL); + int item_add(ItemType, int, const Element&, int = -1, types::destination::Type = types::destination::ALL); bool animations_deserialize(const std::string&, int, std::set&, std::string* = nullptr, std::set* = nullptr); int animations_merge(int, std::set&, types::merge::Type = types::merge::APPEND, bool = true); diff --git a/src/anm2/element.cpp b/src/anm2/element.cpp index b6cedfe..61b9b05 100644 --- a/src/anm2/element.cpp +++ b/src/anm2/element.cpp @@ -6,47 +6,20 @@ using namespace tinyxml2; namespace anm2ed { constexpr std::array ELEMENT_TAGS = { -#define X(symbol, tag) std::string_view{tag}, +#define X(symbol, tag, container) std::string_view{tag}, ANM2_ELEMENT_TYPES #undef X }; - ElementType element_type_get(std::string_view tag) { - for (std::size_t i = 0; i < ELEMENT_TAGS.size(); ++i) - if (ELEMENT_TAGS[i] == tag) return (ElementType)i; - return ElementType::UNKNOWN; + auto it = std::ranges::find(ELEMENT_TAGS, tag); + return it == ELEMENT_TAGS.end() ? ElementType::UNKNOWN : (ElementType)(it - ELEMENT_TAGS.begin()); } std::string_view element_tag_get(ElementType type) { - auto index = (std::size_t)type; - if (index >= ELEMENT_TAGS.size()) return {}; - return ELEMENT_TAGS[index]; - } - - ElementType element_container_type_get(ElementType type) - { - switch (type) - { - case ElementType::SPRITESHEET: - return ElementType::SPRITESHEETS; - case ElementType::SHADER: - return ElementType::SHADERS; - case ElementType::LAYER_ELEMENT: - return ElementType::LAYERS; - case ElementType::NULL_ELEMENT: - return ElementType::NULLS; - case ElementType::EVENT_ELEMENT: - return ElementType::EVENTS; - case ElementType::SOUND_ELEMENT: - return ElementType::SOUNDS; - case ElementType::ANIMATION: - return ElementType::ANIMATIONS; - default: - return ElementType::UNKNOWN; - } + return (std::size_t)type < ELEMENT_TAGS.size() ? ELEMENT_TAGS[(std::size_t)type] : std::string_view{}; } Element element_make(ElementType type) @@ -64,82 +37,29 @@ namespace anm2ed return root; } - Element* child_first_get(Element& element, ElementType type) + Element& child_ensure(Element& element, ElementType type) { + if (auto child = child_first_get(element, type)) return *child; + return element.children.emplace_back(element_make(type)); + } + + Element* content_container_get(Element& root, ElementType type) + { + auto content = child_first_get(root, ElementType::CONTENT); + return content ? child_first_get(*content, type) : nullptr; + } + + std::unordered_map child_ids_compact(Element& element, ElementType type) + { + std::unordered_map remap{}; + int nextId{}; for (auto& child : element.children) - if (child.type == type) return &child; - return nullptr; - } - - const Element* child_first_get(const Element& element, ElementType type) - { - for (const auto& child : element.children) - if (child.type == type) return &child; - return nullptr; - } - - Element* element_child_first_get(Element& element, ElementType type) { return child_first_get(element, type); } - - const Element* element_child_first_get(const Element& element, ElementType type) - { - return child_first_get(element, type); - } - - Element* shader_uniform_get(Element& shader, std::string_view name, bool isCreate) - { - for (auto& child : shader.children) - if (child.type == ElementType::UNIFORM && child.name == name) return &child; - if (!isCreate) return nullptr; - - shader.children.push_back(element_make(ElementType::UNIFORM)); - shader.children.back().name = std::string(name); - return &shader.children.back(); - } - - const Element* shader_uniform_get(const Element& shader, std::string_view name) - { - for (const auto& child : shader.children) - if (child.type == ElementType::UNIFORM && child.name == name) return &child; - return nullptr; - } - - Element* shader_uniform_component_get(Element& uniform, int index, bool isCreate) - { - for (auto& child : uniform.children) - if (child.type == ElementType::COMPONENT && child.index == index) return &child; - if (!isCreate) return nullptr; - - uniform.children.push_back(element_make(ElementType::COMPONENT)); - uniform.children.back().index = index; - return &uniform.children.back(); - } - - const Element* shader_uniform_component_get(const Element& uniform, int index) - { - for (const auto& child : uniform.children) - if (child.type == ElementType::COMPONENT && child.index == index) return &child; - return nullptr; - } - - Element* child_id_get(Element& element, ElementType type, int id) - { - for (auto& child : element.children) - if (child.type == type && child.id == id) return &child; - return nullptr; - } - - const Element* child_id_get(const Element& element, ElementType type, int id) - { - for (const auto& child : element.children) - if (child.type == type && child.id == id) return &child; - return nullptr; - } - - Element* element_child_id_get(Element& element, ElementType type, int id) { return child_id_get(element, type, id); } - - const Element* element_child_id_get(const Element& element, ElementType type, int id) - { - return child_id_get(element, type, id); + if (child.type == type) + { + remap[child.id] = nextId; + child.id = nextId++; + } + return remap; } int element_child_max_id_get(const Element& element, ElementType type) @@ -157,29 +77,7 @@ namespace anm2ed bool element_child_id_erase(Element& element, ElementType type, int id) { - for (auto it = element.children.begin(); it != element.children.end(); ++it) - { - if (it->type != type || it->id != id) continue; - element.children.erase(it); - return true; - } - return false; - } - - Element* element_first_get(Element& element, ElementType type) - { - if (element.type == type) return &element; - for (auto& child : element.children) - if (auto result = element_first_get(child, type); result) return result; - return nullptr; - } - - const Element* element_first_get(const Element& element, ElementType type) - { - if (element.type == type) return &element; - for (const auto& child : element.children) - if (auto result = element_first_get(child, type); result) return result; - return nullptr; + return std::erase_if(element.children, [&](const Element& child) { return child.type == type && child.id == id; }); } float interpolation_factor(Interpolation interpolation, float value) @@ -193,12 +91,6 @@ namespace anm2ed return 0.0f; } - bool is_track(const Element& element) - { - return element.type == ElementType::ROOT_ANIMATION || element.type == ElementType::LAYER_ANIMATION || - element.type == ElementType::NULL_ANIMATION || element.type == ElementType::TRIGGERS; - } - bool is_track_child_valid(ElementType parentType, ElementType childType) { if (parentType == ElementType::CONTENT && childType == ElementType::SHADERS) return true; @@ -218,157 +110,10 @@ namespace anm2ed return true; } - ElementType track_frame_type_get(const Element& track) - { - return track.type == ElementType::TRIGGERS ? ElementType::TRIGGER : ElementType::FRAME; - } - - ElementType item_type_to_track_type_get(ItemType type) - { - if (type == ItemType::ROOT) return ElementType::ROOT_ANIMATION; - if (type == ItemType::LAYER) return ElementType::LAYER_ANIMATION; - if (type == ItemType::NULL_) return ElementType::NULL_ANIMATION; - if (type == ItemType::TRIGGER) return ElementType::TRIGGERS; - return ElementType::UNKNOWN; - } - - ElementType item_type_to_container_type_get(ItemType type) - { - if (type == ItemType::LAYER) return ElementType::LAYER_ANIMATIONS; - if (type == ItemType::NULL_) return ElementType::NULL_ANIMATIONS; - return ElementType::UNKNOWN; - } - - Element* animation_group_get(Element& animation, int groupType, int groupId) - { - if (groupId < 0) return nullptr; - auto containerType = item_type_to_container_type_get(static_cast(groupType)); - auto container = child_first_get(animation, containerType); - return container ? child_id_get(*container, ElementType::GROUP, groupId) : nullptr; - } - - const Element* animation_group_get(const Element& animation, int groupType, int groupId) - { - if (groupId < 0) return nullptr; - auto containerType = item_type_to_container_type_get(static_cast(groupType)); - auto container = child_first_get(animation, containerType); - return container ? child_id_get(*container, ElementType::GROUP, groupId) : nullptr; - } - - Element* animation_group_root_get(Element& animation, int groupType, int groupId) - { - auto group = animation_group_get(animation, groupType, groupId); - return group ? child_first_get(*group, ElementType::ROOT_ANIMATION) : nullptr; - } - - const Element* animation_group_root_get(const Element& animation, int groupType, int groupId) - { - auto group = animation_group_get(animation, groupType, groupId); - return group ? child_first_get(*group, ElementType::ROOT_ANIMATION) : nullptr; - } - - Element* animation_group_root_ensure(Element& animation, int groupType, int groupId) - { - auto group = animation_group_get(animation, groupType, groupId); - if (!group) return nullptr; - if (auto root = child_first_get(*group, ElementType::ROOT_ANIMATION)) return root; - group->children.push_back(root_animation_make()); - return &group->children.back(); - } - - int track_id_get(const Element& track) - { - if (track.type == ElementType::LAYER_ANIMATION) return track.layerId; - if (track.type == ElementType::NULL_ANIMATION) return track.nullId; - return -1; - } - - Element* track_find(Element& parent, ElementType trackType, int id) - { - for (auto& child : parent.children) - { - if (child.type == trackType && track_id_get(child) == id) return &child; - if (child.type == ElementType::GROUP) - if (auto found = track_find(child, trackType, id)) return found; - } - return nullptr; - } - - const Element* track_find(const Element& parent, ElementType trackType, int id) - { - for (const auto& child : parent.children) - { - if (child.type == trackType && track_id_get(child) == id) return &child; - if (child.type == ElementType::GROUP) - if (auto found = track_find(child, trackType, id)) return found; - } - return nullptr; - } - - Element* track_group_find(Element& parent, ElementType trackType, int id, int groupId) - { - for (auto& child : parent.children) - { - if (child.type == trackType && track_id_get(child) == id && child.groupId == groupId) return &child; - if (child.type == ElementType::GROUP) - if (auto found = track_group_find(child, trackType, id, groupId)) return found; - } - return nullptr; - } - - const Element* track_group_find(const Element& parent, ElementType trackType, int id, int groupId) - { - for (const auto& child : parent.children) - { - if (child.type == trackType && track_id_get(child) == id && child.groupId == groupId) return &child; - if (child.type == ElementType::GROUP) - if (auto found = track_group_find(child, trackType, id, groupId)) return found; - } - return nullptr; - } - bool is_track_group_visible(const Element& container, const Element& track) { - if (track.groupId == -1) return true; - for (const auto& child : container.children) - if (child.type == ElementType::GROUP && child.id == track.groupId) return child.isVisible; - return true; - } - - Element* animation_item_get(Element& animation, ItemType type, int id, int groupType, int groupId) - { - auto trackType = item_type_to_track_type_get(type); - if (type == ItemType::ROOT && groupType != NONE && groupId != -1) - return animation_group_root_get(animation, groupType, groupId); - if (type == ItemType::ROOT || type == ItemType::TRIGGER) return child_first_get(animation, trackType); - - auto container = child_first_get(animation, item_type_to_container_type_get(type)); - if (!container) return nullptr; - return groupType != NONE && groupId != -1 ? track_group_find(*container, trackType, id, groupId) - : track_find(*container, trackType, id); - } - - Element* animation_item_get(Element& animation, ItemType type, int id) - { - return animation_item_get(animation, type, id, NONE, -1); - } - - const Element* animation_item_get(const Element& animation, ItemType type, int id, int groupType, int groupId) - { - auto trackType = item_type_to_track_type_get(type); - if (type == ItemType::ROOT && groupType != NONE && groupId != -1) - return animation_group_root_get(animation, groupType, groupId); - if (type == ItemType::ROOT || type == ItemType::TRIGGER) return child_first_get(animation, trackType); - - auto container = child_first_get(animation, item_type_to_container_type_get(type)); - if (!container) return nullptr; - return groupType != NONE && groupId != -1 ? track_group_find(*container, trackType, id, groupId) - : track_find(*container, trackType, id); - } - - const Element* animation_item_get(const Element& animation, ItemType type, int id) - { - return animation_item_get(animation, type, id, NONE, -1); + auto group = track.groupId == -1 ? nullptr : child_id_get(container, ElementType::GROUP, track.groupId); + return !group || group->isVisible; } int track_frame_child_index_get(const Element& track, int index) @@ -377,307 +122,131 @@ namespace anm2ed auto frameType = track_frame_type_get(track); int frameIndex{}; for (int i = 0; i < (int)track.children.size(); ++i) - { - if (track.children[i].type != frameType) continue; - if (frameIndex == index) return i; - ++frameIndex; - } + if (track.children[i].type == frameType && frameIndex++ == index) return i; return -1; } + int track_frame_insert_child_index_get(const Element& track, int index) + { + auto childIndex = track_frame_child_index_get(track, std::max(0, index)); + return childIndex == -1 ? (int)track.children.size() : childIndex; + } + int track_frames_count_get(const Element& track) { auto frameType = track_frame_type_get(track); - int count{}; - for (const auto& child : track.children) - if (child.type == frameType) ++count; - return count; + return (int)std::ranges::count_if(track.children, [&](const Element& child) { return child.type == frameType; }); } - int track_frame_insert_child_index_get(const Element& track, int index) + template ElementPointer Anm2::element_get(this Self& self, ElementType type) { - auto targetFrameIndex = std::max(0, index); - auto frameType = track_frame_type_get(track); - int frameIndex{}; - for (int i = 0; i < (int)track.children.size(); ++i) - { - if (track.children[i].type != frameType) continue; - if (frameIndex == targetFrameIndex) return i; - ++frameIndex; - } - return (int)track.children.size(); - } + if (type == ElementType::ANIMATED_ACTOR) return &self.root; + if (type == ElementType::ANIMATIONS) return element_first_get(self.root, type); - Element* track_frame_get(Element& track, int index) - { - auto childIndex = track_frame_child_index_get(track, index); - return childIndex == -1 ? nullptr : &track.children[childIndex]; - } - - const Element* track_frame_get(const Element& track, int index) - { - auto childIndex = track_frame_child_index_get(track, index); - return childIndex == -1 ? nullptr : &track.children[childIndex]; - } - - Element* Anm2::element_get(ElementType type) - { - if (type == ElementType::ANIMATED_ACTOR) return &root; - if (type == ElementType::ANIMATIONS) return element_first_get(root, type); - - auto content = child_first_get(root, ElementType::CONTENT); + auto content = child_first_get(self.root, ElementType::CONTENT); if (!content) return nullptr; if (type == ElementType::CONTENT) return content; if (auto container = child_first_get(*content, type)) return container; + if constexpr (!std::is_const_v) + if (type != ElementType::UNKNOWN && std::ranges::contains(ELEMENT_CONTAINERS, type)) + return &content->children.emplace_back(element_make(type)); + return element_first_get(self.root, type); + } - switch (type) + template ElementPointer Anm2::element_get(this Self& self, ElementType type, int id) + { + if (type != ElementType::ANIMATION) { - case ElementType::SPRITESHEETS: - case ElementType::SHADERS: - case ElementType::LAYERS: - case ElementType::NULLS: - case ElementType::EVENTS: - case ElementType::SOUNDS: - content->children.push_back(element_make(type)); - return &content->children.back(); - default: - break; + auto container = self.element_get(ELEMENT_CONTAINERS[(int)type]); + return container ? child_id_get(*container, type, id) : nullptr; } - return element_first_get(root, type); + auto animations = self.element_get(ElementType::ANIMATIONS); + auto childIndex = animations ? animations_child_index_get(*animations, id) : -1; + return childIndex == -1 ? nullptr : &animations->children[childIndex]; } - const Element* Anm2::element_get(ElementType type) const + template ElementPointer Anm2::element_get(this Self& self, Reference reference) { - if (type == ElementType::ANIMATED_ACTOR) return &root; - if (type == ElementType::ANIMATIONS) return element_first_get(root, type); - - auto content = child_first_get(root, ElementType::CONTENT); - if (!content) return nullptr; - if (type == ElementType::CONTENT) return content; - if (auto container = child_first_get(*content, type)) return container; - return element_first_get(root, type); + auto animation = self.element_get(ElementType::ANIMATION, reference.animationIndex); + auto item = animation ? animation_item_get(*animation, (ItemType)reference.itemType, reference.itemID, + reference.groupType, reference.groupId) + : nullptr; + if (reference.frameIndex < 0 || !item) return item; + return track_frame_get(*item, reference.frameIndex); } - Element* Anm2::element_get(ElementType type, int id) +#define X(Self) \ + template ElementPointer Anm2::element_get(this Self&, ElementType); \ + template ElementPointer Anm2::element_get(this Self&, ElementType, int); \ + template ElementPointer Anm2::element_get(this Self&, Reference); + X(Anm2) + X(const Anm2) +#undef X + + std::set children_unused_get(const Element* element, ElementType type, const std::set& used) { - if (type == ElementType::ANIMATION) - { - auto animations = element_get(ElementType::ANIMATIONS); - if (!animations || id < 0) return nullptr; - int current{}; - for (auto& animation : animations->children) - { - if (animation.type != ElementType::ANIMATION) continue; - if (current == id) return &animation; - ++current; - } - return nullptr; - } - - auto container = element_get(element_container_type_get(type)); - return container ? child_id_get(*container, type, id) : nullptr; + std::set unused{}; + if (element) + for (const auto& child : element->children) + if (child.type == type && !used.contains(child.id)) unused.insert(child.id); + return unused; } - const Element* Anm2::element_get(ElementType type, int id) const - { - if (type == ElementType::ANIMATION) - { - auto animations = element_get(ElementType::ANIMATIONS); - if (!animations || id < 0) return nullptr; - int current{}; - for (const auto& animation : animations->children) - { - if (animation.type != ElementType::ANIMATION) continue; - if (current == id) return &animation; - ++current; - } - return nullptr; - } - - auto container = element_get(element_container_type_get(type)); - return container ? child_id_get(*container, type, id) : nullptr; - } - - Element* Anm2::element_get(int animationIndex, ItemType type, int id) - { - auto animation = element_get(ElementType::ANIMATION, animationIndex); - return animation ? animation_item_get(*animation, type, id) : nullptr; - } - - const Element* Anm2::element_get(int animationIndex, ItemType type, int id) const - { - auto animation = element_get(ElementType::ANIMATION, animationIndex); - return animation ? animation_item_get(*animation, type, id) : nullptr; - } - - Element* Anm2::element_get(int animationIndex, ItemType type, int id, int groupType, int groupId) - { - auto animation = element_get(ElementType::ANIMATION, animationIndex); - return animation ? animation_item_get(*animation, type, id, groupType, groupId) : nullptr; - } - - const Element* Anm2::element_get(int animationIndex, ItemType type, int id, int groupType, int groupId) const - { - auto animation = element_get(ElementType::ANIMATION, animationIndex); - return animation ? animation_item_get(*animation, type, id, groupType, groupId) : nullptr; - } - - Element* Anm2::element_get(int animationIndex, ItemType type, int frameIndex, int id) - { - auto item = element_get(animationIndex, type, id); - return item ? track_frame_get(*item, frameIndex) : nullptr; - } - - const Element* Anm2::element_get(int animationIndex, ItemType type, int frameIndex, int id) const - { - auto item = element_get(animationIndex, type, id); - return item ? track_frame_get(*item, frameIndex) : nullptr; - } - - Element* Anm2::element_get(Reference reference) - { - auto itemType = static_cast(reference.itemType); - auto item = - element_get(reference.animationIndex, itemType, reference.itemID, reference.groupType, reference.groupId); - if (reference.frameIndex < 0) return item; - return item ? track_frame_get(*item, reference.frameIndex) : nullptr; - } - - const Element* Anm2::element_get(Reference reference) const - { - auto itemType = static_cast(reference.itemType); - auto item = - element_get(reference.animationIndex, itemType, reference.itemID, reference.groupType, reference.groupId); - if (reference.frameIndex < 0) return item; - return item ? track_frame_get(*item, reference.frameIndex) : nullptr; - } - - std::set Anm2::element_unused(ElementType type) const + std::set Anm2::element_unused(ElementType type, const Element* animation) const { std::set used{}; + auto row = track_container_get(type); + auto used_insert = [&](const Element& animation) + { + if (row) + if (auto tracks = child_first_get(animation, row->container)) + tracks_each(*tracks, row->track, [&](const Element& track) { used.insert(track.*(row->id)); }); + + if (auto triggers = child_first_get(animation, ElementType::TRIGGERS)) + for (const auto& trigger : triggers->children) + if (trigger.type == ElementType::TRIGGER) + { + if (type == ElementType::EVENT_ELEMENT) used.insert(trigger.eventId); + if (type == ElementType::SOUND_ELEMENT) used.insert(trigger.soundIds.begin(), trigger.soundIds.end()); + } + + if (type == ElementType::SHADER) + if (auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS)) + tracks_each(*layerAnimations, ElementType::LAYER_ANIMATION, + [&](const Element& track) + { + for (const auto& frame : track.children) + if (frame.type == ElementType::FRAME) used.insert(frame.shaderId); + }); + }; if (type == ElementType::SPRITESHEET) { if (auto layers = element_get(ElementType::LAYERS)) for (const auto& layer : layers->children) - if (layer.type == ElementType::LAYER_ELEMENT && layer.spritesheetId != -1) used.insert(layer.spritesheetId); + if (layer.type == ElementType::LAYER_ELEMENT) used.insert(layer.spritesheetId); } + else if (animation) + used_insert(*animation); else if (auto animations = element_get(ElementType::ANIMATIONS)) - for (const auto& animation : animations->children) - { - if (animation.type != ElementType::ANIMATION) continue; - if (type == ElementType::EVENT_ELEMENT) - { - auto triggers = child_first_get(animation, ElementType::TRIGGERS); - if (!triggers) continue; - for (const auto& trigger : triggers->children) - if (trigger.type == ElementType::TRIGGER && trigger.eventId != -1) used.insert(trigger.eventId); - } - else if (type == ElementType::SOUND_ELEMENT) - { - auto triggers = child_first_get(animation, ElementType::TRIGGERS); - if (!triggers) continue; - for (const auto& trigger : triggers->children) - { - if (trigger.type != ElementType::TRIGGER) continue; - for (auto id : trigger.soundIds) - used.insert(id); - } - } - else if (type == ElementType::SHADER) - { - auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS); - if (!layerAnimations) continue; - tracks_each(*layerAnimations, ElementType::LAYER_ANIMATION, - [&](const Element& layerAnimation) - { - for (const auto& frame : layerAnimation.children) - if (frame.type == ElementType::FRAME && frame.shaderId != -1) used.insert(frame.shaderId); - }); - } - else if (type == ElementType::LAYER_ELEMENT || type == ElementType::NULL_ELEMENT) - { - auto containerType = - type == ElementType::LAYER_ELEMENT ? ElementType::LAYER_ANIMATIONS : ElementType::NULL_ANIMATIONS; - auto tracks = child_first_get(animation, containerType); - if (!tracks) continue; - auto trackType = - type == ElementType::LAYER_ELEMENT ? ElementType::LAYER_ANIMATION : ElementType::NULL_ANIMATION; - tracks_each(*tracks, trackType, - [&](const Element& track) - { - if (track.type == ElementType::LAYER_ANIMATION) - used.insert(track.layerId); - else if (track.type == ElementType::NULL_ANIMATION) - used.insert(track.nullId); - }); - } - } + for (const auto& child : animations->children) + if (child.type == ElementType::ANIMATION) used_insert(child); - std::set unused{}; - if (auto container = element_get(element_container_type_get(type))) - for (const auto& element : container->children) - if (element.type == type && !used.contains(element.id)) unused.insert(element.id); - return unused; + return children_unused_get(element_get(ELEMENT_CONTAINERS[(int)type]), type, used); } - std::set Anm2::element_unused(ElementType type, const Element& animation) const + std::set Anm2::region_unused(int spritesheetId) const { - if (type != ElementType::LAYER_ELEMENT && type != ElementType::NULL_ELEMENT) return {}; - std::set used{}; - auto containerType = - type == ElementType::LAYER_ELEMENT ? ElementType::LAYER_ANIMATIONS : ElementType::NULL_ANIMATIONS; - if (auto tracks = child_first_get(animation, containerType)) - { - auto trackType = type == ElementType::LAYER_ELEMENT ? ElementType::LAYER_ANIMATION : ElementType::NULL_ANIMATION; - tracks_each(*tracks, trackType, - [&](const Element& track) - { - if (track.type == ElementType::LAYER_ANIMATION) - used.insert(track.layerId); - else if (track.type == ElementType::NULL_ANIMATION) - used.insert(track.nullId); - }); - } - - std::set unused{}; - if (auto container = element_get(element_container_type_get(type))) - for (const auto& element : container->children) - if (element.type == type && !used.contains(element.id)) unused.insert(element.id); - return unused; - } - - std::set Anm2::element_unused(ElementType type, int parentId) const - { - if (type != ElementType::REGION) return {}; - - std::set used{}; - auto animations = element_first_get(root, ElementType::ANIMATIONS); - if (animations) - for (const auto& animation : animations->children) - { - if (animation.type != ElementType::ANIMATION) continue; - auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS); - if (!layerAnimations) continue; - tracks_each(*layerAnimations, ElementType::LAYER_ANIMATION, - [&](const Element& layerAnimation) - { - auto layer = element_get(ElementType::LAYER_ELEMENT, layerAnimation.layerId); - if (!layer || layer->spritesheetId != parentId) return; - for (const auto& frame : layerAnimation.children) - { - if (frame.type != ElementType::FRAME) continue; - if (frame.regionId != -1) used.insert(frame.regionId); - } - }); - } - - std::set unused{}; - if (auto spritesheet = element_get(ElementType::SPRITESHEET, parentId)) - for (const auto& child : spritesheet->children) - if (child.type == type && !used.contains(child.id)) unused.insert(child.id); - return unused; + animations_tracks_each(root, ElementType::LAYER_ANIMATION, + [&](const Element& track) + { + auto layer = element_get(ElementType::LAYER_ELEMENT, track.layerId); + if (!layer || layer->spritesheetId != spritesheetId) return; + for (const auto& frame : track.children) + if (frame.type == ElementType::FRAME) used.insert(frame.regionId); + }); + return children_unused_get(element_get(ElementType::SPRITESHEET, spritesheetId), ElementType::REGION, used); } } diff --git a/src/anm2/frames.cpp b/src/anm2/frames.cpp index 6b48d64..0ac99bc 100644 --- a/src/anm2/frames.cpp +++ b/src/anm2/frames.cpp @@ -12,6 +12,35 @@ namespace anm2ed }; + void frame_mix(Element& frame, const Element& next, float amount) + { + frame.rotation = glm::mix(frame.rotation, next.rotation, amount); + frame.position = glm::mix(frame.position, next.position, amount); + frame.scale = glm::mix(frame.scale, next.scale, amount); + frame.shear = glm::mix(frame.shear, next.shear, amount); + frame.colorOffset = glm::mix(frame.colorOffset, next.colorOffset, amount); + frame.tint = glm::mix(frame.tint, next.tint, amount); + } + + std::vector frame_bake_split(const Element& original, const Element& next, int interval, bool isRoundScale, + bool isRoundRotation) + { + auto total = std::max(original.duration, FRAME_DURATION_MIN); + interval = std::max(interval, FRAME_DURATION_MIN); + std::vector frames{}; + frames.reserve((total + interval - 1) / interval); + for (int duration = 0; duration < total; duration += interval) + { + auto& baked = frames.emplace_back(original); + frame_mix(baked, next, interpolation_factor(original.interpolation, (float)duration / total)); + baked.duration = std::min(interval, total - duration); + baked.interpolation = Interpolation::NONE; + if (isRoundScale) baked.scale = glm::round(baked.scale); + if (isRoundRotation) baked.rotation = std::round(baked.rotation); + } + return frames; + } + Element frame_generate(const Element& track, float time) { auto frame = element_make(track_frame_type_get(track)); @@ -60,14 +89,8 @@ namespace anm2ed if (frameType != ElementType::TRIGGER && frame.interpolation != Interpolation::NONE && frameNext && frame.duration > 1) { - auto amount = - interpolation_factor(frame.interpolation, (time - durationCurrent) / (durationNext - durationCurrent)); - frame.rotation = glm::mix(frame.rotation, frameNext->rotation, amount); - frame.position = glm::mix(frame.position, frameNext->position, amount); - frame.scale = glm::mix(frame.scale, frameNext->scale, amount); - frame.shear = glm::mix(frame.shear, frameNext->shear, amount); - frame.colorOffset = glm::mix(frame.colorOffset, frameNext->colorOffset, amount); - frame.tint = glm::mix(frame.tint, frameNext->tint, amount); + frame_mix(frame, *frameNext, + interpolation_factor(frame.interpolation, (time - durationCurrent) / (durationNext - durationCurrent))); } return frame; @@ -90,9 +113,7 @@ namespace anm2ed if (track.type == ElementType::TRIGGERS) return frame_index_from_at_frame_get(track, (int)time); auto frameType = track_frame_type_get(track); - int frameCount{}; - for (const auto& frame : track.children) - if (frame.type == frameType) ++frameCount; + auto frameCount = track_frames_count_get(track); if (frameCount == 0) return -1; if (time <= 0.0f) return 0; @@ -130,45 +151,18 @@ namespace anm2ed auto childIndex = track_frame_child_index_get(track, index); if (childIndex == -1) return; - auto frame = &track.children[childIndex]; - - auto original = *frame; + auto original = track.children[childIndex]; if (original.duration <= FRAME_DURATION_MIN) { - frame->interpolation = Interpolation::NONE; + track.children[childIndex].interpolation = Interpolation::NONE; return; } auto nextFrame = track_frame_get(track, index + 1); - auto next = nextFrame ? *nextFrame : original; - int duration{}; - interval = std::max(interval, FRAME_DURATION_MIN); - std::vector bakedFrames{}; - bakedFrames.reserve((original.duration + interval - 1) / interval); - - while (duration < original.duration) - { - auto baked = original; - auto amount = interpolation_factor(original.interpolation, (float)duration / original.duration); - baked.duration = std::min(interval, original.duration - duration); - baked.interpolation = Interpolation::NONE; - baked.rotation = glm::mix(original.rotation, next.rotation, amount); - baked.position = glm::mix(original.position, next.position, amount); - baked.scale = glm::mix(original.scale, next.scale, amount); - baked.shear = glm::mix(original.shear, next.shear, amount); - baked.colorOffset = glm::mix(original.colorOffset, next.colorOffset, amount); - baked.tint = glm::mix(original.tint, next.tint, amount); - if (isRoundScale) baked.scale = glm::round(baked.scale); - if (isRoundRotation) baked.rotation = std::round(baked.rotation); - - bakedFrames.push_back(baked); - duration += baked.duration; - } - - if (bakedFrames.empty()) return; - track.children[childIndex] = std::move(bakedFrames.front()); - track.children.insert(track.children.begin() + childIndex + 1, std::make_move_iterator(bakedFrames.begin() + 1), - std::make_move_iterator(bakedFrames.end())); + auto baked = frame_bake_split(original, nextFrame ? *nextFrame : original, interval, isRoundScale, isRoundRotation); + track.children.erase(track.children.begin() + childIndex); + track.children.insert(track.children.begin() + childIndex, std::make_move_iterator(baked.begin()), + std::make_move_iterator(baked.end())); } void frames_generate_from_grid(Element& track, glm::ivec2 startPosition, glm::ivec2 size, glm::vec2 pivot, @@ -202,11 +196,11 @@ namespace anm2ed } auto frameType = track_frame_type_get(track); - auto first = - frameType == ElementType::TRIGGER ? document.FirstChildElement("Trigger") : document.FirstChildElement("Frame"); + auto tag = element_tag_get(frameType).data(); + auto first = document.FirstChildElement(tag); if (!first) { - if (errorString) *errorString = frameType == ElementType::TRIGGER ? "No valid trigger(s)." : "No valid frame(s)."; + if (errorString) *errorString = std::format("No valid {}(s).", tag); return false; } @@ -214,7 +208,7 @@ namespace anm2ed { start = std::clamp(start, 0, track_frames_count_get(track)); std::vector frames{}; - for (auto element = first; element; element = element->NextSiblingElement("Frame")) + for (auto element = first; element; element = element->NextSiblingElement(tag)) { auto frame = element_read(element); if (frame.type != ElementType::FRAME) continue; @@ -240,7 +234,7 @@ namespace anm2ed std::vector atFrames{}; int count{}; - for (auto element = first; element; element = element->NextSiblingElement("Trigger")) + for (auto element = first; element; element = element->NextSiblingElement(tag)) { auto trigger = element_read(element); if (trigger.type != ElementType::TRIGGER) continue; @@ -358,8 +352,7 @@ namespace anm2ed for (int index = (int)track.children.size() - 1; index >= 0; --index) { auto original = track.children[index]; - if (original.type != ElementType::FRAME) continue; - if (!is_frame_interpolation_baked(original, type)) continue; + if (original.type != ElementType::FRAME || !is_frame_interpolation_baked(original, type)) continue; if (original.duration <= FRAME_DURATION_MIN) { @@ -367,35 +360,12 @@ namespace anm2ed continue; } - auto nextFrame = index + 1 < (int)track.children.size() && track.children[index + 1].type == ElementType::FRAME - ? track.children[index + 1] - : original; - int duration{}; - int insertIndex = index; - - while (duration < original.duration) - { - auto baked = original; - float amount = interpolation_factor(original.interpolation, (float)duration / original.duration); - baked.duration = std::min(interval, original.duration - duration); - baked.interpolation = Interpolation::NONE; - baked.rotation = glm::mix(original.rotation, nextFrame.rotation, amount); - baked.position = glm::mix(original.position, nextFrame.position, amount); - baked.scale = glm::mix(original.scale, nextFrame.scale, amount); - baked.shear = glm::mix(original.shear, nextFrame.shear, amount); - baked.colorOffset = glm::mix(original.colorOffset, nextFrame.colorOffset, amount); - baked.tint = glm::mix(original.tint, nextFrame.tint, amount); - if (isRoundScale) baked.scale = glm::round(baked.scale); - if (isRoundRotation) baked.rotation = std::round(baked.rotation); - - if (insertIndex == index) - track.children[insertIndex] = baked; - else - track.children.insert(track.children.begin() + insertIndex, baked); - - duration += baked.duration; - ++insertIndex; - } + auto isNextFrame = index + 1 < (int)track.children.size() && track.children[index + 1].type == ElementType::FRAME; + auto baked = frame_bake_split(original, isNextFrame ? track.children[index + 1] : original, interval, + isRoundScale, isRoundRotation); + track.children.erase(track.children.begin() + index); + track.children.insert(track.children.begin() + index, std::make_move_iterator(baked.begin()), + std::make_move_iterator(baked.end())); } } @@ -422,15 +392,6 @@ namespace anm2ed interpolated_frames_bake(element, interval, isRoundScale, isRoundRotation, InterpolationBakeType::ALL); } - void layer_animation_ids_remap(Element& element, const std::unordered_map& remap) - { - if (element.type == ElementType::LAYER_ANIMATION) - if (auto it = remap.find(element.layerId); it != remap.end()) element.layerId = it->second; - - for (auto& child : element.children) - layer_animation_ids_remap(child, remap); - } - int track_length_get(const Element& track) { int length{}; @@ -459,18 +420,13 @@ namespace anm2ed if (auto rootAnimation = child_first_get(animation, ElementType::ROOT_ANIMATION)) length = std::max(length, track_length_get(*rootAnimation)); - if (auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS)) - { - group_roots_length_apply(*layerAnimations); - tracks_each(*layerAnimations, ElementType::LAYER_ANIMATION, - [&](const Element& track) { length = std::max(length, track_length_get(track)); }); - } - if (auto nullAnimations = child_first_get(animation, ElementType::NULL_ANIMATIONS)) - { - group_roots_length_apply(*nullAnimations); - tracks_each(*nullAnimations, ElementType::NULL_ANIMATION, - [&](const Element& track) { length = std::max(length, track_length_get(track)); }); - } + for (const auto& row : TRACK_CONTAINERS) + if (auto container = child_first_get(animation, row.container)) + { + group_roots_length_apply(*container); + tracks_each(*container, row.track, + [&](const Element& track) { length = std::max(length, track_length_get(track)); }); + } if (auto triggers = child_first_get(animation, ElementType::TRIGGERS)) length = std::max(length, track_length_get(*triggers)); return std::max(length, FRAME_DURATION_MIN); diff --git a/src/anm2/groups.cpp b/src/anm2/groups.cpp index 7b4672d..ae6a761 100644 --- a/src/anm2/groups.cpp +++ b/src/anm2/groups.cpp @@ -101,18 +101,21 @@ namespace anm2ed } } - void group_frames_bake(Element& element) + template void animation_track_containers_each(Element& root, Callback&& callback) { - if (element.type == ElementType::ANIMATION) - { - if (auto layerAnimations = child_first_get(element, ElementType::LAYER_ANIMATIONS)) - group_frames_bake(*layerAnimations, ElementType::LAYER_ANIMATION, element.frameNum); - if (auto nullAnimations = child_first_get(element, ElementType::NULL_ANIMATIONS)) - group_frames_bake(*nullAnimations, ElementType::NULL_ANIMATION, element.frameNum); - } + element_each(root, + [&](Element& element) + { + if (element.type != ElementType::ANIMATION) return; + for (const auto& row : TRACK_CONTAINERS) + if (auto container = child_first_get(element, row.container)) callback(element, *container, row); + }); + } - for (auto& child : element.children) - group_frames_bake(child); + void group_frames_bake(Element& root) + { + animation_track_containers_each(root, [](Element& animation, Element& container, const TrackContainer& row) + { group_frames_bake(container, row.track, animation.frameNum); }); } void group_frames_restore(Element& container, ElementType trackType) @@ -168,39 +171,23 @@ namespace anm2ed { return item.type == trackType && restoredGroupIds.contains(item.groupId); }); } - void group_frames_restore(Element& element) + void group_frames_restore(Element& root) { - if (element.type == ElementType::ANIMATION) - { - if (auto layerAnimations = child_first_get(element, ElementType::LAYER_ANIMATIONS)) - group_frames_restore(*layerAnimations, ElementType::LAYER_ANIMATION); - if (auto nullAnimations = child_first_get(element, ElementType::NULL_ANIMATIONS)) - group_frames_restore(*nullAnimations, ElementType::NULL_ANIMATION); - } - - for (auto& child : element.children) - group_frames_restore(child); + animation_track_containers_each(root, [](Element&, Element& container, const TrackContainer& row) + { group_frames_restore(container, row.track); }); } int child_index_get(const Element& element, ElementType type) { - for (int i = 0; i < (int)element.children.size(); ++i) - if (element.children[i].type == type) return i; - return -1; + auto child = child_first_get(element, type); + return child ? (int)(child - element.children.data()) : -1; } - ElementType track_container_track_type_get(ElementType containerType) + ElementType group_child_type_get(ElementType parentType) { - if (containerType == ElementType::LAYER_ANIMATIONS) return ElementType::LAYER_ANIMATION; - if (containerType == ElementType::NULL_ANIMATIONS) return ElementType::NULL_ANIMATION; - return ElementType::UNKNOWN; - } - - ElementType track_container_group_container_type_get(ElementType containerType) - { - if (containerType == ElementType::LAYER_ANIMATIONS) return ElementType::LAYER_ANIMATION_GROUPS; - if (containerType == ElementType::NULL_ANIMATIONS) return ElementType::NULL_ANIMATION_GROUPS; - return ElementType::UNKNOWN; + if (parentType == ElementType::ANIMATIONS) return ElementType::ANIMATION; + auto row = track_container_get(parentType); + return row && row->container == parentType ? row->track : ElementType::UNKNOWN; } int track_group_insert_index_get(const Element& container, ElementType trackType, int groupId) @@ -210,27 +197,18 @@ namespace anm2ed return (int)container.children.size(); } - void group_metadata_embed(Element& animation, ElementType trackContainerType) + void group_metadata_embed(Element& animation, const TrackContainer& row) { - auto groupContainerType = track_container_group_container_type_get(trackContainerType); - auto trackType = track_container_track_type_get(trackContainerType); - auto groupContainerIndex = child_index_get(animation, groupContainerType); - if (groupContainerIndex < 0 || trackType == ElementType::UNKNOWN) return; + auto groupContainer = child_first_get(animation, row.groups); + if (!groupContainer) return; std::vector groups{}; - for (const auto& child : animation.children[groupContainerIndex].children) + for (const auto& child : groupContainer->children) if (child.type == ElementType::GROUP) groups.push_back(child); if (!groups.empty()) { - auto trackContainerIndex = child_index_get(animation, trackContainerType); - if (trackContainerIndex < 0) - { - animation.children.push_back(element_make(trackContainerType)); - trackContainerIndex = (int)animation.children.size() - 1; - } - - auto& container = animation.children[trackContainerIndex]; + auto& container = child_ensure(animation, row.container); std::set groupIds{}; for (const auto& group : groups) if (group.id >= 0) groupIds.insert(group.id); @@ -247,40 +225,36 @@ namespace anm2ed for (auto& group : groups) { auto insertIndex = group.index >= 0 ? std::min(group.index, (int)container.children.size()) - : track_group_insert_index_get(container, trackType, group.id); + : track_group_insert_index_get(container, row.track, group.id); group.index = -1; container.children.insert(container.children.begin() + insertIndex, std::move(group)); } } - std::erase_if(animation.children, [&](const Element& child) { return child.type == groupContainerType; }); + std::erase_if(animation.children, [&](const Element& child) { return child.type == row.groups; }); } - void group_metadata_embed(Element& element) + void group_metadata_embed(Element& root) { - if (element.type == ElementType::ANIMATION) - { - group_metadata_embed(element, ElementType::LAYER_ANIMATIONS); - group_metadata_embed(element, ElementType::NULL_ANIMATIONS); - } - - for (auto& child : element.children) - group_metadata_embed(child); + element_each(root, + [](Element& element) + { + if (element.type != ElementType::ANIMATION) return; + for (const auto& row : TRACK_CONTAINERS) + group_metadata_embed(element, row); + }); } - void group_metadata_extract(Element& animation, ElementType trackContainerType, Flags flags) + void group_metadata_extract(Element& animation, const TrackContainer& row, Flags flags) { - auto groupContainerType = track_container_group_container_type_get(trackContainerType); - if (groupContainerType == ElementType::UNKNOWN) return; - - std::erase_if(animation.children, [&](const Element& child) { return child.type == groupContainerType; }); + std::erase_if(animation.children, [&](const Element& child) { return child.type == row.groups; }); if (!has_flag(flags, SERIALIZE_GROUPS)) return; - auto trackContainerIndex = child_index_get(animation, trackContainerType); + auto trackContainerIndex = child_index_get(animation, row.container); if (trackContainerIndex < 0) return; auto& container = animation.children[trackContainerIndex]; - auto groupContainer = element_make(groupContainerType); + auto groupContainer = element_make(row.groups); std::vector tracks{}; tracks.reserve(container.children.size()); @@ -301,84 +275,67 @@ namespace anm2ed animation.children.insert(animation.children.begin() + trackContainerIndex + 1, std::move(groupContainer)); } - void group_metadata_extract(Element& element, Flags flags) + void group_metadata_extract(Element& root, Flags flags) { - if (element.type == ElementType::ANIMATION) - { - group_metadata_extract(element, ElementType::LAYER_ANIMATIONS, flags); - group_metadata_extract(element, ElementType::NULL_ANIMATIONS, flags); - } - - for (auto& child : element.children) - group_metadata_extract(child, flags); + element_each(root, + [&](Element& element) + { + if (element.type != ElementType::ANIMATION) return; + for (const auto& row : TRACK_CONTAINERS) + group_metadata_extract(element, row, flags); + }); } - void groups_flatten(Element& element) + void group_children_flatten(Element& container, ElementType childType) { - auto container_flatten = [](Element& container, ElementType childType, bool isRootKept) + if (childType == ElementType::UNKNOWN) return; + + int nextGroupId{}; + for (const auto& item : container.children) + if (item.type == ElementType::GROUP) nextGroupId = std::max(nextGroupId, item.id + 1); + + std::vector flattened{}; + for (auto item : container.children) { - int nextGroupId{}; - for (const auto& item : container.children) - if (item.type == ElementType::GROUP) nextGroupId = std::max(nextGroupId, item.id + 1); - - std::vector flattened{}; - for (auto item : container.children) + if (item.type != ElementType::GROUP) { - if (item.type != ElementType::GROUP) - { - flattened.push_back(item); - continue; - } - - if (item.id < 0) item.id = nextGroupId++; - auto groupId = item.id; - auto children = std::move(item.children); - item.children.clear(); - - if (isRootKept) - { - bool isRootFound{}; - - for (auto child : children) - if (child.type == ElementType::ROOT_ANIMATION) - { - if (isRootFound) continue; - item.children.push_back(child); - isRootFound = true; - } - - if (!isRootFound) item.children.push_back(root_animation_make()); - } - flattened.push_back(item); - - for (auto child : children) - { - if (child.type != childType) continue; - child.groupId = groupId; - flattened.push_back(child); - } + continue; } - std::set groupIds{}; - for (const auto& item : flattened) - if (item.type == ElementType::GROUP) groupIds.insert(item.id); + if (item.id < 0) item.id = nextGroupId++; + auto children = std::move(item.children); + item.children.clear(); - for (auto& item : flattened) - if (item.type == childType && !groupIds.contains(item.groupId)) item.groupId = -1; + if (childType != ElementType::ANIMATION) + { + auto root = std::ranges::find(children, ElementType::ROOT_ANIMATION, &Element::type); + item.children.push_back(root == children.end() ? root_animation_make() : *root); + } - container.children = std::move(flattened); - }; + flattened.push_back(item); - if (element.type == ElementType::ANIMATIONS) - container_flatten(element, ElementType::ANIMATION, false); - else if (element.type == ElementType::LAYER_ANIMATIONS) - container_flatten(element, ElementType::LAYER_ANIMATION, true); - else if (element.type == ElementType::NULL_ANIMATIONS) - container_flatten(element, ElementType::NULL_ANIMATION, true); + for (auto child : children) + { + if (child.type != childType) continue; + child.groupId = item.id; + flattened.push_back(child); + } + } - for (auto& child : element.children) - groups_flatten(child); + std::set groupIds{}; + for (const auto& item : flattened) + if (item.type == ElementType::GROUP) groupIds.insert(item.id); + + for (auto& item : flattened) + if (item.type == childType && !groupIds.contains(item.groupId)) item.groupId = -1; + + container.children = std::move(flattened); + } + + void groups_flatten(Element& root) + { + element_each(root, [](Element& element) { group_children_flatten(element, group_child_type_get(element.type)); }); } void group_children_nest(Element& container, ElementType childType) @@ -418,23 +375,14 @@ namespace anm2ed container.children = std::move(nested); } - void groups_nest(Element& element) + void groups_nest(Element& root) { - if (element.type == ElementType::ANIMATIONS) - group_children_nest(element, ElementType::ANIMATION); - else if (element.type == ElementType::LAYER_ANIMATIONS) - group_children_nest(element, ElementType::LAYER_ANIMATION); - else if (element.type == ElementType::NULL_ANIMATIONS) - group_children_nest(element, ElementType::NULL_ANIMATION); - - for (auto& child : element.children) - groups_nest(child); + element_each(root, [](Element& element) { group_children_nest(element, group_child_type_get(element.type)); }); } - void source_document_erase(Element& element) + void source_document_erase(Element& root) { - std::erase_if(element.children, [](const Element& child) { return is_source_document_tag(child.tag); }); - for (auto& child : element.children) - source_document_erase(child); + element_each(root, [](Element& element) + { std::erase_if(element.children, [](const Element& child) { return is_source_document_tag(child.tag); }); }); } } diff --git a/src/anm2/hash.cpp b/src/anm2/hash.cpp index 94c9035..cf59ee5 100644 --- a/src/anm2/hash.cpp +++ b/src/anm2/hash.cpp @@ -1,333 +1,75 @@ #include "internal.hpp" -using namespace anm2ed::util; using namespace tinyxml2; namespace anm2ed { constexpr std::uint64_t ANM2_HASH_OFFSET = 14695981039346656037ull; constexpr std::uint64_t ANM2_HASH_PRIME = 1099511628211ull; + constexpr std::size_t HASH_BUFFER_SIZE = 64; - struct BakeAttributes + struct HashSink : ElementSink { - Interpolation interpolation{Interpolation::NONE}; - int delay{FRAME_DURATION_MIN}; + std::uint64_t hash{ANM2_HASH_OFFSET}; + + void byte_append(unsigned char value) + { + hash ^= value; + hash *= ANM2_HASH_PRIME; + } + + void bytes_append(const char* data, std::size_t size) + { + for (std::size_t i = 0; i < size; ++i) + byte_append((unsigned char)data[i]); + } + + void string_append(std::string_view value) + { + auto size = (std::uint64_t)value.size(); + bytes_append(reinterpret_cast(&size), sizeof(size)); + bytes_append(value.data(), value.size()); + } + + template void formatted_append(const char* name, T value) + { + char buffer[HASH_BUFFER_SIZE]{}; + XMLUtil::ToStr(value, buffer, sizeof(buffer)); + attribute(name, (const char*)buffer); + } + + void open(std::string_view tag) override + { + byte_append('E'); + string_append(tag); + } + + void attribute(const char* name, const char* value) override + { + byte_append('A'); + string_append(name); + string_append(value); + } + + void attribute(const char* name, int value) override { formatted_append(name, value); } + void attribute(const char* name, bool value) override { formatted_append(name, value); } + void attribute(const char* name, float value) override { formatted_append(name, value); } + void body() override { byte_append('['); } + void close() override { byte_append(']'); } }; - void element_hash_append(std::uint64_t&, const Element&, ElementType, Flags, - std::optional = std::nullopt); - - - void hash_byte_append(std::uint64_t& hash, unsigned char value) - { - hash ^= value; - hash *= ANM2_HASH_PRIME; - } - - void hash_bytes_append(std::uint64_t& hash, const char* data, std::size_t size) - { - for (std::size_t i = 0; i < size; ++i) - hash_byte_append(hash, (unsigned char)data[i]); - } - - void hash_string_append(std::uint64_t& hash, std::string_view value) - { - auto size = (std::uint64_t)value.size(); - hash_bytes_append(hash, reinterpret_cast(&size), sizeof(size)); - hash_bytes_append(hash, value.data(), value.size()); - } - - void hash_attr_append(std::uint64_t& hash, std::string_view name, std::string_view value) - { - hash_byte_append(hash, 'A'); - hash_string_append(hash, name); - hash_string_append(hash, value); - } - - void hash_attr_append(std::uint64_t& hash, std::string_view name, int value) - { - char buffer[32]{}; - XMLUtil::ToStr(value, buffer, sizeof(buffer)); - hash_attr_append(hash, name, std::string_view(buffer)); - } - - void hash_attr_append(std::uint64_t& hash, std::string_view name, bool isValue) - { - char buffer[8]{}; - XMLUtil::ToStr(isValue, buffer, sizeof(buffer)); - hash_attr_append(hash, name, std::string_view(buffer)); - } - - void hash_attr_append(std::uint64_t& hash, std::string_view name, float value) - { - char buffer[64]{}; - XMLUtil::ToStr(value, buffer, sizeof(buffer)); - hash_attr_append(hash, name, std::string_view(buffer)); - } - - void hash_path_attr_append(std::uint64_t& hash, std::string_view name, const std::filesystem::path& pathValue) - { - if (pathValue.empty()) return; - auto value = path::to_utf8(pathValue); - hash_attr_append(hash, name, value); - } - - void hash_interpolation_attr_append(std::uint64_t& hash, Interpolation interpolation) - { - if (interpolation == Interpolation::NONE || interpolation == Interpolation::LINEAR) - { - hash_attr_append(hash, "Interpolated", interpolation == Interpolation::LINEAR); - return; - } - - auto value = INTERPOLATION_VALUES[(std::size_t)interpolation]; - if (!value.empty()) hash_attr_append(hash, "Interpolated", value); - } - - void hash_origin_attr_append(std::uint64_t& hash, const Element& element) - { - auto origin = ORIGIN_VALUES[(std::size_t)element.origin]; - if (!origin.empty()) - { - hash_attr_append(hash, "Origin", origin); - return; - } - - hash_attr_append(hash, "XPivot", element.pivot.x); - hash_attr_append(hash, "YPivot", element.pivot.y); - } - - void frame_attributes_hash_append(std::uint64_t& hash, const Element& element, ElementType parentType, Flags flags) - { - if (parentType == ElementType::LAYER_ANIMATION) - { - bool isHasValidRegion = has_flag(flags, SERIALIZE_REGIONS) && element.regionId != -1; - bool isWriteRegionValues = has_flag(flags, SERIALIZE_REDUNDANT_FRAME_REGION_VALUES) || !isHasValidRegion; - - if (isHasValidRegion) hash_attr_append(hash, "RegionId", element.regionId); - if (isWriteRegionValues) - { - hash_attr_append(hash, "XPivot", element.pivot.x); - hash_attr_append(hash, "YPivot", element.pivot.y); - hash_attr_append(hash, "XCrop", element.crop.x); - hash_attr_append(hash, "YCrop", element.crop.y); - hash_attr_append(hash, "Width", element.size.x); - hash_attr_append(hash, "Height", element.size.y); - } - } - - hash_attr_append(hash, "XPosition", element.position.x); - hash_attr_append(hash, "YPosition", element.position.y); - hash_attr_append(hash, "Delay", element.duration); - hash_attr_append(hash, "Visible", element.isVisible); - hash_attr_append(hash, "XScale", element.scale.x); - hash_attr_append(hash, "YScale", element.scale.y); - if (has_flag(flags, SERIALIZE_EXTENSIONS)) - { - hash_attr_append(hash, "ShearX", element.shear.x); - hash_attr_append(hash, "ShearY", element.shear.y); - } - hash_attr_append(hash, "RedTint", color_write(element.tint.r)); - hash_attr_append(hash, "GreenTint", color_write(element.tint.g)); - hash_attr_append(hash, "BlueTint", color_write(element.tint.b)); - hash_attr_append(hash, "AlphaTint", color_write(element.tint.a)); - hash_attr_append(hash, "RedOffset", color_write(element.colorOffset.r)); - hash_attr_append(hash, "GreenOffset", color_write(element.colorOffset.g)); - hash_attr_append(hash, "BlueOffset", color_write(element.colorOffset.b)); - hash_attr_append(hash, "Rotation", element.rotation); - hash_interpolation_attr_append(hash, element.interpolation); - if (has_flag(flags, SERIALIZE_EXTENSIONS) && element.shaderId != -1) - hash_attr_append(hash, "ShaderId", element.shaderId); - } - - void bake_attributes_hash_append(std::uint64_t& hash, Interpolation interpolation, int bakeDelay) - { - auto value = INTERPOLATION_VALUES[(std::size_t)interpolation]; - if (!value.empty()) hash_attr_append(hash, "BakeInterpolation", value); - hash_attr_append(hash, "BakeDelay", bakeDelay); - } - - void element_attributes_hash_append(std::uint64_t& hash, const Element& element, ElementType parentType, Flags flags) - { - if (element.type == ElementType::INFO) - { - hash_attr_append(hash, "CreatedBy", element.createdBy); - hash_attr_append(hash, "CreatedOn", element.createdOn); - hash_attr_append(hash, "Fps", element.fps); - hash_attr_append(hash, "Version", element.version); - } - else if (element.type == ElementType::ANIMATIONS) - hash_attr_append(hash, "DefaultAnimation", element.defaultAnimation); - else if (element.type == ElementType::SPRITESHEET) - { - hash_attr_append(hash, "Id", element.id); - hash_path_attr_append(hash, "Path", element.path); - } - else if (element.type == ElementType::SHADER) - { - hash_attr_append(hash, "Id", element.id); - hash_attr_append(hash, "Name", element.name); - hash_path_attr_append(hash, "Vertex", element.vertex); - hash_path_attr_append(hash, "Fragment", element.fragment); - } - else if (element.type == ElementType::UNIFORM) - { - hash_attr_append(hash, "Name", element.name); - if (!element.binding.empty()) hash_attr_append(hash, "Binding", element.binding); - if (!element.value.empty()) hash_attr_append(hash, "Value", element.value); - } - else if (element.type == ElementType::COMPONENT) - { - hash_attr_append(hash, "Index", element.index); - if (!element.binding.empty()) hash_attr_append(hash, "Binding", element.binding); - if (!element.value.empty()) hash_attr_append(hash, "Value", element.value); - } - else if (element.type == ElementType::REGION) - { - hash_attr_append(hash, "Id", element.id); - hash_attr_append(hash, "Name", element.name); - hash_attr_append(hash, "XCrop", element.crop.x); - hash_attr_append(hash, "YCrop", element.crop.y); - hash_attr_append(hash, "Width", element.size.x); - hash_attr_append(hash, "Height", element.size.y); - hash_origin_attr_append(hash, element); - } - else if (element.type == ElementType::LAYER_ELEMENT) - { - hash_attr_append(hash, "Id", element.id); - hash_attr_append(hash, "Name", element.name); - hash_attr_append(hash, "SpritesheetId", element.spritesheetId); - } - else if (element.type == ElementType::NULL_ELEMENT) - { - hash_attr_append(hash, "Id", element.id); - hash_attr_append(hash, "Name", element.name); - if (element.isShowRect) hash_attr_append(hash, "ShowRect", element.isShowRect); - } - else if (element.type == ElementType::GROUP) - { - if (!is_nested_group_parent(parentType, flags)) hash_attr_append(hash, "Id", element.id); - hash_attr_append(hash, "Name", element.name); - hash_attr_append(hash, "IsExpanded", element.isExpanded); - hash_attr_append(hash, "Visible", element.isVisible); - if ((parentType == ElementType::LAYER_ANIMATION_GROUPS || parentType == ElementType::NULL_ANIMATION_GROUPS) && - element.index != -1) - hash_attr_append(hash, "Index", element.index); - } - else if (element.type == ElementType::EVENT_ELEMENT) - { - hash_attr_append(hash, "Id", element.id); - hash_attr_append(hash, "Name", element.name); - } - else if (element.type == ElementType::SOUND_ELEMENT) - { - hash_attr_append(hash, "Id", element.id); - if (parentType != ElementType::TRIGGER) hash_path_attr_append(hash, "Path", element.path); - } - else if (element.type == ElementType::ANIMATION) - { - hash_attr_append(hash, "Name", element.name); - hash_attr_append(hash, "FrameNum", element.frameNum); - hash_attr_append(hash, "Loop", element.isLoop); - if (element.groupId != -1 && is_group_id_serialized(flags)) hash_attr_append(hash, "GroupId", element.groupId); - } - else if (element.type == ElementType::LAYER_ANIMATION) - { - hash_attr_append(hash, "LayerId", element.layerId); - hash_attr_append(hash, "Visible", element.isVisible); - if (element.groupId != -1 && is_group_id_serialized(flags)) hash_attr_append(hash, "GroupId", element.groupId); - } - else if (element.type == ElementType::NULL_ANIMATION) - { - hash_attr_append(hash, "NullId", element.nullId); - hash_attr_append(hash, "Visible", element.isVisible); - if (element.groupId != -1 && is_group_id_serialized(flags)) hash_attr_append(hash, "GroupId", element.groupId); - } - else if (element.type == ElementType::FRAME) - frame_attributes_hash_append(hash, element, parentType, flags); - else if (element.type == ElementType::TRIGGER) - { - if (element.eventId != -1) hash_attr_append(hash, "EventId", element.eventId); - hash_attr_append(hash, "AtFrame", element.atFrame); - } - } - - void trigger_sounds_hash_append(std::uint64_t& hash, const Element& element, Flags flags) - { - if (element.type != ElementType::TRIGGER || !has_flag(flags, SERIALIZE_SOUNDS)) return; - - for (auto soundId : element.soundIds) - { - if (soundId == -1) continue; - auto sound = element_make(ElementType::SOUND_ELEMENT); - sound.id = soundId; - element_hash_append(hash, sound, ElementType::TRIGGER, flags); - } - } - - void baked_frames_hash_append(std::uint64_t& hash, const Element& track, int index, Flags flags) - { - const auto& original = track.children[index]; - auto nextFrame = index + 1 < (int)track.children.size() && track.children[index + 1].type == ElementType::FRAME - ? track.children[index + 1] - : original; - auto bakeDelay = std::max(original.duration, FRAME_DURATION_MIN); - - for (int bakeIndex = 0; bakeIndex < bakeDelay; ++bakeIndex) - { - auto baked = original; - auto amount = interpolation_factor(original.interpolation, (float)bakeIndex / (float)bakeDelay); - baked.duration = FRAME_DURATION_MIN; - baked.interpolation = Interpolation::NONE; - baked.rotation = glm::mix(original.rotation, nextFrame.rotation, amount); - baked.position = glm::mix(original.position, nextFrame.position, amount); - baked.scale = glm::mix(original.scale, nextFrame.scale, amount); - baked.shear = glm::mix(original.shear, nextFrame.shear, amount); - baked.colorOffset = glm::mix(original.colorOffset, nextFrame.colorOffset, amount); - baked.tint = glm::mix(original.tint, nextFrame.tint, amount); - element_hash_append(hash, baked, track.type, flags, - bakeIndex == 0 ? std::optional({original.interpolation, bakeDelay}) - : std::nullopt); - } - } - - void element_hash_append(std::uint64_t& hash, const Element& element, ElementType parentType, Flags flags, - std::optional bakeAttributes) - { - auto tag = element.type == ElementType::UNKNOWN ? std::string_view(element.tag) : element_tag_get(element.type); - hash_byte_append(hash, 'E'); - hash_string_append(hash, tag.empty() ? std::string_view(element.tag) : tag); - - element_attributes_hash_append(hash, element, parentType, flags); - if (bakeAttributes) bake_attributes_hash_append(hash, bakeAttributes->interpolation, bakeAttributes->delay); - - hash_byte_append(hash, '['); - trigger_sounds_hash_append(hash, element, flags); - for (int i = 0; i < (int)element.children.size(); ++i) - { - const auto& child = element.children[i]; - if (element_write_skip(child, element.type, flags)) continue; - - if (is_track(element) && is_frame_bake_serialized(child, flags)) - baked_frames_hash_append(hash, element, i, flags); - else - element_hash_append(hash, child, element.type, flags); - } - hash_byte_append(hash, ']'); - } - std::uint64_t element_hash(const Element& element, Flags flags) { - auto hash = ANM2_HASH_OFFSET; - element_hash_append(hash, element, ElementType::UNKNOWN, flags); - return hash; + HashSink sink{}; + element_emit(sink, element, ElementType::UNKNOWN, flags); + return sink.hash; } std::uint64_t anm2_hash_get(const Element& root, Options options) { - auto hash = ANM2_HASH_OFFSET; - hash_byte_append(hash, options.isExtendedFormat ? 'E' : 'A'); - element_hash_append(hash, root, ElementType::UNKNOWN, SERIALIZE_ANM2ED_DEFAULT); - return hash; + HashSink sink{}; + sink.byte_append(options.isExtendedFormat ? 'E' : 'A'); + element_emit(sink, root, ElementType::UNKNOWN, SERIALIZE_ANM2ED_DEFAULT); + return sink.hash; } } diff --git a/src/anm2/internal.hpp b/src/anm2/internal.hpp index 8db765c..4d6e151 100644 --- a/src/anm2/internal.hpp +++ b/src/anm2/internal.hpp @@ -35,10 +35,32 @@ namespace anm2ed inline constexpr std::array ORIGIN_VALUES = {"", "TopLeft", "Center"}; inline constexpr std::string_view SOURCE_DOCUMENT_TAG = "SourceDocument"; + struct ElementSink + { + virtual ~ElementSink() = default; + virtual void open(std::string_view) = 0; + virtual void attribute(const char*, const char*) = 0; + virtual void attribute(const char*, int) = 0; + virtual void attribute(const char*, bool) = 0; + virtual void attribute(const char*, float) = 0; + virtual void body() {} + virtual void close() = 0; + }; + + struct BakeAttributes + { + Interpolation interpolation{Interpolation::NONE}; + int delay{FRAME_DURATION_MIN}; + }; + + void element_emit(ElementSink&, const Element&, ElementType, Flags, std::optional = std::nullopt); + bool is_source_document_tag(std::string_view); ElementType element_type_get(std::string_view); std::string_view element_tag_get(ElementType); - ElementType element_container_type_get(ElementType); + ElementType group_child_type_get(ElementType); + Element* content_container_get(Element&, ElementType); + std::unordered_map child_ids_compact(Element&, ElementType); int animations_count_get(const Element&); int animations_child_index_get(const Element&, int); int animations_child_insert_index_get(const Element&, int); @@ -55,12 +77,6 @@ namespace anm2ed void shader_ids_remap(Element&); void region_ids_remap(Element&); void region_frame_ids_repair(Element&); - void layer_animation_ids_remap(Element&, const std::unordered_map&); - Element* child_first_get(Element&, ElementType); - const Element* child_first_get(const Element&, ElementType); - Element* child_id_get(Element&, ElementType, int); - const Element* child_id_get(const Element&, ElementType, int); - bool is_track(const Element&); bool is_track_child_valid(ElementType, ElementType); bool is_track_group_visible(const Element&, const Element&); bool is_nested_group_parent(ElementType, Flags); @@ -68,39 +84,10 @@ namespace anm2ed bool element_write_skip(const Element&, ElementType, Flags); bool is_frame_bake_serialized(const Element&, Flags); bool is_special_interpolated_frames(const Element&); + std::vector frame_bake_split(const Element&, const Element&, int, bool, bool); void special_interpolated_frames_bake(Element&, int, bool, bool); void all_interpolated_frames_bake(Element&, int, bool, bool); - float interpolation_factor(Interpolation, float); int color_write(float); std::uint64_t anm2_hash_get(const Element&, Options); std::uint64_t element_hash(const Element&, Flags); - ElementType track_frame_type_get(const Element&); - ElementType item_type_to_container_type_get(ItemType); - int track_id_get(const Element&); - int track_frame_child_index_get(const Element&, int); - int track_frames_count_get(const Element&); - int track_frame_insert_child_index_get(const Element&, int); - Element* track_find(Element&, ElementType, int); - const Element* track_find(const Element&, ElementType, int); - Element* track_group_find(Element&, ElementType, int, int); - const Element* track_group_find(const Element&, ElementType, int, int); - Element* animation_container_get(Element&, ElementType); - - template void tracks_each(Element& parent, ElementType trackType, Callback&& callback) - { - for (auto& child : parent.children) - if (child.type == trackType) - callback(child); - else if (child.type == ElementType::GROUP) - tracks_each(child, trackType, callback); - } - - template void tracks_each(const Element& parent, ElementType trackType, Callback&& callback) - { - for (const auto& child : parent.children) - if (child.type == trackType) - callback(child); - else if (child.type == ElementType::GROUP) - tracks_each(child, trackType, callback); - } } diff --git a/src/anm2/merge.cpp b/src/anm2/merge.cpp index 676fa34..40bba96 100644 --- a/src/anm2/merge.cpp +++ b/src/anm2/merge.cpp @@ -92,12 +92,7 @@ namespace anm2ed auto sourceTracks = child_first_get(source, containerType); if (!sourceTracks) return; - auto destinationTracks = child_first_get(destination, containerType); - if (!destinationTracks) - { - destination.children.push_back(element_make(containerType)); - destinationTracks = &destination.children.back(); - } + auto destinationTracks = &child_ensure(destination, containerType); std::unordered_map groupRemap{}; std::set matchedGroupIds{}; @@ -120,15 +115,7 @@ namespace anm2ed destinationGroup = std::prev(destinationTracks->children.end()); } else if (auto sourceRoot = child_first_get(sourceGroup, ElementType::ROOT_ANIMATION)) - { - auto destinationRoot = child_first_get(*destinationGroup, ElementType::ROOT_ANIMATION); - if (!destinationRoot) - { - destinationGroup->children.push_back(root_animation_make()); - destinationRoot = &destinationGroup->children.back(); - } - track_merge(*destinationRoot, *sourceRoot, type); - } + track_merge(child_ensure(*destinationGroup, ElementType::ROOT_ANIMATION), *sourceRoot, type); groupRemap[sourceGroup.id] = destinationGroup->id; matchedGroupIds.insert(destinationGroup->id); @@ -141,8 +128,7 @@ namespace anm2ed auto mappedGroup = groupRemap.find(sourceTrack.groupId); auto groupId = mappedGroup != groupRemap.end() ? mappedGroup->second : -1; - if (auto destinationTrack = - track_group_find(*destinationTracks, sourceTrack.type, track_id_get(sourceTrack), groupId)) + if (auto destinationTrack = track_find(*destinationTracks, sourceTrack.type, track_id_get(sourceTrack), groupId)) track_merge(*destinationTrack, sourceTrack, type); else { @@ -169,30 +155,16 @@ namespace anm2ed targetAnimation = element_get(ElementType::ANIMATION, target); if (!source || !targetAnimation) continue; - if (auto sourceRoot = child_first_get(*source, ElementType::ROOT_ANIMATION)) + auto single_track_merge = [&](ElementType trackType) { - auto targetRoot = child_first_get(*targetAnimation, ElementType::ROOT_ANIMATION); - if (!targetRoot) - { - targetAnimation->children.push_back(element_make(ElementType::ROOT_ANIMATION)); - targetRoot = &targetAnimation->children.back(); - } - track_merge(*targetRoot, *sourceRoot, type); - } + if (auto sourceTrack = child_first_get(*source, trackType)) + track_merge(child_ensure(*targetAnimation, trackType), *sourceTrack, type); + }; - animation_tracks_merge(*targetAnimation, *source, ElementType::LAYER_ANIMATIONS, type); - animation_tracks_merge(*targetAnimation, *source, ElementType::NULL_ANIMATIONS, type); - - if (auto sourceTriggers = child_first_get(*source, ElementType::TRIGGERS)) - { - auto targetTriggers = child_first_get(*targetAnimation, ElementType::TRIGGERS); - if (!targetTriggers) - { - targetAnimation->children.push_back(element_make(ElementType::TRIGGERS)); - targetTriggers = &targetAnimation->children.back(); - } - track_merge(*targetTriggers, *sourceTriggers, type); - } + single_track_merge(ElementType::ROOT_ANIMATION); + for (const auto& row : TRACK_CONTAINERS) + animation_tracks_merge(*targetAnimation, *source, row.container, type); + single_track_merge(ElementType::TRIGGERS); } int finalIndex = target; diff --git a/src/anm2/normalize.cpp b/src/anm2/normalize.cpp index f7f37cd..eea7815 100644 --- a/src/anm2/normalize.cpp +++ b/src/anm2/normalize.cpp @@ -1,14 +1,42 @@ #include "internal.hpp" -using namespace anm2ed::util; -using namespace tinyxml2; - namespace anm2ed { + constexpr std::string_view SHADER_NAME_DEFAULT = "New Shader"; + + void frame_ids_remap(Element& root, int Element::* member, const std::unordered_map& remap) + { + element_each(root, + [&](Element& element) + { + if (element.type != ElementType::FRAME || element.*member == -1) return; + auto it = remap.find(element.*member); + element.*member = it == remap.end() ? -1 : it->second; + }); + } + + template void layer_frames_each(Element& root, Callback&& callback) + { + auto layers = content_container_get(root, ElementType::LAYERS); + auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS); + if (!layers || !spritesheets) return; + + animations_tracks_each(root, ElementType::LAYER_ANIMATION, + [&](Element& track) + { + auto layer = child_id_get(*layers, ElementType::LAYER_ELEMENT, track.layerId); + auto spritesheet = layer ? child_id_get(*spritesheets, ElementType::SPRITESHEET, + layer->spritesheetId) + : nullptr; + if (!spritesheet) return; + for (auto& frame : track.children) + if (frame.type == ElementType::FRAME) callback(frame, *spritesheet); + }); + } + void shader_ids_repair(Element& root) { - auto content = child_first_get(root, ElementType::CONTENT); - auto shaders = content ? child_first_get(*content, ElementType::SHADERS) : nullptr; + auto shaders = content_container_get(root, ElementType::SHADERS); if (!shaders) return; std::set usedIds{}; @@ -21,207 +49,91 @@ namespace anm2ed ++nextId; if (shader.id < 0 || usedIds.contains(shader.id)) shader.id = nextId++; usedIds.insert(shader.id); - if (shader.name.empty()) shader.name = "New Shader"; + if (shader.name.empty()) shader.name = SHADER_NAME_DEFAULT; } } void shader_frame_ids_repair(Element& root) { - auto content = child_first_get(root, ElementType::CONTENT); - auto shaders = content ? child_first_get(*content, ElementType::SHADERS) : nullptr; + auto shaders = content_container_get(root, ElementType::SHADERS); auto animations = element_first_get(root, ElementType::ANIMATIONS); if (!animations) return; - std::set shaderIds{}; + std::unordered_map identity{}; if (shaders) for (const auto& shader : shaders->children) - if (shader.type == ElementType::SHADER) shaderIds.insert(shader.id); - - auto frame_repair = [&](auto&& self, Element& element) -> void - { - if (element.type == ElementType::FRAME && element.shaderId != -1 && !shaderIds.contains(element.shaderId)) - element.shaderId = -1; - for (auto& child : element.children) - self(self, child); - }; - frame_repair(frame_repair, *animations); + if (shader.type == ElementType::SHADER) identity[shader.id] = shader.id; + frame_ids_remap(*animations, &Element::shaderId, identity); } void region_frame_ids_repair(Element& root) { - auto content = child_first_get(root, ElementType::CONTENT); - auto layers = content ? child_first_get(*content, ElementType::LAYERS) : nullptr; - auto spritesheets = content ? child_first_get(*content, ElementType::SPRITESHEETS) : nullptr; - auto animations = element_first_get(root, ElementType::ANIMATIONS); - if (!layers || !spritesheets || !animations) return; - - for (auto& animation : animations->children) - { - if (animation.type != ElementType::ANIMATION) continue; - auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS); - if (!layerAnimations) continue; - - tracks_each(*layerAnimations, ElementType::LAYER_ANIMATION, - [&](Element& layerAnimation) - { - auto layer = child_id_get(*layers, ElementType::LAYER_ELEMENT, layerAnimation.layerId); - auto spritesheet = - layer ? child_id_get(*spritesheets, ElementType::SPRITESHEET, layer->spritesheetId) : nullptr; - if (!spritesheet) return; - - for (auto& frame : layerAnimation.children) - { - if (frame.type != ElementType::FRAME) continue; - - auto frameCrop = glm::ivec2(frame.crop); - auto frameSize = glm::ivec2(frame.size); - auto framePivot = glm::ivec2(frame.pivot); - auto is_region_match = [&](const Element& region) + layer_frames_each(root, + [](Element& frame, const Element& spritesheet) { - return region.type == ElementType::REGION && glm::ivec2(region.crop) == frameCrop && - glm::ivec2(region.size) == frameSize && glm::ivec2(region.pivot) == framePivot; - }; - - auto region = frame.regionId == -1 - ? nullptr - : child_id_get(*spritesheet, ElementType::REGION, frame.regionId); - if (region && is_region_match(*region)) continue; - - for (const auto& candidate : spritesheet->children) - if (is_region_match(candidate)) + auto is_region_match = [&](const Element& region) { - frame.regionId = candidate.id; - break; - } - } - }); - } + return region.type == ElementType::REGION && + glm::ivec2(region.crop) == glm::ivec2(frame.crop) && + glm::ivec2(region.size) == glm::ivec2(frame.size) && + glm::ivec2(region.pivot) == glm::ivec2(frame.pivot); + }; + + auto region = frame.regionId == -1 + ? nullptr + : child_id_get(spritesheet, ElementType::REGION, frame.regionId); + if (region && is_region_match(*region)) return; + if (auto candidate = child_find(spritesheet, is_region_match)) frame.regionId = candidate->id; + }); } void region_ids_remap(Element& root) { - auto content = child_first_get(root, ElementType::CONTENT); - auto layers = content ? child_first_get(*content, ElementType::LAYERS) : nullptr; - auto spritesheets = content ? child_first_get(*content, ElementType::SPRITESHEETS) : nullptr; - auto animations = element_first_get(root, ElementType::ANIMATIONS); + auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS); if (!spritesheets) return; std::unordered_map> remaps{}; for (auto& spritesheet : spritesheets->children) - { - if (spritesheet.type != ElementType::SPRITESHEET) continue; + if (spritesheet.type == ElementType::SPRITESHEET) + if (auto remap = child_ids_compact(spritesheet, ElementType::REGION); !remap.empty()) + remaps[spritesheet.id] = std::move(remap); - std::unordered_map remap{}; - int nextId{}; - for (auto& region : spritesheet.children) - { - if (region.type != ElementType::REGION) continue; - remap[region.id] = nextId; - region.id = nextId++; - } - if (!remap.empty()) remaps[spritesheet.id] = std::move(remap); - } + if (remaps.empty()) return; - if (!layers || !animations || remaps.empty()) return; - - std::unordered_map layerSpritesheets{}; - for (const auto& layer : layers->children) - if (layer.type == ElementType::LAYER_ELEMENT) layerSpritesheets[layer.id] = layer.spritesheetId; - - for (auto& animation : animations->children) - { - if (animation.type != ElementType::ANIMATION) continue; - auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS); - if (!layerAnimations) continue; - - tracks_each(*layerAnimations, ElementType::LAYER_ANIMATION, - [&](Element& layerAnimation) - { - auto layer = layerSpritesheets.find(layerAnimation.layerId); - if (layer == layerSpritesheets.end()) return; - auto remap = remaps.find(layer->second); - if (remap == remaps.end()) return; - - for (auto& frame : layerAnimation.children) - { - if (frame.type != ElementType::FRAME || frame.regionId == -1) continue; - if (auto it = remap->second.find(frame.regionId); it != remap->second.end()) - frame.regionId = it->second; - else - frame.regionId = -1; - } - }); - } + layer_frames_each(root, + [&](Element& frame, const Element& spritesheet) + { + auto remap = remaps.find(spritesheet.id); + if (remap == remaps.end() || frame.regionId == -1) return; + auto it = remap->second.find(frame.regionId); + frame.regionId = it == remap->second.end() ? -1 : it->second; + }); } void shader_ids_remap(Element& root) { - auto content = child_first_get(root, ElementType::CONTENT); - auto shaders = content ? child_first_get(*content, ElementType::SHADERS) : nullptr; - - std::unordered_map remap{}; - int nextId{}; - if (shaders) - for (auto& shader : shaders->children) - { - if (shader.type != ElementType::SHADER) continue; - remap[shader.id] = nextId; - shader.id = nextId++; - } - - auto frame_remap = [&](auto&& self, Element& element) -> void - { - if (element.type == ElementType::FRAME && element.shaderId != -1) - { - if (auto it = remap.find(element.shaderId); it != remap.end()) - element.shaderId = it->second; - else - element.shaderId = -1; - } - for (auto& child : element.children) - self(self, child); - }; - frame_remap(frame_remap, root); + auto shaders = content_container_get(root, ElementType::SHADERS); + auto remap = shaders ? child_ids_compact(*shaders, ElementType::SHADER) : std::unordered_map{}; + frame_ids_remap(root, &Element::shaderId, remap); } void Anm2::region_frames_sync(bool isClearInvalid) { - auto content = child_first_get(root, ElementType::CONTENT); - auto layers = content ? child_first_get(*content, ElementType::LAYERS) : nullptr; - auto spritesheets = content ? child_first_get(*content, ElementType::SPRITESHEETS) : nullptr; - auto animations = element_first_get(root, ElementType::ANIMATIONS); - if (!layers || !spritesheets || !animations) return; - - for (auto& animation : animations->children) - { - if (animation.type != ElementType::ANIMATION) continue; - auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS); - if (!layerAnimations) continue; - - tracks_each(*layerAnimations, ElementType::LAYER_ANIMATION, - [&](Element& layerAnimation) - { - auto layer = child_id_get(*layers, ElementType::LAYER_ELEMENT, layerAnimation.layerId); - auto spritesheet = - layer ? child_id_get(*spritesheets, ElementType::SPRITESHEET, layer->spritesheetId) : nullptr; - if (!spritesheet) return; - - for (auto& frame : layerAnimation.children) - { - if (frame.type != ElementType::FRAME || frame.regionId == -1) continue; - auto region = child_id_get(*spritesheet, ElementType::REGION, frame.regionId); - if (!region) + layer_frames_each(root, + [&](Element& frame, const Element& spritesheet) { - if (isClearInvalid) frame.regionId = -1; - continue; - } + if (frame.regionId == -1) return; + auto region = child_id_get(spritesheet, ElementType::REGION, frame.regionId); + if (!region) + { + if (isClearInvalid) frame.regionId = -1; + return; + } - frame.crop = region->crop; - frame.size = region->size; - frame.pivot = region->pivot; - } - }); - } + frame.crop = region->crop; + frame.size = region->size; + frame.pivot = region->pivot; + }); } Anm2 Anm2::normalized_for_serialize(Flags flags) const @@ -240,19 +152,17 @@ namespace anm2ed else group_metadata_extract(normalized.root, flags); - auto content = child_first_get(normalized.root, ElementType::CONTENT); - if (content) std::erase_if(content->children, [](const Element& element) { return element.tag == "Groups"; }); - if (auto layers = content ? child_first_get(*content, ElementType::LAYERS) : nullptr) + if (auto content = child_first_get(normalized.root, ElementType::CONTENT)) + std::erase_if(content->children, [](const Element& element) { return element.tag == "Groups"; }); + if (auto layers = content_container_get(normalized.root, ElementType::LAYERS)) { - std::unordered_map remap{}; - int nextId{}; - for (auto& layer : layers->children) - { - if (layer.type != ElementType::LAYER_ELEMENT) continue; - remap[layer.id] = nextId; - layer.id = nextId++; - } - layer_animation_ids_remap(normalized.root, remap); + auto remap = child_ids_compact(*layers, ElementType::LAYER_ELEMENT); + element_each(normalized.root, + [&](Element& element) + { + if (element.type != ElementType::LAYER_ANIMATION) return; + if (auto it = remap.find(element.layerId); it != remap.end()) element.layerId = it->second; + }); } return normalized; diff --git a/src/anm2/xml.cpp b/src/anm2/xml.cpp index 220c124..dca6835 100644 --- a/src/anm2/xml.cpp +++ b/src/anm2/xml.cpp @@ -88,13 +88,6 @@ namespace anm2ed return true; } - void path_set(XMLElement* element, const char* name, const std::filesystem::path& value) - { - if (!element || value.empty()) return; - auto valueUtf8 = path::to_utf8(value); - element->SetAttribute(name, valueUtf8.c_str()); - } - float color_read(const XMLElement* element, const char* name, float fallback) { int value{}; @@ -120,18 +113,6 @@ namespace anm2ed Interpolation interpolation_read(const XMLElement* element) { return interpolation_read(element, "Interpolated"); } - void interpolation_write(XMLElement* element, Interpolation interpolation) - { - if (interpolation == Interpolation::NONE || interpolation == Interpolation::LINEAR) - { - element->SetAttribute("Interpolated", interpolation == Interpolation::LINEAR); - return; - } - - auto value = INTERPOLATION_VALUES[(std::size_t)interpolation]; - if (!value.empty()) element->SetAttribute("Interpolated", value.data()); - } - Origin origin_read(const XMLElement* element) { auto value = element ? element->Attribute("Origin") : nullptr; @@ -143,19 +124,6 @@ namespace anm2ed return Origin::CUSTOM; } - void origin_write(XMLElement* out, const Element& element) - { - auto origin = ORIGIN_VALUES[(std::size_t)element.origin]; - if (!origin.empty()) - { - out->SetAttribute("Origin", origin.data()); - return; - } - - out->SetAttribute("XPivot", element.pivot.x); - out->SetAttribute("YPivot", element.pivot.y); - } - void element_attributes_read(Element& out, const XMLElement* element) { #define X(attribute, member) string_query(element, attribute, out.member); @@ -265,11 +233,52 @@ namespace anm2ed return out; } + struct WriteRule + { + ElementType type; + Flag flag; + bool isEmptySkipped; + }; + + constexpr WriteRule WRITE_RULES[] = { + {ElementType::SHADERS, SERIALIZE_EXTENSIONS, true}, + {ElementType::SHADER, SERIALIZE_EXTENSIONS, false}, + {ElementType::UNIFORM, SERIALIZE_EXTENSIONS, false}, + {ElementType::COMPONENT, SERIALIZE_EXTENSIONS, false}, + {ElementType::SOUNDS, SERIALIZE_SOUNDS, true}, + {ElementType::LAYER_ANIMATION_GROUPS, SERIALIZE_GROUPS, true}, + {ElementType::NULL_ANIMATION_GROUPS, SERIALIZE_GROUPS, true}, + {ElementType::REGION, SERIALIZE_REGIONS, false}, + {ElementType::GROUP, SERIALIZE_GROUPS, false}}; + + struct XmlSink : ElementSink + { + XMLDocument& document; + std::vector stack{}; + XMLElement* root{}; + + explicit XmlSink(XMLDocument& document) : document(document) {} + + void open(std::string_view tag) override + { + auto element = document.NewElement(std::string(tag).c_str()); + if (stack.empty()) + root = element; + else + stack.back()->InsertEndChild(element); + stack.push_back(element); + } + + void attribute(const char* name, const char* value) override { stack.back()->SetAttribute(name, value); } + void attribute(const char* name, int value) override { stack.back()->SetAttribute(name, value); } + void attribute(const char* name, bool value) override { stack.back()->SetAttribute(name, value); } + void attribute(const char* name, float value) override { stack.back()->SetAttribute(name, value); } + void close() override { stack.pop_back(); } + }; + bool is_nested_group_parent(ElementType parentType, Flags flags) { - return has_flag(flags, SERIALIZE_NESTED_GROUPS) && - (parentType == ElementType::ANIMATIONS || parentType == ElementType::LAYER_ANIMATIONS || - parentType == ElementType::NULL_ANIMATIONS); + return has_flag(flags, SERIALIZE_NESTED_GROUPS) && group_child_type_get(parentType) != ElementType::UNKNOWN; } bool is_group_id_serialized(Flags flags) @@ -280,241 +289,235 @@ namespace anm2ed bool element_write_skip(const Element& element, ElementType parentType, Flags flags) { if (!is_track_child_valid(parentType, element.type)) return true; - if ((element.type == ElementType::SHADERS || element.type == ElementType::SHADER || - element.type == ElementType::UNIFORM || element.type == ElementType::COMPONENT) && - !has_flag(flags, SERIALIZE_EXTENSIONS)) - return true; - if (element.type == ElementType::SHADERS && element.children.empty()) return true; - if (element.type == ElementType::SOUNDS && (!has_flag(flags, SERIALIZE_SOUNDS) || element.children.empty())) - return true; - if ((element.type == ElementType::LAYER_ANIMATION_GROUPS || element.type == ElementType::NULL_ANIMATION_GROUPS) && - (!has_flag(flags, SERIALIZE_GROUPS) || element.children.empty())) - return true; if (element.type == ElementType::SOUND_ELEMENT && parentType == ElementType::TRIGGER) return true; - if (element.type == ElementType::REGION && !has_flag(flags, SERIALIZE_REGIONS)) return true; - if (element.type == ElementType::GROUP && !has_flag(flags, SERIALIZE_GROUPS)) return true; + for (const auto& rule : WRITE_RULES) + if (rule.type == element.type) return !has_flag(flags, rule.flag) || (rule.isEmptySkipped && element.children.empty()); return false; } - void frame_attributes_write(XMLElement* out, const Element& element, ElementType parentType, Flags flags) - { - if (parentType == ElementType::LAYER_ANIMATION) - { - bool isHasValidRegion = has_flag(flags, SERIALIZE_REGIONS) && element.regionId != -1; - bool isWriteRegionValues = has_flag(flags, SERIALIZE_REDUNDANT_FRAME_REGION_VALUES) || !isHasValidRegion; - - if (isHasValidRegion) out->SetAttribute("RegionId", element.regionId); - if (isWriteRegionValues) - { - out->SetAttribute("XPivot", element.pivot.x); - out->SetAttribute("YPivot", element.pivot.y); - out->SetAttribute("XCrop", element.crop.x); - out->SetAttribute("YCrop", element.crop.y); - out->SetAttribute("Width", element.size.x); - out->SetAttribute("Height", element.size.y); - } - } - - out->SetAttribute("XPosition", element.position.x); - out->SetAttribute("YPosition", element.position.y); - out->SetAttribute("Delay", element.duration); - out->SetAttribute("Visible", element.isVisible); - out->SetAttribute("XScale", element.scale.x); - out->SetAttribute("YScale", element.scale.y); - if (has_flag(flags, SERIALIZE_EXTENSIONS)) - { - out->SetAttribute("ShearX", element.shear.x); - out->SetAttribute("ShearY", element.shear.y); - } - out->SetAttribute("RedTint", color_write(element.tint.r)); - out->SetAttribute("GreenTint", color_write(element.tint.g)); - out->SetAttribute("BlueTint", color_write(element.tint.b)); - out->SetAttribute("AlphaTint", color_write(element.tint.a)); - out->SetAttribute("RedOffset", color_write(element.colorOffset.r)); - out->SetAttribute("GreenOffset", color_write(element.colorOffset.g)); - out->SetAttribute("BlueOffset", color_write(element.colorOffset.b)); - out->SetAttribute("Rotation", element.rotation); - interpolation_write(out, element.interpolation); - if (has_flag(flags, SERIALIZE_EXTENSIONS) && element.shaderId != -1) - out->SetAttribute("ShaderId", element.shaderId); - } - - void element_attributes_write(XMLElement* out, const Element& element, ElementType parentType, Flags flags) - { - if (element.type == ElementType::INFO) - { - out->SetAttribute("CreatedBy", element.createdBy.c_str()); - out->SetAttribute("CreatedOn", element.createdOn.c_str()); - out->SetAttribute("Fps", element.fps); - out->SetAttribute("Version", element.version); - } - else if (element.type == ElementType::ANIMATIONS) - out->SetAttribute("DefaultAnimation", element.defaultAnimation.c_str()); - else if (element.type == ElementType::SPRITESHEET) - { - out->SetAttribute("Id", element.id); - path_set(out, "Path", element.path); - } - else if (element.type == ElementType::SHADER) - { - out->SetAttribute("Id", element.id); - out->SetAttribute("Name", element.name.c_str()); - path_set(out, "Vertex", element.vertex); - path_set(out, "Fragment", element.fragment); - } - else if (element.type == ElementType::UNIFORM) - { - out->SetAttribute("Name", element.name.c_str()); - if (!element.binding.empty()) out->SetAttribute("Binding", element.binding.c_str()); - if (!element.value.empty()) out->SetAttribute("Value", element.value.c_str()); - } - else if (element.type == ElementType::COMPONENT) - { - out->SetAttribute("Index", element.index); - if (!element.binding.empty()) out->SetAttribute("Binding", element.binding.c_str()); - if (!element.value.empty()) out->SetAttribute("Value", element.value.c_str()); - } - else if (element.type == ElementType::REGION) - { - out->SetAttribute("Id", element.id); - out->SetAttribute("Name", element.name.c_str()); - out->SetAttribute("XCrop", element.crop.x); - out->SetAttribute("YCrop", element.crop.y); - out->SetAttribute("Width", element.size.x); - out->SetAttribute("Height", element.size.y); - origin_write(out, element); - } - else if (element.type == ElementType::LAYER_ELEMENT) - { - out->SetAttribute("Id", element.id); - out->SetAttribute("Name", element.name.c_str()); - out->SetAttribute("SpritesheetId", element.spritesheetId); - } - else if (element.type == ElementType::NULL_ELEMENT) - { - out->SetAttribute("Id", element.id); - out->SetAttribute("Name", element.name.c_str()); - if (element.isShowRect) out->SetAttribute("ShowRect", element.isShowRect); - } - else if (element.type == ElementType::GROUP) - { - if (!is_nested_group_parent(parentType, flags)) out->SetAttribute("Id", element.id); - out->SetAttribute("Name", element.name.c_str()); - out->SetAttribute("IsExpanded", element.isExpanded); - out->SetAttribute("Visible", element.isVisible); - if ((parentType == ElementType::LAYER_ANIMATION_GROUPS || parentType == ElementType::NULL_ANIMATION_GROUPS) && - element.index != -1) - out->SetAttribute("Index", element.index); - } - else if (element.type == ElementType::EVENT_ELEMENT) - { - out->SetAttribute("Id", element.id); - out->SetAttribute("Name", element.name.c_str()); - } - else if (element.type == ElementType::SOUND_ELEMENT) - { - out->SetAttribute("Id", element.id); - if (parentType != ElementType::TRIGGER) path_set(out, "Path", element.path); - } - else if (element.type == ElementType::ANIMATION) - { - out->SetAttribute("Name", element.name.c_str()); - out->SetAttribute("FrameNum", element.frameNum); - out->SetAttribute("Loop", element.isLoop); - if (element.groupId != -1 && is_group_id_serialized(flags)) out->SetAttribute("GroupId", element.groupId); - } - else if (element.type == ElementType::LAYER_ANIMATION) - { - out->SetAttribute("LayerId", element.layerId); - out->SetAttribute("Visible", element.isVisible); - if (element.groupId != -1 && is_group_id_serialized(flags)) out->SetAttribute("GroupId", element.groupId); - } - else if (element.type == ElementType::NULL_ANIMATION) - { - out->SetAttribute("NullId", element.nullId); - out->SetAttribute("Visible", element.isVisible); - if (element.groupId != -1 && is_group_id_serialized(flags)) out->SetAttribute("GroupId", element.groupId); - } - else if (element.type == ElementType::FRAME) - frame_attributes_write(out, element, parentType, flags); - else if (element.type == ElementType::TRIGGER) - { - if (element.eventId != -1) out->SetAttribute("EventId", element.eventId); - out->SetAttribute("AtFrame", element.atFrame); - } - } - - void bake_attributes_write(XMLElement* out, Interpolation interpolation, int bakeDelay) - { - auto value = INTERPOLATION_VALUES[(std::size_t)interpolation]; - if (!value.empty()) out->SetAttribute("BakeInterpolation", value.data()); - out->SetAttribute("BakeDelay", bakeDelay); - } - bool is_frame_bake_serialized(const Element& frame, Flags flags) { return has_flag(flags, SERIALIZE_BAKE_SPECIAL_INTERPOLATED_FRAMES) && frame.type == ElementType::FRAME && frame.interpolation != Interpolation::NONE && frame.interpolation != Interpolation::LINEAR; } - void baked_frames_insert(XMLDocument& document, XMLElement* out, const Element& track, int index, Flags flags) + void path_emit(ElementSink& sink, const char* name, const std::filesystem::path& value) { - const auto& original = track.children[index]; - auto nextFrame = index + 1 < (int)track.children.size() && track.children[index + 1].type == ElementType::FRAME - ? track.children[index + 1] - : original; - auto bakeDelay = std::max(original.duration, FRAME_DURATION_MIN); + if (!value.empty()) sink.attribute(name, path::to_utf8(value).c_str()); + } - for (int bakeIndex = 0; bakeIndex < bakeDelay; ++bakeIndex) + void interpolation_emit(ElementSink& sink, const char* name, Interpolation interpolation) + { + auto value = INTERPOLATION_VALUES[(std::size_t)interpolation]; + if (interpolation == Interpolation::NONE || interpolation == Interpolation::LINEAR) + sink.attribute(name, interpolation == Interpolation::LINEAR); + else if (!value.empty()) + sink.attribute(name, value.data()); + } + + void id_name_emit(ElementSink& sink, const Element& element) + { + sink.attribute("Id", element.id); + sink.attribute("Name", element.name.c_str()); + } + + void binding_value_emit(ElementSink& sink, const Element& element) + { + if (!element.binding.empty()) sink.attribute("Binding", element.binding.c_str()); + if (!element.value.empty()) sink.attribute("Value", element.value.c_str()); + } + + void group_id_emit(ElementSink& sink, const Element& element, Flags flags) + { + if (element.groupId != -1 && is_group_id_serialized(flags)) sink.attribute("GroupId", element.groupId); + } + + void crop_size_emit(ElementSink& sink, const Element& element) + { + sink.attribute("XCrop", element.crop.x); + sink.attribute("YCrop", element.crop.y); + sink.attribute("Width", element.size.x); + sink.attribute("Height", element.size.y); + } + + void frame_attributes_emit(ElementSink& sink, const Element& element, ElementType parentType, Flags flags) + { + if (parentType == ElementType::LAYER_ANIMATION) { - auto baked = original; - auto amount = interpolation_factor(original.interpolation, (float)bakeIndex / (float)bakeDelay); - baked.duration = FRAME_DURATION_MIN; - baked.interpolation = Interpolation::NONE; - baked.rotation = glm::mix(original.rotation, nextFrame.rotation, amount); - baked.position = glm::mix(original.position, nextFrame.position, amount); - baked.scale = glm::mix(original.scale, nextFrame.scale, amount); - baked.shear = glm::mix(original.shear, nextFrame.shear, amount); - baked.colorOffset = glm::mix(original.colorOffset, nextFrame.colorOffset, amount); - baked.tint = glm::mix(original.tint, nextFrame.tint, amount); - auto frame = element_to_xml(document, baked, track.type, flags); - if (bakeIndex == 0) bake_attributes_write(frame, original.interpolation, bakeDelay); - out->InsertEndChild(frame); + bool isHasValidRegion = has_flag(flags, SERIALIZE_REGIONS) && element.regionId != -1; + if (isHasValidRegion) sink.attribute("RegionId", element.regionId); + if (has_flag(flags, SERIALIZE_REDUNDANT_FRAME_REGION_VALUES) || !isHasValidRegion) + { + sink.attribute("XPivot", element.pivot.x); + sink.attribute("YPivot", element.pivot.y); + crop_size_emit(sink, element); + } + } + + sink.attribute("XPosition", element.position.x); + sink.attribute("YPosition", element.position.y); + sink.attribute("Delay", element.duration); + sink.attribute("Visible", element.isVisible); + sink.attribute("XScale", element.scale.x); + sink.attribute("YScale", element.scale.y); + if (has_flag(flags, SERIALIZE_EXTENSIONS)) + { + sink.attribute("ShearX", element.shear.x); + sink.attribute("ShearY", element.shear.y); + } + sink.attribute("RedTint", color_write(element.tint.r)); + sink.attribute("GreenTint", color_write(element.tint.g)); + sink.attribute("BlueTint", color_write(element.tint.b)); + sink.attribute("AlphaTint", color_write(element.tint.a)); + sink.attribute("RedOffset", color_write(element.colorOffset.r)); + sink.attribute("GreenOffset", color_write(element.colorOffset.g)); + sink.attribute("BlueOffset", color_write(element.colorOffset.b)); + sink.attribute("Rotation", element.rotation); + interpolation_emit(sink, "Interpolated", element.interpolation); + if (has_flag(flags, SERIALIZE_EXTENSIONS) && element.shaderId != -1) sink.attribute("ShaderId", element.shaderId); + } + + void element_attributes_emit(ElementSink& sink, const Element& element, ElementType parentType, Flags flags) + { + switch (element.type) + { + case ElementType::INFO: + sink.attribute("CreatedBy", element.createdBy.c_str()); + sink.attribute("CreatedOn", element.createdOn.c_str()); + sink.attribute("Fps", element.fps); + sink.attribute("Version", element.version); + break; + case ElementType::ANIMATIONS: + sink.attribute("DefaultAnimation", element.defaultAnimation.c_str()); + break; + case ElementType::SPRITESHEET: + sink.attribute("Id", element.id); + path_emit(sink, "Path", element.path); + break; + case ElementType::SHADER: + id_name_emit(sink, element); + path_emit(sink, "Vertex", element.vertex); + path_emit(sink, "Fragment", element.fragment); + break; + case ElementType::UNIFORM: + sink.attribute("Name", element.name.c_str()); + binding_value_emit(sink, element); + break; + case ElementType::COMPONENT: + sink.attribute("Index", element.index); + binding_value_emit(sink, element); + break; + case ElementType::REGION: + { + id_name_emit(sink, element); + crop_size_emit(sink, element); + auto origin = ORIGIN_VALUES[(std::size_t)element.origin]; + if (!origin.empty()) + sink.attribute("Origin", origin.data()); + else + { + sink.attribute("XPivot", element.pivot.x); + sink.attribute("YPivot", element.pivot.y); + } + break; + } + case ElementType::LAYER_ELEMENT: + id_name_emit(sink, element); + sink.attribute("SpritesheetId", element.spritesheetId); + break; + case ElementType::NULL_ELEMENT: + id_name_emit(sink, element); + if (element.isShowRect) sink.attribute("ShowRect", element.isShowRect); + break; + case ElementType::GROUP: + if (!is_nested_group_parent(parentType, flags)) sink.attribute("Id", element.id); + sink.attribute("Name", element.name.c_str()); + sink.attribute("IsExpanded", element.isExpanded); + sink.attribute("Visible", element.isVisible); + if ((parentType == ElementType::LAYER_ANIMATION_GROUPS || parentType == ElementType::NULL_ANIMATION_GROUPS) && + element.index != -1) + sink.attribute("Index", element.index); + break; + case ElementType::EVENT_ELEMENT: + id_name_emit(sink, element); + break; + case ElementType::SOUND_ELEMENT: + sink.attribute("Id", element.id); + if (parentType != ElementType::TRIGGER) path_emit(sink, "Path", element.path); + break; + case ElementType::ANIMATION: + sink.attribute("Name", element.name.c_str()); + sink.attribute("FrameNum", element.frameNum); + sink.attribute("Loop", element.isLoop); + group_id_emit(sink, element, flags); + break; + case ElementType::LAYER_ANIMATION: + case ElementType::NULL_ANIMATION: + sink.attribute(element.type == ElementType::LAYER_ANIMATION ? "LayerId" : "NullId", track_id_get(element)); + sink.attribute("Visible", element.isVisible); + group_id_emit(sink, element, flags); + break; + case ElementType::FRAME: + frame_attributes_emit(sink, element, parentType, flags); + break; + case ElementType::TRIGGER: + if (element.eventId != -1) sink.attribute("EventId", element.eventId); + sink.attribute("AtFrame", element.atFrame); + break; + default: + break; } } - void trigger_sounds_xml_insert(XMLDocument& document, XMLElement* out, const Element& element, Flags flags) - { - if (element.type != ElementType::TRIGGER || !has_flag(flags, SERIALIZE_SOUNDS)) return; - - for (auto soundId : element.soundIds) - { - if (soundId == -1) continue; - auto sound = document.NewElement(element_tag_get(ElementType::SOUND_ELEMENT).data()); - sound->SetAttribute("Id", soundId); - out->InsertEndChild(sound); - } - } - - XMLElement* element_to_xml(XMLDocument& document, const Element& element, ElementType parentType, Flags flags) + void element_emit(ElementSink& sink, const Element& element, ElementType parentType, Flags flags, + std::optional bake) { auto tag = element.type == ElementType::UNKNOWN ? std::string_view(element.tag) : element_tag_get(element.type); - auto out = document.NewElement(tag.empty() ? element.tag.c_str() : tag.data()); - element_attributes_write(out, element, parentType, flags); - trigger_sounds_xml_insert(document, out, element, flags); + sink.open(tag.empty() ? std::string_view(element.tag) : tag); + element_attributes_emit(sink, element, parentType, flags); + if (bake) + { + auto value = INTERPOLATION_VALUES[(std::size_t)bake->interpolation]; + if (!value.empty()) sink.attribute("BakeInterpolation", value.data()); + sink.attribute("BakeDelay", bake->delay); + } + sink.body(); + + if (element.type == ElementType::TRIGGER && has_flag(flags, SERIALIZE_SOUNDS)) + for (auto soundId : element.soundIds) + { + if (soundId == -1) continue; + auto sound = element_make(ElementType::SOUND_ELEMENT); + sound.id = soundId; + element_emit(sink, sound, ElementType::TRIGGER, flags); + } for (int i = 0; i < (int)element.children.size(); ++i) { const auto& child = element.children[i]; if (element_write_skip(child, element.type, flags)) continue; + if (!is_track(element) || !is_frame_bake_serialized(child, flags)) + { + element_emit(sink, child, element.type, flags); + continue; + } - if (is_track(element) && is_frame_bake_serialized(child, flags)) - baked_frames_insert(document, out, element, i, flags); - else - out->InsertEndChild(element_to_xml(document, child, element.type, flags)); + auto isNextFrame = i + 1 < (int)element.children.size() && element.children[i + 1].type == ElementType::FRAME; + auto baked = frame_bake_split(child, isNextFrame ? element.children[i + 1] : child, FRAME_DURATION_MIN, false, + false); + for (int bakeIndex = 0; bakeIndex < (int)baked.size(); ++bakeIndex) + element_emit(sink, baked[bakeIndex], element.type, flags, + bakeIndex == 0 ? std::optional({child.interpolation, (int)baked.size()}) + : std::nullopt); } + sink.close(); + } - return out; + XMLElement* element_to_xml(XMLDocument& document, const Element& element, ElementType parentType, Flags flags) + { + XmlSink sink(document); + element_emit(sink, element, parentType, flags); + return sink.root; } XMLElement* element_to_xml(XMLDocument& document, const Element& element, Flags flags) @@ -535,7 +538,7 @@ namespace anm2ed } bool Anm2::deserialize(ElementType type, const std::string& string, bool isAppend, std::string* errorString, - const std::filesystem::path& directory) + const std::filesystem::path& directory, int spritesheetId) { XMLDocument document{}; if (document.Parse(string.c_str()) != XML_SUCCESS) @@ -551,17 +554,17 @@ namespace anm2ed return false; } - auto containerType = element_container_type_get(type); - auto container = element_get(containerType); + auto containerType = type == ElementType::REGION ? ElementType::SPRITESHEET : ELEMENT_CONTAINERS[(int)type]; + auto container = type == ElementType::REGION ? element_get(containerType, spritesheetId) : element_get(containerType); if (!container) { if (errorString) *errorString = std::format("No {} container.", element_tag_get(containerType)); return false; } + auto isPath = type == ElementType::SOUND_ELEMENT || type == ElementType::SPRITESHEET; std::optional workingDirectory{}; - if ((type == ElementType::SOUND_ELEMENT || type == ElementType::SPRITESHEET) && !directory.empty()) - workingDirectory.emplace(directory); + if (isPath && !directory.empty()) workingDirectory.emplace(directory); for (auto xmlElement = document.FirstChildElement(tag.data()); xmlElement; xmlElement = xmlElement->NextSiblingElement(tag.data())) @@ -572,47 +575,10 @@ namespace anm2ed element.id = element_child_next_id_get(*container, type); else element_child_id_erase(*container, type, element.id); - if (type == ElementType::SOUND_ELEMENT || type == ElementType::SPRITESHEET) - element.path = path::backslash_handle(element.path); + if (isPath) element.path = path::backslash_handle(element.path); container->children.push_back(element); } return true; } - - bool Anm2::regions_deserialize(int spritesheetId, const std::string& string, bool isAppend, std::string* errorString) - { - XMLDocument document{}; - if (document.Parse(string.c_str()) != XML_SUCCESS) - { - if (errorString) *errorString = document.ErrorStr(); - return false; - } - - if (!document.FirstChildElement("Region")) - { - if (errorString) *errorString = "No valid region(s)."; - return false; - } - - auto spritesheet = element_get(ElementType::SPRITESHEET, spritesheetId); - if (!spritesheet) - { - if (errorString) *errorString = "No spritesheet."; - return false; - } - - for (auto element = document.FirstChildElement("Region"); element; element = element->NextSiblingElement("Region")) - { - auto region = element_read(element); - if (region.type != ElementType::REGION) continue; - if (isAppend) - region.id = element_child_next_id_get(*spritesheet, ElementType::REGION); - else - element_child_id_erase(*spritesheet, ElementType::REGION, region.id); - spritesheet->children.push_back(region); - } - - return true; - } } diff --git a/src/document.cpp b/src/document.cpp index 74c43c8..33d2d08 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -62,13 +62,13 @@ namespace anm2ed::document const Element* shader_element_get(const Anm2& anm2, int id) { auto shaders = anm2.element_get(ElementType::SHADERS); - return shaders ? element_child_id_get(*shaders, ElementType::SHADER, id) : nullptr; + return shaders ? child_id_get(*shaders, ElementType::SHADER, id) : nullptr; } Element* shader_element_get(Anm2& anm2, int id) { auto shaders = anm2.element_get(ElementType::SHADERS); - return shaders ? element_child_id_get(*shaders, ElementType::SHADER, id) : nullptr; + return shaders ? child_id_get(*shaders, ElementType::SHADER, id) : nullptr; } std::filesystem::path shader_absolute_path_get(Document& document, const std::filesystem::path& path) @@ -421,10 +421,10 @@ namespace anm2ed::document auto animation = document.anm2.element_get(ElementType::ANIMATION, it->animationIndex); Element* container{}; if (animation && it->itemType == LAYER) - container = element_child_first_get(*animation, ElementType::LAYER_ANIMATIONS); + container = child_first_get(*animation, ElementType::LAYER_ANIMATIONS); else if (animation && it->itemType == NULL_) - container = element_child_first_get(*animation, ElementType::NULL_ANIMATIONS); - auto group = container ? element_child_id_get(*container, ElementType::GROUP, it->itemID) : nullptr; + container = child_first_get(*animation, ElementType::NULL_ANIMATIONS); + auto group = container ? child_id_get(*container, ElementType::GROUP, it->itemID) : nullptr; if (!group) it = groupReferences.erase(it); else @@ -853,7 +853,7 @@ namespace anm2ed bool isChanged{}; for (auto id : ids) { - auto region = element_child_id_get(*spritesheet, ElementType::REGION, id); + auto region = child_id_get(*spritesheet, ElementType::REGION, id); if (!region) continue; auto minPoint = glm::ivec2(glm::min(region->crop, region->crop + region->size)); @@ -920,7 +920,7 @@ namespace anm2ed auto animation = anm2.element_get(ElementType::ANIMATION, animationIndex); if (!animation) continue; - auto layerAnimations = element_child_first_get(*animation, ElementType::LAYER_ANIMATIONS); + auto layerAnimations = child_first_get(*animation, ElementType::LAYER_ANIMATIONS); if (!layerAnimations) continue; auto layer_animation_regions_generate = [&](auto&& self, Element& layerAnimation) -> void @@ -1350,7 +1350,7 @@ namespace anm2ed for (auto& animation : animations->children) { if (animation.type != ElementType::ANIMATION) continue; - auto layerAnimations = element_child_first_get(animation, ElementType::LAYER_ANIMATIONS); + auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS); if (!layerAnimations) continue; auto layer_animation_update = [&](auto&& self, Element& layerAnimation) -> void { @@ -1403,7 +1403,7 @@ namespace anm2ed for (auto& animation : animations->children) { if (animation.type != ElementType::ANIMATION) continue; - auto layerAnimations = element_child_first_get(animation, ElementType::LAYER_ANIMATIONS); + auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS); if (!layerAnimations) continue; auto layer_animation_update = [&](auto&& self, Element& layerAnimation) -> void { @@ -1526,7 +1526,7 @@ namespace anm2ed auto child_set = [](Element& container, Element child, ElementType type) { - if (auto existing = element_child_first_get(container, type)) + if (auto existing = child_first_get(container, type)) *existing = std::move(child); else container.children.push_back(std::move(child)); @@ -1551,7 +1551,7 @@ namespace anm2ed auto track_container_get = [](Element& animation, ElementType type) { - if (auto container = element_child_first_get(animation, type)) return container; + if (auto container = child_first_get(animation, type)) return container; animation.children.push_back(element_make(type)); return &animation.children.back(); }; @@ -1570,7 +1570,7 @@ namespace anm2ed auto sourceSpritesheets = source.element_get(ElementType::SPRITESHEETS); auto destinationSpritesheets = anm2.element_get(ElementType::SPRITESHEETS); auto spritesheet = - sourceSpritesheets ? element_child_id_get(*sourceSpritesheets, ElementType::SPRITESHEET, sourceId) : nullptr; + sourceSpritesheets ? child_id_get(*sourceSpritesheets, ElementType::SPRITESHEET, sourceId) : nullptr; if (!spritesheet || !destinationSpritesheets) return -1; auto imported = *spritesheet; @@ -1660,7 +1660,7 @@ namespace anm2ed group.name = item.name; group.isExpanded = item.isExpanded; group.isVisible = item.isVisible; - if (auto root = element_child_first_get(item, ElementType::ROOT_ANIMATION)) + if (auto root = child_first_get(item, ElementType::ROOT_ANIMATION)) group.children.push_back(*root); else { @@ -1692,14 +1692,14 @@ namespace anm2ed animation.frameNum = incoming.frameNum; animation.isLoop = incoming.isLoop; - if (auto root = element_child_first_get(incoming, ElementType::ROOT_ANIMATION)) + if (auto root = child_first_get(incoming, ElementType::ROOT_ANIMATION)) { auto item = *root; item_remap(item); animation.children.push_back(item); } - if (auto layerAnimations = element_child_first_get(incoming, ElementType::LAYER_ANIMATIONS)) + if (auto layerAnimations = child_first_get(incoming, ElementType::LAYER_ANIMATIONS)) { auto container = element_make(ElementType::LAYER_ANIMATIONS); for (auto item : layerAnimations->children) @@ -1707,7 +1707,7 @@ namespace anm2ed animation.children.push_back(container); } - if (auto nullAnimations = element_child_first_get(incoming, ElementType::NULL_ANIMATIONS)) + if (auto nullAnimations = child_first_get(incoming, ElementType::NULL_ANIMATIONS)) { auto container = element_make(ElementType::NULL_ANIMATIONS); for (auto item : nullAnimations->children) @@ -1715,7 +1715,7 @@ namespace anm2ed animation.children.push_back(container); } - if (auto triggers = element_child_first_get(incoming, ElementType::TRIGGERS)) + if (auto triggers = child_first_get(incoming, ElementType::TRIGGERS)) { auto item = *triggers; item_remap(item); @@ -1745,7 +1745,7 @@ namespace anm2ed auto sourceGroupId = item.id; item.id = element_child_next_id_get(destinationContainer, ElementType::GROUP); std::erase_if(item.children, [](const Element& child) { return child.type != ElementType::ROOT_ANIMATION; }); - if (!element_child_first_get(item, ElementType::ROOT_ANIMATION)) + if (!child_first_get(item, ElementType::ROOT_ANIMATION)) { auto root = element_make(ElementType::ROOT_ANIMATION); root.children.push_back(element_make(ElementType::FRAME)); @@ -1792,20 +1792,20 @@ namespace anm2ed destination->frameNum = std::max(destination->frameNum, processed.frameNum); destination->isLoop = processed.isLoop; - if (auto root = element_child_first_get(processed, ElementType::ROOT_ANIMATION); + if (auto root = child_first_get(processed, ElementType::ROOT_ANIMATION); root && !root->children.empty()) child_set(*destination, *root, ElementType::ROOT_ANIMATION); - if (auto triggers = element_child_first_get(processed, ElementType::TRIGGERS); + if (auto triggers = child_first_get(processed, ElementType::TRIGGERS); triggers && !triggers->children.empty()) child_set(*destination, *triggers, ElementType::TRIGGERS); - if (auto layerAnimations = element_child_first_get(processed, ElementType::LAYER_ANIMATIONS)) + if (auto layerAnimations = child_first_get(processed, ElementType::LAYER_ANIMATIONS)) { auto destinationLayerAnimations = track_container_get(*destination, ElementType::LAYER_ANIMATIONS); track_container_merge(*destinationLayerAnimations, *layerAnimations, ElementType::LAYER_ANIMATION); } - if (auto nullAnimations = element_child_first_get(processed, ElementType::NULL_ANIMATIONS)) + if (auto nullAnimations = child_first_get(processed, ElementType::NULL_ANIMATIONS)) { auto destinationNullAnimations = track_container_get(*destination, ElementType::NULL_ANIMATIONS); track_container_merge(*destinationNullAnimations, *nullAnimations, ElementType::NULL_ANIMATION); diff --git a/src/imgui/popup/item_properties.cpp b/src/imgui/popup/item_properties.cpp index 6adaf85..6421aae 100644 --- a/src/imgui/popup/item_properties.cpp +++ b/src/imgui/popup/item_properties.cpp @@ -20,10 +20,8 @@ namespace anm2ed::imgui::popup std::set ItemProperties::unused_items_get(Anm2& anm2, const Element* animation, int type) { - if (!animation) return {}; - if (type == LAYER) return anm2.element_unused(ElementType::LAYER_ELEMENT, *animation); - if (type == NULL_) return anm2.element_unused(ElementType::NULL_ELEMENT, *animation); - return {}; + auto row = track_container_get((ItemType)type); + return animation && row ? anm2.element_unused(row->element, animation) : std::set{}; } void ItemProperties::open() @@ -162,7 +160,7 @@ namespace anm2ed::imgui::popup else if (type == NULL_) { auto nulls = anm2.element_get(ElementType::NULLS); - auto null = nulls ? element_child_id_get(*nulls, ElementType::NULL_ELEMENT, id) : nullptr; + auto null = nulls ? child_id_get(*nulls, ElementType::NULL_ELEMENT, id) : nullptr; if (null) { auto label = std::vformat(localize.get(FORMAT_NULL), std::make_format_args(id, null->name)); @@ -191,7 +189,7 @@ namespace anm2ed::imgui::popup auto queuedType = type; auto queuedDestination = destination; auto queuedAnimationIndex = reference.animationIndex; - auto queuedInsertBeforeID = reference.itemType == LAYER ? reference.itemID : -1; + auto queuedInsertBeforeID = reference.itemType == LAYER && type == LAYER ? reference.itemID : -1; auto queuedAddItemID = addItemID; auto queuedAddItemName = addItemName; auto queuedAddItemSpritesheetID = addItemSpritesheetID; @@ -200,17 +198,16 @@ namespace anm2ed::imgui::popup manager.command_push({manager.selected, [=](Manager&, Document& document) { - int addId{-1}; + Element item{}; + item.id = queuedAddItemID; + item.name = queuedAddItemName; + item.spritesheetId = queuedAddItemSpritesheetID; + item.isShowRect = queuedAddItemIsShowRect; document.anm2_snapshot(localize.get(EDIT_ADD_ITEM)); - if (queuedType == LAYER) - addId = document.anm2.layer_animation_add( - queuedAnimationIndex, queuedAddItemID, queuedInsertBeforeID, queuedAddItemName, - queuedAddItemSpritesheetID, (destination::Type)queuedDestination); - else if (queuedType == NULL_) - addId = document.anm2.null_animation_add(queuedAnimationIndex, queuedAddItemID, - queuedAddItemName, queuedAddItemIsShowRect, - (destination::Type)queuedDestination); + auto addId = document.anm2.item_add((ItemType)queuedType, queuedAnimationIndex, item, + queuedInsertBeforeID, + (destination::Type)queuedDestination); document.anm2_change(Document::ITEMS); diff --git a/src/imgui/window/animation_preview.cpp b/src/imgui/window/animation_preview.cpp index e7ae14a..cef7684 100644 --- a/src/imgui/window/animation_preview.cpp +++ b/src/imgui/window/animation_preview.cpp @@ -944,8 +944,8 @@ namespace anm2ed::imgui float t) -> std::optional { if (track.groupId == -1) return std::nullopt; - auto group = element_child_id_get(container, ElementType::GROUP, track.groupId); - auto root = group ? element_child_first_get(*group, ElementType::ROOT_ANIMATION) : nullptr; + auto group = child_id_get(container, ElementType::GROUP, track.groupId); + auto root = group ? child_first_get(*group, ElementType::ROOT_ANIMATION) : nullptr; if (!root) return std::nullopt; return frame_generate(*root, t); }; @@ -995,7 +995,7 @@ namespace anm2ed::imgui auto draw_group_root = [&](Element& group, int groupType, float sampleTime, const glm::mat4& sampleTransform, vec3 sampleColor, float sampleAlpha, bool isOnion) { - auto groupRoot = element_child_first_get(group, ElementType::ROOT_ANIMATION); + auto groupRoot = child_first_get(group, ElementType::ROOT_ANIMATION); if (!groupRoot) return; auto rootFrame = frame_generate(*groupRoot, sampleTime); if (isOnlyShowLayers || !rootFrame.isVisible || !groupRoot->isVisible) return; @@ -1019,7 +1019,7 @@ namespace anm2ed::imgui texture_render(shaderTexture, resources.icons[icon].id, rootTransform, color); }; - if (auto layerAnimations = element_child_first_get(*animation, ElementType::LAYER_ANIMATIONS)) + if (auto layerAnimations = child_first_get(*animation, ElementType::LAYER_ANIMATIONS)) { auto layer_animation_draw = [&](auto&& self, Element& layerAnimation, bool isParentVisible = true) -> void { @@ -1027,7 +1027,7 @@ namespace anm2ed::imgui { if (isParentVisible && layerAnimation.isVisible) { - auto groupRoot = element_child_first_get(layerAnimation, ElementType::ROOT_ANIMATION); + auto groupRoot = child_first_get(layerAnimation, ElementType::ROOT_ANIMATION); if (layeredOnions && groupRoot) for (auto& sample : *layeredOnions) if (auto sampleTime = sample_time_for_item(*groupRoot, sample)) @@ -1134,7 +1134,7 @@ namespace anm2ed::imgui layer_animation_draw(layer_animation_draw, layerAnimation); } - if (auto nullAnimations = element_child_first_get(*animation, ElementType::NULL_ANIMATIONS)) + if (auto nullAnimations = child_first_get(*animation, ElementType::NULL_ANIMATIONS)) { auto null_animation_draw = [&](auto&& self, Element& nullAnimation, bool isParentVisible = true) -> void { @@ -1142,7 +1142,7 @@ namespace anm2ed::imgui { if (isParentVisible && nullAnimation.isVisible) { - auto groupRoot = element_child_first_get(nullAnimation, ElementType::ROOT_ANIMATION); + auto groupRoot = child_first_get(nullAnimation, ElementType::ROOT_ANIMATION); if (layeredOnions && groupRoot) for (auto& sample : *layeredOnions) if (auto sampleTime = sample_time_for_item(*groupRoot, sample)) @@ -1163,7 +1163,7 @@ namespace anm2ed::imgui auto id = nullAnimation.nullId; auto nulls = sampleAnm2.element_get(ElementType::NULLS); - auto nullInfo = nulls ? element_child_id_get(*nulls, ElementType::NULL_ELEMENT, id) : nullptr; + auto nullInfo = nulls ? child_id_get(*nulls, ElementType::NULL_ELEMENT, id) : nullptr; if (!nullInfo) return; auto isShowRect = nullInfo->isShowRect; @@ -1314,7 +1314,7 @@ namespace anm2ed::imgui ImGui::PushFont(resources.fonts[font::BOLD].get(), font::SIZE_LARGE); auto triggerTextColor = isLightTheme ? TRIGGER_TEXT_COLOR_LIGHT : TRIGGER_TEXT_COLOR_DARK; auto events = anm2.element_get(ElementType::EVENTS); - auto event = events ? element_child_id_get(*events, ElementType::EVENT_ELEMENT, trigger.eventId) : nullptr; + auto event = events ? child_id_get(*events, ElementType::EVENT_ELEMENT, trigger.eventId) : nullptr; if (event) drawList->AddText(textPos, ImGui::GetColorU32(triggerTextColor), event->name.c_str()); ImGui::PopFont(); drawList->PopClipRect(); @@ -1379,7 +1379,7 @@ namespace anm2ed::imgui auto item = anm2.element_get(editItemReference); auto nulls = anm2.element_get(ElementType::NULLS); auto selectedNull = editItemType == ItemType::NULL_ && nulls - ? element_child_id_get(*nulls, ElementType::NULL_ELEMENT, editReference.itemID) + ? child_id_get(*nulls, ElementType::NULL_ELEMENT, editReference.itemID) : nullptr; bool isSelectedNullRect = selectedNull && selectedNull->isShowRect; auto null_rect_top_left = [](const Element& frame) { return frame.position - (frame.scale * 0.5f); }; diff --git a/src/imgui/window/shaders.cpp b/src/imgui/window/shaders.cpp index f5605ae..50e689d 100644 --- a/src/imgui/window/shaders.cpp +++ b/src/imgui/window/shaders.cpp @@ -55,7 +55,7 @@ namespace anm2ed::imgui Element* shader_element_get(Document& document, int id) { auto shaders = document.anm2.element_get(ElementType::SHADERS); - return shaders ? element_child_id_get(*shaders, ElementType::SHADER, id) : nullptr; + return shaders ? child_id_get(*shaders, ElementType::SHADER, id) : nullptr; } std::filesystem::path shader_asset_path_get(Document& document, const std::filesystem::path& path) @@ -124,8 +124,12 @@ namespace anm2ed::imgui void shader_uniform_config_save(Document& document, Element& shaderElement, const resource::shader::Uniform& uniform) { - auto config = shader_uniform_get(shaderElement, uniform.name, true); - if (!config) return; + auto config = shader_uniform_get(shaderElement, uniform.name); + if (!config) + { + config = &shaderElement.children.emplace_back(element_make(ElementType::UNIFORM)); + config->name = uniform.name; + } config->binding = std::string(resource::shader::uniform_binding_value_get(uniform.binding)); config->value = resource::shader::uniform_value_string_get(uniform); @@ -133,8 +137,12 @@ namespace anm2ed::imgui { for (int index = 0; index < (int)uniform.components.size(); ++index) { - auto component = shader_uniform_component_get(*config, index, true); - if (!component) continue; + auto component = shader_uniform_component_get(*config, index); + if (!component) + { + component = &config->children.emplace_back(element_make(ElementType::COMPONENT)); + component->index = index; + } component->binding = std::string(resource::shader::uniform_binding_value_get(uniform.components[index].binding)); component->value = std::format("{:.6g}", uniform.components[index].value); @@ -382,10 +390,10 @@ namespace anm2ed::imgui for (auto& shader : shaders->children) if (shader.type == ElementType::SHADER) ids.push_back(shader.id); - if (reference != -1 && !element_child_id_get(*shaders, ElementType::SHADER, reference)) reference = -1; + if (reference != -1 && !child_id_get(*shaders, ElementType::SHADER, reference)) reference = -1; for (auto it = selection.begin(); it != selection.end();) { - if (!element_child_id_get(*shaders, ElementType::SHADER, *it)) + if (!child_id_get(*shaders, ElementType::SHADER, *it)) it = selection.erase(it); else ++it; diff --git a/src/imgui/window/spritesheet_editor.cpp b/src/imgui/window/spritesheet_editor.cpp index 4bd6dba..814bd80 100644 --- a/src/imgui/window/spritesheet_editor.cpp +++ b/src/imgui/window/spritesheet_editor.cpp @@ -122,7 +122,7 @@ namespace anm2ed::imgui auto zoom_out = [&]() { zoom_adjust(-ZOOM_LEVEL_STEP); }; auto region_get = [&](int id) - { return spritesheet ? element_child_id_get(*spritesheet, ElementType::REGION, id) : nullptr; }; + { return spritesheet ? child_id_get(*spritesheet, ElementType::REGION, id) : nullptr; }; if (ImGui::Begin(localize.get(LABEL_SPRITESHEET_EDITOR_WINDOW), &settings.windowIsSpritesheetEditor)) { @@ -553,7 +553,7 @@ namespace anm2ed::imgui auto spritesheet = document.anm2.element_get(ElementType::SPRITESHEET, queuedSpritesheet); if (!spritesheet) return; - auto region = element_child_id_get(*spritesheet, ElementType::REGION, id); + auto region = child_id_get(*spritesheet, ElementType::REGION, id); if (!region) return; update(*region); }}); @@ -569,7 +569,7 @@ namespace anm2ed::imgui if (!spritesheet) return; for (auto id : queuedSelection) { - auto region = element_child_id_get(*spritesheet, ElementType::REGION, id); + auto region = child_id_get(*spritesheet, ElementType::REGION, id); if (!region) continue; update(*region); } diff --git a/src/imgui/window/timeline/commands.cpp b/src/imgui/window/timeline/commands.cpp index fb50fe7..cb87467 100644 --- a/src/imgui/window/timeline/commands.cpp +++ b/src/imgui/window/timeline/commands.cpp @@ -60,7 +60,7 @@ namespace anm2ed::imgui null_get = [&](int id) { auto nulls = anm2.element_get(ElementType::NULLS); - return nulls ? element_child_id_get(*nulls, ElementType::NULL_ELEMENT, id) : nullptr; + return nulls ? child_id_get(*nulls, ElementType::NULL_ELEMENT, id) : nullptr; }; spritesheet_get = [&](int id) { return anm2.element_get(ElementType::SPRITESHEET, id); }; info_get = [&]() { return element_first_get(anm2.root, ElementType::INFO); }; @@ -84,12 +84,12 @@ namespace anm2ed::imgui }; track_container_get = [&](int type) { - return animation ? element_child_first_get(*animation, container_type_get(type)) : nullptr; + return animation ? child_first_get(*animation, container_type_get(type)) : nullptr; }; track_group_get = [&](int type, int groupId) { auto container = track_container_get(type); - return container ? element_child_id_get(*container, ElementType::GROUP, groupId) : nullptr; + return container ? child_id_get(*container, ElementType::GROUP, groupId) : nullptr; }; is_track_group_visible = [&](int type, int groupId) { @@ -829,7 +829,7 @@ namespace anm2ed::imgui auto add_items = [&](const Element& container, int itemType) { - auto trackType = item_type_to_track_type_get(static_cast(itemType)); + auto trackType = TYPE_TRACKS[itemType]; auto add_item = [&](auto&& self, const Element& item) -> void { if (item.type == ElementType::GROUP) @@ -848,10 +848,10 @@ namespace anm2ed::imgui }; if (isLayers) - if (auto layerAnimations = element_child_first_get(targetAnimation, ElementType::LAYER_ANIMATIONS)) + if (auto layerAnimations = child_first_get(targetAnimation, ElementType::LAYER_ANIMATIONS)) add_items(*layerAnimations, LAYER); if (isNulls) - if (auto nullAnimations = element_child_first_get(targetAnimation, ElementType::NULL_ANIMATIONS)) + if (auto nullAnimations = child_first_get(targetAnimation, ElementType::NULL_ANIMATIONS)) add_items(*nullAnimations, NULL_); return result; }; @@ -1070,33 +1070,9 @@ namespace anm2ed::imgui auto nextFrame = track_frame_get(*item, targetReference.frameIndex + 1); if (frame->interpolation != Interpolation::NONE && nextFrame) - { - float interpolation = (float)firstDuration / (float)originalDuration; - switch (frame->interpolation) - { - case Interpolation::EASE_IN: - interpolation *= interpolation; - break; - case Interpolation::EASE_OUT: - interpolation = 1.0f - ((1.0f - interpolation) * (1.0f - interpolation)); - break; - case Interpolation::EASE_IN_OUT: - interpolation = interpolation < 0.5f - ? (2.0f * interpolation * interpolation) - : (1.0f - std::pow(-2.0f * interpolation + 2.0f, 2.0f) * 0.5f); - break; - case Interpolation::LINEAR: - case Interpolation::NONE: - default: - break; - } - - splitFrame.rotation = glm::mix(frame->rotation, nextFrame->rotation, interpolation); - splitFrame.position = glm::mix(frame->position, nextFrame->position, interpolation); - splitFrame.scale = glm::mix(frame->scale, nextFrame->scale, interpolation); - splitFrame.colorOffset = glm::mix(frame->colorOffset, nextFrame->colorOffset, interpolation); - splitFrame.tint = glm::mix(frame->tint, nextFrame->tint, interpolation); - } + frame_mix(splitFrame, *nextFrame, + interpolation_factor(frame->interpolation, + (float)firstDuration / (float)originalDuration)); frame->duration = firstDuration; auto insertIndex = item_frame_insert_index_get(*item, targetReference.frameIndex + 1); @@ -1152,7 +1128,7 @@ namespace anm2ed::imgui rows.push_back({.type = ROOT}); - if (auto layerAnimations = element_child_first_get(*animation, ElementType::LAYER_ANIMATIONS)) + if (auto layerAnimations = child_first_get(*animation, ElementType::LAYER_ANIMATIONS)) { auto groupIds = group_ids_get(*layerAnimations); auto layer_track_push = [&](int groupId, int depth) @@ -1183,7 +1159,7 @@ namespace anm2ed::imgui } } - if (auto nullAnimations = element_child_first_get(*animation, ElementType::NULL_ANIMATIONS)) + if (auto nullAnimations = child_first_get(*animation, ElementType::NULL_ANIMATIONS)) { auto groupIds = group_ids_get(*nullAnimations); auto null_track_push = [&](int groupId, int depth) @@ -1418,7 +1394,7 @@ namespace anm2ed::imgui { auto containerType = container_type_get(targetType); auto targetTrackType = track_type_get(targetType); - auto container = element_child_first_get(*animation, containerType); + auto container = child_first_get(*animation, containerType); if (!container) continue; auto ids = targetIds.contains(targetType) ? targetIds.at(targetType) : std::set{}; auto groupIds = @@ -1480,7 +1456,7 @@ namespace anm2ed::imgui auto containerType = container_type_get(targetType); auto targetTrackType = track_type_get(targetType); - auto container = element_child_first_get(*animation, containerType); + auto container = child_first_get(*animation, containerType); if (!container) return; std::set targetIds{}; @@ -1538,7 +1514,7 @@ namespace anm2ed::imgui { auto animation = command_animation_get(document, animationIndex); if (!animation) return; - auto container = element_child_first_get(*animation, container_type_get(targetType)); + auto container = child_first_get(*animation, container_type_get(targetType)); if (!container) return; auto targetTrackType = track_type_get(targetType); @@ -1681,7 +1657,7 @@ namespace anm2ed::imgui frameReference.groupId); auto frame = item ? track_frame_get(*item, frameReference.frameIndex) : nullptr; if (!frame) continue; - auto parentType = item_type_to_track_type_get(item_type_get(frameReference.itemType)); + auto parentType = TYPE_TRACKS[frameReference.itemType]; clipboardString += element_to_string(*frame, parentType); } if (!clipboardString.empty()) clipboard.set(clipboardString); @@ -1758,7 +1734,7 @@ namespace anm2ed::imgui auto frame = track_frame_get(*item, i); if (!frame || frame->regionId == -1) continue; auto region = - spritesheet ? element_child_id_get(*spritesheet, ElementType::REGION, frame->regionId) + spritesheet ? child_id_get(*spritesheet, ElementType::REGION, frame->regionId) : nullptr; if (!region) frame->regionId = -1; } @@ -2001,9 +1977,9 @@ namespace anm2ed::imgui { auto animation = command_animation_get(document, targetAnimationIndex); auto container = - animation ? element_child_first_get(*animation, container_type_get(targetType)) + animation ? child_first_get(*animation, container_type_get(targetType)) : nullptr; - auto group = container ? element_child_id_get(*container, ElementType::GROUP, targetId) + auto group = container ? child_id_get(*container, ElementType::GROUP, targetId) : nullptr; if (!group) return; group->name = targetName; diff --git a/src/imgui/window/timeline/tracks.cpp b/src/imgui/window/timeline/tracks.cpp index bb8fab3..b30c3ca 100644 --- a/src/imgui/window/timeline/tracks.cpp +++ b/src/imgui/window/timeline/tracks.cpp @@ -51,9 +51,9 @@ namespace anm2ed::imgui { auto animation = command_animation_get(document, targetAnimationIndex); auto container = - animation ? element_child_first_get(*animation, container_type_get(targetRow.type)) + animation ? child_first_get(*animation, container_type_get(targetRow.type)) : nullptr; - auto group = container ? element_child_id_get(*container, ElementType::GROUP, + auto group = container ? child_id_get(*container, ElementType::GROUP, targetRow.id) : nullptr; if (!group) return; @@ -154,10 +154,10 @@ namespace anm2ed::imgui { auto animation = command_animation_get(document, targetAnimationIndex); auto container = - animation ? element_child_first_get(*animation, container_type_get(targetType)) + animation ? child_first_get(*animation, container_type_get(targetType)) : nullptr; if (!container) return; - auto group = element_child_id_get(*container, ElementType::GROUP, targetGroupId); + auto group = child_id_get(*container, ElementType::GROUP, targetGroupId); if (!group) return; group->isVisible = targetVisible; }); @@ -453,7 +453,7 @@ namespace anm2ed::imgui [=, this](Manager&, Document& document) { auto nulls = document.anm2.element_get(ElementType::NULLS); - auto null = nulls ? element_child_id_get(*nulls, ElementType::NULL_ELEMENT, nullID) + auto null = nulls ? child_id_get(*nulls, ElementType::NULL_ELEMENT, nullID) : nullptr; if (!null) return; null->isShowRect = !null->isShowRect; diff --git a/src/imgui/window/window.cpp b/src/imgui/window/window.cpp index 4a987df..ca0f2fa 100644 --- a/src/imgui/window/window.cpp +++ b/src/imgui/window/window.cpp @@ -106,7 +106,7 @@ namespace anm2ed::imgui { if (window.element_get) return window.element_get(anm2, key); auto container = window_container_get(window, anm2); - return container ? element_child_id_get(*container, window.elementType, key) : nullptr; + return container ? child_id_get(*container, window.elementType, key) : nullptr; } int region_insert_index_get(const Element& spritesheet, const Storage& storage) @@ -208,7 +208,7 @@ namespace anm2ed::imgui auto element = elementGet ? elementGet(document.anm2, key) : nullptr; if (!element) if (auto container = document.anm2.element_get(containerType)) - element = element_child_id_get(*container, elementType, key); + element = child_id_get(*container, elementType, key); if (!element || element->name == name) return; document.anm2_snapshot(localize.get(renameEdit)); @@ -987,7 +987,7 @@ namespace anm2ed::imgui } auto spritesheet = document.anm2.element_get(ElementType::SPRITESHEET, options.spritesheetId); - auto region = spritesheet ? element_child_id_get(*spritesheet, ElementType::REGION, options.regionId) : nullptr; + auto region = spritesheet ? child_id_get(*spritesheet, ElementType::REGION, options.regionId) : nullptr; auto texture = document.texture_get(options.spritesheetId); if (!spritesheet || !region || !texture || !texture->is_valid()) { @@ -1774,7 +1774,7 @@ namespace anm2ed::imgui manager.command_push({manager.selected, [targetGroupId, targetName](Manager&, Document& document) { auto animations = document.anm2.element_get(ElementType::ANIMATIONS); - auto group = animations ? element_child_id_get(*animations, ElementType::GROUP, + auto group = animations ? child_id_get(*animations, ElementType::GROUP, targetGroupId) : nullptr; if (!group || group->name == targetName) return; @@ -1793,7 +1793,7 @@ namespace anm2ed::imgui manager.command_push({manager.selected, [targetGroupId, isGroupOpen](Manager&, Document& document) { auto animations = document.anm2.element_get(ElementType::ANIMATIONS); - auto group = animations ? element_child_id_get(*animations, ElementType::GROUP, + auto group = animations ? child_id_get(*animations, ElementType::GROUP, targetGroupId) : nullptr; if (!group || group->isExpanded == isGroupOpen) return; @@ -1883,8 +1883,8 @@ namespace anm2ed::imgui const Element* referenceNullAnimations{}; if (auto referenceAnimation = anm2.element_get(ElementType::ANIMATION, reference.animationIndex)) { - referenceLayerAnimations = element_child_first_get(*referenceAnimation, ElementType::LAYER_ANIMATIONS); - referenceNullAnimations = element_child_first_get(*referenceAnimation, ElementType::NULL_ANIMATIONS); + referenceLayerAnimations = child_first_get(*referenceAnimation, ElementType::LAYER_ANIMATIONS); + referenceNullAnimations = child_first_get(*referenceAnimation, ElementType::NULL_ANIMATIONS); } animation.children.push_back(window_track_container_shell_copy(referenceLayerAnimations, @@ -2387,7 +2387,7 @@ namespace anm2ed::imgui auto spritesheet = document.anm2.element_get(ElementType::SPRITESHEET, document.spritesheet.reference); if (!spritesheet || selection.size() != 1) return; auto id = *selection.begin(); - if (!element_child_id_get(*spritesheet, ElementType::REGION, id)) return; + if (!child_id_get(*spritesheet, ElementType::REGION, id)) return; reference = id; window.popup.open(); }; @@ -2398,7 +2398,7 @@ namespace anm2ed::imgui auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, spritesheetReference); if (!spritesheet) return; - auto unused = anm2.element_unused(ElementType::REGION, spritesheetReference); + auto unused = anm2.region_unused(spritesheetReference); if (unused.empty()) return; auto behavior = [&]() @@ -2411,7 +2411,7 @@ namespace anm2ed::imgui for (auto& animation : animations->children) { if (animation.type != ElementType::ANIMATION) continue; - auto layerAnimations = element_child_first_get(animation, ElementType::LAYER_ANIMATIONS); + auto layerAnimations = child_first_get(animation, ElementType::LAYER_ANIMATIONS); if (!layerAnimations) continue; auto region_clear = [&](auto&& self, Element& layerAnimation) -> void { @@ -2486,7 +2486,7 @@ namespace anm2ed::imgui auto insertIndex = region_insert_index_get(*spritesheet, document.region); auto pasted = anm2; std::string errorString{}; - if (pasted.regions_deserialize(spritesheetReference, clipboard.get(), true, &errorString)) + if (pasted.deserialize(ElementType::REGION, clipboard.get(), true, &errorString, {}, spritesheetReference)) { document.anm2_snapshot(localize.get(EDIT_PASTE_REGIONS)); anm2 = std::move(pasted); @@ -2590,7 +2590,7 @@ namespace anm2ed::imgui for (int i = 0; i < (int)window.order.size(); i++) { int id = window.order[i]; - auto region = element_child_id_get(*spritesheet, ElementType::REGION, id); + auto region = child_id_get(*spritesheet, ElementType::REGION, id); if (!region) continue; auto isNewRegion = window.newElementId == id; auto nameCStr = region->name.c_str(); @@ -2817,7 +2817,7 @@ namespace anm2ed::imgui { for (auto regionId : window.dragSelection) { - auto drag = element_child_id_get(*spritesheet, ElementType::REGION, regionId); + auto drag = child_id_get(*spritesheet, ElementType::REGION, regionId); if (drag) ImGui::TextUnformatted(drag->name.c_str()); } ImGui::EndTooltip(); @@ -2925,7 +2925,7 @@ namespace anm2ed::imgui auto region_get = [&](int id) { auto spritesheet = spritesheet_get(); - return spritesheet ? element_child_id_get(*spritesheet, ElementType::REGION, id) : nullptr; + return spritesheet ? child_id_get(*spritesheet, ElementType::REGION, id) : nullptr; }; window.popup.trigger(); @@ -3026,7 +3026,7 @@ namespace anm2ed::imgui auto set = [&]() { auto target = - element_child_id_get(*spritesheet, ElementType::REGION, editedReference); + child_id_get(*spritesheet, ElementType::REGION, editedReference); if (!target) return; auto changed = editedRegion; changed.type = ElementType::REGION; @@ -3917,7 +3917,7 @@ namespace anm2ed::imgui auto sounds = document.anm2.element_get(ElementType::SOUNDS); if (!sounds) return; for (auto& id : selection) - if (auto sound = element_child_id_get(*sounds, ElementType::SOUND_ELEMENT, id)) + if (auto sound = child_id_get(*sounds, ElementType::SOUND_ELEMENT, id)) clipboardText += element_to_string(*sound); clipboard.set(clipboardText); }; @@ -4304,7 +4304,7 @@ namespace anm2ed::imgui } auto target = - element_child_id_get(*layers, ElementType::LAYER_ELEMENT, editedReference); + child_id_get(*layers, ElementType::LAYER_ELEMENT, editedReference); if (!target) return; changed.id = editedReference; *target = changed; @@ -4410,7 +4410,7 @@ namespace anm2ed::imgui return; } - auto target = element_child_id_get(*nulls, ElementType::NULL_ELEMENT, editedReference); + auto target = child_id_get(*nulls, ElementType::NULL_ELEMENT, editedReference); if (!target) return; changed.id = editedReference; *target = changed; diff --git a/src/imgui/wizard/about.cpp b/src/imgui/wizard/about.cpp index 24de14a..f2efd23 100644 --- a/src/imgui/wizard/about.cpp +++ b/src/imgui/wizard/about.cpp @@ -256,7 +256,7 @@ namespace anm2ed::imgui::wizard rootFrame.rotation, math::percent_to_unit(rootFrame.shear)); } - if (auto layerAnimations = element_child_first_get(*animation, ElementType::LAYER_ANIMATIONS)) + if (auto layerAnimations = child_first_get(*animation, ElementType::LAYER_ANIMATIONS)) for (auto& layerAnimation : layerAnimations->children) friend_layer_draw(state, resources, transform, layerAnimation, rootFrame); diff --git a/src/imgui/wizard/change_all_frame_properties.cpp b/src/imgui/wizard/change_all_frame_properties.cpp index 13bda08..e7629ca 100644 --- a/src/imgui/wizard/change_all_frame_properties.cpp +++ b/src/imgui/wizard/change_all_frame_properties.cpp @@ -460,7 +460,7 @@ namespace anm2ed::imgui::wizard if (queuedIsLayers) { auto layerAnimations = - element_child_first_get(*animation, ElementType::LAYER_ANIMATIONS); + child_first_get(*animation, ElementType::LAYER_ANIMATIONS); if (layerAnimations) { auto item_change = [&](auto&& self, Element& item) -> void @@ -485,7 +485,7 @@ namespace anm2ed::imgui::wizard if (queuedIsNulls) { auto nullAnimations = - element_child_first_get(*animation, ElementType::NULL_ANIMATIONS); + child_first_get(*animation, ElementType::NULL_ANIMATIONS); if (nullAnimations) { auto item_change = [&](auto&& self, Element& item) -> void diff --git a/src/manager.cpp b/src/manager.cpp index 959e6b7..4aa66f1 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -267,7 +267,7 @@ namespace anm2ed auto nulls = document->anm2.element_get(ElementType::NULLS); if (id == -1) editNull = element_make(ElementType::NULL_ELEMENT); - else if (auto null = nulls ? element_child_id_get(*nulls, ElementType::NULL_ELEMENT, id) : nullptr; null) + else if (auto null = nulls ? child_id_get(*nulls, ElementType::NULL_ELEMENT, id) : nullptr; null) editNull = *null; else return; diff --git a/src/snapshots.cpp b/src/snapshots.cpp index d5c75c5..328dcd1 100644 --- a/src/snapshots.cpp +++ b/src/snapshots.cpp @@ -190,7 +190,7 @@ namespace anm2ed::snapshots { auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, spritesheetId); if (!spritesheet) return std::nullopt; - auto region = element_child_id_get(*spritesheet, ElementType::REGION, regionId); + auto region = child_id_get(*spritesheet, ElementType::REGION, regionId); if (!region) return std::nullopt; std::vector path{}; @@ -256,7 +256,7 @@ namespace anm2ed::snapshots auto spritesheet = snapshot.anm2.element_get(ElementType::SPRITESHEET, spritesheetId); if (!spritesheet) return; - auto region = element_child_id_get(*spritesheet, ElementType::REGION, regionId); + auto region = child_id_get(*spritesheet, ElementType::REGION, regionId); if (!region) return; step.anm2.elements.push_back({std::move(*path), *region, {}});