Skip to content

Commit

Permalink
Rename undo_buffer to revision tracker
Browse files Browse the repository at this point in the history
Signed-off-by: Arjo Chakravarty <arjoc@google.com>
  • Loading branch information
arjo129 committed Jan 23, 2025
1 parent 67d591b commit f8311b2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
12 changes: 6 additions & 6 deletions rmf_site_editor/src/interaction/gizmo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::collections::HashMap;

use crate::{
interaction::*,
site::{UndoBuffer, UndoEvent},
site::{RevisionTracker, UndoEvent},
};
use bevy::{math::Affine3A, prelude::*, window::PrimaryWindow};
use bevy_mod_raycast::{deferred::RaycastMesh, deferred::RaycastSource, primitives::rays::Ray3d};
Expand Down Expand Up @@ -391,14 +391,14 @@ pub fn update_gizmo_click_start(

#[derive(Debug)]
pub struct GizmoMoveChange {
entity: Entity,
prev_pos: Transform,
dest_pos: Transform,
pub entity: Entity,
pub prev_pos: Transform,
pub dest_pos: Transform,
}

#[derive(Resource, Default)]
pub struct GizmoMoveUndoBuffer {
revisions: HashMap<usize, GizmoMoveChange>,
pub revisions: HashMap<usize, GizmoMoveChange>,
}

pub(crate) fn undo_gizmo_change(
Expand Down Expand Up @@ -429,7 +429,7 @@ pub fn update_gizmo_release(
mut gizmo_state: ResMut<GizmoState>,
mouse_button_input: Res<Input<MouseButton>>,
mut picked: ResMut<Picked>,
mut version_tracker: ResMut<UndoBuffer>,
mut version_tracker: ResMut<RevisionTracker>,
mut change_history: ResMut<GizmoMoveUndoBuffer>,
last_dragged: Res<LastDraggedPos>,
) {
Expand Down
4 changes: 2 additions & 2 deletions rmf_site_editor/src/site/change_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::site::SiteUpdateSet;
use bevy::prelude::*;
use std::{fmt::Debug, sync::Arc};

use super::{UndoBuffer, UndoEvent};
use super::{RevisionTracker, UndoEvent};

/// The Change component is used as an event to indicate that the value of a
/// component should change for some entity. Using these events instead of
Expand Down Expand Up @@ -121,7 +121,7 @@ fn update_changed_values<T: Component + Clone + Debug>(
mut commands: Commands,
mut values: Query<&mut T>,
mut changes: EventReader<Change<T>>,
mut undo_buffer: ResMut<UndoBuffer>,
mut undo_buffer: ResMut<RevisionTracker>,
mut change_history: ResMut<ChangeHistory<T>>,
) {
for change in changes.read() {
Expand Down
27 changes: 20 additions & 7 deletions rmf_site_editor/src/site/undo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ use std::fmt::Debug;

use crate::{EditMenu, MenuEvent, MenuItem};

/// This represents an undo event. If you want to implement some form of undo-able
/// action, your plugin should watch for this event. Specifically, we should be
#[derive(Event, Debug, Clone, Copy)]
pub struct UndoEvent {
/// The action id that should be reverted
pub action_id: usize,
}

/// This struct represents the undo menu item.
#[derive(Resource)]
struct UndoMenu {
menu_entity: Entity,
}

/// This is responsible for drawing the undo GUI menu
///TODO(arjo): Decouple
impl FromWorld for UndoMenu {
fn from_world(world: &mut World) -> Self {
Expand All @@ -25,10 +31,11 @@ impl FromWorld for UndoMenu {
}
}

/// This item watches the GUI menu to detect if an undo action was clicked.
fn watch_undo_click(
mut reader: EventReader<MenuEvent>,
menu_handle: Res<UndoMenu>,
mut undo_buffer: ResMut<UndoBuffer>,
mut undo_buffer: ResMut<RevisionTracker>,
mut event_writer: EventWriter<UndoEvent>,
) {
for menu_click in reader.read() {
Expand All @@ -43,20 +50,26 @@ fn watch_undo_click(
}
}

/// The RevisionTracker resource is used to manage the undo
/// stack. When a plugin wishes to commit an action it should request anew revision
/// from the RevisionTracker. It is up to the plugin to maintain its
/// own history of what state changes ocurred.
#[derive(Resource, Default)]
pub struct UndoBuffer {
pub prev_value: usize,
pub undo_stack: Vec<usize>,
pub struct RevisionTracker {
pub(crate) prev_value: usize,
pub(crate) undo_stack: Vec<usize>,
}

impl UndoBuffer {
impl RevisionTracker {
/// Request a new revision id. This is associated with the action you sent
pub fn get_next_revision(&mut self) -> usize {
self.prev_value += 1;
self.undo_stack.push(self.prev_value);
self.prev_value
}

pub fn undo_last(&mut self) -> Option<usize> {
/// Get the last item that was supposed to be undone.
pub(crate) fn undo_last(&mut self) -> Option<usize> {
self.undo_stack.pop()
}
}
Expand All @@ -68,7 +81,7 @@ impl Plugin for UndoPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<EditMenu>()
.init_resource::<UndoMenu>()
.init_resource::<UndoBuffer>()
.init_resource::<RevisionTracker>()
.add_event::<UndoEvent>()
.add_systems(Update, (watch_undo_click));
}
Expand Down

0 comments on commit f8311b2

Please sign in to comment.