Files
anm2ed/src/snapshots.hpp
T
shweetandClaude Opus 5.5 26ab394fe4 Stage 1: headless Image and AudioData; GPU and playback caches in the UI
- resource::Image (pixels, size, uid) replaces Texture in documents, undo and
  wizards; it lives in anm2ed_core with no GL. uid changes with the pixels.
- resource::texture::id_get uploads lazily and evicts textures not drawn in a frame
- resource::AudioData (bytes, uid) replaces Audio; resource::audio::play/stop/
  track_detach/is_playing cache decoded audio and tracks per uid
- Resources::icon_id_get shortens the icon call sites
- tests/assets.cpp covers Image png round trip, uid tracking, merge_append

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 14:52:34 -04:00

154 lines
4.8 KiB
C++

#pragma once
#include <deque>
#include <map>
#include <optional>
#include <set>
#include <vector>
#include "anm2/anm2.hpp"
#include "audio_data.hpp"
#include "image.hpp"
#include "playback.hpp"
#include "storage.hpp"
namespace anm2ed::snapshots
{
constexpr auto ACTION = "Action";
constexpr auto MAX = 20;
};
namespace anm2ed
{
enum class SnapshotStepDirection
{
UNDO,
REDO
};
template <typename T> struct SnapshotStepValue
{
T undo{};
T redo{};
};
struct SnapshotElementStep
{
std::vector<int> path{};
Element undo{};
Element redo{};
};
struct SnapshotAnm2Step
{
std::optional<SnapshotStepValue<bool>> isValid{};
std::vector<SnapshotElementStep> elements{};
bool is_empty() const;
void apply(Anm2&, SnapshotStepDirection) const;
};
class Snapshot
{
public:
Playback playback{};
Storage animation{};
Storage event{};
Storage frames{};
Storage items{};
Storage layer{};
Storage merge{};
Storage null{};
Storage region{};
Storage shader{};
Storage sound{};
Storage spritesheet{};
std::map<int, resource::Image> textures{};
std::map<int, resource::AudioData> sounds{};
Anm2 anm2{};
Reference reference{};
std::set<Reference> groupReferences{};
float frameTime{};
std::string message = snapshots::ACTION;
};
using SnapshotTextureMap = std::map<int, resource::Image>;
using SnapshotSoundMap = std::map<int, resource::AudioData>;
#define SNAPSHOT_STEP_STATE_FIELDS \
X(Playback, playback) \
X(Storage, animation) \
X(Storage, event) \
X(Storage, frames) \
X(Storage, items) \
X(Storage, layer) \
X(Storage, merge) \
X(Storage, null) \
X(Storage, region) \
X(Storage, shader) \
X(Storage, sound) \
X(Storage, spritesheet) \
X(Reference, reference) \
X(std::set<Reference>, groupReferences) \
X(float, frameTime)
#define SNAPSHOT_STEP_RESOURCE_FIELDS \
X(SnapshotTextureMap, textures) \
X(SnapshotSoundMap, sounds)
struct SnapshotStep
{
std::string message = snapshots::ACTION;
SnapshotAnm2Step anm2{};
#define X(type, name) std::optional<SnapshotStepValue<type>> name{};
SNAPSHOT_STEP_STATE_FIELDS
SNAPSHOT_STEP_RESOURCE_FIELDS
#undef X
bool is_empty() const;
void apply(Snapshot&, SnapshotStepDirection) const;
};
class SnapshotStack
{
public:
SnapshotStack() = default;
bool is_empty();
void push(SnapshotStep);
std::optional<SnapshotStep> pop();
void clear();
void trim_to_limit();
static void max_size_set(int);
static int max_size_get();
private:
static int maxSize;
std::deque<SnapshotStep> stack;
};
class Snapshots
{
public:
SnapshotStack undoStack{};
SnapshotStack redoStack{};
Snapshot current{};
std::optional<SnapshotStep> pendingStep{};
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();
void apply_limit();
};
}