diff --git a/src/anm2/anm2.cpp b/src/anm2/anm2.cpp index c78903c..a0ce075 100644 --- a/src/anm2/anm2.cpp +++ b/src/anm2/anm2.cpp @@ -262,7 +262,7 @@ namespace anm2ed out.interpolation = interpolation_read(element); out.origin = origin_read(element); if (out.type == ElementType::REGION && out.origin == Origin::TOP_LEFT) out.pivot = {}; - if (out.type == ElementType::REGION && out.origin == Origin::CENTER) out.pivot = glm::ivec2(out.size / 2.0f); + if (out.type == ElementType::REGION && out.origin == Origin::CENTER) out.pivot = out.size * 0.5f; } bool bake_attributes_read(const XMLElement* element, Interpolation& interpolation, int& bakeDelay) diff --git a/src/document.cpp b/src/document.cpp index 5b3919f..617f5aa 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -552,7 +552,7 @@ namespace anm2ed if (region->origin == Origin::TOP_LEFT) region->pivot = {}; else if (region->origin == Origin::CENTER) - region->pivot = {static_cast(region->size.x / 2.0f), static_cast(region->size.y / 2.0f)}; + region->pivot = region->size * 0.5f; else region->pivot -= region->crop - previousCrop; isChanged = true; diff --git a/src/imgui/documents.cpp b/src/imgui/documents.cpp index 1c23b74..44fa081 100644 --- a/src/imgui/documents.cpp +++ b/src/imgui/documents.cpp @@ -224,7 +224,12 @@ namespace anm2ed::imgui if (ImGui::Button(localize.get(BASIC_NO), widgetSize)) { auto index = closeDocumentIndex; - manager.command_push({.runManager = [index](Manager& manager) { manager.close(index); }}); + manager.command_push({.runManager = + [index](Manager& manager) + { + manager.autosave_file_clear(index); + manager.close(index); + }}); close(); } diff --git a/src/imgui/window/spritesheet_editor.cpp b/src/imgui/window/spritesheet_editor.cpp index 7178183..4f79bc7 100644 --- a/src/imgui/window/spritesheet_editor.cpp +++ b/src/imgui/window/spritesheet_editor.cpp @@ -725,8 +725,10 @@ namespace anm2ed::imgui if (isMouseDown) { auto frameCrop = ivec2(frame->crop); - frame_change_apply({.pivotX = (float)(int)(mousePos.x - frameCrop.x), - .pivotY = (float)(int)(mousePos.y - frameCrop.y), + auto pivot = mousePos - vec2(frameCrop); + if (isGridSnap) pivot = vec2(ivec2(pivot)); + frame_change_apply({.pivotX = pivot.x, + .pivotY = pivot.y, .cropX = (float)frameCrop.x, .cropY = (float)frameCrop.y}); } @@ -735,10 +737,6 @@ namespace anm2ed::imgui if (isUpPressed) frame_change_apply({.pivotY = step}, ChangeType::SUBTRACT); if (isDownPressed) frame_change_apply({.pivotY = step}, ChangeType::ADD); - frame_change_from_current_apply( - [](const Element& frame) - { return FrameChange{.pivotX = (float)(int)frame.pivot.x, .pivotY = (float)(int)frame.pivot.y}; }); - if (isDuring) { if (ImGui::BeginTooltip()) diff --git a/src/imgui/window/window.cpp b/src/imgui/window/window.cpp index 140d36e..438de5a 100644 --- a/src/imgui/window/window.cpp +++ b/src/imgui/window/window.cpp @@ -66,6 +66,19 @@ namespace anm2ed::imgui return container ? element_child_id_get(*container, window.elementType, key) : nullptr; } + int region_insert_index_get(const Element& spritesheet, const Storage& storage) + { + int index = (int)spritesheet.children.size(); + for (int i = 0; i < (int)spritesheet.children.size(); i++) + { + const auto& child = spritesheet.children[i]; + if (child.type != ElementType::REGION) continue; + if (storage.selection.contains(child.id) || (storage.selection.empty() && storage.reference == child.id)) + index = i + 1; + } + return index; + } + int window_track_id_get(const Element& item, ElementType trackType) { return trackType == ElementType::LAYER_ANIMATION ? item.layerId : item.nullId; @@ -1294,9 +1307,9 @@ namespace anm2ed::imgui if (!spritesheet || selection.empty()) return; std::string clipboardText{}; - for (auto& id : selection) - if (auto region = element_child_id_get(*spritesheet, ElementType::REGION, id)) - clipboardText += element_to_string(*region); + for (auto& region : spritesheet->children) + if (region.type == ElementType::REGION && selection.contains(region.id)) + clipboardText += element_to_string(region); clipboard.set(clipboardText); }; window.paste = [](Window& window, Manager&, Settings&, Document& document, Clipboard& clipboard) @@ -1309,6 +1322,7 @@ namespace anm2ed::imgui auto& selection = document.region.selection; auto& reference = document.region.reference; auto maxRegionIdBefore = element_child_max_id_get(*spritesheet, ElementType::REGION); + auto insertIndex = region_insert_index_get(*spritesheet, document.region); auto pasted = anm2; std::string errorString{}; if (pasted.regions_deserialize(spritesheetReference, clipboard.get(), true, &errorString)) @@ -1320,6 +1334,12 @@ namespace anm2ed::imgui auto maxRegionIdAfter = element_child_max_id_get(*pastedSpritesheet, ElementType::REGION); if (maxRegionIdAfter > maxRegionIdBefore) { + std::vector pastedIndices{}; + for (int i = 0; i < (int)pastedSpritesheet->children.size(); i++) + if (pastedSpritesheet->children[i].type == ElementType::REGION && + pastedSpritesheet->children[i].id > maxRegionIdBefore) + pastedIndices.push_back(i); + vector::move_indices_to_position(pastedSpritesheet->children, pastedIndices, insertIndex); window.newElementId = maxRegionIdAfter; selection = {maxRegionIdAfter}; reference = maxRegionIdAfter; @@ -1770,7 +1790,7 @@ namespace anm2ed::imgui if (region.origin == Origin::TOP_LEFT) region.pivot = {}; else if (region.origin == Origin::CENTER) - region.pivot = {(int)(region.size.x / 2.0f), (int)(region.size.y / 2.0f)}; + region.pivot = region.size * 0.5f; } ImGui::EndChild(); @@ -1802,7 +1822,8 @@ namespace anm2ed::imgui added.type = ElementType::REGION; added.tag = "Region"; added.id = id; - spritesheet->children.push_back(added); + auto insertIndex = region_insert_index_get(*spritesheet, document.region); + spritesheet->children.insert(spritesheet->children.begin() + insertIndex, added); selection = {id}; reference = id; window.newElementId = id; diff --git a/src/manager.cpp b/src/manager.cpp index 6b15776..959e6b7 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -178,6 +178,17 @@ namespace anm2ed autosave_files_write(); } + void Manager::autosave_file_clear(int index) + { + auto document = get(index); + if (!document) return; + + auto autosavePath = document->autosave_path_get(); + autosaveFiles.erase(std::remove(autosaveFiles.begin(), autosaveFiles.end(), autosavePath), autosaveFiles.end()); + autosave_file_remove(autosavePath); + autosave_files_write(); + } + void Manager::close(int index) { if (!vector::in_bounds(documents, index)) return; diff --git a/src/manager.hpp b/src/manager.hpp index 10a8c76..ea96795 100644 --- a/src/manager.hpp +++ b/src/manager.hpp @@ -87,6 +87,7 @@ namespace anm2ed bool save(int, const std::filesystem::path& = {}, Options = {}); bool save(const std::filesystem::path& = {}, Options = {}); void autosave(Document&, Options = {}); + void autosave_file_clear(int); void set(int); void close(int); void layer_properties_open(int = -1);