Compare commits

...

3 Commits

Author SHA1 Message Date
enrico 9943681465 Merge pull request 'Implement Undo/Redo' (#4) from undo_redo into master
Reviewed-on: https://git.openglass.it/enrico/stiletto/pulls/4
2020-11-09 10:49:20 +01:00
Francesco Magliocca eca25f765b Rename History to VersionedCanvas and remove useless comments 2020-11-09 10:46:50 +01:00
Francesco Magliocca 6c7df141ea Implement Undo/Redo 2020-11-09 00:15:59 +01:00
2 changed files with 102 additions and 8 deletions

View File

@ -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,
// 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());
}
}

View File

@ -14,23 +14,36 @@
// You should have received a copy of the GNU Affero General Public License // 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/>. // 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::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; 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>,
elements: VersionedCanvas,
} }
impl CanvasData { impl CanvasData {
fn is_drawing(&self) -> bool { fn is_drawing(&self) -> bool {
self.current_element.is_some() 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; struct CanvasWidget;
@ -60,7 +73,13 @@ impl Widget<CanvasData> for CanvasWidget {
Event::MouseUp(_) => { Event::MouseUp(_) => {
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.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. // and we only want to clear this widget's area.
ctx.fill(rect, &Color::WHITE); ctx.fill(rect, &Color::WHITE);
for element in &data.elements { 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 {
@ -138,8 +157,10 @@ fn build_ui() -> impl Widget<CanvasData> {
let toolbar = Flex::row() let toolbar = Flex::row()
.cross_axis_alignment(CrossAxisAlignment::Center) .cross_axis_alignment(CrossAxisAlignment::Center)
.with_spacer(30.0) .with_spacer(30.0)
.with_child(Button::new("Undo")) .with_child(
.with_child(Button::new("Redo")); 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() Flex::column()
.cross_axis_alignment(CrossAxisAlignment::Center) .cross_axis_alignment(CrossAxisAlignment::Center)
@ -156,7 +177,7 @@ pub fn main() {
); );
let canvas_data = CanvasData { let canvas_data = CanvasData {
current_element: None, current_element: None,
elements: vector![], elements: VersionedCanvas::new(vector![]),
}; };
AppLauncher::with_window(window) AppLauncher::with_window(window)
.use_simple_logger() .use_simple_logger()