use super::UndoAction;
use crate::document::Document;
use crate::layer::LayerId;
use std::collections::{HashMap, HashSet};
pub struct FilterAddAction {
filter_id: LayerId,
host_id: LayerId,
}
impl FilterAddAction {
pub fn new(filter_id: LayerId, host_id: LayerId) -> Self {
FilterAddAction { filter_id, host_id }
}
}
impl UndoAction for FilterAddAction {
fn undo(&mut self, doc: &mut Document) -> HashMap<LayerId, HashSet<(i32, i32)>> {
doc.detach_filter_for_undo(self.filter_id);
HashMap::new()
}
fn redo(&mut self, doc: &mut Document) -> HashMap<LayerId, HashSet<(i32, i32)>> {
doc.reinsert_filter(self.filter_id, self.host_id);
HashMap::new()
}
}
pub struct FilterRemoveAction {
filter_id: LayerId,
host_id: LayerId,
}
impl FilterRemoveAction {
pub fn new(filter_id: LayerId, host_id: LayerId) -> Self {
FilterRemoveAction { filter_id, host_id }
}
}
impl UndoAction for FilterRemoveAction {
fn undo(&mut self, doc: &mut Document) -> HashMap<LayerId, HashSet<(i32, i32)>> {
doc.reinsert_filter(self.filter_id, self.host_id);
HashMap::new()
}
fn redo(&mut self, doc: &mut Document) -> HashMap<LayerId, HashSet<(i32, i32)>> {
doc.detach_filter_for_undo(self.filter_id);
HashMap::new()
}
}
pub struct NodeVisibleAction {
node_id: LayerId,
saved: bool,
}
impl NodeVisibleAction {
pub fn new(node_id: LayerId, saved: bool) -> Self {
NodeVisibleAction { node_id, saved }
}
fn swap(&mut self, doc: &mut Document) {
if let Some(node) = doc.find_node_mut(self.node_id) {
std::mem::swap(&mut node.common_mut().visible, &mut self.saved);
} else if let Some(filter) = doc.find_filter_mut(self.node_id) {
std::mem::swap(&mut filter.common.visible, &mut self.saved);
}
}
}
impl UndoAction for NodeVisibleAction {
fn undo(&mut self, doc: &mut Document) -> HashMap<LayerId, HashSet<(i32, i32)>> {
self.swap(doc);
HashMap::new()
}
fn redo(&mut self, doc: &mut Document) -> HashMap<LayerId, HashSet<(i32, i32)>> {
self.swap(doc);
HashMap::new()
}
}
pub struct NodeLockedAction {
node_id: LayerId,
saved: bool,
}
impl NodeLockedAction {
pub fn new(node_id: LayerId, saved: bool) -> Self {
NodeLockedAction { node_id, saved }
}
fn swap(&mut self, doc: &mut Document) {
if let Some(node) = doc.find_node_mut(self.node_id) {
std::mem::swap(&mut node.common_mut().locked, &mut self.saved);
} else if let Some(filter) = doc.find_filter_mut(self.node_id) {
std::mem::swap(&mut filter.common.locked, &mut self.saved);
}
}
}
impl UndoAction for NodeLockedAction {
fn undo(&mut self, doc: &mut Document) -> HashMap<LayerId, HashSet<(i32, i32)>> {
self.swap(doc);
HashMap::new()
}
fn redo(&mut self, doc: &mut Document) -> HashMap<LayerId, HashSet<(i32, i32)>> {
self.swap(doc);
HashMap::new()
}
}