-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Allows for undo/redo functionality - Less mutations on our runtime state - Middleware functionality via messages
- Loading branch information
Showing
16 changed files
with
500 additions
and
386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
pub mod machine; | ||
pub mod tool; | ||
|
||
pub mod primitives; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#[macro_export] | ||
macro_rules! icon { | ||
($name:tt) => {{ | ||
let image = image::load_from_memory(include_bytes!(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/icons/", | ||
$name, | ||
".png" | ||
))) | ||
.expect("Failed to load image"); | ||
let size = [image.width() as _, image.height() as _]; | ||
let image_buffer = image.to_rgba8(); | ||
let pixels = image_buffer.as_flat_samples(); | ||
egui::ColorImage::from_rgba_unmultiplied(size, pixels.as_slice()) | ||
}}; | ||
} | ||
|
||
pub struct Icons { | ||
pub group: egui::TextureHandle, | ||
pub object: egui::TextureHandle, | ||
pub toolpath: egui::TextureHandle, | ||
} | ||
|
||
impl Icons { | ||
pub fn load(ctx: &egui::Context) -> Self { | ||
let group = ctx.load_texture("group", icon!("group"), Default::default()); | ||
let object = ctx.load_texture("object", icon!("object"), Default::default()); | ||
let toolpath = ctx.load_texture("toolpath", icon!("toolpath"), Default::default()); | ||
|
||
Self { | ||
group, | ||
object, | ||
toolpath, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::collections::HashSet; | ||
|
||
use crate::core::primitives::Mesh; | ||
|
||
use super::{object::Object, tool::Tool}; | ||
|
||
/// An action is a message in the application log, the ActionTree. | ||
/// Actions are re- and undoable. | ||
pub enum Message { | ||
Tool(Tool), | ||
Selection(HashSet<u32>), | ||
Object { id: u32, object: Option<Object> }, | ||
Mesh { id: u32, mesh: Mesh }, | ||
None, | ||
} | ||
|
||
impl Default for Message { | ||
fn default() -> Self { | ||
Self::None | ||
} | ||
} | ||
|
||
pub const UNDO_CAPACITY: usize = 20; | ||
|
||
/// The stack allocated application log. It stores the last n actions that occured for undoing | ||
/// and redoing, where n is the number stored in the UNDO_CAPACITY constant. | ||
pub struct Log { | ||
pub actions: Vec<Message>, | ||
pub cursor: usize, | ||
before: usize, | ||
after: usize, | ||
} | ||
|
||
impl Log { | ||
pub fn new(capacity: usize) -> Self { | ||
assert!(capacity > 0); | ||
|
||
let mut actions = Vec::with_capacity(capacity); | ||
for _ in 0..actions.capacity() { | ||
actions.push(Message::None); | ||
} | ||
|
||
Self { | ||
actions, | ||
cursor: 0, | ||
before: 0, | ||
after: 0, | ||
} | ||
} | ||
|
||
/// Pushes an action. This returns the action that was overwritten (if any). | ||
pub fn push(&mut self, mut message: Message) -> Option<Message> { | ||
if matches!(message, Message::None) { | ||
return None; | ||
} | ||
|
||
self.cursor = (self.cursor + 1) % self.actions.len(); | ||
self.after = 0; | ||
|
||
if self.before == self.actions.len() { | ||
std::mem::swap(&mut message, &mut self.actions[self.cursor]); | ||
Some(message) | ||
} else { | ||
self.before += 1; | ||
self.actions[self.cursor] = message; | ||
None | ||
} | ||
} | ||
|
||
pub fn can_undo(&self) -> bool { | ||
self.before > 0 | ||
} | ||
|
||
/// Retracts the cursor | ||
pub fn undo(&mut self) { | ||
self.cursor = self.cursor.checked_sub(1).unwrap_or(self.actions.len() - 1); | ||
self.after = (self.after + 1).min(self.actions.len()); | ||
self.before -= 1; | ||
} | ||
|
||
pub fn can_redo(&self) -> bool { | ||
self.after > 0 | ||
} | ||
|
||
/// Advances the cursor | ||
pub fn redo(&mut self) { | ||
self.cursor = (self.cursor + 1) % self.actions.len(); | ||
self.before = (self.before + 1).min(self.actions.len()); | ||
self.after -= 1; | ||
} | ||
} | ||
|
||
impl Default for Log { | ||
fn default() -> Self { | ||
Self::new(UNDO_CAPACITY) | ||
} | ||
} |
Oops, something went wrong.