Stage 7b: model/xml.cpp reads and writes tinyxml2 directly; Element tree removed
- Reading goes from tinyxml2 straight into the typed model, including legacy layouts (flat GroupId groups, *AnimationGroups with Index, FrameRestore backups, baked frames, SoundId, Overlay) and load-time id/region repair. - Writing builds an XmlNode tree from the typed model for a set of flags: id renumbering, region sync, group-root and interpolation baking happen on typed data; groups are nested or extracted the way the old pipeline did. - Unknown XML is kept verbatim (XmlNode) instead of being re-emitted through the Element writer. - model_hash is an FNV hash of the editor-format node tree (what a save would write); the per-fixture hash goldens are gone. - Deletes src/anm2 (Element, normalize, groups, hash, xml) and the unused util/xml attribute helpers and ElementType/ItemType table columns. - All goldens, invariants and the local corpus are byte-identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0b6f67d9bd
commit
e50d76c784
33 files changed
+1365
-2914
No files matched your search
+1
-3
@@ -84,8 +84,6 @@ set(IMGUI_SRC
|
||||
set(TINYXML2_SRC external/tinyxml2/tinyxml2.cpp)
|
||||
|
||||
file(GLOB PROJECT_SRC CONFIGURE_DEPENDS
|
||||
src/anm2/*.cpp
|
||||
src/anm2/*.hpp
|
||||
src/resource/*.cpp
|
||||
src/resource/*.hpp
|
||||
src/imgui/*.cpp
|
||||
@@ -108,7 +106,7 @@ file(GLOB PROJECT_SRC CONFIGURE_DEPENDS
|
||||
src/*.hpp)
|
||||
|
||||
set(CORE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
||||
file(GLOB CORE_SRC CONFIGURE_DEPENDS ${CORE_DIR}/anm2/*.cpp ${CORE_DIR}/edit/*.cpp ${CORE_DIR}/model/*.cpp)
|
||||
file(GLOB CORE_SRC CONFIGURE_DEPENDS ${CORE_DIR}/edit/*.cpp ${CORE_DIR}/model/*.cpp)
|
||||
foreach(CORE_FILE log resource/strings resource/image resource/audio_data resource/asset_store util/file util/path util/math util/string util/time util/working_directory
|
||||
util/xml util/pack util/imgui/multiselect snapshots storage playback selection)
|
||||
list(APPEND CORE_SRC ${CORE_DIR}/${CORE_FILE}.cpp)
|
||||
|
||||
@@ -1,148 +0,0 @@
|
||||
#include "internal.hpp"
|
||||
|
||||
using namespace anm2ed::util;
|
||||
using namespace tinyxml2;
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
constexpr const char* CREATED_ON_FORMAT = "%m/%d/%Y %I:%M:%S %p";
|
||||
|
||||
bool is_source_document_tag(std::string_view tag) { return tag == SOURCE_DOCUMENT_TAG; }
|
||||
|
||||
const XMLElement* source_document_get(const XMLElement* rootElement)
|
||||
{
|
||||
if (!rootElement) return nullptr;
|
||||
if (is_source_document_tag(rootElement->Name() ? rootElement->Name() : "")) return rootElement;
|
||||
|
||||
for (auto child = rootElement->FirstChildElement(); child; child = child->NextSiblingElement())
|
||||
if (is_source_document_tag(child->Name() ? child->Name() : "")) return child;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool anm2_document_load(Anm2& anm2, XMLDocument& document, std::string* errorString)
|
||||
{
|
||||
auto rootElement = document.RootElement();
|
||||
if (!rootElement)
|
||||
{
|
||||
if (errorString) *errorString = "No root element.";
|
||||
anm2.isValid = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto sourceElement = source_document_get(rootElement);
|
||||
anm2.root = element_read(sourceElement ? sourceElement : rootElement);
|
||||
if (sourceElement)
|
||||
{
|
||||
anm2.root.type = ElementType::ANIMATED_ACTOR;
|
||||
anm2.root.tag = "AnimatedActor";
|
||||
}
|
||||
group_metadata_embed(anm2.root);
|
||||
group_frames_restore(anm2.root);
|
||||
groups_flatten(anm2.root);
|
||||
content_ids_repair(anm2.root);
|
||||
shader_frame_ids_repair(anm2.root);
|
||||
region_frame_ids_repair(anm2.root);
|
||||
anm2.region_frames_sync(true);
|
||||
anm2.isValid = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
Anm2::Anm2()
|
||||
{
|
||||
root = element_make(ElementType::ANIMATED_ACTOR);
|
||||
|
||||
auto info = element_make(ElementType::INFO);
|
||||
info.createdOn = time::get(CREATED_ON_FORMAT);
|
||||
|
||||
auto content = element_make(ElementType::CONTENT);
|
||||
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));
|
||||
root.children.push_back(element_make(ElementType::ANIMATIONS));
|
||||
}
|
||||
|
||||
bool Anm2::load(const std::filesystem::path& path, std::string* errorString)
|
||||
{
|
||||
XMLDocument document{};
|
||||
File file(path, "rb");
|
||||
if (!file)
|
||||
{
|
||||
if (errorString) *errorString = "File not found.";
|
||||
isValid = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (document.LoadFile(file.get()) != XML_SUCCESS)
|
||||
{
|
||||
if (errorString) *errorString = document.ErrorStr();
|
||||
isValid = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
return anm2_document_load(*this, document, errorString);
|
||||
}
|
||||
|
||||
bool Anm2::load_string(std::string_view string, std::string* errorString)
|
||||
{
|
||||
XMLDocument document{};
|
||||
if (document.Parse(string.data(), string.size()) != XML_SUCCESS)
|
||||
{
|
||||
if (errorString) *errorString = document.ErrorStr();
|
||||
isValid = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
return anm2_document_load(*this, document, errorString);
|
||||
}
|
||||
|
||||
bool Anm2::save(const std::filesystem::path& path, std::string* errorString, Options options) const
|
||||
{
|
||||
XMLDocument document{};
|
||||
document.InsertFirstChild(to_element(document, options));
|
||||
|
||||
File file(path, "wb");
|
||||
if (!file)
|
||||
{
|
||||
if (errorString) *errorString = "File permissions.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (document.SaveFile(file.get()) != XML_SUCCESS)
|
||||
{
|
||||
if (errorString) *errorString = document.ErrorStr();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string Anm2::to_string(Options options) const
|
||||
{
|
||||
XMLDocument document{};
|
||||
document.InsertEndChild(to_element(document, options));
|
||||
return xml::document_to_string(document);
|
||||
}
|
||||
|
||||
XMLElement* Anm2::to_element(XMLDocument& document, Options options) const
|
||||
{
|
||||
auto serialize = [&](Flags flags)
|
||||
{
|
||||
auto normalized = normalized_for_serialize(flags);
|
||||
normalized.region_frames_sync(true);
|
||||
return element_to_xml(document, normalized.root, ElementType::UNKNOWN, flags);
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,256 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <tinyxml2/tinyxml2.h>
|
||||
|
||||
#include "model/common.hpp"
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
struct Element
|
||||
{
|
||||
ElementType type{ElementType::UNKNOWN};
|
||||
std::string tag{};
|
||||
std::vector<Element> children{};
|
||||
std::string name{};
|
||||
std::string createdBy{"robot"};
|
||||
std::string createdOn{};
|
||||
std::filesystem::path path{};
|
||||
std::filesystem::path vertex{};
|
||||
std::filesystem::path fragment{};
|
||||
std::string binding{};
|
||||
std::string value{};
|
||||
std::string defaultAnimation{};
|
||||
int id{-1};
|
||||
int layerId{-1};
|
||||
int nullId{-1};
|
||||
int spritesheetId{};
|
||||
int fps{30};
|
||||
int version{};
|
||||
int frameNum{1};
|
||||
int duration{1};
|
||||
int atFrame{-1};
|
||||
int eventId{-1};
|
||||
int regionId{-1};
|
||||
int shaderId{-1};
|
||||
int soundId{-1};
|
||||
int groupId{-1};
|
||||
int index{-1};
|
||||
bool isLoop{true};
|
||||
bool isVisible{true};
|
||||
bool isShowRect{};
|
||||
bool isExpanded{true};
|
||||
bool isEnabled{true};
|
||||
Interpolation interpolation{Interpolation::NONE};
|
||||
Origin origin{Origin::CUSTOM};
|
||||
float rotation{};
|
||||
std::vector<int> soundIds{};
|
||||
std::vector<std::pair<std::string, std::string>> extraAttributes{};
|
||||
std::string text{};
|
||||
glm::vec2 pivot{};
|
||||
glm::vec2 crop{};
|
||||
glm::vec2 position{};
|
||||
glm::vec2 size{};
|
||||
glm::vec2 scale{100.0f, 100.0f};
|
||||
glm::vec2 shear{};
|
||||
glm::vec3 colorOffset{};
|
||||
glm::vec4 tint{types::color::WHITE};
|
||||
};
|
||||
|
||||
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 <class E> using ElementPointer = std::conditional_t<std::is_const_v<E>, 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 <class E, class Predicate> ElementPointer<E> child_find(E& element, Predicate&& isMatch)
|
||||
{
|
||||
for (auto& child : element.children)
|
||||
if (isMatch(child)) return &child;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <class E> ElementPointer<E> child_first_get(E& element, ElementType type)
|
||||
{
|
||||
return child_find(element, [&](const Element& child) { return child.type == type; });
|
||||
}
|
||||
|
||||
template <class E> ElementPointer<E> child_id_get(E& element, ElementType type, int id)
|
||||
{
|
||||
return child_find(element, [&](const Element& child) { return child.type == type && child.id == id; });
|
||||
}
|
||||
|
||||
template <class E> ElementPointer<E> 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 <class E> ElementPointer<E> shader_uniform_component_get(E& uniform, int index)
|
||||
{
|
||||
return child_find(uniform, [&](const Element& child)
|
||||
{ return child.type == ElementType::COMPONENT && child.index == index; });
|
||||
}
|
||||
|
||||
template <class E> ElementPointer<E> 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 <class E> ElementPointer<E> 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 <class E> ElementPointer<E> 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 <class E>
|
||||
ElementPointer<E> 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;
|
||||
}
|
||||
|
||||
template <class E> ElementPointer<E> track_frame_get(E& track, int index)
|
||||
{
|
||||
auto childIndex = track_frame_child_index_get(track, index);
|
||||
return childIndex == -1 ? nullptr : &track.children[childIndex];
|
||||
}
|
||||
|
||||
template <class E, class Callback> 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 <class E, class Callback> 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 <class E, class Callback> void element_each(E& element, Callback&& callback)
|
||||
{
|
||||
callback(element);
|
||||
for (auto& child : element.children)
|
||||
element_each(child, callback);
|
||||
}
|
||||
|
||||
Element element_make(ElementType);
|
||||
Element& child_ensure(Element&, ElementType);
|
||||
Element element_read(const tinyxml2::XMLElement*);
|
||||
std::string element_to_string(const Element&, Flags = SERIALIZE_EDITOR_DEFAULT);
|
||||
std::string element_to_string(const Element&, ElementType, Flags = SERIALIZE_EDITOR_DEFAULT);
|
||||
tinyxml2::XMLElement* element_to_xml(tinyxml2::XMLDocument&, const Element&, ElementType,
|
||||
Flags = SERIALIZE_EDITOR_DEFAULT);
|
||||
Element root_animation_make();
|
||||
void frame_mix(Element&, const Element&, float);
|
||||
Element frame_generate(const Element&, float);
|
||||
int track_length_get(const Element&);
|
||||
|
||||
class Anm2
|
||||
{
|
||||
public:
|
||||
bool isValid{true};
|
||||
Element root{};
|
||||
|
||||
Anm2();
|
||||
|
||||
bool load(const std::filesystem::path&, std::string* = nullptr);
|
||||
bool load_string(std::string_view, std::string* = nullptr);
|
||||
bool save(const std::filesystem::path&, std::string* = nullptr, Options = {}) const;
|
||||
std::string to_string(Options = {}) const;
|
||||
tinyxml2::XMLElement* to_element(tinyxml2::XMLDocument&, Options = {}) const;
|
||||
void region_frames_sync(bool);
|
||||
Anm2 normalized_for_serialize(Flags = SERIALIZE_DEFAULT) const;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
#include "internal.hpp"
|
||||
|
||||
using namespace anm2ed::util;
|
||||
using namespace tinyxml2;
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
constexpr std::array<std::string_view, (std::size_t)ElementType::COUNT> ELEMENT_TAGS = {
|
||||
#define X(symbol, tag, container) std::string_view{tag},
|
||||
ANM2_ELEMENT_TYPES
|
||||
#undef X
|
||||
};
|
||||
|
||||
ElementType element_type_get(std::string_view tag)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return (std::size_t)type < ELEMENT_TAGS.size() ? ELEMENT_TAGS[(std::size_t)type] : std::string_view{};
|
||||
}
|
||||
|
||||
Element element_make(ElementType type)
|
||||
{
|
||||
Element element{};
|
||||
element.type = type;
|
||||
element.tag = std::string(element_tag_get(type));
|
||||
return element;
|
||||
}
|
||||
|
||||
Element root_animation_make()
|
||||
{
|
||||
auto root = element_make(ElementType::ROOT_ANIMATION);
|
||||
root.children.push_back(element_make(ElementType::FRAME));
|
||||
return root;
|
||||
}
|
||||
|
||||
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<int, int> child_ids_compact(Element& element, ElementType type)
|
||||
{
|
||||
std::unordered_map<int, int> remap{};
|
||||
int nextId{};
|
||||
for (auto& child : element.children)
|
||||
if (child.type == type)
|
||||
{
|
||||
remap[child.id] = nextId;
|
||||
child.id = nextId++;
|
||||
}
|
||||
return remap;
|
||||
}
|
||||
|
||||
bool is_track_child_valid(ElementType parentType, ElementType childType)
|
||||
{
|
||||
if (parentType == ElementType::CONTENT && childType == ElementType::SHADERS) return true;
|
||||
if (parentType == ElementType::SHADERS) return childType == ElementType::SHADER;
|
||||
if (parentType == ElementType::SHADER) return childType == ElementType::UNIFORM;
|
||||
if (parentType == ElementType::UNIFORM) return childType == ElementType::COMPONENT;
|
||||
if (childType == ElementType::SHADERS || childType == ElementType::SHADER || childType == ElementType::UNIFORM ||
|
||||
childType == ElementType::COMPONENT)
|
||||
return false;
|
||||
if (parentType == ElementType::TRIGGERS) return childType == ElementType::TRIGGER;
|
||||
if (parentType == ElementType::TRIGGER) return childType == ElementType::SOUND_ELEMENT;
|
||||
if (parentType == ElementType::LAYER_ANIMATION_GROUPS || parentType == ElementType::NULL_ANIMATION_GROUPS)
|
||||
return childType == ElementType::GROUP;
|
||||
if (parentType == ElementType::ROOT_ANIMATION || parentType == ElementType::LAYER_ANIMATION ||
|
||||
parentType == ElementType::NULL_ANIMATION)
|
||||
return childType == ElementType::FRAME;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
#include "internal.hpp"
|
||||
|
||||
using namespace anm2ed::util;
|
||||
using namespace tinyxml2;
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
enum class InterpolationBakeType
|
||||
{
|
||||
SPECIAL,
|
||||
ALL
|
||||
};
|
||||
|
||||
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<Element> 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<Element> 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));
|
||||
frame.isVisible = false;
|
||||
if (track.children.empty()) return frame;
|
||||
|
||||
time = std::max(time, 0.0f);
|
||||
|
||||
const Element* frameNext = nullptr;
|
||||
int durationCurrent{};
|
||||
int durationNext{};
|
||||
auto frameType = track_frame_type_get(track);
|
||||
|
||||
for (int i = 0; i < (int)track.children.size(); ++i)
|
||||
{
|
||||
const auto& iFrame = track.children[i];
|
||||
if (iFrame.type != frameType) continue;
|
||||
|
||||
if (frameType == ElementType::TRIGGER)
|
||||
{
|
||||
if ((int)time == iFrame.atFrame)
|
||||
{
|
||||
frame = iFrame;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
frame = iFrame;
|
||||
durationNext += frame.duration;
|
||||
|
||||
if (time >= durationCurrent && time < durationNext)
|
||||
{
|
||||
for (int next = i + 1; next < (int)track.children.size(); ++next)
|
||||
if (track.children[next].type == ElementType::FRAME)
|
||||
{
|
||||
frameNext = &track.children[next];
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
durationCurrent += frame.duration;
|
||||
}
|
||||
|
||||
if (frameType != ElementType::TRIGGER && frame.interpolation != Interpolation::NONE && frameNext &&
|
||||
frame.duration > 1)
|
||||
{
|
||||
frame_mix(frame, *frameNext,
|
||||
interpolation_factor(frame.interpolation, (time - durationCurrent) / (durationNext - durationCurrent)));
|
||||
}
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
bool is_frame_interpolation_baked(const Element& frame, InterpolationBakeType type)
|
||||
{
|
||||
if (frame.interpolation == Interpolation::NONE) return false;
|
||||
return type == InterpolationBakeType::ALL || frame.interpolation != Interpolation::LINEAR;
|
||||
}
|
||||
|
||||
void track_frames_bake(Element& track, int interval, bool isRoundScale, bool isRoundRotation,
|
||||
InterpolationBakeType type)
|
||||
{
|
||||
for (int index = (int)track.children.size() - 1; index >= 0; --index)
|
||||
{
|
||||
auto original = track.children[index];
|
||||
if (original.type != ElementType::FRAME || !is_frame_interpolation_baked(original, type)) continue;
|
||||
|
||||
if (original.duration <= FRAME_DURATION_MIN)
|
||||
{
|
||||
track.children[index].interpolation = Interpolation::NONE;
|
||||
continue;
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
void interpolated_frames_bake(Element& element, int interval, bool isRoundScale, bool isRoundRotation,
|
||||
InterpolationBakeType type)
|
||||
{
|
||||
if (is_track(element))
|
||||
{
|
||||
track_frames_bake(element, interval, isRoundScale, isRoundRotation, type);
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& child : element.children)
|
||||
interpolated_frames_bake(child, interval, isRoundScale, isRoundRotation, type);
|
||||
}
|
||||
|
||||
void all_interpolated_frames_bake(Element& element, int interval, bool isRoundScale, bool isRoundRotation)
|
||||
{
|
||||
interpolated_frames_bake(element, interval, isRoundScale, isRoundRotation, InterpolationBakeType::ALL);
|
||||
}
|
||||
|
||||
int track_length_get(const Element& track)
|
||||
{
|
||||
int length{};
|
||||
if (track.type == ElementType::TRIGGERS)
|
||||
{
|
||||
for (const auto& trigger : track.children)
|
||||
if (trigger.type == ElementType::TRIGGER) length = std::max(length, trigger.atFrame);
|
||||
return length;
|
||||
}
|
||||
|
||||
for (const auto& frame : track.children)
|
||||
if (frame.type == ElementType::FRAME) length += frame.duration;
|
||||
return length;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,388 +0,0 @@
|
||||
#include "internal.hpp"
|
||||
|
||||
using namespace anm2ed::util;
|
||||
using namespace tinyxml2;
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
constexpr std::string_view ANM2ED_GROUP_FRAME_BACKUP_TAG = "FrameRestore";
|
||||
|
||||
|
||||
bool is_frame_transform_default(const Element& frame)
|
||||
{
|
||||
auto base = element_make(ElementType::FRAME);
|
||||
return frame.position == base.position && frame.scale == base.scale && frame.rotation == base.rotation &&
|
||||
frame.shear == base.shear && frame.tint == base.tint && frame.colorOffset == base.colorOffset;
|
||||
}
|
||||
|
||||
bool is_root_animation_transform_default(const Element& root)
|
||||
{
|
||||
for (const auto& frame : root.children)
|
||||
if (frame.type == ElementType::FRAME && !is_frame_transform_default(frame)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_group_frame_backup(const Element& element)
|
||||
{
|
||||
return element.type == ElementType::UNKNOWN && element.tag == ANM2ED_GROUP_FRAME_BACKUP_TAG;
|
||||
}
|
||||
|
||||
void group_frame_backups_erase(Element& group)
|
||||
{
|
||||
std::erase_if(group.children, [](const Element& child) { return is_group_frame_backup(child); });
|
||||
}
|
||||
|
||||
Element group_frame_backup_make(const Element& container, const Element& group, ElementType trackType)
|
||||
{
|
||||
Element backup{};
|
||||
backup.type = ElementType::UNKNOWN;
|
||||
backup.tag = std::string(ANM2ED_GROUP_FRAME_BACKUP_TAG);
|
||||
|
||||
if (auto root = child_first_get(group, ElementType::ROOT_ANIMATION)) backup.children.push_back(*root);
|
||||
|
||||
for (const auto& item : container.children)
|
||||
if (item.type == trackType && item.groupId == group.id) backup.children.push_back(item);
|
||||
|
||||
return backup;
|
||||
}
|
||||
|
||||
void frame_group_root_apply(Element& frame, const Element& rootFrame)
|
||||
{
|
||||
auto rootScale = math::percent_to_unit(rootFrame.scale);
|
||||
auto offset = frame.position * rootScale;
|
||||
auto radians = glm::radians(rootFrame.rotation);
|
||||
auto cos = std::cos(radians);
|
||||
auto sin = std::sin(radians);
|
||||
|
||||
frame.position = rootFrame.position + glm::vec2(offset.x * cos - offset.y * sin, offset.x * sin + offset.y * cos);
|
||||
frame.scale *= rootScale;
|
||||
frame.rotation += rootFrame.rotation;
|
||||
frame.tint *= rootFrame.tint;
|
||||
frame.colorOffset += rootFrame.colorOffset;
|
||||
}
|
||||
|
||||
void track_group_root_bake(Element& track, const Element& root, int frameNum)
|
||||
{
|
||||
auto trackLength = track_length_get(track);
|
||||
if (trackLength <= 0) return;
|
||||
|
||||
auto length = std::max({trackLength, track_length_get(root), frameNum, FRAME_DURATION_MIN});
|
||||
std::vector<Element> frames{};
|
||||
frames.reserve(length);
|
||||
|
||||
for (int time = 0; time < length; ++time)
|
||||
{
|
||||
auto frame = frame_generate(track, (float)time);
|
||||
auto rootFrame = frame_generate(root, (float)time);
|
||||
frame_group_root_apply(frame, rootFrame);
|
||||
frame.duration = FRAME_DURATION_MIN;
|
||||
frame.interpolation = Interpolation::NONE;
|
||||
frames.push_back(std::move(frame));
|
||||
}
|
||||
|
||||
track.children = std::move(frames);
|
||||
}
|
||||
|
||||
void group_frames_bake(Element& container, ElementType trackType, int frameNum)
|
||||
{
|
||||
for (auto& group : container.children)
|
||||
{
|
||||
if (group.type != ElementType::GROUP) continue;
|
||||
group_frame_backups_erase(group);
|
||||
auto root = child_first_get(group, ElementType::ROOT_ANIMATION);
|
||||
if (!root || is_root_animation_transform_default(*root)) continue;
|
||||
|
||||
auto backup = group_frame_backup_make(container, group, trackType);
|
||||
for (auto& item : container.children)
|
||||
if (item.type == trackType && item.groupId == group.id) track_group_root_bake(item, *root, frameNum);
|
||||
|
||||
*root = root_animation_make();
|
||||
group.children.push_back(std::move(backup));
|
||||
}
|
||||
}
|
||||
|
||||
template <class Callback> void animation_track_containers_each(Element& root, Callback&& callback)
|
||||
{
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
std::set<int> restoredGroupIds{};
|
||||
|
||||
for (auto& group : container.children)
|
||||
{
|
||||
if (group.type != ElementType::GROUP) continue;
|
||||
if (group.id < 0) continue;
|
||||
|
||||
const Element* backup{};
|
||||
for (const auto& child : group.children)
|
||||
if (is_group_frame_backup(child))
|
||||
{
|
||||
backup = &child;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!backup) continue;
|
||||
|
||||
auto root = root_animation_make();
|
||||
if (auto currentRoot = child_first_get(group, ElementType::ROOT_ANIMATION)) root = *currentRoot;
|
||||
|
||||
std::vector<Element> restoredTracks{};
|
||||
bool isRootRestored{};
|
||||
for (const auto& child : backup->children)
|
||||
{
|
||||
if (child.type == ElementType::ROOT_ANIMATION && !isRootRestored)
|
||||
{
|
||||
root = child;
|
||||
isRootRestored = true;
|
||||
}
|
||||
else if (child.type == trackType)
|
||||
{
|
||||
auto track = child;
|
||||
track.groupId = group.id;
|
||||
restoredTracks.push_back(std::move(track));
|
||||
}
|
||||
}
|
||||
|
||||
if (!isRootRestored && restoredTracks.empty()) continue;
|
||||
|
||||
group.children = {std::move(root)};
|
||||
group.children.insert(group.children.end(), std::make_move_iterator(restoredTracks.begin()),
|
||||
std::make_move_iterator(restoredTracks.end()));
|
||||
restoredGroupIds.insert(group.id);
|
||||
}
|
||||
|
||||
if (restoredGroupIds.empty()) return;
|
||||
|
||||
std::erase_if(container.children, [&](const Element& item)
|
||||
{ return item.type == trackType && restoredGroupIds.contains(item.groupId); });
|
||||
}
|
||||
|
||||
void group_frames_restore(Element& root)
|
||||
{
|
||||
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)
|
||||
{
|
||||
auto child = child_first_get(element, type);
|
||||
return child ? (int)(child - element.children.data()) : -1;
|
||||
}
|
||||
|
||||
ElementType group_child_type_get(ElementType parentType)
|
||||
{
|
||||
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)
|
||||
{
|
||||
for (int i = 0; i < (int)container.children.size(); ++i)
|
||||
if (container.children[i].type == trackType && container.children[i].groupId == groupId) return i;
|
||||
return (int)container.children.size();
|
||||
}
|
||||
|
||||
void group_metadata_embed(Element& animation, const TrackContainer& row)
|
||||
{
|
||||
auto groupContainer = child_first_get(animation, row.groups);
|
||||
if (!groupContainer) return;
|
||||
|
||||
std::vector<Element> groups{};
|
||||
for (const auto& child : groupContainer->children)
|
||||
if (child.type == ElementType::GROUP) groups.push_back(child);
|
||||
|
||||
if (!groups.empty())
|
||||
{
|
||||
auto& container = child_ensure(animation, row.container);
|
||||
std::set<int> groupIds{};
|
||||
for (const auto& group : groups)
|
||||
if (group.id >= 0) groupIds.insert(group.id);
|
||||
std::erase_if(container.children, [&](const Element& child)
|
||||
{ return child.type == ElementType::GROUP && groupIds.contains(child.id); });
|
||||
|
||||
std::stable_sort(groups.begin(), groups.end(),
|
||||
[](const Element& left, const Element& right)
|
||||
{
|
||||
if (left.index == -1 || right.index == -1) return left.index != -1;
|
||||
return left.index < right.index;
|
||||
});
|
||||
|
||||
for (auto& group : groups)
|
||||
{
|
||||
auto insertIndex = group.index >= 0 ? std::min(group.index, (int)container.children.size())
|
||||
: 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 == row.groups; });
|
||||
}
|
||||
|
||||
void group_metadata_embed(Element& root)
|
||||
{
|
||||
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, const TrackContainer& row, Flags flags)
|
||||
{
|
||||
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, row.container);
|
||||
if (trackContainerIndex < 0) return;
|
||||
|
||||
auto& container = animation.children[trackContainerIndex];
|
||||
auto groupContainer = element_make(row.groups);
|
||||
std::vector<Element> tracks{};
|
||||
tracks.reserve(container.children.size());
|
||||
|
||||
for (int i = 0; i < (int)container.children.size(); ++i)
|
||||
{
|
||||
auto child = container.children[i];
|
||||
if (child.type == ElementType::GROUP)
|
||||
{
|
||||
child.index = i;
|
||||
groupContainer.children.push_back(std::move(child));
|
||||
}
|
||||
else
|
||||
tracks.push_back(std::move(child));
|
||||
}
|
||||
|
||||
container.children = std::move(tracks);
|
||||
if (groupContainer.children.empty()) return;
|
||||
animation.children.insert(animation.children.begin() + trackContainerIndex + 1, std::move(groupContainer));
|
||||
}
|
||||
|
||||
void group_metadata_extract(Element& root, Flags 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 group_children_flatten(Element& container, ElementType childType)
|
||||
{
|
||||
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<Element> flattened{};
|
||||
for (auto item : container.children)
|
||||
{
|
||||
if (item.type != ElementType::GROUP)
|
||||
{
|
||||
flattened.push_back(item);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.id < 0) item.id = nextGroupId++;
|
||||
auto children = std::move(item.children);
|
||||
item.children.clear();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
flattened.push_back(item);
|
||||
|
||||
for (auto child : children)
|
||||
{
|
||||
if (child.type != childType) continue;
|
||||
child.groupId = item.id;
|
||||
flattened.push_back(child);
|
||||
}
|
||||
}
|
||||
|
||||
std::set<int> 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)
|
||||
{
|
||||
if (childType == ElementType::UNKNOWN) return;
|
||||
|
||||
std::set<int> groupIds{};
|
||||
for (const auto& child : container.children)
|
||||
if (child.type == ElementType::GROUP && child.id >= 0) groupIds.insert(child.id);
|
||||
if (groupIds.empty()) return;
|
||||
|
||||
std::unordered_map<int, std::vector<Element>> groupedChildren{};
|
||||
for (const auto& child : container.children)
|
||||
{
|
||||
if (child.type != childType || !groupIds.contains(child.groupId)) continue;
|
||||
auto groupedChild = child;
|
||||
groupedChild.groupId = -1;
|
||||
groupedChildren[child.groupId].push_back(std::move(groupedChild));
|
||||
}
|
||||
|
||||
std::vector<Element> nested{};
|
||||
nested.reserve(container.children.size());
|
||||
for (auto child : container.children)
|
||||
{
|
||||
if (child.type == childType && groupIds.contains(child.groupId)) continue;
|
||||
if (child.type == ElementType::GROUP)
|
||||
{
|
||||
auto groupId = child.id;
|
||||
child.index = -1;
|
||||
if (auto grouped = groupedChildren.find(groupId); grouped != groupedChildren.end())
|
||||
child.children.insert(child.children.end(), std::make_move_iterator(grouped->second.begin()),
|
||||
std::make_move_iterator(grouped->second.end()));
|
||||
}
|
||||
nested.push_back(std::move(child));
|
||||
}
|
||||
|
||||
container.children = std::move(nested);
|
||||
}
|
||||
|
||||
void groups_nest(Element& root)
|
||||
{
|
||||
element_each(root, [](Element& element) { group_children_nest(element, group_child_type_get(element.type)); });
|
||||
}
|
||||
|
||||
void source_document_erase(Element& root)
|
||||
{
|
||||
element_each(root, [](Element& element)
|
||||
{ std::erase_if(element.children, [](const Element& child) { return is_source_document_tag(child.tag); }); });
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
#include "internal.hpp"
|
||||
|
||||
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 HashSink : ElementSink
|
||||
{
|
||||
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<const char*>(&size), sizeof(size));
|
||||
bytes_append(value.data(), value.size());
|
||||
}
|
||||
|
||||
template <class T> 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 text(const char* value) override
|
||||
{
|
||||
byte_append('T');
|
||||
string_append(value);
|
||||
}
|
||||
|
||||
void body() override { byte_append('['); }
|
||||
void close() override { byte_append(']'); }
|
||||
};
|
||||
|
||||
std::uint64_t anm2_hash_get(const Element& root, Options options)
|
||||
{
|
||||
HashSink sink{};
|
||||
sink.byte_append(options.isExtendedFormat ? 'E' : 'A');
|
||||
element_emit(sink, root, ElementType::UNKNOWN, SERIALIZE_ANM2ED_DEFAULT);
|
||||
return sink.hash;
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "anm2.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <tinyxml2/tinyxml2.h>
|
||||
|
||||
#include "file.hpp"
|
||||
#include "math.hpp"
|
||||
#include "path.hpp"
|
||||
#include "time.hpp"
|
||||
#include "working_directory.hpp"
|
||||
#include "xml.hpp"
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
inline constexpr std::string_view ANIMATION_MERGED_SUFFIX = " (Merged)";
|
||||
inline constexpr std::array<std::string_view, (std::size_t)Interpolation::COUNT> INTERPOLATION_VALUES = {
|
||||
"", "", "EaseIn", "EaseOut", "EaseInOut"};
|
||||
inline constexpr std::array<std::string_view, (std::size_t)Origin::COUNT> 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 text(const char*) = 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<BakeAttributes> = 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 group_child_type_get(ElementType);
|
||||
Element* content_container_get(Element&, ElementType);
|
||||
std::unordered_map<int, int> child_ids_compact(Element&, ElementType);
|
||||
void group_frames_bake(Element&);
|
||||
void group_frames_restore(Element&);
|
||||
void group_metadata_embed(Element&);
|
||||
void group_metadata_extract(Element&, Flags);
|
||||
void groups_flatten(Element&);
|
||||
void groups_nest(Element&);
|
||||
void source_document_erase(Element&);
|
||||
void content_ids_repair(Element&);
|
||||
void shader_frame_ids_repair(Element&);
|
||||
void shader_ids_remap(Element&);
|
||||
void region_ids_remap(Element&);
|
||||
void region_frame_ids_repair(Element&);
|
||||
bool is_track_child_valid(ElementType, ElementType);
|
||||
bool is_nested_group_parent(ElementType, Flags);
|
||||
bool is_group_id_serialized(Flags);
|
||||
bool element_write_skip(const Element&, ElementType, Flags);
|
||||
bool is_frame_bake_serialized(const Element&, Flags);
|
||||
std::vector<Element> frame_bake_split(const Element&, const Element&, int, bool, bool);
|
||||
void all_interpolated_frames_bake(Element&, int, bool, bool);
|
||||
int color_write(float);
|
||||
bool is_region_matched(const Element&, const Element&);
|
||||
Element* region_match_get(Element&, const Element&);
|
||||
|
||||
template <class Callback> void layer_frames_each(Element& root, Element& animation, Callback&& callback)
|
||||
{
|
||||
auto layers = content_container_get(root, ElementType::LAYERS);
|
||||
auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS);
|
||||
auto tracks = child_first_get(animation, ElementType::LAYER_ANIMATIONS);
|
||||
if (!layers || !spritesheets || !tracks) return;
|
||||
|
||||
tracks_each(*tracks, 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);
|
||||
});
|
||||
}
|
||||
|
||||
template <class Callback> void layer_frames_each(Element& root, Callback&& callback)
|
||||
{
|
||||
if (auto animations = element_first_get(root, ElementType::ANIMATIONS))
|
||||
for (auto& animation : animations->children)
|
||||
if (animation.type == ElementType::ANIMATION) layer_frames_each(root, animation, callback);
|
||||
}
|
||||
|
||||
std::uint64_t anm2_hash_get(const Element&, Options);
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
#include "internal.hpp"
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
constexpr std::string_view SHADER_NAME_DEFAULT = "New Shader";
|
||||
|
||||
void frame_ids_remap(Element& root, int Element::* member, const std::unordered_map<int, int>& 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;
|
||||
});
|
||||
}
|
||||
|
||||
// Gives every child of a type a unique, non-negative id; the first holder of a duplicated id keeps it.
|
||||
void child_ids_repair(Element& container, ElementType type)
|
||||
{
|
||||
std::set<int> usedIds{};
|
||||
for (const auto& child : container.children)
|
||||
if (child.type == type && child.id >= 0) usedIds.insert(child.id);
|
||||
|
||||
std::set<int> keptIds{};
|
||||
int nextId{};
|
||||
for (auto& child : container.children)
|
||||
{
|
||||
if (child.type != type) continue;
|
||||
if (child.id >= 0 && keptIds.insert(child.id).second) continue;
|
||||
while (usedIds.contains(nextId))
|
||||
++nextId;
|
||||
child.id = nextId;
|
||||
usedIds.insert(nextId);
|
||||
keptIds.insert(nextId);
|
||||
}
|
||||
}
|
||||
|
||||
void content_ids_repair(Element& root)
|
||||
{
|
||||
for (auto type : {ElementType::SPRITESHEET, ElementType::SHADER, ElementType::LAYER_ELEMENT,
|
||||
ElementType::NULL_ELEMENT, ElementType::EVENT_ELEMENT, ElementType::SOUND_ELEMENT})
|
||||
if (auto container = content_container_get(root, ELEMENT_CONTAINERS[(int)type]))
|
||||
child_ids_repair(*container, type);
|
||||
|
||||
if (auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS))
|
||||
for (auto& spritesheet : spritesheets->children)
|
||||
child_ids_repair(spritesheet, ElementType::REGION);
|
||||
|
||||
if (auto shaders = content_container_get(root, ElementType::SHADERS))
|
||||
for (auto& shader : shaders->children)
|
||||
if (shader.type == ElementType::SHADER && shader.name.empty()) shader.name = SHADER_NAME_DEFAULT;
|
||||
}
|
||||
|
||||
void shader_frame_ids_repair(Element& root)
|
||||
{
|
||||
auto shaders = content_container_get(root, ElementType::SHADERS);
|
||||
auto animations = element_first_get(root, ElementType::ANIMATIONS);
|
||||
if (!animations) return;
|
||||
|
||||
std::unordered_map<int, int> identity{};
|
||||
if (shaders)
|
||||
for (const auto& shader : shaders->children)
|
||||
if (shader.type == ElementType::SHADER) identity[shader.id] = shader.id;
|
||||
frame_ids_remap(*animations, &Element::shaderId, identity);
|
||||
}
|
||||
|
||||
bool is_region_matched(const Element& region, const Element& frame)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
Element* region_match_get(Element& spritesheet, const Element& frame)
|
||||
{
|
||||
return child_find(spritesheet, [&](const Element& region) { return is_region_matched(region, frame); });
|
||||
}
|
||||
|
||||
void region_frame_ids_repair(Element& root)
|
||||
{
|
||||
layer_frames_each(root,
|
||||
[](Element& frame, Element& spritesheet)
|
||||
{
|
||||
auto region = frame.regionId == -1
|
||||
? nullptr
|
||||
: child_id_get(spritesheet, ElementType::REGION, frame.regionId);
|
||||
if (region && is_region_matched(*region, frame)) return;
|
||||
if (auto match = region_match_get(spritesheet, frame)) frame.regionId = match->id;
|
||||
});
|
||||
}
|
||||
|
||||
void region_ids_remap(Element& root)
|
||||
{
|
||||
auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS);
|
||||
if (!spritesheets) return;
|
||||
|
||||
std::unordered_map<int, std::unordered_map<int, int>> remaps{};
|
||||
for (auto& spritesheet : spritesheets->children)
|
||||
if (spritesheet.type == ElementType::SPRITESHEET)
|
||||
if (auto remap = child_ids_compact(spritesheet, ElementType::REGION); !remap.empty())
|
||||
remaps[spritesheet.id] = std::move(remap);
|
||||
|
||||
if (remaps.empty()) return;
|
||||
|
||||
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 shaders = content_container_get(root, ElementType::SHADERS);
|
||||
auto remap = shaders ? child_ids_compact(*shaders, ElementType::SHADER) : std::unordered_map<int, int>{};
|
||||
frame_ids_remap(root, &Element::shaderId, remap);
|
||||
}
|
||||
|
||||
void Anm2::region_frames_sync(bool isClearInvalid)
|
||||
{
|
||||
layer_frames_each(root,
|
||||
[&](Element& frame, const Element& spritesheet)
|
||||
{
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
Anm2 Anm2::normalized_for_serialize(Flags flags) const
|
||||
{
|
||||
auto normalized = *this;
|
||||
source_document_erase(normalized.root);
|
||||
group_frames_restore(normalized.root);
|
||||
groups_flatten(normalized.root);
|
||||
if (has_flag(flags, SERIALIZE_BAKE_GROUP_FRAMES)) group_frames_bake(normalized.root);
|
||||
if (has_flag(flags, SERIALIZE_FLATTEN_SPECIAL_INTERPOLATED_FRAMES))
|
||||
all_interpolated_frames_bake(normalized.root, FRAME_DURATION_MIN, false, false);
|
||||
region_ids_remap(normalized.root);
|
||||
shader_ids_remap(normalized.root);
|
||||
if (has_flag(flags, SERIALIZE_GROUPS) && has_flag(flags, SERIALIZE_NESTED_GROUPS))
|
||||
groups_nest(normalized.root);
|
||||
else
|
||||
group_metadata_extract(normalized.root, flags);
|
||||
|
||||
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))
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,555 +0,0 @@
|
||||
#include "internal.hpp"
|
||||
|
||||
using namespace anm2ed::util;
|
||||
using namespace tinyxml2;
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
#define ANM2_STRING_ATTRIBUTES \
|
||||
X("Name", name) \
|
||||
X("CreatedBy", createdBy) \
|
||||
X("CreatedOn", createdOn) \
|
||||
X("Binding", binding) \
|
||||
X("Value", value) \
|
||||
X("DefaultAnimation", defaultAnimation)
|
||||
|
||||
#define ANM2_PATH_ATTRIBUTES \
|
||||
X("Path", path) \
|
||||
X("Vertex", vertex) \
|
||||
X("Fragment", fragment)
|
||||
|
||||
#define ANM2_INT_ATTRIBUTES \
|
||||
X("Id", id) \
|
||||
X("LayerId", layerId) \
|
||||
X("NullId", nullId) \
|
||||
X("SpritesheetId", spritesheetId) \
|
||||
X("Fps", fps) \
|
||||
X("Version", version) \
|
||||
X("FrameNum", frameNum) \
|
||||
X("Delay", duration) \
|
||||
X("AtFrame", atFrame) \
|
||||
X("EventId", eventId) \
|
||||
X("RegionId", regionId) \
|
||||
X("ShaderId", shaderId) \
|
||||
X("SoundId", soundId) \
|
||||
X("GroupId", groupId) \
|
||||
X("Index", index)
|
||||
|
||||
#define ANM2_BOOL_ATTRIBUTES \
|
||||
X("Loop", isLoop) \
|
||||
X("Visible", isVisible) \
|
||||
X("ShowRect", isShowRect) \
|
||||
X("IsExpanded", isExpanded) \
|
||||
X("Enabled", isEnabled)
|
||||
|
||||
#define ANM2_FLOAT_ATTRIBUTES \
|
||||
X("Rotation", rotation) \
|
||||
X("XPivot", pivot.x) \
|
||||
X("YPivot", pivot.y) \
|
||||
X("XCrop", crop.x) \
|
||||
X("YCrop", crop.y) \
|
||||
X("XPosition", position.x) \
|
||||
X("YPosition", position.y) \
|
||||
X("Width", size.x) \
|
||||
X("Height", size.y) \
|
||||
X("XScale", scale.x) \
|
||||
X("YScale", scale.y) \
|
||||
X("ShearX", shear.x) \
|
||||
X("ShearY", shear.y)
|
||||
|
||||
#define ANM2_COLOR_ATTRIBUTES \
|
||||
X("RedTint", tint.r) \
|
||||
X("GreenTint", tint.g) \
|
||||
X("BlueTint", tint.b) \
|
||||
X("AlphaTint", tint.a) \
|
||||
X("RedOffset", colorOffset.r) \
|
||||
X("GreenOffset", colorOffset.g) \
|
||||
X("BlueOffset", colorOffset.b)
|
||||
|
||||
constexpr std::string_view LEGACY_OVERLAY_TAG = "Overlay";
|
||||
|
||||
// Attributes read into Element members (or retired by older anm2ed builds) are not passed through.
|
||||
#define X(attribute, member) attribute,
|
||||
constexpr std::string_view KNOWN_ATTRIBUTES[] = {ANM2_STRING_ATTRIBUTES ANM2_PATH_ATTRIBUTES ANM2_INT_ATTRIBUTES
|
||||
ANM2_BOOL_ATTRIBUTES ANM2_FLOAT_ATTRIBUTES ANM2_COLOR_ATTRIBUTES
|
||||
"Interpolated",
|
||||
"Origin",
|
||||
"BakeInterpolation",
|
||||
"BakeDelay",
|
||||
"BakeCount",
|
||||
"OriginalDelay"};
|
||||
#undef X
|
||||
|
||||
bool string_query(const XMLElement* element, const char* name, std::string& out)
|
||||
{
|
||||
if (!element) return false;
|
||||
if (auto value = element->Attribute(name); value)
|
||||
{
|
||||
out = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool path_query(const XMLElement* element, const char* name, std::filesystem::path& out)
|
||||
{
|
||||
std::string value{};
|
||||
if (!string_query(element, name, value)) return false;
|
||||
out = path::from_utf8(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
float color_read(const XMLElement* element, const char* name, float fallback)
|
||||
{
|
||||
int value{};
|
||||
if (!element || element->QueryIntAttribute(name, &value) != XML_SUCCESS) return fallback;
|
||||
return math::uint8_to_float(value);
|
||||
}
|
||||
|
||||
int color_write(float value) { return math::float_to_uint8(value); }
|
||||
|
||||
Interpolation interpolation_read(const XMLElement* element, const char* attribute)
|
||||
{
|
||||
bool isFallback{};
|
||||
if (element) element->QueryBoolAttribute(attribute, &isFallback);
|
||||
|
||||
auto value = element ? element->Attribute(attribute) : nullptr;
|
||||
if (!value) return Interpolation::NONE;
|
||||
|
||||
for (std::size_t i = 0; i < INTERPOLATION_VALUES.size(); ++i)
|
||||
if (!INTERPOLATION_VALUES[i].empty() && INTERPOLATION_VALUES[i] == value) return (Interpolation)i;
|
||||
|
||||
return isFallback ? Interpolation::LINEAR : Interpolation::NONE;
|
||||
}
|
||||
|
||||
Interpolation interpolation_read(const XMLElement* element) { return interpolation_read(element, "Interpolated"); }
|
||||
|
||||
Origin origin_read(const XMLElement* element)
|
||||
{
|
||||
auto value = element ? element->Attribute("Origin") : nullptr;
|
||||
if (!value) return Origin::CUSTOM;
|
||||
|
||||
for (std::size_t i = 0; i < ORIGIN_VALUES.size(); ++i)
|
||||
if (!ORIGIN_VALUES[i].empty() && ORIGIN_VALUES[i] == value) return (Origin)i;
|
||||
|
||||
return Origin::CUSTOM;
|
||||
}
|
||||
|
||||
void element_attributes_read(Element& out, const XMLElement* element)
|
||||
{
|
||||
#define X(attribute, member) string_query(element, attribute, out.member);
|
||||
ANM2_STRING_ATTRIBUTES
|
||||
#undef X
|
||||
#define X(attribute, member) path_query(element, attribute, out.member);
|
||||
ANM2_PATH_ATTRIBUTES
|
||||
#undef X
|
||||
#define X(attribute, member) element->QueryIntAttribute(attribute, &out.member);
|
||||
ANM2_INT_ATTRIBUTES
|
||||
#undef X
|
||||
#define X(attribute, member) element->QueryBoolAttribute(attribute, &out.member);
|
||||
ANM2_BOOL_ATTRIBUTES
|
||||
#undef X
|
||||
#define X(attribute, member) element->QueryFloatAttribute(attribute, &out.member);
|
||||
ANM2_FLOAT_ATTRIBUTES
|
||||
#undef X
|
||||
#define X(attribute, member) out.member = color_read(element, attribute, out.member);
|
||||
ANM2_COLOR_ATTRIBUTES
|
||||
#undef X
|
||||
|
||||
for (auto attribute = element->FirstAttribute(); attribute; attribute = attribute->Next())
|
||||
if (!std::ranges::contains(KNOWN_ATTRIBUTES, std::string_view(attribute->Name())))
|
||||
out.extraAttributes.emplace_back(attribute->Name(), attribute->Value());
|
||||
if (auto text = element->GetText()) out.text = text;
|
||||
|
||||
out.interpolation = interpolation_read(element);
|
||||
out.origin = origin_read(element);
|
||||
if (out.type == ElementType::REGION && out.origin == Origin::TOP_LEFT) out.pivot = {};
|
||||
if (out.type == ElementType::REGION && out.origin == Origin::CENTER) out.pivot = out.size * 0.5f;
|
||||
}
|
||||
|
||||
bool bake_attributes_read(const XMLElement* element, Interpolation& interpolation, int& bakeDelay)
|
||||
{
|
||||
if (!element || !element->Attribute("BakeInterpolation")) return false;
|
||||
|
||||
interpolation = interpolation_read(element, "BakeInterpolation");
|
||||
element->QueryIntAttribute("BakeDelay", &bakeDelay);
|
||||
return bakeDelay >= FRAME_DURATION_MIN;
|
||||
}
|
||||
|
||||
const XMLElement* bake_frames_skip(const XMLElement* element, int bakeDelay)
|
||||
{
|
||||
for (int i = 0; element && i < bakeDelay; ++i)
|
||||
element = element->NextSiblingElement();
|
||||
return element;
|
||||
}
|
||||
|
||||
void element_children_read(Element& out, const XMLElement* element)
|
||||
{
|
||||
auto child = element->FirstChildElement();
|
||||
while (child)
|
||||
{
|
||||
auto childName = std::string_view(child->Name() ? child->Name() : "");
|
||||
if (childName == LEGACY_OVERLAY_TAG)
|
||||
{
|
||||
child = child->NextSiblingElement();
|
||||
continue;
|
||||
}
|
||||
|
||||
auto childType = element_type_get(childName);
|
||||
if (childType == ElementType::FRAME && is_track(out))
|
||||
{
|
||||
Interpolation bakeInterpolation{};
|
||||
int bakeDelay{};
|
||||
if (bake_attributes_read(child, bakeInterpolation, bakeDelay))
|
||||
{
|
||||
auto restored = element_read(child);
|
||||
restored.interpolation = bakeInterpolation;
|
||||
restored.duration = bakeDelay;
|
||||
if (is_track_child_valid(out.type, restored.type)) out.children.push_back(std::move(restored));
|
||||
child = bake_frames_skip(child, bakeDelay);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
auto childElement = element_read(child);
|
||||
if (is_track_child_valid(out.type, childElement.type)) out.children.push_back(std::move(childElement));
|
||||
child = child->NextSiblingElement();
|
||||
}
|
||||
}
|
||||
|
||||
Element element_read(const XMLElement* element)
|
||||
{
|
||||
Element out{};
|
||||
if (!element) return out;
|
||||
|
||||
out.tag = element->Name() ? element->Name() : "";
|
||||
out.type = element_type_get(out.tag);
|
||||
element_attributes_read(out, element);
|
||||
|
||||
element_children_read(out, element);
|
||||
|
||||
if (out.type == ElementType::TRIGGER && out.soundId != -1)
|
||||
{
|
||||
bool isSoundFound{};
|
||||
for (const auto& child : out.children)
|
||||
if (child.type == ElementType::SOUND_ELEMENT && child.id == out.soundId) isSoundFound = true;
|
||||
|
||||
if (!isSoundFound)
|
||||
{
|
||||
auto sound = element_make(ElementType::SOUND_ELEMENT);
|
||||
sound.id = out.soundId;
|
||||
out.children.insert(out.children.begin(), sound);
|
||||
}
|
||||
}
|
||||
|
||||
if (out.type == ElementType::TRIGGER)
|
||||
for (const auto& child : out.children)
|
||||
if (child.type == ElementType::SOUND_ELEMENT && child.id != -1) out.soundIds.push_back(child.id);
|
||||
|
||||
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<XMLElement*> 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 text(const char* value) override { stack.back()->SetText(value); }
|
||||
void close() override { stack.pop_back(); }
|
||||
};
|
||||
|
||||
bool is_nested_group_parent(ElementType parentType, Flags flags)
|
||||
{
|
||||
return has_flag(flags, SERIALIZE_NESTED_GROUPS) && group_child_type_get(parentType) != ElementType::UNKNOWN;
|
||||
}
|
||||
|
||||
bool is_group_id_serialized(Flags flags)
|
||||
{
|
||||
return has_flag(flags, SERIALIZE_GROUPS) && !has_flag(flags, SERIALIZE_NESTED_GROUPS);
|
||||
}
|
||||
|
||||
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::SOUND_ELEMENT && parentType == ElementType::TRIGGER) 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;
|
||||
}
|
||||
|
||||
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 path_emit(ElementSink& sink, const char* name, const std::filesystem::path& value)
|
||||
{
|
||||
if (!value.empty()) sink.attribute(name, path::to_utf8(value).c_str());
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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 element_emit(ElementSink& sink, const Element& element, ElementType parentType, Flags flags,
|
||||
std::optional<BakeAttributes> bake)
|
||||
{
|
||||
auto tag = element.type == ElementType::UNKNOWN ? std::string_view(element.tag) : element_tag_get(element.type);
|
||||
sink.open(tag.empty() ? std::string_view(element.tag) : tag);
|
||||
element_attributes_emit(sink, element, parentType, flags);
|
||||
for (const auto& [name, value] : element.extraAttributes)
|
||||
sink.attribute(name.c_str(), value.c_str());
|
||||
if (!element.text.empty()) sink.text(element.text.c_str());
|
||||
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;
|
||||
}
|
||||
|
||||
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<BakeAttributes>({child.interpolation, (int)baked.size()})
|
||||
: std::nullopt);
|
||||
}
|
||||
sink.close();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
std::string element_to_string(const Element& element, ElementType parentType, Flags flags)
|
||||
{
|
||||
XMLDocument document{};
|
||||
document.InsertEndChild(element_to_xml(document, element, parentType, flags));
|
||||
return xml::document_to_string(document);
|
||||
}
|
||||
|
||||
std::string element_to_string(const Element& element, Flags flags)
|
||||
{
|
||||
return element_to_string(element, ElementType::UNKNOWN, flags);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -73,7 +73,6 @@ namespace anm2ed::edit
|
||||
Uids triggers_sort(model::Model&, Reference);
|
||||
Uids frames_paste(model::Model&, Reference, const std::set<Reference>&, const std::string&, int, std::string*);
|
||||
Uids root_bake_into(model::Model&, const std::set<Reference>&, const std::set<Reference>&, RootBakeOptions);
|
||||
void frame_root_transform_apply(model::Frame&, const model::Frame&, bool, bool, bool);
|
||||
|
||||
Uids items_remove(model::Model&, int, const std::map<int, std::set<int>>&, const std::map<int, std::set<int>>&);
|
||||
Uids items_group(model::Model&, int, int, const std::set<int>&, const std::string&);
|
||||
|
||||
@@ -273,26 +273,6 @@ namespace anm2ed::edit
|
||||
return uids;
|
||||
}
|
||||
|
||||
void frame_root_transform_apply(Frame& frame, const Frame& rootFrame, bool isRoundScale, bool isRoundRotation,
|
||||
bool isUseRootPivot)
|
||||
{
|
||||
auto rootScale = math::percent_to_unit(rootFrame.scale);
|
||||
auto pivot = isUseRootPivot ? rootFrame.position : glm::vec2();
|
||||
auto offset = (frame.position - pivot) * rootScale;
|
||||
auto radians = glm::radians(rootFrame.rotation);
|
||||
auto cos = std::cos(radians);
|
||||
auto sin = std::sin(radians);
|
||||
|
||||
frame.position = rootFrame.position + glm::vec2(offset.x * cos - offset.y * sin, offset.x * sin + offset.y * cos);
|
||||
frame.scale *= rootScale;
|
||||
frame.rotation += rootFrame.rotation;
|
||||
frame.tint *= rootFrame.tint;
|
||||
frame.colorOffset += rootFrame.colorOffset;
|
||||
|
||||
if (isRoundScale) frame.scale = glm::round(frame.scale);
|
||||
if (isRoundRotation) frame.rotation = std::round(frame.rotation);
|
||||
}
|
||||
|
||||
// Applies the selected root frames' transforms to the target tracks' frames under them, then resets those root
|
||||
// frames to a default frame spanning the same time.
|
||||
Uids root_bake_into(Model& model, const std::set<Reference>& rootFrames, const std::set<Reference>& targets,
|
||||
|
||||
+43
-72
@@ -12,55 +12,49 @@
|
||||
#include "types.hpp"
|
||||
|
||||
#define ANM2_ELEMENT_TYPES \
|
||||
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)
|
||||
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")
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
enum class ElementType
|
||||
{
|
||||
#define X(symbol, tag, container) symbol,
|
||||
#define X(symbol, tag) 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,
|
||||
@@ -129,60 +123,38 @@ 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(), 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_ANIMATION, LAYER_ANIMATIONS) \
|
||||
X(NULL_, BASIC_NULL_ANIMATION, "NullAnimation", resource::icon::NULL_, NULL_COLOR, NULL_COLOR_ACTIVE, \
|
||||
NULL_COLOR_HOVERED, NULL_ANIMATION, NULL_ANIMATIONS) \
|
||||
X(TRIGGER, BASIC_TRIGGERS, "Triggers", resource::icon::TRIGGERS, TRIGGER_COLOR, TRIGGER_COLOR_ACTIVE, \
|
||||
TRIGGER_COLOR_HOVERED, TRIGGERS, UNKNOWN)
|
||||
X(NONE, STRING_UNDEFINED, resource::icon::NONE, glm::vec4(), glm::vec4(), glm::vec4()) \
|
||||
X(ROOT, BASIC_ROOT, resource::icon::ROOT, ROOT_COLOR, ROOT_COLOR_ACTIVE, ROOT_COLOR_HOVERED) \
|
||||
X(LAYER, BASIC_LAYER_ANIMATION, resource::icon::LAYER, LAYER_COLOR, LAYER_COLOR_ACTIVE, LAYER_COLOR_HOVERED) \
|
||||
X(NULL_, BASIC_NULL_ANIMATION, resource::icon::NULL_, NULL_COLOR, NULL_COLOR_ACTIVE, NULL_COLOR_HOVERED) \
|
||||
X(TRIGGER, BASIC_TRIGGERS, resource::icon::TRIGGERS, TRIGGER_COLOR, TRIGGER_COLOR_ACTIVE, TRIGGER_COLOR_HOVERED)
|
||||
|
||||
constexpr StringType TYPE_STRINGS[] = {
|
||||
#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, track, container) itemString,
|
||||
#define X(symbol, string, icon, color, colorActive, colorHovered) string,
|
||||
ANM2_ITEM_TYPES
|
||||
#undef X
|
||||
};
|
||||
|
||||
constexpr resource::icon::Type TYPE_ICONS[] = {
|
||||
#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) icon,
|
||||
#define X(symbol, string, icon, color, colorActive, colorHovered) icon,
|
||||
ANM2_ITEM_TYPES
|
||||
#undef X
|
||||
};
|
||||
|
||||
inline const glm::vec4 TYPE_COLOR[] = {
|
||||
#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) color,
|
||||
#define X(symbol, string, icon, color, colorActive, colorHovered) color,
|
||||
ANM2_ITEM_TYPES
|
||||
#undef X
|
||||
};
|
||||
|
||||
inline const glm::vec4 TYPE_COLOR_ACTIVE[] = {
|
||||
#define X(symbol, string, itemString, icon, color, colorActive, colorHovered, track, container) colorActive,
|
||||
#define X(symbol, string, icon, color, colorActive, colorHovered) colorActive,
|
||||
ANM2_ITEM_TYPES
|
||||
#undef X
|
||||
};
|
||||
|
||||
inline const glm::vec4 TYPE_COLOR_HOVERED[] = {
|
||||
#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,
|
||||
#define X(symbol, string, icon, color, colorActive, colorHovered) colorHovered,
|
||||
ANM2_ITEM_TYPES
|
||||
#undef X
|
||||
};
|
||||
@@ -236,7 +208,6 @@ namespace anm2ed
|
||||
|
||||
struct Options
|
||||
{
|
||||
Flags flags{SERIALIZE_DEFAULT};
|
||||
bool isExtendedFormat{};
|
||||
};
|
||||
|
||||
|
||||
@@ -44,6 +44,26 @@ namespace anm2ed::model
|
||||
math::percent_to_unit(frame.shear));
|
||||
}
|
||||
|
||||
void frame_root_transform_apply(Frame& frame, const Frame& rootFrame, bool isRoundScale, bool isRoundRotation,
|
||||
bool isUseRootPivot)
|
||||
{
|
||||
auto rootScale = math::percent_to_unit(rootFrame.scale);
|
||||
auto pivot = isUseRootPivot ? rootFrame.position : glm::vec2();
|
||||
auto offset = (frame.position - pivot) * rootScale;
|
||||
auto radians = glm::radians(rootFrame.rotation);
|
||||
auto cos = std::cos(radians);
|
||||
auto sin = std::sin(radians);
|
||||
|
||||
frame.position = rootFrame.position + glm::vec2(offset.x * cos - offset.y * sin, offset.x * sin + offset.y * cos);
|
||||
frame.scale *= rootScale;
|
||||
frame.rotation += rootFrame.rotation;
|
||||
frame.tint *= rootFrame.tint;
|
||||
frame.colorOffset += rootFrame.colorOffset;
|
||||
|
||||
if (isRoundScale) frame.scale = glm::round(frame.scale);
|
||||
if (isRoundRotation) frame.rotation = std::round(frame.rotation);
|
||||
}
|
||||
|
||||
// The track's state at a time: the frame playing then, mixed toward the next one when interpolated. A trigger is
|
||||
// returned only on its exact frame; otherwise the result is invisible.
|
||||
Frame frame_generate(const Track& track, float time)
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace anm2ed::model
|
||||
void frame_mix(Frame&, const Frame&, float);
|
||||
std::vector<Frame> frame_bake_split(const Frame&, const Frame&, int, bool, bool);
|
||||
glm::mat4 frame_parent_model_get(const Frame&);
|
||||
void frame_root_transform_apply(Frame&, const Frame&, bool, bool, bool);
|
||||
Frame frame_generate(const Track&, float);
|
||||
int frame_index_from_at_frame_get(const Track&, int);
|
||||
int frame_index_from_time_get(const Track&, float);
|
||||
|
||||
+1298
-638
File diff suppressed because it is too large.
Load diff
+1
-1
@@ -15,7 +15,7 @@ namespace anm2ed::model
|
||||
bool model_save(const Model&, const std::filesystem::path&, std::string* = nullptr, Options = {});
|
||||
std::string model_to_string(const Model&, Options = {});
|
||||
std::string model_serialize(const Model&, Flags);
|
||||
std::uint64_t model_hash(const Model&, Options = {});
|
||||
std::uint64_t model_hash(const Model&);
|
||||
bool is_special_interpolated_frames(const Model&);
|
||||
|
||||
std::string frame_to_string(const Frame&, ItemType);
|
||||
|
||||
+1
-4
@@ -266,10 +266,7 @@ DockSpace ID=0x123F8F08 Window=0x6D581B32 Pos=8,62 Size=1902,991
|
||||
}
|
||||
}
|
||||
|
||||
Options Settings::anm2_options_get() const
|
||||
{
|
||||
return {.flags = SERIALIZE_DEFAULT, .isExtendedFormat = isFileExtendedFormat};
|
||||
}
|
||||
Options Settings::anm2_options_get() const { return {.isExtendedFormat = isFileExtendedFormat}; }
|
||||
|
||||
std::string Settings::imgui_data_load(const std::filesystem::path& path)
|
||||
{
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
#include "xml.hpp"
|
||||
|
||||
#include "math.hpp"
|
||||
#include "path.hpp"
|
||||
|
||||
using namespace anm2ed::util;
|
||||
using namespace tinyxml2;
|
||||
|
||||
namespace anm2ed::util::xml
|
||||
@@ -14,102 +10,4 @@ namespace anm2ed::util::xml
|
||||
self.Print(&printer);
|
||||
return std::string(printer.CStr());
|
||||
}
|
||||
|
||||
bool query_string_attribute(const XMLElement* element, const char* attribute, std::string& out)
|
||||
{
|
||||
if (!element || !attribute) return false;
|
||||
|
||||
const char* value = nullptr;
|
||||
if (element->QueryStringAttribute(attribute, &value) != XML_SUCCESS || !value) return false;
|
||||
out = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool query_path_attribute(const XMLElement* element, const char* attribute, std::filesystem::path& out)
|
||||
{
|
||||
std::string temp{};
|
||||
if (!query_string_attribute(element, attribute, temp)) return false;
|
||||
out = path::from_utf8(temp);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool query_int_attribute(const XMLElement* element, const char* attribute, int& out)
|
||||
{
|
||||
return element && attribute && element->QueryIntAttribute(attribute, &out) == XML_SUCCESS;
|
||||
}
|
||||
|
||||
bool query_float_attribute(const XMLElement* element, const char* attribute, float& out)
|
||||
{
|
||||
return element && attribute && element->QueryFloatAttribute(attribute, &out) == XML_SUCCESS;
|
||||
}
|
||||
|
||||
bool query_bool_attribute(const XMLElement* element, const char* attribute, bool& out)
|
||||
{
|
||||
return element && attribute && element->QueryBoolAttribute(attribute, &out) == XML_SUCCESS;
|
||||
}
|
||||
|
||||
bool query_color_attribute(const XMLElement* element, const char* attribute, float& out)
|
||||
{
|
||||
int value{};
|
||||
if (!query_int_attribute(element, attribute, value)) return false;
|
||||
out = math::uint8_to_float(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
void set_string_attribute(XMLElement* element, const char* attribute, const std::string& value)
|
||||
{
|
||||
if (element && attribute && !value.empty()) element->SetAttribute(attribute, value.c_str());
|
||||
}
|
||||
|
||||
void set_path_attribute(XMLElement* element, const char* attribute, const std::filesystem::path& value)
|
||||
{
|
||||
if (!element || !attribute || value.empty()) return;
|
||||
auto utf8 = path::to_utf8(value);
|
||||
element->SetAttribute(attribute, utf8.c_str());
|
||||
}
|
||||
|
||||
void set_int_attribute(XMLElement* element, const char* attribute, int value)
|
||||
{
|
||||
if (element && attribute) element->SetAttribute(attribute, value);
|
||||
}
|
||||
|
||||
void set_float_attribute(XMLElement* element, const char* attribute, float value)
|
||||
{
|
||||
if (element && attribute) element->SetAttribute(attribute, value);
|
||||
}
|
||||
|
||||
void set_bool_attribute(XMLElement* element, const char* attribute, bool value)
|
||||
{
|
||||
if (element && attribute) element->SetAttribute(attribute, value);
|
||||
}
|
||||
|
||||
void set_color_attribute(XMLElement* element, const char* attribute, float value)
|
||||
{
|
||||
if (element && attribute) element->SetAttribute(attribute, math::float_to_uint8(value));
|
||||
}
|
||||
|
||||
XMLError query_string_attribute(XMLElement* element, const char* attribute, std::string* out)
|
||||
{
|
||||
if (!element || !attribute || !out) return XML_NO_ATTRIBUTE;
|
||||
|
||||
const char* value = nullptr;
|
||||
auto result = element->QueryStringAttribute(attribute, &value);
|
||||
if (result == XML_SUCCESS && value) *out = value;
|
||||
return result;
|
||||
}
|
||||
|
||||
XMLError query_path_attribute(XMLElement* element, const char* attribute, std::filesystem::path* out)
|
||||
{
|
||||
if (!out) return XML_NO_ATTRIBUTE;
|
||||
|
||||
std::string temp{};
|
||||
auto result = query_string_attribute(element, attribute, &temp);
|
||||
if (result == XML_SUCCESS) *out = path::from_utf8(temp);
|
||||
return result;
|
||||
}
|
||||
|
||||
void query_color_attribute(XMLElement* element, const char* attribute, float& out)
|
||||
{
|
||||
query_color_attribute(static_cast<const XMLElement*>(element), attribute, out);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
#include <tinyxml2/tinyxml2.h>
|
||||
@@ -8,22 +7,4 @@
|
||||
namespace anm2ed::util::xml
|
||||
{
|
||||
std::string document_to_string(tinyxml2::XMLDocument&);
|
||||
|
||||
bool query_string_attribute(const tinyxml2::XMLElement*, const char*, std::string&);
|
||||
bool query_path_attribute(const tinyxml2::XMLElement*, const char*, std::filesystem::path&);
|
||||
bool query_int_attribute(const tinyxml2::XMLElement*, const char*, int&);
|
||||
bool query_float_attribute(const tinyxml2::XMLElement*, const char*, float&);
|
||||
bool query_bool_attribute(const tinyxml2::XMLElement*, const char*, bool&);
|
||||
bool query_color_attribute(const tinyxml2::XMLElement*, const char*, float&);
|
||||
|
||||
void set_string_attribute(tinyxml2::XMLElement*, const char*, const std::string&);
|
||||
void set_path_attribute(tinyxml2::XMLElement*, const char*, const std::filesystem::path&);
|
||||
void set_int_attribute(tinyxml2::XMLElement*, const char*, int);
|
||||
void set_float_attribute(tinyxml2::XMLElement*, const char*, float);
|
||||
void set_bool_attribute(tinyxml2::XMLElement*, const char*, bool);
|
||||
void set_color_attribute(tinyxml2::XMLElement*, const char*, float);
|
||||
|
||||
tinyxml2::XMLError query_string_attribute(tinyxml2::XMLElement*, const char*, std::string*);
|
||||
tinyxml2::XMLError query_path_attribute(tinyxml2::XMLElement*, const char*, std::filesystem::path*);
|
||||
void query_color_attribute(tinyxml2::XMLElement*, const char*, float&);
|
||||
}
|
||||
@@ -64,13 +64,8 @@ namespace anm2ed::test
|
||||
inline std::vector<Variant> variants_get(const model::Model& model)
|
||||
{
|
||||
std::vector<Variant> variants{};
|
||||
std::string hashes{};
|
||||
for (auto [name, flags] : PRESETS)
|
||||
{
|
||||
variants.emplace_back(name, model::model_serialize(model, flags));
|
||||
for (auto [formatName, isExtended] : FORMATS)
|
||||
hashes += std::format("{}.{} {:016x}\n", name, formatName, model::model_hash(model, {flags, isExtended}));
|
||||
}
|
||||
for (auto [name, isExtended] : FORMATS)
|
||||
variants.emplace_back(name, model::model_to_string(model, {.isExtendedFormat = isExtended}));
|
||||
for (auto [name, type] : MERGES)
|
||||
@@ -81,7 +76,6 @@ namespace anm2ed::test
|
||||
{});
|
||||
variants.emplace_back(name, model::model_to_string(merged, {.isExtendedFormat = true}));
|
||||
}
|
||||
variants.emplace_back("hashes", hashes);
|
||||
return variants;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 85e1752e262873f9
|
||||
default.extended d659a6dc486bdc1d
|
||||
editor.save 85e1752e262873f9
|
||||
editor.extended d659a6dc486bdc1d
|
||||
isaac.save 85e1752e262873f9
|
||||
isaac.extended d659a6dc486bdc1d
|
||||
anm2ed.save 85e1752e262873f9
|
||||
anm2ed.extended d659a6dc486bdc1d
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 8065c1ac59ae41c3
|
||||
default.extended 74639ef0767af8bf
|
||||
editor.save 8065c1ac59ae41c3
|
||||
editor.extended 74639ef0767af8bf
|
||||
isaac.save 8065c1ac59ae41c3
|
||||
isaac.extended 74639ef0767af8bf
|
||||
anm2ed.save 8065c1ac59ae41c3
|
||||
anm2ed.extended 74639ef0767af8bf
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 0ad05788dd0e2efb
|
||||
default.extended 205c9b7c539d95ff
|
||||
editor.save 0ad05788dd0e2efb
|
||||
editor.extended 205c9b7c539d95ff
|
||||
isaac.save 0ad05788dd0e2efb
|
||||
isaac.extended 205c9b7c539d95ff
|
||||
anm2ed.save 0ad05788dd0e2efb
|
||||
anm2ed.extended 205c9b7c539d95ff
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 6d3d8c95645b9c17
|
||||
default.extended cc73ebf8ebc18b03
|
||||
editor.save 6d3d8c95645b9c17
|
||||
editor.extended cc73ebf8ebc18b03
|
||||
isaac.save 6d3d8c95645b9c17
|
||||
isaac.extended cc73ebf8ebc18b03
|
||||
anm2ed.save 6d3d8c95645b9c17
|
||||
anm2ed.extended cc73ebf8ebc18b03
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save eeca10ea8173394c
|
||||
default.extended 372eba70a0319040
|
||||
editor.save eeca10ea8173394c
|
||||
editor.extended 372eba70a0319040
|
||||
isaac.save eeca10ea8173394c
|
||||
isaac.extended 372eba70a0319040
|
||||
anm2ed.save eeca10ea8173394c
|
||||
anm2ed.extended 372eba70a0319040
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 7e865b837f99ffb7
|
||||
default.extended 7ba7db7402487463
|
||||
editor.save 7e865b837f99ffb7
|
||||
editor.extended 7ba7db7402487463
|
||||
isaac.save 7e865b837f99ffb7
|
||||
isaac.extended 7ba7db7402487463
|
||||
anm2ed.save 7e865b837f99ffb7
|
||||
anm2ed.extended 7ba7db7402487463
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 69d0c8bb8d35f83a
|
||||
default.extended 1816db2c80b34ffe
|
||||
editor.save 69d0c8bb8d35f83a
|
||||
editor.extended 1816db2c80b34ffe
|
||||
isaac.save 69d0c8bb8d35f83a
|
||||
isaac.extended 1816db2c80b34ffe
|
||||
anm2ed.save 69d0c8bb8d35f83a
|
||||
anm2ed.extended 1816db2c80b34ffe
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 8cfeb73803e8ced0
|
||||
default.extended 67fc2f29d9f7bc2c
|
||||
editor.save 8cfeb73803e8ced0
|
||||
editor.extended 67fc2f29d9f7bc2c
|
||||
isaac.save 8cfeb73803e8ced0
|
||||
isaac.extended 67fc2f29d9f7bc2c
|
||||
anm2ed.save 8cfeb73803e8ced0
|
||||
anm2ed.extended 67fc2f29d9f7bc2c
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 275b545f8362708a
|
||||
default.extended a0eb28cc14731106
|
||||
editor.save 275b545f8362708a
|
||||
editor.extended a0eb28cc14731106
|
||||
isaac.save 275b545f8362708a
|
||||
isaac.extended a0eb28cc14731106
|
||||
anm2ed.save 275b545f8362708a
|
||||
anm2ed.extended a0eb28cc14731106
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 840c57e5bb2a0fe6
|
||||
default.extended ee0626347a260722
|
||||
editor.save 840c57e5bb2a0fe6
|
||||
editor.extended ee0626347a260722
|
||||
isaac.save 840c57e5bb2a0fe6
|
||||
isaac.extended ee0626347a260722
|
||||
anm2ed.save 840c57e5bb2a0fe6
|
||||
anm2ed.extended ee0626347a260722
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 1ec436489ff9aa8d
|
||||
default.extended 2dd8495758796ce9
|
||||
editor.save 1ec436489ff9aa8d
|
||||
editor.extended 2dd8495758796ce9
|
||||
isaac.save 1ec436489ff9aa8d
|
||||
isaac.extended 2dd8495758796ce9
|
||||
anm2ed.save 1ec436489ff9aa8d
|
||||
anm2ed.extended 2dd8495758796ce9
|
||||
@@ -1,8 +0,0 @@
|
||||
default.save 2e68569c0d94cf3c
|
||||
default.extended 54ef7a9072214850
|
||||
editor.save 2e68569c0d94cf3c
|
||||
editor.extended 54ef7a9072214850
|
||||
isaac.save 2e68569c0d94cf3c
|
||||
isaac.extended 54ef7a9072214850
|
||||
anm2ed.save 2e68569c0d94cf3c
|
||||
anm2ed.extended 54ef7a9072214850
|
||||
Reference in new issue
Block a user