Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.3

0.11.3 / src / history.luc

## Bounded undo history of immutable geometry plus copied editor graph state.
## A snapshot keeps the component selection too: selections are immutable
## luce-geocore objects, so keeping one costs a reference.
from network import Network
from luce_geocore.geocore import Selection

pub class Snapshot:
    pub let graph: Network
    pub let selection: Selection?
    pub let mode: int

    pub func init(self, graph: Network, selection: Selection?, mode: int) -> unit!:
        self.graph = graph.copy()
        self.selection = selection
        self.mode = mode

pub class Change:
    pub let label: str
    pub let before: Snapshot
    pub let after: Snapshot

    pub func init(self, label: str, before: Snapshot, after: Snapshot):
        self.label = label
        self.before = before
        self.after = after

pub class History:
    pub var changes: list[Change]
    pub var cursor: int = 0

    pub func init(self):
        self.changes = []

    pub func push(self, label: str, before: Snapshot, after: Snapshot):
        while self.changes.length > self.cursor:
            _ = self.changes.pop()
        self.changes.append(Change(label, before, after))
        if self.changes.length > 64:
            _ = self.changes.remove_at(0)
        self.cursor = self.changes.length

    pub func can_undo(self) -> bool:
        return self.cursor > 0
    pub func can_redo(self) -> bool:
        return self.cursor < self.changes.length
    pub func undo_label(self) -> str:
        return "Undo " + self.changes[self.cursor - 1].label if self.can_undo() else "Undo"
    pub func redo_label(self) -> str:
        return "Redo " + self.changes[self.cursor].label if self.can_redo() else "Redo"