Implement Undo/Redo #4
73
src/lib.rs
73
src/lib.rs
|
|
@ -36,4 +36,77 @@ impl CanvasElement {
|
|||
}
|
||||
}
|
||||
|
||||
use im::Vector;
|
||||
|
||||
// A canvas contains all elements to be drawn
|
||||
pub type Canvas = Vector<CanvasElement>;
|
||||
|
||||
#[derive(Clone, druid::Data)]
|
||||
pub struct VersionedCanvas {
|
||||
// We internally guarantee that this vector
|
||||
// is never empty
|
||||
versions: Vector<Canvas>,
|
||||
curr_version: usize,
|
||||
}
|
||||
|
||||
impl VersionedCanvas {
|
||||
pub fn new(canvas: Canvas) -> VersionedCanvas {
|
||||
VersionedCanvas {
|
||||
versions: im::vector![canvas],
|
||||
curr_version: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// Get current canvas version
|
||||
pub fn get(&self) -> &Canvas {
|
||||
let focus = self.versions.focus();
|
||||
self.versions.get(self.curr_version).unwrap()
|
||||
}
|
||||
|
||||
fn has_newer_versions(&self) -> bool {
|
||||
self.curr_version + 1 < self.versions.len()
|
||||
}
|
||||
|
||||
fn has_older_versions(&self) -> bool {
|
||||
self.curr_version > 0
|
||||
}
|
||||
|
||||
pub fn undo(&mut self) {
|
||||
if self.has_older_versions() {
|
||||
self.curr_version = self.curr_version - 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn redo(&mut self) {
|
||||
if self.has_newer_versions() {
|
||||
self.curr_version = self.curr_version + 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, update_fn: impl FnOnce(&Canvas) -> Canvas) {
|
||||
// This is a linear history,
|
||||
|
Franciman marked this conversation as resolved
|
||||
// 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
|
||||
// and creates a new future.
|
||||
if self.has_newer_versions() {
|
||||
self.versions = self.versions.take(self.curr_version + 1);
|
||||
}
|
||||
let new_version = update_fn(self.get());
|
||||
self.versions.push_back(new_version);
|
||||
self.curr_version = self.curr_version + 1;
|
||||
}
|
||||
|
||||
// Do inplace update, which will be irreversible
|
||||
pub fn irreversible_update(&mut self, update_fn: impl FnOnce(&mut Canvas)) {
|
||||
// This is a linear history,
|
||||
// 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
|
||||
// and creates a new future.
|
||||
if self.has_newer_versions() {
|
||||
self.versions = self.versions.take(self.curr_version + 1);
|
||||
}
|
||||
|
||||
update_fn(self.versions.back_mut().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
37
src/main.rs
37
src/main.rs
|
|
@ -14,23 +14,36 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use druid::im::{vector, Vector};
|
||||
use druid::im::{vector};
|
||||
use druid::kurbo::BezPath;
|
||||
use druid::widget::prelude::*;
|
||||
use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc};
|
||||
|
||||
use stiletto::CanvasElement;
|
||||
use stiletto::{CanvasElement, VersionedCanvas, Canvas};
|
||||
|
||||
#[derive(Clone, Data)]
|
||||
struct CanvasData {
|
||||
current_element: Option<CanvasElement>,
|
||||
elements: Vector<CanvasElement>,
|
||||
//elements: Vector<CanvasElement>,
|
||||
elements: VersionedCanvas,
|
||||
|
Franciman marked this conversation as resolved
Outdated
enrico
commented
We could give it a more descriptive name, maybe VersionedCanvas? We could give it a more descriptive name, maybe VersionedCanvas?
|
||||
}
|
||||
|
||||
impl CanvasData {
|
||||
fn is_drawing(&self) -> bool {
|
||||
self.current_element.is_some()
|
||||
}
|
||||
|
||||
fn perform_undo(&mut self) {
|
||||
if !self.is_drawing() {
|
||||
self.elements.undo();
|
||||
}
|
||||
}
|
||||
|
||||
fn perform_redo(&mut self) {
|
||||
if !self.is_drawing() {
|
||||
self.elements.redo();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CanvasWidget;
|
||||
|
|
@ -60,7 +73,13 @@ impl Widget<CanvasData> for CanvasWidget {
|
|||
Event::MouseUp(_) => {
|
||||
if data.is_drawing() {
|
||||
if let Some(current_element) = data.current_element.take() {
|
||||
data.elements.push_back(current_element);
|
||||
|
||||
data.elements.update(move |canvas: &Canvas| -> Canvas {
|
||||
let mut new_canvas = canvas.clone();
|
||||
new_canvas.push_back(current_element);
|
||||
return new_canvas;
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -124,7 +143,7 @@ impl Widget<CanvasData> for CanvasWidget {
|
|||
// and we only want to clear this widget's area.
|
||||
ctx.fill(rect, &Color::WHITE);
|
||||
|
||||
for element in &data.elements {
|
||||
for element in data.elements.get().iter() {
|
||||
element.draw(ctx);
|
||||
}
|
||||
if let Some(element) = &data.current_element {
|
||||
|
|
@ -138,8 +157,10 @@ fn build_ui() -> impl Widget<CanvasData> {
|
|||
let toolbar = Flex::row()
|
||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||
.with_spacer(30.0)
|
||||
.with_child(Button::new("Undo"))
|
||||
.with_child(Button::new("Redo"));
|
||||
.with_child(
|
||||
Button::new("Undo").on_click(|_ctx: &mut EventCtx, data: &mut CanvasData, _env: &Env| data.perform_undo())
|
||||
)
|
||||
.with_child(Button::new("Redo").on_click(|_ctx: &mut EventCtx, data: &mut CanvasData, _env: &Env| data.perform_redo()));
|
||||
|
||||
Flex::column()
|
||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||
|
|
@ -156,7 +177,7 @@ pub fn main() {
|
|||
);
|
||||
let canvas_data = CanvasData {
|
||||
current_element: None,
|
||||
elements: vector![],
|
||||
elements: VersionedCanvas::new(vector![]),
|
||||
};
|
||||
AppLauncher::with_window(window)
|
||||
.use_simple_logger()
|
||||
|
|
|
|||
Loading…
Reference in New Issue
I agree, I’ll leave out this consideration before pushing