Slim Document: move data-model code out, unify edit plumbing
- Region generate/scan move to anm2/regions.cpp; both generators number from 1 - file_merge moves to anm2/merge.cpp and is table-driven - MaxRects packer moves to util/pack - Uniform config apply/trim/save move to resource/shader - Snapshots push from their owned state; Document snapshot wrappers removed - DocumentData base replaces the hand-written move constructor - toast_log replaces 55 duplicated toast + English log pairs - Apply repo .clang-format to touched files Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a9f48bc3a0
commit
3f31cedeed
32 files changed
+3405
-4428
No files matched your search
+22
-5
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
@@ -8,7 +9,6 @@
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <tinyxml2/tinyxml2.h>
|
||||
@@ -193,6 +193,20 @@ namespace anm2ed
|
||||
#undef X
|
||||
};
|
||||
|
||||
enum class RegionFrameMapping
|
||||
{
|
||||
PRESERVE,
|
||||
SET
|
||||
};
|
||||
|
||||
enum FileMergePreset
|
||||
{
|
||||
FILE_MERGE_PRESET_MERGE_BY_NAME,
|
||||
FILE_MERGE_PRESET_APPEND_AS_NEW,
|
||||
FILE_MERGE_PRESET_REPLACE_MATCHING,
|
||||
FILE_MERGE_PRESET_COUNT
|
||||
};
|
||||
|
||||
enum class ChangeType
|
||||
{
|
||||
ADJUST,
|
||||
@@ -401,8 +415,8 @@ namespace anm2ed
|
||||
|
||||
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; });
|
||||
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)
|
||||
@@ -432,8 +446,7 @@ namespace anm2ed
|
||||
}
|
||||
|
||||
template <class E>
|
||||
ElementPointer<E> animation_item_get(E& animation, ItemType type, int id = -1, int groupType = NONE,
|
||||
int groupId = -1)
|
||||
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;
|
||||
@@ -495,6 +508,7 @@ namespace anm2ed
|
||||
int element_child_max_id_get(const Element&, ElementType);
|
||||
bool element_child_id_erase(Element&, ElementType, int);
|
||||
int track_frames_count_get(const Element&);
|
||||
int animations_count_get(const Element&);
|
||||
float interpolation_factor(Interpolation, float);
|
||||
void frame_mix(Element&, const Element&, float);
|
||||
Element frame_generate(const Element&, float);
|
||||
@@ -542,5 +556,8 @@ namespace anm2ed
|
||||
bool animations_deserialize(const std::string&, int, std::set<int>&, std::string* = nullptr,
|
||||
std::set<int>* = nullptr);
|
||||
int animations_merge(int, std::set<int>&, types::merge::Type = types::merge::APPEND, bool = true);
|
||||
bool file_merge(const std::filesystem::path&, const std::filesystem::path&, FileMergePreset);
|
||||
bool regions_generate(const std::set<int>&, const std::set<Reference>&, const std::string&, RegionFrameMapping);
|
||||
void regions_scan();
|
||||
};
|
||||
}
|
||||
+29
-1
@@ -61,7 +61,6 @@ namespace anm2ed
|
||||
ElementType group_child_type_get(ElementType);
|
||||
Element* content_container_get(Element&, ElementType);
|
||||
std::unordered_map<int, int> child_ids_compact(Element&, ElementType);
|
||||
int animations_count_get(const Element&);
|
||||
int animations_child_index_get(const Element&, int);
|
||||
int animations_child_insert_index_get(const Element&, int);
|
||||
Element root_animation_make();
|
||||
@@ -88,6 +87,35 @@ namespace anm2ed
|
||||
void special_interpolated_frames_bake(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);
|
||||
std::uint64_t element_hash(const Element&, Flags);
|
||||
}
|
||||
+226
-1
@@ -104,7 +104,8 @@ namespace anm2ed
|
||||
[&](const Element& child)
|
||||
{
|
||||
return child.type == ElementType::GROUP &&
|
||||
child.name == sourceGroup.name && !matchedGroupIds.contains(child.id);
|
||||
child.name == sourceGroup.name &&
|
||||
!matchedGroupIds.contains(child.id);
|
||||
});
|
||||
|
||||
if (destinationGroup == destinationTracks->children.end())
|
||||
@@ -182,4 +183,228 @@ namespace anm2ed
|
||||
finalAnimation->frameNum = animation_length_get(*finalAnimation);
|
||||
return finalIndex;
|
||||
}
|
||||
|
||||
bool Anm2::file_merge(const std::filesystem::path& path, const std::filesystem::path& directory,
|
||||
FileMergePreset preset)
|
||||
{
|
||||
Anm2 source(path);
|
||||
if (!source.isValid) return false;
|
||||
|
||||
auto isAppendAsNew = preset == FILE_MERGE_PRESET_APPEND_AS_NEW;
|
||||
std::map<ElementType, std::unordered_map<int, int>> remaps{};
|
||||
|
||||
auto path_remap = [&](const std::filesystem::path& original)
|
||||
{
|
||||
if (directory.empty() || original.empty()) return original;
|
||||
std::error_code ec{};
|
||||
auto absolute =
|
||||
original.is_absolute() ? original : std::filesystem::weakly_canonical(path.parent_path() / original, ec);
|
||||
if (ec) absolute = path.parent_path() / original;
|
||||
auto relative = std::filesystem::relative(absolute, directory, ec);
|
||||
return ec ? original : relative;
|
||||
};
|
||||
|
||||
auto id_remap = [&](ElementType type, int id)
|
||||
{
|
||||
auto it = remaps[type].find(id);
|
||||
return it == remaps[type].end() ? -1 : it->second;
|
||||
};
|
||||
|
||||
auto name_find = [](Element& container, ElementType type, const std::string& name)
|
||||
{ return child_find(container, [&](const Element& child) { return child.type == type && child.name == name; }); };
|
||||
|
||||
auto name_unique_get = [&](Element& container, ElementType type, const std::string& name)
|
||||
{
|
||||
auto candidate = name;
|
||||
for (int i = 2; name_find(container, type, candidate); ++i)
|
||||
candidate = std::format("{} {}", name, i);
|
||||
return candidate;
|
||||
};
|
||||
|
||||
auto named_element_merge = [&](Element& container, Element item)
|
||||
{
|
||||
auto existing = isAppendAsNew ? nullptr : name_find(container, item.type, item.name);
|
||||
if (existing)
|
||||
{
|
||||
item.id = existing->id;
|
||||
*existing = item;
|
||||
return item.id;
|
||||
}
|
||||
item.id = element_child_next_id_get(container, item.type);
|
||||
if (isAppendAsNew) item.name = name_unique_get(container, item.type, item.name);
|
||||
container.children.push_back(item);
|
||||
return item.id;
|
||||
};
|
||||
|
||||
auto spritesheet_import = [&](int sourceId)
|
||||
{
|
||||
auto& remap = remaps[ElementType::SPRITESHEET];
|
||||
if (remap.contains(sourceId)) return remap[sourceId];
|
||||
auto spritesheet = source.element_get(ElementType::SPRITESHEET, sourceId);
|
||||
auto destination = element_get(ElementType::SPRITESHEETS);
|
||||
if (sourceId < 0 || !spritesheet || !destination) return -1;
|
||||
|
||||
auto imported = *spritesheet;
|
||||
imported.id = element_child_next_id_get(*destination, ElementType::SPRITESHEET);
|
||||
imported.path = path_remap(imported.path);
|
||||
destination->children.push_back(imported);
|
||||
return remap[sourceId] = imported.id;
|
||||
};
|
||||
|
||||
auto sourceSounds = source.element_get(ElementType::SOUNDS);
|
||||
auto destinationSounds = element_get(ElementType::SOUNDS);
|
||||
if (sourceSounds && destinationSounds)
|
||||
for (auto sound : sourceSounds->children)
|
||||
{
|
||||
if (sound.type != ElementType::SOUND_ELEMENT) continue;
|
||||
auto sourceId = sound.id;
|
||||
sound.path = path_remap(sound.path);
|
||||
auto existing = isAppendAsNew ? nullptr
|
||||
: child_find(*destinationSounds, [&](const Element& child)
|
||||
{ return child.type == sound.type && child.path == sound.path; });
|
||||
sound.id = existing ? existing->id : element_child_next_id_get(*destinationSounds, sound.type);
|
||||
if (existing)
|
||||
*existing = sound;
|
||||
else
|
||||
destinationSounds->children.push_back(sound);
|
||||
remaps[sound.type][sourceId] = sound.id;
|
||||
}
|
||||
|
||||
for (auto type : {ElementType::LAYER_ELEMENT, ElementType::NULL_ELEMENT, ElementType::EVENT_ELEMENT})
|
||||
{
|
||||
auto sourceContainer = source.element_get(ELEMENT_CONTAINERS[(int)type]);
|
||||
auto destinationContainer = element_get(ELEMENT_CONTAINERS[(int)type]);
|
||||
if (!sourceContainer || !destinationContainer) continue;
|
||||
for (auto item : sourceContainer->children)
|
||||
{
|
||||
if (item.type != type) continue;
|
||||
if (type == ElementType::LAYER_ELEMENT)
|
||||
{
|
||||
auto existing = isAppendAsNew ? nullptr : name_find(*destinationContainer, type, item.name);
|
||||
item.spritesheetId = existing ? existing->spritesheetId : spritesheet_import(item.spritesheetId);
|
||||
}
|
||||
remaps[type][item.id] = named_element_merge(*destinationContainer, item);
|
||||
}
|
||||
}
|
||||
|
||||
auto item_remap = [&](Element& item)
|
||||
{
|
||||
for (auto& frame : item.children)
|
||||
{
|
||||
if (frame.type != ElementType::FRAME && frame.type != ElementType::TRIGGER) continue;
|
||||
for (auto& soundId : frame.soundIds)
|
||||
soundId = id_remap(ElementType::SOUND_ELEMENT, soundId);
|
||||
frame.eventId = id_remap(ElementType::EVENT_ELEMENT, frame.eventId);
|
||||
}
|
||||
};
|
||||
|
||||
auto animation_build = [&](const Element& incoming)
|
||||
{
|
||||
auto animation = element_make(ElementType::ANIMATION);
|
||||
animation.name = incoming.name;
|
||||
animation.frameNum = incoming.frameNum;
|
||||
animation.isLoop = incoming.isLoop;
|
||||
|
||||
auto single_track_build = [&](ElementType type)
|
||||
{
|
||||
if (auto track = child_first_get(incoming, type)) item_remap(animation.children.emplace_back(*track));
|
||||
};
|
||||
|
||||
single_track_build(ElementType::ROOT_ANIMATION);
|
||||
for (const auto& row : TRACK_CONTAINERS)
|
||||
{
|
||||
auto sourceTracks = child_first_get(incoming, row.container);
|
||||
if (!sourceTracks) continue;
|
||||
auto container = element_make(row.container);
|
||||
for (auto item : sourceTracks->children)
|
||||
{
|
||||
if (item.type == ElementType::GROUP)
|
||||
{
|
||||
std::erase_if(item.children,
|
||||
[](const Element& child) { return child.type != ElementType::ROOT_ANIMATION; });
|
||||
if (item.children.empty()) item.children.push_back(root_animation_make());
|
||||
if (item.id == -1) item.id = element_child_next_id_get(container, ElementType::GROUP);
|
||||
container.children.push_back(item);
|
||||
continue;
|
||||
}
|
||||
if (item.type != row.track) continue;
|
||||
item.*(row.id) = id_remap(row.element, item.*(row.id));
|
||||
if (item.*(row.id) < 0) continue;
|
||||
item_remap(item);
|
||||
container.children.push_back(item);
|
||||
}
|
||||
animation.children.push_back(container);
|
||||
}
|
||||
single_track_build(ElementType::TRIGGERS);
|
||||
return animation;
|
||||
};
|
||||
|
||||
auto sourceAnimations = source.element_get(ElementType::ANIMATIONS);
|
||||
auto destinationAnimations = element_get(ElementType::ANIMATIONS);
|
||||
if (!sourceAnimations || !destinationAnimations) return true;
|
||||
|
||||
std::string defaultAnimationName{};
|
||||
for (const auto& incoming : sourceAnimations->children)
|
||||
{
|
||||
if (incoming.type != ElementType::ANIMATION) continue;
|
||||
auto processed = animation_build(incoming);
|
||||
auto destination = name_find(*destinationAnimations, ElementType::ANIMATION, processed.name);
|
||||
if (isAppendAsNew)
|
||||
processed.name = name_unique_get(*destinationAnimations, ElementType::ANIMATION, processed.name);
|
||||
if (incoming.name == sourceAnimations->defaultAnimation) defaultAnimationName = processed.name;
|
||||
|
||||
if (!destination || isAppendAsNew)
|
||||
{
|
||||
destinationAnimations->children.push_back(processed);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preset == FILE_MERGE_PRESET_REPLACE_MATCHING)
|
||||
{
|
||||
*destination = processed;
|
||||
continue;
|
||||
}
|
||||
|
||||
destination->isLoop = processed.isLoop;
|
||||
for (auto type : {ElementType::ROOT_ANIMATION, ElementType::TRIGGERS})
|
||||
if (auto track = child_first_get(processed, type); track && !track->children.empty())
|
||||
child_ensure(*destination, type) = *track;
|
||||
|
||||
for (const auto& row : TRACK_CONTAINERS)
|
||||
{
|
||||
auto sourceTracks = child_first_get(processed, row.container);
|
||||
if (!sourceTracks) continue;
|
||||
auto& destinationTracks = child_ensure(*destination, row.container);
|
||||
|
||||
std::unordered_map<int, int> groupRemap{};
|
||||
for (auto item : sourceTracks->children)
|
||||
{
|
||||
if (item.type != ElementType::GROUP) continue;
|
||||
auto sourceGroupId = item.id;
|
||||
item.id = element_child_next_id_get(destinationTracks, ElementType::GROUP);
|
||||
destinationTracks.children.push_back(item);
|
||||
groupRemap[sourceGroupId] = item.id;
|
||||
}
|
||||
|
||||
for (auto item : sourceTracks->children)
|
||||
{
|
||||
if (item.type != row.track) continue;
|
||||
if (item.groupId != -1) item.groupId = groupRemap.contains(item.groupId) ? groupRemap[item.groupId] : -1;
|
||||
if (auto existing = track_find(destinationTracks, row.track, track_id_get(item)))
|
||||
{
|
||||
if (!item.children.empty()) *existing = item;
|
||||
}
|
||||
else
|
||||
destinationTracks.children.push_back(item);
|
||||
}
|
||||
}
|
||||
|
||||
destination->frameNum = std::max({destination->frameNum, processed.frameNum, animation_length_get(*destination)});
|
||||
}
|
||||
|
||||
if (destinationAnimations->defaultAnimation.empty() && !sourceAnimations->defaultAnimation.empty())
|
||||
destinationAnimations->defaultAnimation =
|
||||
defaultAnimationName.empty() ? sourceAnimations->defaultAnimation : defaultAnimationName;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+3
-30
@@ -15,25 +15,6 @@ namespace anm2ed
|
||||
});
|
||||
}
|
||||
|
||||
template <class Callback> void layer_frames_each(Element& root, Callback&& callback)
|
||||
{
|
||||
auto layers = content_container_get(root, ElementType::LAYERS);
|
||||
auto spritesheets = content_container_get(root, ElementType::SPRITESHEETS);
|
||||
if (!layers || !spritesheets) return;
|
||||
|
||||
animations_tracks_each(root, ElementType::LAYER_ANIMATION,
|
||||
[&](Element& track)
|
||||
{
|
||||
auto layer = child_id_get(*layers, ElementType::LAYER_ELEMENT, track.layerId);
|
||||
auto spritesheet = layer ? child_id_get(*spritesheets, ElementType::SPRITESHEET,
|
||||
layer->spritesheetId)
|
||||
: nullptr;
|
||||
if (!spritesheet) return;
|
||||
for (auto& frame : track.children)
|
||||
if (frame.type == ElementType::FRAME) callback(frame, *spritesheet);
|
||||
});
|
||||
}
|
||||
|
||||
void shader_ids_repair(Element& root)
|
||||
{
|
||||
auto shaders = content_container_get(root, ElementType::SHADERS);
|
||||
@@ -69,21 +50,13 @@ namespace anm2ed
|
||||
void region_frame_ids_repair(Element& root)
|
||||
{
|
||||
layer_frames_each(root,
|
||||
[](Element& frame, const Element& spritesheet)
|
||||
[](Element& frame, Element& spritesheet)
|
||||
{
|
||||
auto is_region_match = [&](const Element& region)
|
||||
{
|
||||
return region.type == ElementType::REGION &&
|
||||
glm::ivec2(region.crop) == glm::ivec2(frame.crop) &&
|
||||
glm::ivec2(region.size) == glm::ivec2(frame.size) &&
|
||||
glm::ivec2(region.pivot) == glm::ivec2(frame.pivot);
|
||||
};
|
||||
|
||||
auto region = frame.regionId == -1
|
||||
? nullptr
|
||||
: child_id_get(spritesheet, ElementType::REGION, frame.regionId);
|
||||
if (region && is_region_match(*region)) return;
|
||||
if (auto candidate = child_find(spritesheet, is_region_match)) frame.regionId = candidate->id;
|
||||
if (region && is_region_matched(*region, frame)) return;
|
||||
if (auto match = region_match_get(spritesheet, frame)) frame.regionId = match->id;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
#include "internal.hpp"
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
std::string region_name_get(const std::string& format, int number)
|
||||
{
|
||||
try
|
||||
{
|
||||
return std::vformat(format, std::make_format_args(number));
|
||||
}
|
||||
catch (const std::format_error&)
|
||||
{
|
||||
return format;
|
||||
}
|
||||
}
|
||||
|
||||
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); });
|
||||
}
|
||||
|
||||
bool Anm2::regions_generate(const std::set<int>& animationIndices, const std::set<Reference>& frameReferences,
|
||||
const std::string& format, RegionFrameMapping mapping)
|
||||
{
|
||||
std::vector<std::pair<Element*, Element*>> frames{};
|
||||
for (auto animationIndex : animationIndices)
|
||||
if (auto animation = element_get(ElementType::ANIMATION, animationIndex))
|
||||
layer_frames_each(root, *animation,
|
||||
[&](Element& frame, Element& spritesheet) { frames.emplace_back(&frame, &spritesheet); });
|
||||
|
||||
for (auto frameReference : frameReferences)
|
||||
{
|
||||
if (frameReference.itemType != LAYER || frameReference.frameIndex < 0) continue;
|
||||
auto frame = element_get(frameReference);
|
||||
auto layer = element_get(ElementType::LAYER_ELEMENT, frameReference.itemID);
|
||||
auto spritesheet = layer ? element_get(ElementType::SPRITESHEET, layer->spritesheetId) : nullptr;
|
||||
if (frame && frame->type == ElementType::FRAME && frame->regionId == -1 && spritesheet)
|
||||
frames.emplace_back(frame, spritesheet);
|
||||
}
|
||||
|
||||
std::unordered_map<int, int> regionNumbers{};
|
||||
bool isChanged{};
|
||||
for (auto [frame, spritesheet] : frames)
|
||||
{
|
||||
auto region = region_match_get(*spritesheet, *frame);
|
||||
if (!region)
|
||||
{
|
||||
auto id = element_child_next_id_get(*spritesheet, ElementType::REGION);
|
||||
region = &spritesheet->children.emplace_back(element_make(ElementType::REGION));
|
||||
region->id = id;
|
||||
region->name = region_name_get(format, ++regionNumbers[spritesheet->id]);
|
||||
region->crop = frame->crop;
|
||||
region->size = frame->size;
|
||||
region->pivot = frame->pivot;
|
||||
isChanged = true;
|
||||
}
|
||||
if (mapping == RegionFrameMapping::SET && frame->regionId != region->id)
|
||||
{
|
||||
frame->regionId = region->id;
|
||||
isChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
return isChanged;
|
||||
}
|
||||
|
||||
void Anm2::regions_scan()
|
||||
{
|
||||
layer_frames_each(root,
|
||||
[](Element& frame, Element& spritesheet)
|
||||
{
|
||||
if (frame.regionId != -1) return;
|
||||
if (auto region = region_match_get(spritesheet, frame)) frame.regionId = region->id;
|
||||
});
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -85,8 +85,7 @@ namespace anm2ed
|
||||
auto pathUtf8 = path::to_utf8(path);
|
||||
system(std::format("xdg-open \"{}\" &", pathUtf8).c_str());
|
||||
#else
|
||||
toasts.push(localize.get(TOAST_NOT_SUPPORTED));
|
||||
logger.warning(localize.get(TOAST_NOT_SUPPORTED, anm2ed::ENGLISH));
|
||||
toast_log(Level::WARNING, TOAST_NOT_SUPPORTED);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
+365
-1675
File diff suppressed because it is too large.
Load diff
+39
-95
@@ -20,7 +20,41 @@ namespace anm2ed
|
||||
class Manager;
|
||||
struct Command;
|
||||
|
||||
class Document
|
||||
struct DocumentData
|
||||
{
|
||||
std::filesystem::path path{};
|
||||
uint64_t tabId{};
|
||||
Snapshots snapshots{};
|
||||
std::map<int, Storage> regionBySpritesheet{};
|
||||
int changeAllFramePropertiesRegionId{-1};
|
||||
int changeAllFramePropertiesShaderId{-1};
|
||||
|
||||
float previewZoom{200};
|
||||
glm::vec2 previewPan{};
|
||||
glm::vec2 editorPan{};
|
||||
float editorZoom{200};
|
||||
int overlayIndex{-1};
|
||||
uint64_t overlayDocumentId{};
|
||||
|
||||
uint64_t hash{};
|
||||
uint64_t saveHash{};
|
||||
uint64_t autosaveHash{};
|
||||
double lastAutosaveTime{};
|
||||
bool isValid{true};
|
||||
bool isOpen{true};
|
||||
bool isForceDirty{false};
|
||||
std::unordered_map<int, uint64_t> spritesheetHashes{};
|
||||
std::unordered_map<int, uint64_t> spritesheetSaveHashes{};
|
||||
std::unordered_map<int, std::filesystem::path> texturePaths{};
|
||||
std::unordered_map<int, std::filesystem::path> soundPaths{};
|
||||
std::map<int, std::filesystem::path> shaderVertexPaths{};
|
||||
std::map<int, std::filesystem::path> shaderFragmentPaths{};
|
||||
std::map<int, resource::Shader> shaders{};
|
||||
bool isAnimationPreviewSet{false};
|
||||
bool isSpritesheetEditorSet{false};
|
||||
};
|
||||
|
||||
class Document : public DocumentData
|
||||
{
|
||||
public:
|
||||
enum ChangeType
|
||||
@@ -54,72 +88,13 @@ namespace anm2ed
|
||||
SPRITESHEET
|
||||
};
|
||||
|
||||
enum class RegionFrameMapping
|
||||
{
|
||||
PRESERVE,
|
||||
SET
|
||||
};
|
||||
|
||||
enum FileMergePreset
|
||||
{
|
||||
FILE_MERGE_PRESET_MERGE_BY_NAME,
|
||||
FILE_MERGE_PRESET_APPEND_AS_NEW,
|
||||
FILE_MERGE_PRESET_REPLACE_MATCHING,
|
||||
FILE_MERGE_PRESET_COUNT
|
||||
};
|
||||
|
||||
std::filesystem::path path{};
|
||||
uint64_t tabId{};
|
||||
|
||||
Snapshots snapshots{};
|
||||
Snapshot& current = snapshots.current;
|
||||
|
||||
Playback& playback = current.playback;
|
||||
Storage& animation = current.animation;
|
||||
Storage& event = current.event;
|
||||
Storage& frames = current.frames;
|
||||
Storage& items = current.items;
|
||||
Storage& layer = current.layer;
|
||||
Storage& merge = current.merge;
|
||||
Storage& null = current.null;
|
||||
Storage& region = current.region;
|
||||
Storage& shader = current.shader;
|
||||
Storage& sound = current.sound;
|
||||
Storage& spritesheet = current.spritesheet;
|
||||
std::map<int, resource::Texture>& textures = current.textures;
|
||||
std::map<int, resource::Audio>& sounds = current.sounds;
|
||||
#define X(type, name) type& name = current.name;
|
||||
SNAPSHOT_STEP_STATE_FIELDS
|
||||
SNAPSHOT_STEP_RESOURCE_FIELDS
|
||||
#undef X
|
||||
Anm2& anm2 = current.anm2;
|
||||
Reference& reference = current.reference;
|
||||
std::set<Reference>& groupReferences = current.groupReferences;
|
||||
float& frameTime = current.frameTime;
|
||||
std::string& message = current.message;
|
||||
std::map<int, Storage> regionBySpritesheet{};
|
||||
int changeAllFramePropertiesRegionId{-1};
|
||||
int changeAllFramePropertiesShaderId{-1};
|
||||
|
||||
float previewZoom{200};
|
||||
glm::vec2 previewPan{};
|
||||
glm::vec2 editorPan{};
|
||||
float editorZoom{200};
|
||||
int overlayIndex{-1};
|
||||
uint64_t overlayDocumentId{};
|
||||
|
||||
uint64_t hash{};
|
||||
uint64_t saveHash{};
|
||||
uint64_t autosaveHash{};
|
||||
double lastAutosaveTime{};
|
||||
bool isValid{true};
|
||||
bool isOpen{true};
|
||||
bool isForceDirty{false};
|
||||
std::unordered_map<int, uint64_t> spritesheetHashes{};
|
||||
std::unordered_map<int, uint64_t> spritesheetSaveHashes{};
|
||||
std::unordered_map<int, std::filesystem::path> texturePaths{};
|
||||
std::unordered_map<int, std::filesystem::path> soundPaths{};
|
||||
std::map<int, std::filesystem::path> shaderVertexPaths{};
|
||||
std::map<int, std::filesystem::path> shaderFragmentPaths{};
|
||||
std::map<int, resource::Shader> shaders{};
|
||||
bool isAnimationPreviewSet{false};
|
||||
bool isSpritesheetEditorSet{false};
|
||||
EditTarget editTarget{EditTarget::NONE};
|
||||
|
||||
Document(const std::filesystem::path&, bool = false, std::string* = nullptr);
|
||||
@@ -128,25 +103,17 @@ namespace anm2ed
|
||||
Document(Document&&) noexcept;
|
||||
Document& operator=(Document&&) noexcept;
|
||||
bool save(const std::filesystem::path& = {}, std::string* = nullptr, Options = {});
|
||||
void anm2_change(ChangeType);
|
||||
void assets_sync(ChangeType = ALL);
|
||||
void texture_change(int);
|
||||
bool texture_reload(int);
|
||||
bool sound_reload(int);
|
||||
bool shader_reload(int, std::string* = nullptr);
|
||||
resource::Texture* texture_get(int);
|
||||
const resource::Texture* texture_get(int) const;
|
||||
resource::Audio* sound_get(int);
|
||||
const resource::Audio* sound_get(int) const;
|
||||
resource::Shader* shader_get(int);
|
||||
const resource::Shader* shader_get(int) const;
|
||||
bool regions_trim(int, const std::set<int>&);
|
||||
bool regions_generate_from_animations(const std::set<int>&, const std::string&, RegionFrameMapping);
|
||||
bool regions_generate_from_frames(const std::set<Reference>&, const std::string&, RegionFrameMapping);
|
||||
bool spritesheet_pack(int, int);
|
||||
bool spritesheets_merge(const std::set<int>&, bool, bool, bool, origin::Type);
|
||||
void scan_and_set_regions();
|
||||
bool file_merge(const std::filesystem::path&, FileMergePreset = FILE_MERGE_PRESET_MERGE_BY_NAME);
|
||||
void hash_set();
|
||||
void clean();
|
||||
void change(ChangeType);
|
||||
@@ -175,39 +142,16 @@ namespace anm2ed
|
||||
Element* spritesheet_get();
|
||||
Element* animation_get();
|
||||
|
||||
void spritesheet_add(const std::filesystem::path&);
|
||||
void spritesheets_add(const std::vector<std::filesystem::path>&);
|
||||
void sound_add(const std::filesystem::path&);
|
||||
void sounds_add(const std::vector<std::filesystem::path>&);
|
||||
|
||||
bool autosave(std::string* = nullptr, Options = {});
|
||||
std::filesystem::path autosave_path_get();
|
||||
std::filesystem::path path_from_autosave_get(const std::filesystem::path&);
|
||||
|
||||
void anm2_snapshot(const std::string& message);
|
||||
void tracks_snapshot(const std::string& message, const std::set<Reference>&);
|
||||
void frames_snapshot(const std::string& message, const std::set<Reference>&);
|
||||
void regions_snapshot(const std::string& message, int, const std::set<int>&);
|
||||
void textures_snapshot(const std::string& message);
|
||||
void anm2_textures_snapshot(const std::string& message);
|
||||
void undo();
|
||||
void redo();
|
||||
bool is_able_to_undo();
|
||||
bool is_able_to_redo();
|
||||
};
|
||||
|
||||
#define DOCUMENT_EDIT(document, message, changeType, body) \
|
||||
{ \
|
||||
document.anm2_snapshot(message); \
|
||||
body; \
|
||||
document.change(changeType); \
|
||||
}
|
||||
|
||||
#define DOCUMENT_EDIT_PTR(document, message, changeType, body) \
|
||||
{ \
|
||||
document->anm2_snapshot(message); \
|
||||
body; \
|
||||
document->change(changeType); \
|
||||
}
|
||||
|
||||
}
|
||||
+31
-38
@@ -27,18 +27,18 @@ namespace anm2ed::imgui
|
||||
uint64_t documentId{};
|
||||
};
|
||||
|
||||
#define ANM2_DRAG_DROP_MERGE_PRESETS \
|
||||
#define ANM2_DRAG_DROP_MERGE_PRESETS \
|
||||
X(FILE_MERGE_PRESET_MERGE_BY_NAME, BASIC_MERGE, TOOLTIP_MERGE_PRESET_MERGE_BY_NAME) \
|
||||
X(FILE_MERGE_PRESET_APPEND_AS_NEW, BASIC_APPEND, TOOLTIP_MERGE_PRESET_APPEND_AS_NEW) \
|
||||
X(FILE_MERGE_PRESET_APPEND_AS_NEW, BASIC_APPEND, TOOLTIP_MERGE_PRESET_APPEND_AS_NEW) \
|
||||
X(FILE_MERGE_PRESET_REPLACE_MATCHING, BASIC_REPLACE, TOOLTIP_MERGE_PRESET_REPLACE_MATCHING)
|
||||
|
||||
constexpr StringType ANM2_DRAG_DROP_MERGE_PRESET_LABELS[Document::FILE_MERGE_PRESET_COUNT] = {
|
||||
constexpr StringType ANM2_DRAG_DROP_MERGE_PRESET_LABELS[FILE_MERGE_PRESET_COUNT] = {
|
||||
#define X(preset, label, tooltip) label,
|
||||
ANM2_DRAG_DROP_MERGE_PRESETS
|
||||
#undef X
|
||||
};
|
||||
|
||||
constexpr StringType ANM2_DRAG_DROP_MERGE_PRESET_TOOLTIPS[Document::FILE_MERGE_PRESET_COUNT] = {
|
||||
constexpr StringType ANM2_DRAG_DROP_MERGE_PRESET_TOOLTIPS[FILE_MERGE_PRESET_COUNT] = {
|
||||
#define X(preset, label, tooltip) tooltip,
|
||||
ANM2_DRAG_DROP_MERGE_PRESETS
|
||||
#undef X
|
||||
@@ -47,13 +47,13 @@ namespace anm2ed::imgui
|
||||
#undef ANM2_DRAG_DROP_MERGE_PRESETS
|
||||
|
||||
void anm2_drag_drop_merge_queue(Manager& manager, const std::vector<std::filesystem::path>& paths,
|
||||
Document::FileMergePreset preset)
|
||||
FileMergePreset preset)
|
||||
{
|
||||
manager.command_push({manager.selected,
|
||||
[paths, preset](Manager&, Document& document)
|
||||
manager.command_push({manager.selected, [paths, preset](Manager&, Document& document)
|
||||
{
|
||||
document.anm2_snapshot(localize.get(EDIT_MERGE_ANM2));
|
||||
for (auto& path : paths) document.file_merge(path, preset);
|
||||
document.snapshots.anm2_push(localize.get(EDIT_MERGE_ANM2));
|
||||
for (auto& path : paths)
|
||||
document.anm2.file_merge(path, document.directory_get(), preset);
|
||||
document.change(Document::ALL);
|
||||
}});
|
||||
}
|
||||
@@ -70,8 +70,8 @@ namespace anm2ed::imgui
|
||||
order.reserve(manager.documents.size());
|
||||
for (auto documentId : pendingDocumentTabIds)
|
||||
{
|
||||
auto it = std::ranges::find_if(manager.documents, [documentId](const Document& document)
|
||||
{ return document.tabId == documentId; });
|
||||
auto it = std::ranges::find_if(manager.documents,
|
||||
[documentId](const Document& document) { return document.tabId == documentId; });
|
||||
if (it == manager.documents.end())
|
||||
{
|
||||
pendingDocumentTabIds.clear();
|
||||
@@ -115,7 +115,8 @@ namespace anm2ed::imgui
|
||||
index_remap(manager.selected);
|
||||
index_remap(manager.pendingSelected);
|
||||
index_remap(closeDocumentIndex);
|
||||
for (auto& index : manager.selectionHistory) index_remap(index);
|
||||
for (auto& index : manager.selectionHistory)
|
||||
index_remap(index);
|
||||
for (auto& command : manager.commands)
|
||||
if (!command.runManager) index_remap(command.documentIndex);
|
||||
std::erase(manager.selectionHistory, -1);
|
||||
@@ -151,8 +152,8 @@ namespace anm2ed::imgui
|
||||
if (document.lastAutosaveTime > time::SECOND_M)
|
||||
{
|
||||
auto options = settings.anm2_options_get();
|
||||
manager.command_push({i, [options](Manager& manager, Document& document)
|
||||
{ manager.autosave(document, options); }});
|
||||
manager.command_push(
|
||||
{i, [options](Manager& manager, Document& document) { manager.autosave(document, options); }});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -324,17 +325,11 @@ namespace anm2ed::imgui
|
||||
if (texture->write_png(savePath))
|
||||
{
|
||||
closeDocument.spritesheet_hash_set_saved(id);
|
||||
toasts.push(
|
||||
std::vformat(localize.get(TOAST_SAVE_SPRITESHEET), std::make_format_args(id, pathString)));
|
||||
logger.info(std::vformat(localize.get(TOAST_SAVE_SPRITESHEET, anm2ed::ENGLISH),
|
||||
std::make_format_args(id, pathString)));
|
||||
toast_log(Level::INFO, TOAST_SAVE_SPRITESHEET, id, pathString);
|
||||
}
|
||||
else
|
||||
{
|
||||
toasts.push(std::vformat(localize.get(TOAST_SAVE_SPRITESHEET_FAILED),
|
||||
std::make_format_args(id, pathString)));
|
||||
logger.error(std::vformat(localize.get(TOAST_SAVE_SPRITESHEET_FAILED, anm2ed::ENGLISH),
|
||||
std::make_format_args(id, pathString)));
|
||||
toast_log(Level::ERROR, TOAST_SAVE_SPRITESHEET_FAILED, id, pathString);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -350,12 +345,11 @@ namespace anm2ed::imgui
|
||||
if (ImGui::Button(localize.get(BASIC_NO), widgetSize))
|
||||
{
|
||||
auto index = closeDocumentIndex;
|
||||
manager.command_push({.runManager =
|
||||
[index](Manager& manager)
|
||||
{
|
||||
manager.autosave_file_clear(index);
|
||||
manager.close(index);
|
||||
}});
|
||||
manager.command_push({.runManager = [index](Manager& manager)
|
||||
{
|
||||
manager.autosave_file_clear(index);
|
||||
manager.close(index);
|
||||
}});
|
||||
close();
|
||||
}
|
||||
|
||||
@@ -389,7 +383,7 @@ namespace anm2ed::imgui
|
||||
manager.anm2DragDropPaths.clear();
|
||||
manager.anm2DragDropPopup.close();
|
||||
manager.anm2DragDropMergePopup.close();
|
||||
manager.anm2DragDropMergePreset = Document::FILE_MERGE_PRESET_MERGE_BY_NAME;
|
||||
manager.anm2DragDropMergePreset = FILE_MERGE_PRESET_MERGE_BY_NAME;
|
||||
};
|
||||
|
||||
if (manager.anm2DragDropPaths.empty())
|
||||
@@ -402,10 +396,9 @@ namespace anm2ed::imgui
|
||||
ImGuiWindowFlags_NoResize))
|
||||
{
|
||||
auto document = manager.get();
|
||||
manager.anm2DragDropMergePreset =
|
||||
std::clamp(manager.anm2DragDropMergePreset, 0, Document::FILE_MERGE_PRESET_COUNT - 1);
|
||||
manager.anm2DragDropMergePreset = std::clamp(manager.anm2DragDropMergePreset, 0, FILE_MERGE_PRESET_COUNT - 1);
|
||||
|
||||
for (int preset = 0; preset < Document::FILE_MERGE_PRESET_COUNT; ++preset)
|
||||
for (int preset = 0; preset < FILE_MERGE_PRESET_COUNT; ++preset)
|
||||
{
|
||||
ImGui::PushID(preset);
|
||||
ImGui::RadioButton(localize.get(ANM2_DRAG_DROP_MERGE_PRESET_LABELS[preset]),
|
||||
@@ -420,7 +413,7 @@ namespace anm2ed::imgui
|
||||
if (ImGui::Button(localize.get(BASIC_MERGE), widgetSize))
|
||||
{
|
||||
auto paths = manager.anm2DragDropPaths;
|
||||
auto preset = (Document::FileMergePreset)manager.anm2DragDropMergePreset;
|
||||
auto preset = (FileMergePreset)manager.anm2DragDropMergePreset;
|
||||
anm2_drag_drop_merge_queue(manager, paths, preset);
|
||||
drag_drop_reset();
|
||||
}
|
||||
@@ -452,11 +445,11 @@ namespace anm2ed::imgui
|
||||
: localize.get(LABEL_DOCUMENTS_OPEN_NEW)))
|
||||
{
|
||||
auto paths = manager.anm2DragDropPaths;
|
||||
manager.command_push({.runManager =
|
||||
[paths](Manager& manager)
|
||||
{
|
||||
for (auto& path : paths) manager.open(path);
|
||||
}});
|
||||
manager.command_push({.runManager = [paths](Manager& manager)
|
||||
{
|
||||
for (auto& path : paths)
|
||||
manager.open(path);
|
||||
}});
|
||||
drag_drop_reset();
|
||||
}
|
||||
|
||||
|
||||
@@ -195,8 +195,7 @@ namespace anm2ed::imgui::popup
|
||||
auto queuedAddItemSpritesheetID = addItemSpritesheetID;
|
||||
auto queuedAddItemIsShowRect = addItemIsShowRect;
|
||||
|
||||
manager.command_push({manager.selected,
|
||||
[=](Manager&, Document& document)
|
||||
manager.command_push({manager.selected, [=](Manager&, Document& document)
|
||||
{
|
||||
Element item{};
|
||||
item.id = queuedAddItemID;
|
||||
@@ -204,12 +203,12 @@ namespace anm2ed::imgui::popup
|
||||
item.spritesheetId = queuedAddItemSpritesheetID;
|
||||
item.isShowRect = queuedAddItemIsShowRect;
|
||||
|
||||
document.anm2_snapshot(localize.get(EDIT_ADD_ITEM));
|
||||
auto addId = document.anm2.item_add((ItemType)queuedType, queuedAnimationIndex, item,
|
||||
queuedInsertBeforeID,
|
||||
(destination::Type)queuedDestination);
|
||||
document.snapshots.anm2_push(localize.get(EDIT_ADD_ITEM));
|
||||
auto addId =
|
||||
document.anm2.item_add((ItemType)queuedType, queuedAnimationIndex, item,
|
||||
queuedInsertBeforeID, (destination::Type)queuedDestination);
|
||||
|
||||
document.anm2_change(Document::ITEMS);
|
||||
document.change(Document::ITEMS);
|
||||
|
||||
if (addId != -1)
|
||||
{
|
||||
|
||||
+19
-27
@@ -38,9 +38,7 @@ namespace anm2ed::imgui
|
||||
auto path = request.path;
|
||||
auto options = request.options;
|
||||
|
||||
manager.command_push({.runManager =
|
||||
[=](Manager& manager)
|
||||
{ manager.save(index, path, options); }});
|
||||
manager.command_push({.runManager = [=](Manager& manager) { manager.save(index, path, options); }});
|
||||
}
|
||||
|
||||
bool Taskbar::save_request(Manager& manager, Settings& settings, int index, const std::filesystem::path& path,
|
||||
@@ -145,11 +143,11 @@ namespace anm2ed::imgui
|
||||
if (dialog.is_selected(Dialog::ANM2_OPEN))
|
||||
{
|
||||
auto paths = dialog.paths;
|
||||
manager.command_push({.runManager =
|
||||
[paths](Manager& manager)
|
||||
{
|
||||
for (auto& path : paths) manager.open(path);
|
||||
}});
|
||||
manager.command_push({.runManager = [paths](Manager& manager)
|
||||
{
|
||||
for (auto& path : paths)
|
||||
manager.open(path);
|
||||
}});
|
||||
dialog.reset();
|
||||
}
|
||||
|
||||
@@ -172,15 +170,13 @@ namespace anm2ed::imgui
|
||||
|
||||
if (ImGui::MenuItem(localize.get(LABEL_SCAN_AND_SET_REGIONS), nullptr, false, document && hasRegions))
|
||||
{
|
||||
manager.command_push({manager.selected,
|
||||
[](Manager&, Document& document)
|
||||
manager.command_push({manager.selected, [](Manager&, Document& document)
|
||||
{
|
||||
document.anm2_snapshot(localize.get(EDIT_SCAN_AND_SET_REGIONS));
|
||||
document.scan_and_set_regions();
|
||||
document.snapshots.anm2_push(localize.get(EDIT_SCAN_AND_SET_REGIONS));
|
||||
document.anm2.regions_scan();
|
||||
document.change(Document::FRAMES);
|
||||
}});
|
||||
toasts.push(localize.get(TOAST_SCAN_AND_SET_REGIONS));
|
||||
logger.info(localize.get(TOAST_SCAN_AND_SET_REGIONS, anm2ed::ENGLISH));
|
||||
toast_log(Level::INFO, TOAST_SCAN_AND_SET_REGIONS);
|
||||
}
|
||||
ImGui::SetItemTooltip("%s", localize.get(TOOLTIP_WIZARD_SCAN_AND_SET_REGIONS));
|
||||
|
||||
@@ -284,19 +280,15 @@ namespace anm2ed::imgui
|
||||
|
||||
auto queuedAnimationIndices = animationIndices;
|
||||
auto queuedFormat = settings.generateRegionNameFormat;
|
||||
auto queuedMapping = isGenerateRegionsMapFrames ? Document::RegionFrameMapping::SET
|
||||
: Document::RegionFrameMapping::PRESERVE;
|
||||
manager.command_push({manager.selected,
|
||||
[=](Manager&, Document& document)
|
||||
{
|
||||
if (queuedAnimationIndices.empty()) return;
|
||||
document.anm2_snapshot(localize.get(EDIT_GENERATE_REGIONS_FROM_ANIMATIONS));
|
||||
if (document.regions_generate_from_animations(queuedAnimationIndices, queuedFormat,
|
||||
queuedMapping))
|
||||
document.anm2_change(queuedMapping == Document::RegionFrameMapping::SET
|
||||
? Document::ALL
|
||||
: Document::SPRITESHEETS);
|
||||
}});
|
||||
auto queuedMapping = isGenerateRegionsMapFrames ? RegionFrameMapping::SET : RegionFrameMapping::PRESERVE;
|
||||
manager.command_push(
|
||||
{manager.selected, [=](Manager&, Document& document)
|
||||
{
|
||||
if (queuedAnimationIndices.empty()) return;
|
||||
document.snapshots.anm2_push(localize.get(EDIT_GENERATE_REGIONS_FROM_ANIMATIONS));
|
||||
if (document.anm2.regions_generate(queuedAnimationIndices, {}, queuedFormat, queuedMapping))
|
||||
document.change(queuedMapping == RegionFrameMapping::SET ? Document::ALL : Document::SPRITESHEETS);
|
||||
}});
|
||||
generateRegionsPopup.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <format>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "log.hpp"
|
||||
#include "strings.hpp"
|
||||
|
||||
namespace anm2ed::imgui
|
||||
{
|
||||
class Toast
|
||||
@@ -26,3 +30,19 @@ namespace anm2ed::imgui
|
||||
}
|
||||
|
||||
extern anm2ed::imgui::Toasts toasts;
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
template <class... Args> void toast_log(Level level, StringType type, Args&&... args)
|
||||
{
|
||||
auto format = [&](Language language) -> std::string
|
||||
{
|
||||
if constexpr (sizeof...(Args) == 0)
|
||||
return localize.get(type, language);
|
||||
else
|
||||
return std::vformat(localize.get(type, language), std::make_format_args(args...));
|
||||
};
|
||||
toasts.push(format(localize.language));
|
||||
logger.write(level, format(ENGLISH));
|
||||
}
|
||||
}
|
||||
@@ -306,16 +306,11 @@ namespace anm2ed::imgui
|
||||
{
|
||||
if (!renderTempFrames.empty())
|
||||
{
|
||||
toasts.push(std::vformat(localize.get(TOAST_EXPORT_RENDERED_FRAMES), std::make_format_args(pathString)));
|
||||
logger.info(std::vformat(localize.get(TOAST_EXPORT_RENDERED_FRAMES, anm2ed::ENGLISH),
|
||||
std::make_format_args(pathString)));
|
||||
toast_log(Level::INFO, TOAST_EXPORT_RENDERED_FRAMES, pathString);
|
||||
}
|
||||
else
|
||||
{
|
||||
toasts.push(
|
||||
std::vformat(localize.get(TOAST_EXPORT_RENDERED_FRAMES_FAILED), std::make_format_args(pathString)));
|
||||
logger.error(std::vformat(localize.get(TOAST_EXPORT_RENDERED_FRAMES_FAILED, anm2ed::ENGLISH),
|
||||
std::make_format_args(pathString)));
|
||||
toast_log(Level::ERROR, TOAST_EXPORT_RENDERED_FRAMES_FAILED, pathString);
|
||||
}
|
||||
}
|
||||
else if (type == render::SPRITESHEET)
|
||||
@@ -328,16 +323,14 @@ namespace anm2ed::imgui
|
||||
|
||||
if (renderTempFrames.empty())
|
||||
{
|
||||
toasts.push(localize.get(TOAST_SPRITESHEET_NO_FRAMES));
|
||||
logger.warning(localize.get(TOAST_SPRITESHEET_NO_FRAMES, anm2ed::ENGLISH));
|
||||
toast_log(Level::WARNING, TOAST_SPRITESHEET_NO_FRAMES);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto firstFrame = Texture(renderTempFrames.front());
|
||||
if (firstFrame.size.x <= 0 || firstFrame.size.y <= 0 || firstFrame.pixels.empty())
|
||||
{
|
||||
toasts.push(localize.get(TOAST_SPRITESHEET_EMPTY));
|
||||
logger.error(localize.get(TOAST_SPRITESHEET_EMPTY, anm2ed::ENGLISH));
|
||||
toast_log(Level::ERROR, TOAST_SPRITESHEET_EMPTY);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -368,16 +361,11 @@ namespace anm2ed::imgui
|
||||
Texture spritesheetTexture(spritesheet.data(), spritesheetSize);
|
||||
if (spritesheetTexture.write_png(path))
|
||||
{
|
||||
toasts.push(std::vformat(localize.get(TOAST_EXPORT_SPRITESHEET), std::make_format_args(pathString)));
|
||||
logger.info(std::vformat(localize.get(TOAST_EXPORT_SPRITESHEET, anm2ed::ENGLISH),
|
||||
std::make_format_args(pathString)));
|
||||
toast_log(Level::INFO, TOAST_EXPORT_SPRITESHEET, pathString);
|
||||
}
|
||||
else
|
||||
{
|
||||
toasts.push(
|
||||
std::vformat(localize.get(TOAST_EXPORT_SPRITESHEET_FAILED), std::make_format_args(pathString)));
|
||||
logger.error(std::vformat(localize.get(TOAST_EXPORT_SPRITESHEET_FAILED, anm2ed::ENGLISH),
|
||||
std::make_format_args(pathString)));
|
||||
toast_log(Level::ERROR, TOAST_EXPORT_SPRITESHEET_FAILED, pathString);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -398,16 +386,11 @@ namespace anm2ed::imgui
|
||||
|
||||
if (animation_render(ffmpegPath, path, renderTempFrames, audioStream, (render::Type)type, renderFrameRate))
|
||||
{
|
||||
toasts.push(std::vformat(localize.get(TOAST_EXPORT_RENDERED_ANIMATION), std::make_format_args(pathString)));
|
||||
logger.info(std::vformat(localize.get(TOAST_EXPORT_RENDERED_ANIMATION, anm2ed::ENGLISH),
|
||||
std::make_format_args(pathString)));
|
||||
toast_log(Level::INFO, TOAST_EXPORT_RENDERED_ANIMATION, pathString);
|
||||
}
|
||||
else
|
||||
{
|
||||
toasts.push(
|
||||
std::vformat(localize.get(TOAST_EXPORT_RENDERED_ANIMATION_FAILED), std::make_format_args(pathString)));
|
||||
logger.error(std::vformat(localize.get(TOAST_EXPORT_RENDERED_ANIMATION_FAILED, anm2ed::ENGLISH),
|
||||
std::make_format_args(pathString)));
|
||||
toast_log(Level::ERROR, TOAST_EXPORT_RENDERED_ANIMATION_FAILED, pathString);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -791,10 +774,7 @@ namespace anm2ed::imgui
|
||||
}
|
||||
|
||||
auto pathString = path::to_utf8(settings.renderPath);
|
||||
toasts.push(
|
||||
std::vformat(localize.get(TOAST_EXPORT_RENDERED_ANIMATION_FAILED), std::make_format_args(pathString)));
|
||||
logger.error(std::vformat(localize.get(TOAST_EXPORT_RENDERED_ANIMATION_FAILED, anm2ed::ENGLISH),
|
||||
std::make_format_args(pathString)));
|
||||
toast_log(Level::ERROR, TOAST_EXPORT_RENDERED_ANIMATION_FAILED, pathString);
|
||||
manager.isRecording = false;
|
||||
manager.isRecordingStart = false;
|
||||
renderFrameIndex = 0;
|
||||
@@ -1240,10 +1220,7 @@ namespace anm2ed::imgui
|
||||
auto render_capture_fail = [&]()
|
||||
{
|
||||
auto pathString = path::to_utf8(settings.renderPath);
|
||||
toasts.push(
|
||||
std::vformat(localize.get(TOAST_EXPORT_RENDERED_ANIMATION_FAILED), std::make_format_args(pathString)));
|
||||
logger.error(std::vformat(localize.get(TOAST_EXPORT_RENDERED_ANIMATION_FAILED, anm2ed::ENGLISH),
|
||||
std::make_format_args(pathString)));
|
||||
toast_log(Level::ERROR, TOAST_EXPORT_RENDERED_ANIMATION_FAILED, pathString);
|
||||
if (renderType != render::PNGS) render_temp_cleanup(renderTempDirectory, renderTempFrames);
|
||||
renderFrameSoundIDs.clear();
|
||||
if (isRenderPreviewOverridden)
|
||||
@@ -1407,7 +1384,7 @@ namespace anm2ed::imgui
|
||||
{
|
||||
auto queuedFrameReferences = selectedFrameReferences;
|
||||
manager.command_push({manager.selected, [message, queuedFrameReferences](Manager&, Document& document)
|
||||
{ document.frames_snapshot(localize.get(message), queuedFrameReferences); }});
|
||||
{ document.snapshots.frames_push(localize.get(message), queuedFrameReferences); }});
|
||||
};
|
||||
auto frame_change_apply = [&](FrameChange frameChange, ChangeType changeType = ChangeType::ADJUST)
|
||||
{
|
||||
@@ -1450,7 +1427,7 @@ namespace anm2ed::imgui
|
||||
auto frames_changed = [&]()
|
||||
{
|
||||
manager.command_push(
|
||||
{manager.selected, [](Manager&, Document& document) { document.anm2_change(Document::FRAMES); }});
|
||||
{manager.selected, [](Manager&, Document& document) { document.change(Document::FRAMES); }});
|
||||
};
|
||||
auto null_rect_change = [&](vec2 topLeft, vec2 rectSize)
|
||||
{
|
||||
|
||||
@@ -49,9 +49,9 @@ namespace anm2ed::imgui
|
||||
auto item = document.anm2.element_get(itemReference);
|
||||
if (!frame) return;
|
||||
|
||||
document.frames_snapshot(localize.get(message), {queuedReference});
|
||||
document.snapshots.frames_push(localize.get(message), {queuedReference});
|
||||
behavior(document, *frame, item, queuedReference);
|
||||
document.anm2_change(Document::FRAMES);
|
||||
document.change(Document::FRAMES);
|
||||
}});
|
||||
};
|
||||
|
||||
@@ -70,9 +70,9 @@ namespace anm2ed::imgui
|
||||
if (!frame) return;
|
||||
|
||||
if (state == edit::START)
|
||||
document.frames_snapshot(localize.get(message), {queuedReference});
|
||||
document.snapshots.frames_push(localize.get(message), {queuedReference});
|
||||
behavior(document, *frame, item, queuedReference);
|
||||
if (state == edit::END) document.anm2_change(Document::FRAMES);
|
||||
if (state == edit::END) document.change(Document::FRAMES);
|
||||
}});
|
||||
};
|
||||
|
||||
|
||||
@@ -124,57 +124,20 @@ namespace anm2ed::imgui
|
||||
|
||||
void shader_uniform_config_save(Document& document, Element& shaderElement, const resource::shader::Uniform& uniform)
|
||||
{
|
||||
auto config = shader_uniform_get(shaderElement, uniform.name);
|
||||
if (!config)
|
||||
{
|
||||
config = &shaderElement.children.emplace_back(element_make(ElementType::UNIFORM));
|
||||
config->name = uniform.name;
|
||||
}
|
||||
|
||||
config->binding = std::string(resource::shader::uniform_binding_value_get(uniform.binding));
|
||||
config->value = resource::shader::uniform_value_string_get(uniform);
|
||||
if (uniform.binding == resource::shader::UNIFORM_BINDING_COMPONENTS)
|
||||
{
|
||||
for (int index = 0; index < (int)uniform.components.size(); ++index)
|
||||
{
|
||||
auto component = shader_uniform_component_get(*config, index);
|
||||
if (!component)
|
||||
{
|
||||
component = &config->children.emplace_back(element_make(ElementType::COMPONENT));
|
||||
component->index = index;
|
||||
}
|
||||
component->binding =
|
||||
std::string(resource::shader::uniform_binding_value_get(uniform.components[index].binding));
|
||||
component->value = std::format("{:.6g}", uniform.components[index].value);
|
||||
}
|
||||
}
|
||||
else
|
||||
std::erase_if(config->children, [](const Element& child) { return child.type == ElementType::COMPONENT; });
|
||||
resource::shader::uniform_config_save(shaderElement, uniform);
|
||||
document.change(Document::SHADERS);
|
||||
}
|
||||
|
||||
int shader_uniform_component_count_get(resource::shader::UniformValueType type)
|
||||
{
|
||||
if (type == resource::shader::UNIFORM_VALUE_VEC2) return 2;
|
||||
if (type == resource::shader::UNIFORM_VALUE_VEC3) return 3;
|
||||
if (type == resource::shader::UNIFORM_VALUE_VEC4) return 4;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void shader_uniform_components_from_value_set(resource::shader::Uniform& uniform)
|
||||
{
|
||||
uniform.components[0].value = uniform.value.x;
|
||||
uniform.components[1].value = uniform.value.y;
|
||||
uniform.components[2].value = uniform.value.z;
|
||||
uniform.components[3].value = uniform.value.w;
|
||||
for (int i = 0; i < (int)uniform.components.size(); ++i)
|
||||
uniform.components[i].value = uniform.value[i];
|
||||
}
|
||||
|
||||
void shader_uniform_value_from_components_set(resource::shader::Uniform& uniform)
|
||||
{
|
||||
uniform.value.x = uniform.components[0].value;
|
||||
uniform.value.y = uniform.components[1].value;
|
||||
uniform.value.z = uniform.components[2].value;
|
||||
uniform.value.w = uniform.components[3].value;
|
||||
for (int i = 0; i < (int)uniform.components.size(); ++i)
|
||||
uniform.value[i] = uniform.components[i].value;
|
||||
}
|
||||
|
||||
bool shader_uniform_value_update(resource::shader::Uniform& uniform)
|
||||
@@ -227,7 +190,7 @@ namespace anm2ed::imgui
|
||||
{
|
||||
bool isChanged{};
|
||||
constexpr const char* labels[] = {"X", "Y", "Z", "W"};
|
||||
auto componentCount = shader_uniform_component_count_get(uniform.valueType);
|
||||
auto componentCount = resource::shader::UNIFORM_VALUE_TYPE_INFOS[uniform.valueType].componentCount;
|
||||
|
||||
for (int index = 0; index < componentCount; ++index)
|
||||
{
|
||||
@@ -328,12 +291,7 @@ namespace anm2ed::imgui
|
||||
if (ImGui::Begin(localize.get(LABEL_SHADERS_WINDOW), &settings.windowIsShaders))
|
||||
{
|
||||
auto document = manager.get();
|
||||
if (!document)
|
||||
{
|
||||
ImGui::End();
|
||||
return;
|
||||
}
|
||||
auto shaders = document->anm2.element_get(ElementType::SHADERS);
|
||||
auto shaders = document ? document->anm2.element_get(ElementType::SHADERS) : nullptr;
|
||||
if (!shaders)
|
||||
{
|
||||
ImGui::End();
|
||||
@@ -354,12 +312,12 @@ namespace anm2ed::imgui
|
||||
shader.id = element_child_next_id_get(*shaders, ElementType::SHADER);
|
||||
shader.name = localize.get(TEXT_NEW_SHADER);
|
||||
|
||||
document.anm2_snapshot(localize.get(EDIT_ADD_SHADER));
|
||||
document.snapshots.anm2_push(localize.get(EDIT_ADD_SHADER));
|
||||
shaders->children.push_back(shader);
|
||||
document.shader.selection = {shader.id};
|
||||
document.shader.reference = shader.id;
|
||||
newElementId = shader.id;
|
||||
document.anm2_change(Document::SHADERS);
|
||||
document.change(Document::SHADERS);
|
||||
}});
|
||||
};
|
||||
|
||||
@@ -372,7 +330,7 @@ namespace anm2ed::imgui
|
||||
auto unused = document.anm2.element_unused(ElementType::SHADER);
|
||||
if (unused.empty()) return;
|
||||
|
||||
document.anm2_snapshot(localize.get(EDIT_REMOVE_UNUSED_SHADERS));
|
||||
document.snapshots.anm2_push(localize.get(EDIT_REMOVE_UNUSED_SHADERS));
|
||||
for (auto id : unused)
|
||||
{
|
||||
element_child_id_erase(*shaders, ElementType::SHADER, id);
|
||||
@@ -380,7 +338,7 @@ namespace anm2ed::imgui
|
||||
if (document.shader.reference == id) document.shader.reference = -1;
|
||||
if (popupShaderId == id) propertiesPopup.close();
|
||||
}
|
||||
document.anm2_change(Document::SHADERS);
|
||||
document.change(Document::SHADERS);
|
||||
}});
|
||||
};
|
||||
|
||||
|
||||
@@ -238,7 +238,8 @@ namespace anm2ed::imgui
|
||||
auto spritesheetModel = math::quad_model_get(viewTexture->size);
|
||||
auto spritesheetTransform = transform * spritesheetModel;
|
||||
|
||||
if (baseTexture && baseTexture->is_valid()) texture_render(shaderTexture, baseTexture->id, spritesheetTransform);
|
||||
if (baseTexture && baseTexture->is_valid())
|
||||
texture_render(shaderTexture, baseTexture->id, spritesheetTransform);
|
||||
|
||||
if (isGrid) grid_render(shaderGrid, zoom, pan, gridSize, gridOffset, gridColor);
|
||||
|
||||
@@ -431,9 +432,8 @@ namespace anm2ed::imgui
|
||||
{
|
||||
auto message = std::string(localize.get(messageType));
|
||||
auto queuedFrameReferences = frameReferences;
|
||||
manager.command_push({manager.selected,
|
||||
[message, queuedFrameReferences](Manager&, Document& document)
|
||||
{ document.frames_snapshot(message, queuedFrameReferences); }});
|
||||
manager.command_push({manager.selected, [message, queuedFrameReferences](Manager&, Document& document)
|
||||
{ document.snapshots.frames_push(message, queuedFrameReferences); }});
|
||||
};
|
||||
auto region_snapshot_push = [&](StringType messageType, const std::set<int>& regionIds)
|
||||
{
|
||||
@@ -442,42 +442,41 @@ namespace anm2ed::imgui
|
||||
auto queuedRegionIds = regionIds;
|
||||
manager.command_push({manager.selected,
|
||||
[message, queuedSpritesheet, queuedRegionIds](Manager&, Document& document)
|
||||
{ document.regions_snapshot(message, queuedSpritesheet, queuedRegionIds); }});
|
||||
{ document.snapshots.regions_push(message, queuedSpritesheet, queuedRegionIds); }});
|
||||
};
|
||||
auto texture_snapshot_push = [&](StringType messageType)
|
||||
{
|
||||
auto message = std::string(localize.get(messageType));
|
||||
manager.command_push(
|
||||
{manager.selected, [message](Manager&, Document& document) { document.textures_snapshot(message); }});
|
||||
manager.command_push({manager.selected, [message](Manager&, Document& document)
|
||||
{ document.snapshots.textures_push(message); }});
|
||||
};
|
||||
auto document_change_push = [&](Document::ChangeType changeType)
|
||||
{
|
||||
manager.command_push(
|
||||
{manager.selected, [changeType](Manager&, Document& document) { document.anm2_change(changeType); }});
|
||||
{manager.selected, [changeType](Manager&, Document& document) { document.change(changeType); }});
|
||||
};
|
||||
auto frame_change_apply_to = [&](const std::set<Reference>& frameReferences, FrameChange frameChange,
|
||||
ChangeType changeType = ChangeType::ADJUST)
|
||||
{
|
||||
auto queuedFrameReferences = frameReferences;
|
||||
manager.command_push(
|
||||
{manager.selected, [=](Manager&, Document& document)
|
||||
{
|
||||
std::map<Reference, std::set<int>> groupedFrames{};
|
||||
for (auto frameReference : queuedFrameReferences)
|
||||
{
|
||||
auto itemReference = frameReference;
|
||||
itemReference.frameIndex = -1;
|
||||
groupedFrames[itemReference].insert(frameReference.frameIndex);
|
||||
}
|
||||
manager.command_push({manager.selected, [=](Manager&, Document& document)
|
||||
{
|
||||
std::map<Reference, std::set<int>> groupedFrames{};
|
||||
for (auto frameReference : queuedFrameReferences)
|
||||
{
|
||||
auto itemReference = frameReference;
|
||||
itemReference.frameIndex = -1;
|
||||
groupedFrames[itemReference].insert(frameReference.frameIndex);
|
||||
}
|
||||
|
||||
for (auto& [itemReference, itemFrames] : groupedFrames)
|
||||
{
|
||||
auto itemType = static_cast<ItemType>(itemReference.itemType);
|
||||
auto item = document.anm2.element_get(itemReference);
|
||||
if (!item) continue;
|
||||
frames_change(*item, frameChange, itemType, changeType, itemFrames);
|
||||
}
|
||||
}});
|
||||
for (auto& [itemReference, itemFrames] : groupedFrames)
|
||||
{
|
||||
auto itemType = static_cast<ItemType>(itemReference.itemType);
|
||||
auto item = document.anm2.element_get(itemReference);
|
||||
if (!item) continue;
|
||||
frames_change(*item, frameChange, itemType, changeType, itemFrames);
|
||||
}
|
||||
}});
|
||||
};
|
||||
auto frame_change_apply = [&](FrameChange frameChange, ChangeType changeType = ChangeType::ADJUST)
|
||||
{ frame_change_apply_to(selectedFrameReferences, frameChange, changeType); };
|
||||
@@ -486,29 +485,28 @@ namespace anm2ed::imgui
|
||||
{
|
||||
auto queuedReference = editReference;
|
||||
auto queuedFrameReferences = frameReferences;
|
||||
manager.command_push(
|
||||
{manager.selected, [=](Manager&, Document& document)
|
||||
{
|
||||
if (queuedReference.frameIndex < 0) return;
|
||||
auto frame = document.anm2.element_get(queuedReference);
|
||||
if (!frame) return;
|
||||
manager.command_push({manager.selected, [=](Manager&, Document& document)
|
||||
{
|
||||
if (queuedReference.frameIndex < 0) return;
|
||||
auto frame = document.anm2.element_get(queuedReference);
|
||||
if (!frame) return;
|
||||
|
||||
std::map<Reference, std::set<int>> groupedFrames{};
|
||||
for (auto frameReference : queuedFrameReferences)
|
||||
{
|
||||
auto itemReference = frameReference;
|
||||
itemReference.frameIndex = -1;
|
||||
groupedFrames[itemReference].insert(frameReference.frameIndex);
|
||||
}
|
||||
std::map<Reference, std::set<int>> groupedFrames{};
|
||||
for (auto frameReference : queuedFrameReferences)
|
||||
{
|
||||
auto itemReference = frameReference;
|
||||
itemReference.frameIndex = -1;
|
||||
groupedFrames[itemReference].insert(frameReference.frameIndex);
|
||||
}
|
||||
|
||||
for (auto& [itemReference, itemFrames] : groupedFrames)
|
||||
{
|
||||
auto itemType = static_cast<ItemType>(itemReference.itemType);
|
||||
auto item = document.anm2.element_get(itemReference);
|
||||
if (!item) continue;
|
||||
frames_change(*item, frameChangeGet(*frame), itemType, changeType, itemFrames);
|
||||
}
|
||||
}});
|
||||
for (auto& [itemReference, itemFrames] : groupedFrames)
|
||||
{
|
||||
auto itemType = static_cast<ItemType>(itemReference.itemType);
|
||||
auto item = document.anm2.element_get(itemReference);
|
||||
if (!item) continue;
|
||||
frames_change(*item, frameChangeGet(*frame), itemType, changeType, itemFrames);
|
||||
}
|
||||
}});
|
||||
};
|
||||
auto frame_crop_normalize_apply_to =
|
||||
[&](const std::set<Reference>& frameReferences, bool isSnap, ivec2 snapGridSize, ivec2 snapGridOffset)
|
||||
@@ -639,8 +637,7 @@ namespace anm2ed::imgui
|
||||
{
|
||||
auto queuedSpritesheet = referenceSpritesheet;
|
||||
manager.command_push(
|
||||
{manager.selected, [=](Manager&, Document& document)
|
||||
{ document.texture_change(queuedSpritesheet); }});
|
||||
{manager.selected, [=](Manager&, Document& document) { document.texture_change(queuedSpritesheet); }});
|
||||
};
|
||||
|
||||
auto region_selection_set = [&](int id)
|
||||
@@ -808,7 +805,8 @@ namespace anm2ed::imgui
|
||||
if (isEnd) document_change_push(Document::FRAMES);
|
||||
break;
|
||||
case tool::CROP:
|
||||
if ((isRegionEditTarget || isRegionInUse || selectedFrameToolReferences.empty()) && !regionSelection.empty())
|
||||
if ((isRegionEditTarget || isRegionInUse || selectedFrameToolReferences.empty()) &&
|
||||
!regionSelection.empty())
|
||||
{
|
||||
if (!spritesheet || regionSelection.empty()) break;
|
||||
if (isBegin) region_snapshot_push(EDIT_REGION_CROP, regionSelection);
|
||||
@@ -1022,8 +1020,9 @@ namespace anm2ed::imgui
|
||||
actions_undo_redo_add(actions, manager, document);
|
||||
actions.separator();
|
||||
actions.add(ACTION_CENTER_VIEW, []() { return true; }, center_view);
|
||||
actions.add(ACTION_FIT_VIEW, [&]()
|
||||
{ return (baseTexture && baseTexture->is_valid()) || (texture && texture->is_valid()); }, fit_view);
|
||||
actions.add(
|
||||
ACTION_FIT_VIEW,
|
||||
[&]() { return (baseTexture && baseTexture->is_valid()) || (texture && texture->is_valid()); }, fit_view);
|
||||
actions.separator();
|
||||
actions.add(ACTION_ZOOM_IN, []() { return true; }, zoom_in);
|
||||
actions.add(ACTION_ZOOM_OUT, []() { return true; }, zoom_out);
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -36,11 +36,10 @@ namespace anm2ed::imgui
|
||||
{
|
||||
auto targetFrames = makeManyRegionReferences;
|
||||
auto format = settings.generateRegionNameFormat;
|
||||
auto mapping = isMakeManyRegionsMapFrames ? Document::RegionFrameMapping::SET
|
||||
: Document::RegionFrameMapping::PRESERVE;
|
||||
auto mapping = isMakeManyRegionsMapFrames ? RegionFrameMapping::SET : RegionFrameMapping::PRESERVE;
|
||||
edit_command_push(EDIT_GENERATE_REGIONS_FROM_ANIMATIONS, Document::ALL,
|
||||
[=, this](Manager&, Document& document) mutable
|
||||
{ document.regions_generate_from_frames(targetFrames, format, mapping); });
|
||||
{ document.anm2.regions_generate({}, targetFrames, format, mapping); });
|
||||
makeManyRegionsPopup.close();
|
||||
}
|
||||
|
||||
@@ -178,16 +177,17 @@ namespace anm2ed::imgui
|
||||
if (!selectedFrames.empty())
|
||||
{
|
||||
if (!isShortenChordHeld) frames_snapshot_command_push(EDIT_SHORTEN_FRAME, selectedFrames);
|
||||
command_push([=, this](Manager&, Document& document)
|
||||
{
|
||||
for (auto frameReference : selectedFrames)
|
||||
{
|
||||
auto frame = command_frame_get(document, frameReference);
|
||||
if (!frame) continue;
|
||||
frame->duration = std::max(FRAME_DURATION_MIN, frame->duration - 1);
|
||||
}
|
||||
document.anm2_change(Document::FRAMES);
|
||||
});
|
||||
command_push(
|
||||
[=, this](Manager&, Document& document)
|
||||
{
|
||||
for (auto frameReference : selectedFrames)
|
||||
{
|
||||
auto frame = command_frame_get(document, frameReference);
|
||||
if (!frame) continue;
|
||||
frame->duration = std::max(FRAME_DURATION_MIN, frame->duration - 1);
|
||||
}
|
||||
document.change(Document::FRAMES);
|
||||
});
|
||||
}
|
||||
}
|
||||
isShortenChordHeld = isShortenFrame;
|
||||
@@ -203,16 +203,17 @@ namespace anm2ed::imgui
|
||||
if (!selectedFrames.empty())
|
||||
{
|
||||
if (!isExtendChordHeld) frames_snapshot_command_push(EDIT_EXTEND_FRAME, selectedFrames);
|
||||
command_push([=, this](Manager&, Document& document)
|
||||
{
|
||||
for (auto frameReference : selectedFrames)
|
||||
{
|
||||
auto frame = command_frame_get(document, frameReference);
|
||||
if (!frame) continue;
|
||||
frame->duration = std::min(FRAME_DURATION_MAX, frame->duration + 1);
|
||||
}
|
||||
document.anm2_change(Document::FRAMES);
|
||||
});
|
||||
command_push(
|
||||
[=, this](Manager&, Document& document)
|
||||
{
|
||||
for (auto frameReference : selectedFrames)
|
||||
{
|
||||
auto frame = command_frame_get(document, frameReference);
|
||||
if (!frame) continue;
|
||||
frame->duration = std::min(FRAME_DURATION_MAX, frame->duration + 1);
|
||||
}
|
||||
document.change(Document::FRAMES);
|
||||
});
|
||||
}
|
||||
}
|
||||
isExtendChordHeld = isExtendFrame;
|
||||
|
||||
+378
-396
File diff suppressed because it is too large.
Load diff
@@ -413,7 +413,7 @@ namespace anm2ed::imgui::wizard
|
||||
groupedFrames[itemReference].insert(frameReference.frameIndex);
|
||||
}
|
||||
|
||||
document.anm2_snapshot(localize.get(EDIT_CHANGE_FRAME_PROPERTIES));
|
||||
document.snapshots.anm2_push(localize.get(EDIT_CHANGE_FRAME_PROPERTIES));
|
||||
for (auto& [itemReference, selection] : groupedFrames)
|
||||
{
|
||||
auto item = anm2.element_get(itemReference);
|
||||
@@ -421,13 +421,13 @@ namespace anm2ed::imgui::wizard
|
||||
frames_change(*item, frameChange, static_cast<ItemType>(itemReference.itemType),
|
||||
changeType, selection);
|
||||
}
|
||||
document.anm2_change(Document::FRAMES);
|
||||
document.change(Document::FRAMES);
|
||||
return;
|
||||
}
|
||||
|
||||
if (queuedIsItemsDestination)
|
||||
{
|
||||
document.anm2_snapshot(localize.get(EDIT_CHANGE_FRAME_PROPERTIES));
|
||||
document.snapshots.anm2_push(localize.get(EDIT_CHANGE_FRAME_PROPERTIES));
|
||||
for (auto itemReference : queuedItemReferences)
|
||||
{
|
||||
if (itemReference.itemType == TRIGGER) continue;
|
||||
@@ -438,11 +438,11 @@ namespace anm2ed::imgui::wizard
|
||||
frames_change(*item, frameChange, static_cast<ItemType>(itemReference.itemType),
|
||||
changeType, selection);
|
||||
}
|
||||
document.anm2_change(Document::FRAMES);
|
||||
document.change(Document::FRAMES);
|
||||
return;
|
||||
}
|
||||
|
||||
document.anm2_snapshot(localize.get(EDIT_CHANGE_FRAME_PROPERTIES));
|
||||
document.snapshots.anm2_push(localize.get(EDIT_CHANGE_FRAME_PROPERTIES));
|
||||
for (auto animationIndex : queuedAnimations)
|
||||
{
|
||||
auto animation = anm2.element_get(ElementType::ANIMATION, animationIndex);
|
||||
@@ -459,8 +459,7 @@ namespace anm2ed::imgui::wizard
|
||||
|
||||
if (queuedIsLayers)
|
||||
{
|
||||
auto layerAnimations =
|
||||
child_first_get(*animation, ElementType::LAYER_ANIMATIONS);
|
||||
auto layerAnimations = child_first_get(*animation, ElementType::LAYER_ANIMATIONS);
|
||||
if (layerAnimations)
|
||||
{
|
||||
auto item_change = [&](auto&& self, Element& item) -> void
|
||||
@@ -484,8 +483,7 @@ namespace anm2ed::imgui::wizard
|
||||
|
||||
if (queuedIsNulls)
|
||||
{
|
||||
auto nullAnimations =
|
||||
child_first_get(*animation, ElementType::NULL_ANIMATIONS);
|
||||
auto nullAnimations = child_first_get(*animation, ElementType::NULL_ANIMATIONS);
|
||||
if (nullAnimations)
|
||||
{
|
||||
auto item_change = [&](auto&& self, Element& item) -> void
|
||||
@@ -507,7 +505,7 @@ namespace anm2ed::imgui::wizard
|
||||
}
|
||||
}
|
||||
}
|
||||
document.anm2_change(Document::FRAMES);
|
||||
document.change(Document::FRAMES);
|
||||
}});
|
||||
isChanged = true;
|
||||
};
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
#include "generate_animation_from_grid.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <format>
|
||||
#include "math.hpp"
|
||||
#include "types.hpp"
|
||||
#include "util/imgui/draw.hpp"
|
||||
#include "util/imgui/input.hpp"
|
||||
#include "util/imgui/layout.hpp"
|
||||
#include <algorithm>
|
||||
#include <format>
|
||||
|
||||
using namespace anm2ed::types;
|
||||
using namespace anm2ed::util;
|
||||
@@ -102,10 +102,10 @@ namespace anm2ed::imgui::wizard
|
||||
{
|
||||
auto uvMin = frame.crop / textureSize;
|
||||
auto uvMax = (frame.crop + frame.size) / textureSize;
|
||||
mat4 transform = transform_get(zoom) *
|
||||
math::quad_model_get(frame.size, frame.position, frame.pivot,
|
||||
math::percent_to_unit(frame.scale), frame.rotation,
|
||||
math::percent_to_unit(frame.shear));
|
||||
mat4 transform =
|
||||
transform_get(zoom) * math::quad_model_get(frame.size, frame.position, frame.pivot,
|
||||
math::percent_to_unit(frame.scale), frame.rotation,
|
||||
math::percent_to_unit(frame.shear));
|
||||
auto vertices = math::uv_vertices_get(uvMin, uvMax);
|
||||
|
||||
texture_render(shaderTexture, sourceTexture->id, transform, frame.tint, frame.colorOffset, vertices.data());
|
||||
@@ -144,64 +144,60 @@ namespace anm2ed::imgui::wizard
|
||||
auto queuedIsMakeRegions = isMakeRegions;
|
||||
auto queuedRegionNameFormat = regionNameFormat;
|
||||
|
||||
manager.command_push({manager.selected,
|
||||
[=](Manager&, Document& document)
|
||||
{
|
||||
auto region_name_get = [](const std::string& format, int frameNumber)
|
||||
{
|
||||
try
|
||||
{
|
||||
return std::vformat(format, std::make_format_args(frameNumber));
|
||||
}
|
||||
catch (const std::format_error&)
|
||||
{
|
||||
return format;
|
||||
}
|
||||
};
|
||||
manager.command_push(
|
||||
{manager.selected, [=](Manager&, Document& document)
|
||||
{
|
||||
auto region_name_get = [](const std::string& format, int frameNumber)
|
||||
{
|
||||
try
|
||||
{
|
||||
return std::vformat(format, std::make_format_args(frameNumber));
|
||||
}
|
||||
catch (const std::format_error&)
|
||||
{
|
||||
return format;
|
||||
}
|
||||
};
|
||||
|
||||
bool isChanged{};
|
||||
bool isRegionsChanged{};
|
||||
for (auto queuedReference : queuedLayerReferences)
|
||||
{
|
||||
auto item = document.anm2.element_get(queuedReference);
|
||||
auto animation =
|
||||
document.anm2.element_get(ElementType::ANIMATION, queuedReference.animationIndex);
|
||||
auto layer = document.anm2.element_get(ElementType::LAYER_ELEMENT,
|
||||
queuedReference.itemID);
|
||||
auto spritesheet = layer ? document.anm2.element_get(ElementType::SPRITESHEET,
|
||||
layer->spritesheetId)
|
||||
: nullptr;
|
||||
if (!item || !animation) continue;
|
||||
bool isChanged{};
|
||||
bool isRegionsChanged{};
|
||||
for (auto queuedReference : queuedLayerReferences)
|
||||
{
|
||||
auto item = document.anm2.element_get(queuedReference);
|
||||
auto animation = document.anm2.element_get(ElementType::ANIMATION, queuedReference.animationIndex);
|
||||
auto layer = document.anm2.element_get(ElementType::LAYER_ELEMENT, queuedReference.itemID);
|
||||
auto spritesheet =
|
||||
layer ? document.anm2.element_get(ElementType::SPRITESHEET, layer->spritesheetId) : nullptr;
|
||||
if (!item || !animation) continue;
|
||||
|
||||
if (!isChanged)
|
||||
{
|
||||
document.anm2_snapshot(localize.get(EDIT_GENERATE_ANIMATION_FROM_GRID));
|
||||
isChanged = true;
|
||||
}
|
||||
auto frameIndexStart = (int)item->children.size();
|
||||
frames_generate_from_grid(*item, queuedStartPosition, queuedSize, queuedPivot,
|
||||
queuedColumns, queuedCount, queuedDelay);
|
||||
if (queuedIsMakeRegions && spritesheet)
|
||||
for (int frameIndex = frameIndexStart; frameIndex < (int)item->children.size();
|
||||
++frameIndex)
|
||||
{
|
||||
auto& frame = item->children[frameIndex];
|
||||
if (frame.type != ElementType::FRAME) continue;
|
||||
if (!isChanged)
|
||||
{
|
||||
document.snapshots.anm2_push(localize.get(EDIT_GENERATE_ANIMATION_FROM_GRID));
|
||||
isChanged = true;
|
||||
}
|
||||
auto frameIndexStart = (int)item->children.size();
|
||||
frames_generate_from_grid(*item, queuedStartPosition, queuedSize, queuedPivot, queuedColumns,
|
||||
queuedCount, queuedDelay);
|
||||
if (queuedIsMakeRegions && spritesheet)
|
||||
for (int frameIndex = frameIndexStart; frameIndex < (int)item->children.size(); ++frameIndex)
|
||||
{
|
||||
auto& frame = item->children[frameIndex];
|
||||
if (frame.type != ElementType::FRAME) continue;
|
||||
|
||||
auto region = element_make(ElementType::REGION);
|
||||
region.id = element_child_next_id_get(*spritesheet, ElementType::REGION);
|
||||
region.name = region_name_get(queuedRegionNameFormat, frameIndex - frameIndexStart);
|
||||
region.crop = frame.crop;
|
||||
region.size = frame.size;
|
||||
region.pivot = frame.pivot;
|
||||
frame.regionId = region.id;
|
||||
spritesheet->children.push_back(region);
|
||||
isRegionsChanged = true;
|
||||
}
|
||||
animation->frameNum = animation_length_get(*animation);
|
||||
}
|
||||
if (isChanged) document.anm2_change(isRegionsChanged ? Document::ALL : Document::FRAMES);
|
||||
}});
|
||||
auto region = element_make(ElementType::REGION);
|
||||
region.id = element_child_next_id_get(*spritesheet, ElementType::REGION);
|
||||
region.name = region_name_get(queuedRegionNameFormat, frameIndex - frameIndexStart);
|
||||
region.crop = frame.crop;
|
||||
region.size = frame.size;
|
||||
region.pivot = frame.pivot;
|
||||
frame.regionId = region.id;
|
||||
spritesheet->children.push_back(region);
|
||||
isRegionsChanged = true;
|
||||
}
|
||||
animation->frameNum = animation_length_get(*animation);
|
||||
}
|
||||
if (isChanged) document.change(isRegionsChanged ? Document::ALL : Document::FRAMES);
|
||||
}});
|
||||
isEnd = true;
|
||||
}
|
||||
ImGui::EndDisabled();
|
||||
|
||||
@@ -37,7 +37,8 @@ namespace anm2ed::imgui::wizard
|
||||
{
|
||||
return frameReference.animationIndex != reference.animationIndex ||
|
||||
frameReference.itemType != reference.itemType || frameReference.itemID != reference.itemID ||
|
||||
frameReference.groupType != reference.groupType || frameReference.groupId != reference.groupId;
|
||||
frameReference.groupType != reference.groupType ||
|
||||
frameReference.groupId != reference.groupId;
|
||||
});
|
||||
if (!frameReferences.empty())
|
||||
{
|
||||
@@ -283,8 +284,7 @@ namespace anm2ed::imgui::wizard
|
||||
{
|
||||
if (!ffmpeg_is_valid())
|
||||
{
|
||||
toasts.push(localize.get(TOAST_INVALID_FFMPEG));
|
||||
logger.error(localize.get(TOAST_INVALID_FFMPEG, anm2ed::ENGLISH));
|
||||
toast_log(Level::ERROR, TOAST_INVALID_FFMPEG);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -295,8 +295,7 @@ namespace anm2ed::imgui::wizard
|
||||
auto formatString = path::to_utf8(format);
|
||||
if (!formatString.contains("{}"))
|
||||
{
|
||||
toasts.push(localize.get(TOAST_PNG_FORMAT_INVALID));
|
||||
logger.error(localize.get(TOAST_PNG_FORMAT_INVALID, anm2ed::ENGLISH));
|
||||
toast_log(Level::ERROR, TOAST_PNG_FORMAT_INVALID);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -306,8 +305,7 @@ namespace anm2ed::imgui::wizard
|
||||
{
|
||||
if (path.empty())
|
||||
{
|
||||
toasts.push(localize.get(TOAST_RENDER_PATH_EMPTY));
|
||||
logger.error(localize.get(TOAST_RENDER_PATH_EMPTY, anm2ed::ENGLISH));
|
||||
toast_log(Level::ERROR, TOAST_RENDER_PATH_EMPTY);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -317,8 +315,7 @@ namespace anm2ed::imgui::wizard
|
||||
{
|
||||
if (!path::ensure_directory(path))
|
||||
{
|
||||
toasts.push(localize.get(TOAST_PNG_DIRECTORY_INVALID));
|
||||
logger.error(localize.get(TOAST_PNG_DIRECTORY_INVALID, anm2ed::ENGLISH));
|
||||
toast_log(Level::ERROR, TOAST_PNG_DIRECTORY_INVALID);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -328,8 +325,7 @@ namespace anm2ed::imgui::wizard
|
||||
{
|
||||
if (rows <= 0 && columns <= 0)
|
||||
{
|
||||
toasts.push(localize.get(TOAST_RENDER_PATH_EMPTY));
|
||||
logger.error(localize.get(TOAST_RENDER_PATH_EMPTY, anm2ed::ENGLISH));
|
||||
toast_log(Level::ERROR, TOAST_RENDER_PATH_EMPTY);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
+5
-14
@@ -112,10 +112,7 @@ namespace anm2ed
|
||||
if (!document.is_valid())
|
||||
{
|
||||
documents.pop_back();
|
||||
toasts.push(
|
||||
std::vformat(localize.get(TOAST_OPEN_DOCUMENT_FAILED), std::make_format_args(pathString, errorString)));
|
||||
logger.error(std::vformat(localize.get(TOAST_OPEN_DOCUMENT_FAILED, anm2ed::ENGLISH),
|
||||
std::make_format_args(pathString, errorString)));
|
||||
toast_log(Level::ERROR, TOAST_OPEN_DOCUMENT_FAILED, pathString, errorString);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -124,8 +121,7 @@ namespace anm2ed
|
||||
selected = (int)documents.size() - 1;
|
||||
pendingSelected = selected;
|
||||
selection_history_push(selected);
|
||||
toasts.push(std::vformat(localize.get(TOAST_OPEN_DOCUMENT), std::make_format_args(pathString)));
|
||||
logger.info(std::vformat(localize.get(TOAST_OPEN_DOCUMENT, anm2ed::ENGLISH), std::make_format_args(pathString)));
|
||||
toast_log(Level::INFO, TOAST_OPEN_DOCUMENT, pathString);
|
||||
|
||||
return &document;
|
||||
}
|
||||
@@ -142,8 +138,7 @@ namespace anm2ed
|
||||
savePath.replace_extension(".anm2");
|
||||
ensure_parent_directory_exists(savePath);
|
||||
|
||||
if (!document->save(savePath, &errorString, options))
|
||||
return false;
|
||||
if (!document->save(savePath, &errorString, options)) return false;
|
||||
|
||||
const auto autosavePath = document->autosave_path_get();
|
||||
autosaveFiles.erase(std::remove(autosaveFiles.begin(), autosaveFiles.end(), previousAutosavePath),
|
||||
@@ -160,17 +155,13 @@ namespace anm2ed
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Manager::save(const std::filesystem::path& path, Options options)
|
||||
{
|
||||
return save(selected, path, options);
|
||||
}
|
||||
bool Manager::save(const std::filesystem::path& path, Options options) { return save(selected, path, options); }
|
||||
|
||||
void Manager::autosave(Document& document, Options options)
|
||||
{
|
||||
std::string errorString{};
|
||||
auto autosavePath = document.autosave_path_get();
|
||||
if (!document.autosave(&errorString, options))
|
||||
return;
|
||||
if (!document.autosave(&errorString, options)) return;
|
||||
|
||||
autosaveFiles.erase(std::remove(autosaveFiles.begin(), autosaveFiles.end(), autosavePath), autosaveFiles.end());
|
||||
autosaveFiles.insert(autosaveFiles.begin(), autosavePath);
|
||||
|
||||
+1
-1
@@ -53,7 +53,7 @@ namespace anm2ed
|
||||
|
||||
std::vector<std::filesystem::path> anm2DragDropPaths{};
|
||||
bool isAnm2DragDrop{};
|
||||
int anm2DragDropMergePreset{Document::FILE_MERGE_PRESET_MERGE_BY_NAME};
|
||||
int anm2DragDropMergePreset{FILE_MERGE_PRESET_MERGE_BY_NAME};
|
||||
imgui::PopupHelper anm2DragDropPopup{
|
||||
imgui::PopupHelper(LABEL_MANAGER_ANM2_DRAG_DROP, imgui::POPUP_NORMAL, imgui::POPUP_BY_CURSOR)};
|
||||
imgui::PopupHelper anm2DragDropMergePopup{
|
||||
|
||||
+105
-25
@@ -1,10 +1,12 @@
|
||||
#include "shader.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <format>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
#include "anm2/anm2.hpp"
|
||||
#include "log.hpp"
|
||||
|
||||
namespace anm2ed::resource::shader
|
||||
@@ -26,15 +28,15 @@ namespace anm2ed::resource::shader
|
||||
X(UNIFORM_BINDING_MAIN_TEXTURE, "Texture") \
|
||||
X(UNIFORM_BINDING_MAIN_TEXTURE, "Sampler0") \
|
||||
X(UNIFORM_BINDING_MAIN_TEXTURE, "ColorMap") \
|
||||
X(UNIFORM_BINDING_TRANSFORM, UNIFORM_TRANSFORM) \
|
||||
X(UNIFORM_BINDING_TRANSFORM, UNIFORM_TRANSFORM) \
|
||||
X(UNIFORM_BINDING_TRANSFORM, "Transform") \
|
||||
X(UNIFORM_BINDING_TRANSFORM, "MVP") \
|
||||
X(UNIFORM_BINDING_TRANSFORM, "Matrix") \
|
||||
X(UNIFORM_BINDING_FRAME_TINT, UNIFORM_TINT) \
|
||||
X(UNIFORM_BINDING_FRAME_TINT, UNIFORM_TINT) \
|
||||
X(UNIFORM_BINDING_FRAME_TINT, "Tint") \
|
||||
X(UNIFORM_BINDING_FRAME_TINT, "Color") \
|
||||
X(UNIFORM_BINDING_FRAME_TINT, "Color0") \
|
||||
X(UNIFORM_BINDING_COLOR_OFFSET, UNIFORM_COLOR_OFFSET) \
|
||||
X(UNIFORM_BINDING_COLOR_OFFSET, UNIFORM_COLOR_OFFSET) \
|
||||
X(UNIFORM_BINDING_COLOR_OFFSET, "ColorOffset") \
|
||||
X(UNIFORM_BINDING_COLOR_OFFSET, "ColorOffset0") \
|
||||
X(UNIFORM_BINDING_TEXTURE_SIZE, "u_texture_size") \
|
||||
@@ -47,10 +49,7 @@ namespace anm2ed::resource::shader
|
||||
#undef X
|
||||
};
|
||||
|
||||
bool is_shader_identifier(char character)
|
||||
{
|
||||
return std::isalnum((unsigned char)character) || character == '_';
|
||||
}
|
||||
bool is_shader_identifier(char character) { return std::isalnum((unsigned char)character) || character == '_'; }
|
||||
|
||||
std::string shader_text_replace(std::string string, std::string_view from, std::string_view to)
|
||||
{
|
||||
@@ -234,9 +233,7 @@ namespace anm2ed::resource::shader
|
||||
|
||||
std::string_view uniform_value_type_label_get(UniformValueType type)
|
||||
{
|
||||
for (const auto& info : UNIFORM_VALUE_TYPE_INFOS)
|
||||
if (type == info.type) return info.label;
|
||||
return UNIFORM_VALUE_TYPE_INFOS[0].label;
|
||||
return UNIFORM_VALUE_TYPE_INFOS[type < UNIFORM_VALUE_COUNT ? type : UNIFORM_VALUE_UNKNOWN].label;
|
||||
}
|
||||
|
||||
bool is_uniform_value_editable(UniformValueType type)
|
||||
@@ -245,9 +242,100 @@ namespace anm2ed::resource::shader
|
||||
type == UNIFORM_VALUE_VEC3 || type == UNIFORM_VALUE_VEC4 || type == UNIFORM_VALUE_SAMPLER2D;
|
||||
}
|
||||
|
||||
bool is_uniform_value_vector(UniformValueType type)
|
||||
bool is_uniform_value_vector(UniformValueType type) { return UNIFORM_VALUE_TYPE_INFOS[type].componentCount > 0; }
|
||||
|
||||
bool is_component_binding_valid(UniformBinding binding)
|
||||
{
|
||||
return type == UNIFORM_VALUE_VEC2 || type == UNIFORM_VALUE_VEC3 || type == UNIFORM_VALUE_VEC4;
|
||||
return binding == UNIFORM_BINDING_MANUAL || binding == UNIFORM_BINDING_PLAYBACK_TIME;
|
||||
}
|
||||
|
||||
void uniform_configs_apply(const Element& shaderElement, std::vector<Uniform>& uniforms)
|
||||
{
|
||||
for (auto& uniform : uniforms)
|
||||
{
|
||||
auto config = shader_uniform_get(shaderElement, uniform.name);
|
||||
if (!config) continue;
|
||||
|
||||
if (auto binding = uniform_binding_get(config->binding);
|
||||
!config->binding.empty() && is_uniform_binding_valid(binding, uniform.valueType))
|
||||
uniform.binding = binding;
|
||||
if (!config->value.empty()) uniform_value_parse(uniform, config->value);
|
||||
for (int index = 0; index < (int)uniform.components.size(); ++index)
|
||||
{
|
||||
auto component = shader_uniform_component_get(*config, index);
|
||||
if (!component) continue;
|
||||
if (auto binding = uniform_binding_get(component->binding);
|
||||
!component->binding.empty() && is_component_binding_valid(binding))
|
||||
uniform.components[index].binding = binding;
|
||||
if (!component->value.empty()) std::stringstream{component->value} >> uniform.components[index].value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool uniform_configs_trim(Element& shaderElement, const std::vector<Uniform>& uniforms)
|
||||
{
|
||||
bool isChanged = std::erase_if(shaderElement.children,
|
||||
[&](const Element& child)
|
||||
{
|
||||
return child.type == ElementType::UNIFORM &&
|
||||
std::ranges::none_of(uniforms, [&](const Uniform& uniform)
|
||||
{ return uniform.name == child.name; });
|
||||
}) > 0;
|
||||
|
||||
for (auto& config : shaderElement.children)
|
||||
{
|
||||
if (config.type != ElementType::UNIFORM) continue;
|
||||
auto& uniform = *std::ranges::find(uniforms, config.name, &Uniform::name);
|
||||
|
||||
if (!config.binding.empty() && !is_uniform_binding_valid(uniform_binding_get(config.binding), uniform.valueType))
|
||||
{
|
||||
config.binding.clear();
|
||||
isChanged = true;
|
||||
}
|
||||
|
||||
auto componentCount = UNIFORM_VALUE_TYPE_INFOS[uniform.valueType].componentCount;
|
||||
auto isComponents = uniform_binding_get(config.binding) == UNIFORM_BINDING_COMPONENTS;
|
||||
isChanged |= std::erase_if(config.children,
|
||||
[&](const Element& component)
|
||||
{
|
||||
return component.type == ElementType::COMPONENT &&
|
||||
(!isComponents || component.index < 0 || component.index >= componentCount);
|
||||
}) > 0;
|
||||
|
||||
for (auto& component : config.children)
|
||||
{
|
||||
if (component.type != ElementType::COMPONENT || component.binding.empty() ||
|
||||
is_component_binding_valid(uniform_binding_get(component.binding)))
|
||||
continue;
|
||||
component.binding.clear();
|
||||
isChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
return isChanged;
|
||||
}
|
||||
|
||||
void uniform_config_save(Element& shaderElement, const Uniform& uniform)
|
||||
{
|
||||
auto config = shader_uniform_get(shaderElement, uniform.name);
|
||||
if (!config)
|
||||
{
|
||||
config = &shaderElement.children.emplace_back(element_make(ElementType::UNIFORM));
|
||||
config->name = uniform.name;
|
||||
}
|
||||
|
||||
config->binding = std::string(uniform_binding_value_get(uniform.binding));
|
||||
config->value = uniform_value_string_get(uniform);
|
||||
std::erase_if(config->children, [](const Element& child) { return child.type == ElementType::COMPONENT; });
|
||||
if (uniform.binding != UNIFORM_BINDING_COMPONENTS) return;
|
||||
|
||||
for (int index = 0; index < (int)uniform.components.size(); ++index)
|
||||
{
|
||||
auto& component = config->children.emplace_back(element_make(ElementType::COMPONENT));
|
||||
component.index = index;
|
||||
component.binding = std::string(uniform_binding_value_get(uniform.components[index].binding));
|
||||
component.value = std::format("{:.6g}", uniform.components[index].value);
|
||||
}
|
||||
}
|
||||
|
||||
bool is_uniform_binding_valid(UniformBinding binding, UniformValueType type)
|
||||
@@ -302,7 +390,8 @@ namespace anm2ed::resource::shader
|
||||
case UNIFORM_VALUE_SAMPLER2D:
|
||||
return std::format("{}", uniform.intValue);
|
||||
case UNIFORM_VALUE_VEC2:
|
||||
return std::format("{} {}", uniform_float_string_get(uniform.value.x), uniform_float_string_get(uniform.value.y));
|
||||
return std::format("{} {}", uniform_float_string_get(uniform.value.x),
|
||||
uniform_float_string_get(uniform.value.y));
|
||||
case UNIFORM_VALUE_VEC3:
|
||||
return std::format("{} {} {}", uniform_float_string_get(uniform.value.x),
|
||||
uniform_float_string_get(uniform.value.y), uniform_float_string_get(uniform.value.z));
|
||||
@@ -374,15 +463,9 @@ namespace anm2ed::resource::shader
|
||||
return "#version 330 core\n" + declarations + body;
|
||||
}
|
||||
|
||||
std::string gles_fragment_convert(std::string_view source)
|
||||
{
|
||||
return gles_convert(source, SHADER_STAGE_FRAGMENT);
|
||||
}
|
||||
std::string gles_fragment_convert(std::string_view source) { return gles_convert(source, SHADER_STAGE_FRAGMENT); }
|
||||
|
||||
std::string gles_vertex_convert(std::string_view source)
|
||||
{
|
||||
return gles_convert(source, SHADER_STAGE_VERTEX);
|
||||
}
|
||||
std::string gles_vertex_convert(std::string_view source) { return gles_convert(source, SHADER_STAGE_VERTEX); }
|
||||
}
|
||||
|
||||
namespace anm2ed::resource
|
||||
@@ -485,8 +568,5 @@ namespace anm2ed::resource
|
||||
if (is_valid()) glDeleteProgram(id);
|
||||
}
|
||||
|
||||
bool Shader::is_valid() const
|
||||
{
|
||||
return id != 0;
|
||||
}
|
||||
bool Shader::is_valid() const { return id != 0; }
|
||||
}
|
||||
+21
-12
@@ -8,6 +8,11 @@
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
struct Element;
|
||||
}
|
||||
|
||||
namespace anm2ed::resource::shader
|
||||
{
|
||||
struct Info
|
||||
@@ -257,14 +262,14 @@ namespace anm2ed::resource::shader
|
||||
};
|
||||
|
||||
#define RESOURCE_SHADER_UNIFORM_BINDINGS \
|
||||
X(IGNORE, "Ignore", "Ignore") \
|
||||
X(IGNORE, "Ignore", "Ignore") \
|
||||
X(MAIN_TEXTURE, "MainTexture", "Main Texture") \
|
||||
X(TRANSFORM, "Transform", "Transform") \
|
||||
X(FRAME_TINT, "FrameTint", "Frame Tint") \
|
||||
X(COLOR_OFFSET, "ColorOffset", "Color Offset") \
|
||||
X(TEXTURE_SIZE, "TextureSize", "Texture Size") \
|
||||
X(PLAYBACK_TIME, "PlaybackTime", "Playback Time") \
|
||||
X(COMPONENTS, "Components", "Per Component") \
|
||||
X(COMPONENTS, "Components", "Per Component") \
|
||||
X(MANUAL, "Manual", "Manual")
|
||||
|
||||
enum UniformBinding
|
||||
@@ -289,18 +294,18 @@ namespace anm2ed::resource::shader
|
||||
};
|
||||
|
||||
#define RESOURCE_SHADER_UNIFORM_VALUE_TYPES \
|
||||
X(UNKNOWN, "Unknown") \
|
||||
X(FLOAT, "Float") \
|
||||
X(INT, "Int") \
|
||||
X(VEC2, "Vec2") \
|
||||
X(VEC3, "Vec3") \
|
||||
X(VEC4, "Vec4") \
|
||||
X(MAT4, "Mat4") \
|
||||
X(SAMPLER2D, "Sampler2D")
|
||||
X(UNKNOWN, "Unknown", 0) \
|
||||
X(FLOAT, "Float", 0) \
|
||||
X(INT, "Int", 0) \
|
||||
X(VEC2, "Vec2", 2) \
|
||||
X(VEC3, "Vec3", 3) \
|
||||
X(VEC4, "Vec4", 4) \
|
||||
X(MAT4, "Mat4", 0) \
|
||||
X(SAMPLER2D, "Sampler2D", 0)
|
||||
|
||||
enum UniformValueType
|
||||
{
|
||||
#define X(symbol, label) UNIFORM_VALUE_##symbol,
|
||||
#define X(symbol, label, componentCount) UNIFORM_VALUE_##symbol,
|
||||
RESOURCE_SHADER_UNIFORM_VALUE_TYPES
|
||||
#undef X
|
||||
UNIFORM_VALUE_COUNT
|
||||
@@ -310,10 +315,11 @@ namespace anm2ed::resource::shader
|
||||
{
|
||||
UniformValueType type{};
|
||||
const char* label{};
|
||||
int componentCount{};
|
||||
};
|
||||
|
||||
inline constexpr UniformValueTypeInfo UNIFORM_VALUE_TYPE_INFOS[] = {
|
||||
#define X(symbol, label) {UNIFORM_VALUE_##symbol, label},
|
||||
#define X(symbol, label, componentCount) {UNIFORM_VALUE_##symbol, label, componentCount},
|
||||
RESOURCE_SHADER_UNIFORM_VALUE_TYPES
|
||||
#undef X
|
||||
};
|
||||
@@ -360,6 +366,9 @@ namespace anm2ed::resource::shader
|
||||
{GRID_VERTEX, GRID_FRAGMENT}};
|
||||
|
||||
UniformBinding uniform_binding_auto_get(std::string_view, UniformValueType);
|
||||
void uniform_configs_apply(const Element&, std::vector<Uniform>&);
|
||||
bool uniform_configs_trim(Element&, const std::vector<Uniform>&);
|
||||
void uniform_config_save(Element&, const Uniform&);
|
||||
UniformBinding uniform_binding_get(std::string_view);
|
||||
std::string_view uniform_binding_value_get(UniformBinding);
|
||||
std::string_view uniform_binding_label_get(UniformBinding);
|
||||
|
||||
+53
-139
@@ -131,20 +131,9 @@ namespace anm2ed::snapshots
|
||||
}
|
||||
}
|
||||
|
||||
Element* element_get(Element& root, const std::vector<int>& path)
|
||||
template <class E> ElementPointer<E> element_get(E& root, const std::vector<int>& path)
|
||||
{
|
||||
auto* element = &root;
|
||||
for (auto index : path)
|
||||
{
|
||||
if (index < 0 || index >= (int)element->children.size()) return nullptr;
|
||||
element = &element->children[index];
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
const Element* element_get(const Element& root, const std::vector<int>& path)
|
||||
{
|
||||
auto* element = &root;
|
||||
auto element = &root;
|
||||
for (auto index : path)
|
||||
{
|
||||
if (index < 0 || index >= (int)element->children.size()) return nullptr;
|
||||
@@ -158,46 +147,12 @@ namespace anm2ed::snapshots
|
||||
for (size_t i = 0; i < element.children.size(); ++i)
|
||||
{
|
||||
path.push_back((int)i);
|
||||
if (&element.children[i] == target) return true;
|
||||
if (element_path_get(element.children[i], target, path)) return true;
|
||||
if (&element.children[i] == target || element_path_get(element.children[i], target, path)) return true;
|
||||
path.pop_back();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::optional<std::vector<int>> frame_path_get(const Anm2& anm2, Reference frameReference)
|
||||
{
|
||||
auto frame = anm2.element_get(frameReference);
|
||||
if (!frame) return std::nullopt;
|
||||
|
||||
std::vector<int> path{};
|
||||
if (!element_path_get(anm2.root, frame, path)) return std::nullopt;
|
||||
return path;
|
||||
}
|
||||
|
||||
std::optional<std::vector<int>> track_path_get(const Anm2& anm2, Reference trackReference)
|
||||
{
|
||||
trackReference.frameIndex = -1;
|
||||
auto track = anm2.element_get(trackReference);
|
||||
if (!track) return std::nullopt;
|
||||
|
||||
std::vector<int> path{};
|
||||
if (!element_path_get(anm2.root, track, path)) return std::nullopt;
|
||||
return path;
|
||||
}
|
||||
|
||||
std::optional<std::vector<int>> region_path_get(const Anm2& anm2, int spritesheetId, int regionId)
|
||||
{
|
||||
auto spritesheet = anm2.element_get(ElementType::SPRITESHEET, spritesheetId);
|
||||
if (!spritesheet) return std::nullopt;
|
||||
auto region = child_id_get(*spritesheet, ElementType::REGION, regionId);
|
||||
if (!region) return std::nullopt;
|
||||
|
||||
std::vector<int> path{};
|
||||
if (!element_path_get(anm2.root, region, path)) return std::nullopt;
|
||||
return path;
|
||||
}
|
||||
|
||||
void step_state_seed(SnapshotStep& step, const Snapshot& snapshot)
|
||||
{
|
||||
#define X(type, name) step.name = SnapshotStepValue<type>{snapshot.name, {}};
|
||||
@@ -205,61 +160,27 @@ namespace anm2ed::snapshots
|
||||
#undef X
|
||||
}
|
||||
|
||||
void step_textures_seed(SnapshotStep& step, const Snapshot& snapshot)
|
||||
{
|
||||
step.textures = SnapshotStepValue<SnapshotTextureMap>{snapshot.textures, {}};
|
||||
}
|
||||
|
||||
SnapshotStep step_anm2_seed(const Snapshot& snapshot, const std::string& message)
|
||||
SnapshotStep step_anm2_make(const Snapshot& snapshot)
|
||||
{
|
||||
SnapshotStep step{};
|
||||
step.message = message;
|
||||
step.anm2.isValid = SnapshotStepValue<bool>{snapshot.anm2.isValid, {}};
|
||||
step.anm2.elements.push_back({{}, snapshot.anm2.root, {}});
|
||||
step_state_seed(step, snapshot);
|
||||
return step;
|
||||
}
|
||||
|
||||
void tracks_step_add(SnapshotStep& step, const Snapshot& snapshot, Reference trackReference)
|
||||
SnapshotStep step_elements_make(const Snapshot& snapshot, const std::vector<const Element*>& elements)
|
||||
{
|
||||
trackReference.frameIndex = -1;
|
||||
auto path = track_path_get(snapshot.anm2, trackReference);
|
||||
if (!path) return;
|
||||
for (auto& elementStep : step.anm2.elements)
|
||||
if (elementStep.path == *path) return;
|
||||
|
||||
auto track = snapshot.anm2.element_get(trackReference);
|
||||
if (!track) return;
|
||||
|
||||
step.anm2.elements.push_back({std::move(*path), *track, {}});
|
||||
}
|
||||
|
||||
void frames_step_add(SnapshotStep& step, const Snapshot& snapshot, Reference frameReference)
|
||||
{
|
||||
auto path = frame_path_get(snapshot.anm2, frameReference);
|
||||
if (!path) return;
|
||||
for (auto& elementStep : step.anm2.elements)
|
||||
if (elementStep.path == *path) return;
|
||||
|
||||
auto frame = snapshot.anm2.element_get(frameReference);
|
||||
if (!frame) return;
|
||||
|
||||
step.anm2.elements.push_back({std::move(*path), *frame, {}});
|
||||
}
|
||||
|
||||
void regions_step_add(SnapshotStep& step, const Snapshot& snapshot, int spritesheetId, int regionId)
|
||||
{
|
||||
auto path = region_path_get(snapshot.anm2, spritesheetId, regionId);
|
||||
if (!path) return;
|
||||
for (auto& elementStep : step.anm2.elements)
|
||||
if (elementStep.path == *path) return;
|
||||
|
||||
auto spritesheet = snapshot.anm2.element_get(ElementType::SPRITESHEET, spritesheetId);
|
||||
if (!spritesheet) return;
|
||||
auto region = child_id_get(*spritesheet, ElementType::REGION, regionId);
|
||||
if (!region) return;
|
||||
|
||||
step.anm2.elements.push_back({std::move(*path), *region, {}});
|
||||
SnapshotStep step{};
|
||||
for (auto element : elements)
|
||||
{
|
||||
std::vector<int> path{};
|
||||
if (!element || !element_path_get(snapshot.anm2.root, element, path)) continue;
|
||||
if (std::ranges::any_of(step.anm2.elements, [&](const SnapshotElementStep& other) { return other.path == path; }))
|
||||
continue;
|
||||
step.anm2.elements.push_back({std::move(path), *element, {}});
|
||||
}
|
||||
return step;
|
||||
}
|
||||
|
||||
SnapshotAnm2Step anm2_step_make(const Anm2& before, const Anm2& after)
|
||||
@@ -360,70 +281,63 @@ namespace anm2ed
|
||||
|
||||
int SnapshotStack::max_size_get() { return maxSize; }
|
||||
|
||||
Snapshot* Snapshots::get() { return ¤t; }
|
||||
|
||||
void Snapshots::anm2_push(const Snapshot& snapshot, const std::string& message)
|
||||
void Snapshots::step_push(const std::string& message, SnapshotStep step)
|
||||
{
|
||||
if (pendingStep) commit(snapshot);
|
||||
pendingStep = snapshots::step_anm2_seed(snapshot, message);
|
||||
if (pendingStep) commit();
|
||||
current.message = message;
|
||||
step.message = message;
|
||||
pendingStep = step.is_empty() ? std::nullopt : std::optional<SnapshotStep>(std::move(step));
|
||||
}
|
||||
|
||||
void Snapshots::tracks_push(const Snapshot& snapshot, const std::string& message,
|
||||
const std::set<Reference>& trackReferences)
|
||||
void Snapshots::anm2_push(const std::string& message) { step_push(message, snapshots::step_anm2_make(current)); }
|
||||
|
||||
void Snapshots::tracks_push(const std::string& message, const std::set<Reference>& trackReferences)
|
||||
{
|
||||
if (pendingStep) commit(snapshot);
|
||||
SnapshotStep step{};
|
||||
step.message = message;
|
||||
std::vector<const Element*> tracks{};
|
||||
for (auto trackReference : trackReferences)
|
||||
snapshots::tracks_step_add(step, snapshot, trackReference);
|
||||
snapshots::step_state_seed(step, snapshot);
|
||||
|
||||
pendingStep = step.is_empty() ? std::nullopt : std::optional<SnapshotStep>(std::move(step));
|
||||
{
|
||||
trackReference.frameIndex = -1;
|
||||
tracks.push_back(current.anm2.element_get(trackReference));
|
||||
}
|
||||
auto step = snapshots::step_elements_make(current, tracks);
|
||||
snapshots::step_state_seed(step, current);
|
||||
step_push(message, std::move(step));
|
||||
}
|
||||
|
||||
void Snapshots::frames_push(const Snapshot& snapshot, const std::string& message,
|
||||
const std::set<Reference>& frameReferences)
|
||||
void Snapshots::frames_push(const std::string& message, const std::set<Reference>& frameReferences)
|
||||
{
|
||||
if (pendingStep) commit(snapshot);
|
||||
SnapshotStep step{};
|
||||
step.message = message;
|
||||
std::vector<const Element*> frames{};
|
||||
for (auto frameReference : frameReferences)
|
||||
snapshots::frames_step_add(step, snapshot, frameReference);
|
||||
|
||||
pendingStep = step.is_empty() ? std::nullopt : std::optional<SnapshotStep>(std::move(step));
|
||||
frames.push_back(current.anm2.element_get(frameReference));
|
||||
step_push(message, snapshots::step_elements_make(current, frames));
|
||||
}
|
||||
|
||||
void Snapshots::regions_push(const Snapshot& snapshot, const std::string& message, int spritesheetId,
|
||||
const std::set<int>& regionIds)
|
||||
void Snapshots::regions_push(const std::string& message, int spritesheetId, const std::set<int>& regionIds)
|
||||
{
|
||||
std::vector<const Element*> regions{};
|
||||
if (auto spritesheet = current.anm2.element_get(ElementType::SPRITESHEET, spritesheetId))
|
||||
for (auto regionId : regionIds)
|
||||
regions.push_back(child_id_get(*spritesheet, ElementType::REGION, regionId));
|
||||
step_push(message, snapshots::step_elements_make(current, regions));
|
||||
}
|
||||
|
||||
void Snapshots::textures_push(const std::string& message)
|
||||
{
|
||||
if (pendingStep) commit(snapshot);
|
||||
SnapshotStep step{};
|
||||
step.message = message;
|
||||
for (auto regionId : regionIds)
|
||||
snapshots::regions_step_add(step, snapshot, spritesheetId, regionId);
|
||||
|
||||
pendingStep = step.is_empty() ? std::nullopt : std::optional<SnapshotStep>(std::move(step));
|
||||
step.textures = SnapshotStepValue<SnapshotTextureMap>{current.textures, {}};
|
||||
step_push(message, std::move(step));
|
||||
}
|
||||
|
||||
void Snapshots::textures_push(const Snapshot& snapshot, const std::string& message)
|
||||
void Snapshots::anm2_textures_push(const std::string& message)
|
||||
{
|
||||
if (pendingStep) commit(snapshot);
|
||||
SnapshotStep step{};
|
||||
step.message = message;
|
||||
snapshots::step_textures_seed(step, snapshot);
|
||||
pendingStep = std::move(step);
|
||||
auto step = snapshots::step_anm2_make(current);
|
||||
step.textures = SnapshotStepValue<SnapshotTextureMap>{current.textures, {}};
|
||||
step_push(message, std::move(step));
|
||||
}
|
||||
|
||||
void Snapshots::anm2_textures_push(const Snapshot& snapshot, const std::string& message)
|
||||
{
|
||||
if (pendingStep) commit(snapshot);
|
||||
auto step = snapshots::step_anm2_seed(snapshot, message);
|
||||
snapshots::step_textures_seed(step, snapshot);
|
||||
pendingStep = std::move(step);
|
||||
}
|
||||
|
||||
void Snapshots::commit(const Snapshot& snapshot)
|
||||
void Snapshots::commit()
|
||||
{
|
||||
const auto& snapshot = current;
|
||||
if (!pendingStep) return;
|
||||
|
||||
auto step = std::move(*pendingStep);
|
||||
|
||||
+8
-8
@@ -137,14 +137,14 @@ namespace anm2ed
|
||||
Snapshot current{};
|
||||
std::optional<SnapshotStep> pendingStep{};
|
||||
|
||||
Snapshot* get();
|
||||
void anm2_push(const Snapshot&, const std::string&);
|
||||
void tracks_push(const Snapshot&, const std::string&, const std::set<Reference>&);
|
||||
void frames_push(const Snapshot&, const std::string&, const std::set<Reference>&);
|
||||
void regions_push(const Snapshot&, const std::string&, int, const std::set<int>&);
|
||||
void textures_push(const Snapshot&, const std::string&);
|
||||
void anm2_textures_push(const Snapshot&, const std::string&);
|
||||
void commit(const Snapshot&);
|
||||
void anm2_push(const std::string&);
|
||||
void tracks_push(const std::string&, const std::set<Reference>&);
|
||||
void frames_push(const std::string&, const std::set<Reference>&);
|
||||
void regions_push(const std::string&, int, const std::set<int>&);
|
||||
void textures_push(const std::string&);
|
||||
void anm2_textures_push(const std::string&);
|
||||
void step_push(const std::string&, SnapshotStep);
|
||||
void commit();
|
||||
bool undo();
|
||||
bool redo();
|
||||
void reset();
|
||||
|
||||
+6
-8
@@ -107,27 +107,25 @@ namespace anm2ed
|
||||
if (!spritesheetDropPaths.empty())
|
||||
{
|
||||
auto paths = spritesheetDropPaths;
|
||||
manager.command_push({manager.selected, [paths](Manager&, Document& document)
|
||||
{ document.spritesheets_add(paths); }});
|
||||
manager.command_push(
|
||||
{manager.selected, [paths](Manager&, Document& document) { document.spritesheets_add(paths); }});
|
||||
}
|
||||
if (!soundDropPaths.empty())
|
||||
{
|
||||
auto paths = soundDropPaths;
|
||||
manager.command_push({manager.selected, [paths](Manager&, Document& document)
|
||||
{ document.sounds_add(paths); }});
|
||||
manager.command_push(
|
||||
{manager.selected, [paths](Manager&, Document& document) { document.sounds_add(paths); }});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!spritesheetDropPaths.empty())
|
||||
{
|
||||
toasts.push(localize.get(TOAST_ADD_SPRITESHEET_FAILED));
|
||||
logger.warning(localize.get(TOAST_ADD_SPRITESHEET_FAILED, anm2ed::ENGLISH));
|
||||
toast_log(Level::WARNING, TOAST_ADD_SPRITESHEET_FAILED);
|
||||
}
|
||||
if (!soundDropPaths.empty())
|
||||
{
|
||||
toasts.push(localize.get(TOAST_ADD_SOUND_FAILED));
|
||||
logger.warning(localize.get(TOAST_ADD_SOUND_FAILED, anm2ed::ENGLISH));
|
||||
toast_log(Level::WARNING, TOAST_ADD_SOUND_FAILED);
|
||||
}
|
||||
}
|
||||
spritesheetDropPaths.clear();
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
#include "pack.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
|
||||
namespace anm2ed::util::pack
|
||||
{
|
||||
constexpr int WIDTH_CANDIDATES_MAX = 512;
|
||||
|
||||
struct Rect
|
||||
{
|
||||
int x{};
|
||||
int y{};
|
||||
int w{};
|
||||
int h{};
|
||||
};
|
||||
|
||||
bool is_rect_intersecting(const Rect& a, const Rect& b)
|
||||
{
|
||||
return !(b.x >= a.x + a.w || b.x + b.w <= a.x || b.y >= a.y + a.h || b.y + b.h <= a.y);
|
||||
}
|
||||
|
||||
bool is_rect_contained(const Rect& a, const Rect& b)
|
||||
{
|
||||
return b.x >= a.x && b.y >= a.y && b.x + b.w <= a.x + a.w && b.y + b.h <= a.y + a.h;
|
||||
}
|
||||
|
||||
void free_rects_split(std::vector<Rect>& freeRects, const Rect& used)
|
||||
{
|
||||
std::vector<Rect> next{};
|
||||
next.reserve(freeRects.size() * 2);
|
||||
for (auto& free : freeRects)
|
||||
{
|
||||
if (!is_rect_intersecting(free, used))
|
||||
{
|
||||
next.push_back(free);
|
||||
continue;
|
||||
}
|
||||
if (used.x > free.x) next.push_back({free.x, free.y, used.x - free.x, free.h});
|
||||
if (used.x + used.w < free.x + free.w)
|
||||
next.push_back({used.x + used.w, free.y, free.x + free.w - (used.x + used.w), free.h});
|
||||
if (used.y > free.y) next.push_back({free.x, free.y, free.w, used.y - free.y});
|
||||
if (used.y + used.h < free.y + free.h)
|
||||
next.push_back({free.x, used.y + used.h, free.w, free.y + free.h - (used.y + used.h)});
|
||||
}
|
||||
freeRects = std::move(next);
|
||||
}
|
||||
|
||||
void free_rects_prune(std::vector<Rect>& freeRects)
|
||||
{
|
||||
for (int i = 0; i < (int)freeRects.size(); ++i)
|
||||
{
|
||||
if (freeRects[i].w <= 0 || freeRects[i].h <= 0)
|
||||
{
|
||||
freeRects.erase(freeRects.begin() + i--);
|
||||
continue;
|
||||
}
|
||||
for (int j = i + 1; j < (int)freeRects.size();)
|
||||
{
|
||||
if (is_rect_contained(freeRects[i], freeRects[j]))
|
||||
freeRects.erase(freeRects.begin() + j);
|
||||
else if (is_rect_contained(freeRects[j], freeRects[i]))
|
||||
{
|
||||
freeRects.erase(freeRects.begin() + i--);
|
||||
break;
|
||||
}
|
||||
else
|
||||
++j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool rect_insert(std::vector<Rect>& freeRects, glm::ivec2 size, Rect& result)
|
||||
{
|
||||
int bestShort = std::numeric_limits<int>::max();
|
||||
int bestLong = std::numeric_limits<int>::max();
|
||||
bool isFound{};
|
||||
for (auto& free : freeRects)
|
||||
{
|
||||
if (size.x > free.w || size.y > free.h) continue;
|
||||
int shortSide = std::min(free.w - size.x, free.h - size.y);
|
||||
int longSide = std::max(free.w - size.x, free.h - size.y);
|
||||
if (shortSide > bestShort || (shortSide == bestShort && longSide >= bestLong)) continue;
|
||||
bestShort = shortSide;
|
||||
bestLong = longSide;
|
||||
result = {free.x, free.y, size.x, size.y};
|
||||
isFound = true;
|
||||
}
|
||||
if (!isFound) return false;
|
||||
free_rects_split(freeRects, result);
|
||||
free_rects_prune(freeRects);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rects_pack(const std::vector<glm::ivec2>& sizes, glm::ivec2& packedSize, std::vector<glm::ivec2>& positions)
|
||||
{
|
||||
if (sizes.empty()) return false;
|
||||
|
||||
glm::ivec2 maxSize{};
|
||||
glm::ivec2 sumSize{};
|
||||
std::int64_t totalArea{};
|
||||
for (auto& size : sizes)
|
||||
{
|
||||
maxSize = glm::max(maxSize, size);
|
||||
sumSize += size;
|
||||
totalArea += (std::int64_t)size.x * size.y;
|
||||
}
|
||||
if (maxSize.x <= 0 || maxSize.y <= 0) return false;
|
||||
|
||||
int bestSquareDelta = std::numeric_limits<int>::max();
|
||||
int bestArea = std::numeric_limits<int>::max();
|
||||
int endWidth = std::max(maxSize.x, sumSize.x);
|
||||
int step = std::max(1, (endWidth - maxSize.x) / WIDTH_CANDIDATES_MAX);
|
||||
|
||||
for (int width = maxSize.x; width <= endWidth; width += step)
|
||||
{
|
||||
std::vector<glm::ivec2> candidate(sizes.size());
|
||||
glm::ivec2 used{};
|
||||
bool isValid{};
|
||||
for (int height = std::max(maxSize.y, (int)std::ceil((double)totalArea / width)); height <= sumSize.y && !isValid;
|
||||
++height)
|
||||
{
|
||||
std::vector<Rect> freeRects{{0, 0, width, height}};
|
||||
used = {};
|
||||
isValid = true;
|
||||
for (int i = 0; i < (int)sizes.size() && isValid; ++i)
|
||||
{
|
||||
Rect rect{};
|
||||
isValid = rect_insert(freeRects, sizes[i], rect);
|
||||
candidate[i] = {rect.x, rect.y};
|
||||
used = glm::max(used, glm::ivec2(rect.x + rect.w, rect.y + rect.h));
|
||||
}
|
||||
}
|
||||
if (!isValid) continue;
|
||||
|
||||
int area = used.x * used.y;
|
||||
int squareDelta = std::abs(used.x - used.y);
|
||||
if (squareDelta > bestSquareDelta || (squareDelta == bestSquareDelta && area >= bestArea)) continue;
|
||||
bestSquareDelta = squareDelta;
|
||||
bestArea = area;
|
||||
packedSize = used;
|
||||
positions = std::move(candidate);
|
||||
if (bestArea == totalArea && bestSquareDelta == 0) break;
|
||||
}
|
||||
|
||||
return bestArea != std::numeric_limits<int>::max();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace anm2ed::util::pack
|
||||
{
|
||||
bool rects_pack(const std::vector<glm::ivec2>&, glm::ivec2&, std::vector<glm::ivec2>&);
|
||||
}
|
||||
Reference in new issue
Block a user