Implement Undo/Redo #4
18
src/lib.rs
18
src/lib.rs
|
|
@ -42,20 +42,24 @@ use im::Vector;
|
||||||
pub type Canvas = Vector<CanvasElement>;
|
pub type Canvas = Vector<CanvasElement>;
|
||||||
|
|
||||||
#[derive(Clone, druid::Data)]
|
#[derive(Clone, druid::Data)]
|
||||||
pub struct History {
|
pub struct VersionedCanvas {
|
||||||
|
// We internally guarantee that this vector
|
||||||
|
// is never empty
|
||||||
versions: Vector<Canvas>,
|
versions: Vector<Canvas>,
|
||||||
curr_version: usize,
|
curr_version: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl History {
|
impl VersionedCanvas {
|
||||||
pub fn new(canvas: Canvas) -> History {
|
pub fn new(canvas: Canvas) -> VersionedCanvas {
|
||||||
History {
|
VersionedCanvas {
|
||||||
versions: im::vector![canvas],
|
versions: im::vector![canvas],
|
||||||
curr_version: 0,
|
curr_version: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get current canvas version
|
||||||
pub fn get(&self) -> &Canvas {
|
pub fn get(&self) -> &Canvas {
|
||||||
|
let focus = self.versions.focus();
|
||||||
self.versions.get(self.curr_version).unwrap()
|
self.versions.get(self.curr_version).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -83,8 +87,7 @@ impl History {
|
||||||
// This is a linear history,
|
// This is a linear history,
|
||||||
|
Franciman marked this conversation as resolved
|
|||||||
// so we first check if there are newer versions, if so
|
// so we first check if there are newer versions, if so
|
||||||
// this means we are in the past, so a change in the past destroys the future
|
// this means we are in the past, so a change in the past destroys the future
|
||||||
// and creates a new future. If we want to keep all ramifications, we should use a tree,
|
// and creates a new future.
|
||||||
// but honestly, this sounds like overkill.
|
|
||||||
if self.has_newer_versions() {
|
if self.has_newer_versions() {
|
||||||
self.versions = self.versions.take(self.curr_version + 1);
|
self.versions = self.versions.take(self.curr_version + 1);
|
||||||
}
|
}
|
||||||
|
|
@ -98,8 +101,7 @@ impl History {
|
||||||
// This is a linear history,
|
// This is a linear history,
|
||||||
// so we first check if there are newer versions, if so
|
// so we first check if there are newer versions, if so
|
||||||
// this means we are in the past, so a change in the past destroys the future
|
// this means we are in the past, so a change in the past destroys the future
|
||||||
// and creates a new future. If we want to keep all ramifications, we should use a tree,
|
// and creates a new future.
|
||||||
// but honestly, this sounds like overkill.
|
|
||||||
if self.has_newer_versions() {
|
if self.has_newer_versions() {
|
||||||
self.versions = self.versions.take(self.curr_version + 1);
|
self.versions = self.versions.take(self.curr_version + 1);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
14
src/main.rs
14
src/main.rs
|
|
@ -19,13 +19,13 @@ use druid::kurbo::BezPath;
|
||||||
use druid::widget::prelude::*;
|
use druid::widget::prelude::*;
|
||||||
use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc};
|
use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc};
|
||||||
|
|
||||||
use stiletto::{CanvasElement, History, Canvas};
|
use stiletto::{CanvasElement, VersionedCanvas, Canvas};
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
struct CanvasData {
|
struct CanvasData {
|
||||||
current_element: Option<CanvasElement>,
|
current_element: Option<CanvasElement>,
|
||||||
//elements: Vector<CanvasElement>,
|
//elements: Vector<CanvasElement>,
|
||||||
history: History,
|
elements: VersionedCanvas,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CanvasData {
|
impl CanvasData {
|
||||||
|
|
@ -35,13 +35,13 @@ impl CanvasData {
|
||||||
|
|
||||||
fn perform_undo(&mut self) {
|
fn perform_undo(&mut self) {
|
||||||
if !self.is_drawing() {
|
if !self.is_drawing() {
|
||||||
self.history.undo();
|
self.elements.undo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn perform_redo(&mut self) {
|
fn perform_redo(&mut self) {
|
||||||
if !self.is_drawing() {
|
if !self.is_drawing() {
|
||||||
self.history.redo();
|
self.elements.redo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -74,7 +74,7 @@ impl Widget<CanvasData> for CanvasWidget {
|
||||||
if data.is_drawing() {
|
if data.is_drawing() {
|
||||||
if let Some(current_element) = data.current_element.take() {
|
if let Some(current_element) = data.current_element.take() {
|
||||||
|
|
||||||
data.history.update(move |canvas: &Canvas| -> Canvas {
|
data.elements.update(move |canvas: &Canvas| -> Canvas {
|
||||||
let mut new_canvas = canvas.clone();
|
let mut new_canvas = canvas.clone();
|
||||||
new_canvas.push_back(current_element);
|
new_canvas.push_back(current_element);
|
||||||
return new_canvas;
|
return new_canvas;
|
||||||
|
|
@ -143,7 +143,7 @@ impl Widget<CanvasData> for CanvasWidget {
|
||||||
// and we only want to clear this widget's area.
|
// and we only want to clear this widget's area.
|
||||||
ctx.fill(rect, &Color::WHITE);
|
ctx.fill(rect, &Color::WHITE);
|
||||||
|
|
||||||
for element in data.history.get().iter() {
|
for element in data.elements.get().iter() {
|
||||||
element.draw(ctx);
|
element.draw(ctx);
|
||||||
}
|
}
|
||||||
if let Some(element) = &data.current_element {
|
if let Some(element) = &data.current_element {
|
||||||
|
|
@ -177,7 +177,7 @@ pub fn main() {
|
||||||
);
|
);
|
||||||
let canvas_data = CanvasData {
|
let canvas_data = CanvasData {
|
||||||
current_element: None,
|
current_element: None,
|
||||||
history: History::new(vector![]),
|
elements: VersionedCanvas::new(vector![]),
|
||||||
};
|
};
|
||||||
AppLauncher::with_window(window)
|
AppLauncher::with_window(window)
|
||||||
.use_simple_logger()
|
.use_simple_logger()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
I agree, I’ll leave out this consideration before pushing