some bullshit to fix potential crash

This commit is contained in:
2026-05-14 22:58:32 -04:00
parent 8f72f0aef5
commit b7b49eb81e
6 changed files with 89 additions and 12 deletions
+62 -2
View File
@@ -1,5 +1,6 @@
#include "document.hpp"
#include <algorithm>
#include <new>
#include <utility>
@@ -26,6 +27,63 @@ namespace anm2ed
return it->second;
return 0;
}
void restored_snapshot_sanitize(Document& document)
{
auto& reference = document.reference;
auto& documentAnm2 = document.anm2;
auto& selection = document.frames.selection;
auto animationCount = (int)documentAnm2.animations.items.size();
if (animationCount <= 0)
{
reference = {};
selection.clear();
document.frameTime = 0.0f;
return;
}
if (reference.animationIndex < 0 || reference.animationIndex >= animationCount)
reference.animationIndex = std::clamp(reference.animationIndex, 0, animationCount - 1);
auto item = documentAnm2.item_get(reference.animationIndex, reference.itemType, reference.itemID);
if (!item)
{
reference.itemType = ROOT;
reference.itemID = -1;
item = documentAnm2.item_get(reference.animationIndex, reference.itemType, reference.itemID);
}
if (!item)
{
reference.frameIndex = -1;
selection.clear();
document.frameTime = 0.0f;
return;
}
auto frameCount = (int)item->frames.size();
for (auto it = selection.begin(); it != selection.end();)
{
if (*it < 0 || *it >= frameCount)
it = selection.erase(it);
else
++it;
}
if (frameCount <= 0)
{
reference.frameIndex = -1;
document.frameTime = 0.0f;
return;
}
if (reference.frameIndex < 0 || reference.frameIndex >= frameCount)
reference.frameIndex = selection.empty() ? std::clamp(reference.frameIndex, 0, frameCount - 1)
: *selection.begin();
document.frameTime = item->frame_time_from_index_get(reference.frameIndex);
}
}
Document::Document(Anm2& anm2, const std::filesystem::path& path)
@@ -390,7 +448,8 @@ namespace anm2ed
void Document::undo()
{
snapshots.undo();
if (!snapshots.undo()) return;
restored_snapshot_sanitize(*this);
toasts.push(std::vformat(localize.get(TOAST_UNDO), std::make_format_args(message)));
logger.info(std::vformat(localize.get(TOAST_UNDO, anm2ed::ENGLISH), std::make_format_args(message)));
change(Document::ALL);
@@ -398,7 +457,8 @@ namespace anm2ed
void Document::redo()
{
snapshots.redo();
if (!snapshots.redo()) return;
restored_snapshot_sanitize(*this);
toasts.push(std::vformat(localize.get(TOAST_REDO), std::make_format_args(message)));
logger.info(std::vformat(localize.get(TOAST_REDO, anm2ed::ENGLISH), std::make_format_args(message)));
change(Document::ALL);
+18 -4
View File
@@ -1132,10 +1132,24 @@ namespace anm2ed::imgui
}
}
if (isLeftPressed) frame_change_apply({.scaleX = step}, anm2::SUBTRACT);
if (isRightPressed) frame_change_apply({.scaleX = step}, anm2::ADD);
if (isUpPressed) frame_change_apply({.scaleY = step}, anm2::SUBTRACT);
if (isDownPressed) frame_change_apply({.scaleY = step}, anm2::ADD);
if (isSelectedNullRect)
{
if (isLeftPressed)
frame_change_apply({.positionX = step * 0.5f, .scaleX = step}, anm2::SUBTRACT);
if (isRightPressed)
frame_change_apply({.positionX = step * 0.5f, .scaleX = step}, anm2::ADD);
if (isUpPressed)
frame_change_apply({.positionY = step * 0.5f, .scaleY = step}, anm2::SUBTRACT);
if (isDownPressed)
frame_change_apply({.positionY = step * 0.5f, .scaleY = step}, anm2::ADD);
}
else
{
if (isLeftPressed) frame_change_apply({.scaleX = step}, anm2::SUBTRACT);
if (isRightPressed) frame_change_apply({.scaleX = step}, anm2::ADD);
if (isUpPressed) frame_change_apply({.scaleY = step}, anm2::SUBTRACT);
if (isDownPressed) frame_change_apply({.scaleY = step}, anm2::ADD);
}
if (isToolDuring)
{
-1
View File
@@ -491,7 +491,6 @@ namespace anm2ed::imgui
if (!animation) return;
if (auto item = animation->item_get(reference.itemType, reference.itemID))
{
document.snapshot(localize.get(EDIT_PASTE_FRAMES));
std::set<int> indices{};
std::string errorString{};
int insertIndex = (int)item->frames.size();
+6 -2
View File
@@ -53,22 +53,26 @@ namespace anm2ed
redoStack.clear();
}
void Snapshots::undo()
bool Snapshots::undo()
{
if (auto snapshot = undoStack.pop())
{
redoStack.push(current);
current = *snapshot;
return true;
}
return false;
}
void Snapshots::redo()
bool Snapshots::redo()
{
if (auto snapshot = redoStack.pop())
{
undoStack.push(current);
current = *snapshot;
return true;
}
return false;
}
void Snapshots::reset()
+2 -2
View File
@@ -63,8 +63,8 @@ namespace anm2ed
Snapshot* get();
void push(const Snapshot&);
void undo();
void redo();
bool undo();
bool redo();
void reset();
void apply_limit();
};