Skip to content

Commit

Permalink
fix: collapsing tree view when object changes
Browse files Browse the repository at this point in the history
also passed around design more instead of it's variables
  • Loading branch information
GwnDaan committed Dec 16, 2024
1 parent 6abb94e commit 6576caf
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 418 deletions.
24 changes: 23 additions & 1 deletion src/editor_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::cell::RefCell;

use ag_iso_stack::object_pool::{NullableObjectId, ObjectPool};
use ag_iso_stack::object_pool::{NullableObjectId, ObjectId, ObjectPool};

const MAX_UNDO_REDO_POOL: usize = 10;
const MAX_UNDO_REDO_SELECTED: usize = 20;
Expand All @@ -21,6 +21,10 @@ pub struct EditorProject {
redo_selected_history: Vec<NullableObjectId>,
pub mask_size: u16,
soft_key_size: (u16, u16),

/// The modification of a object id in the pool that happened last frame
/// (old_id, new_id)
object_id_change_last_frame: RefCell<Option<(ObjectId, ObjectId)>>,
}

impl From<ObjectPool> for EditorProject {
Expand All @@ -37,6 +41,7 @@ impl From<ObjectPool> for EditorProject {
redo_selected_history: Default::default(),
mask_size,
soft_key_size,
object_id_change_last_frame: RefCell::new(None),
}
}
}
Expand Down Expand Up @@ -151,4 +156,21 @@ impl EditorProject {
self.mut_selected_object.replace(selected);
}
}

/// Get the object id change that happened last frame
/// (old_id, new_id)
pub fn get_object_id_change(&self) -> Option<(ObjectId, ObjectId)> {
self.object_id_change_last_frame.borrow().to_owned()
}

/// Set the current object id change
pub fn set_object_id_change(&self, old_id: ObjectId, new_id: ObjectId) {
*self.object_id_change_last_frame.borrow_mut() = Some((old_id, new_id));
}

/// Clear the object id change
/// This should be called just before the next configuration possibility
pub fn clear_object_id_change(&self) {
*self.object_id_change_last_frame.borrow_mut() = None;
}
}
53 changes: 35 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,26 +221,40 @@ fn render_object_hierarchy(
render_selectable_object(ui, object, project);
});
} else {
let mut default_open = false;

// The id of this object might have changed, which would cause the collapsing header to reset and close
if let Some((old_id, new_id)) = project.get_object_id_change() {
if new_id == object.id() {
let id = parent_id.with(old_id.value());

if let Some(state) = egui::collapsing_header::CollapsingState::load(ui.ctx(), id) {
default_open = state.is_open();
}
}
}

let id = parent_id.with(object.id().value());
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, false)
.show_header(ui, |ui| {
render_selectable_object(ui, object, project);
})
.body(|ui| {
for (idx, obj_id) in refs.iter().enumerate() {
match project.get_pool().object_by_id(*obj_id) {
Some(obj) => {
render_object_hierarchy(ui, id.with(idx), obj, project);
}
None => {
ui.colored_label(
egui::Color32::RED,
format!("Missing object: {:?}", id),
);
}
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
id,
default_open,
)
.show_header(ui, |ui| {
render_selectable_object(ui, object, project);
})
.body(|ui| {
for (idx, obj_id) in refs.iter().enumerate() {
match project.get_pool().object_by_id(*obj_id) {
Some(obj) => {
render_object_hierarchy(ui, id.with(idx), obj, project);
}
None => {
ui.colored_label(egui::Color32::RED, format!("Missing object: {:?}", id));
}
}
});
}
});
}
}

Expand Down Expand Up @@ -487,11 +501,14 @@ impl eframe::App for DesignerApp {
}
});

// Before we render the parameters panel, we need to reset any marked changes
pool.clear_object_id_change();

// Parameters panel
egui::SidePanel::right("right_panel").show(ctx, |ui: &mut egui::Ui| {
if let Some(id) = pool.get_selected().into() {
if let Some(obj) = pool.get_mut_pool().borrow_mut().object_mut_by_id(id) {
obj.render_parameters(ui, pool, &mut pool.get_mut_selected().borrow_mut());
obj.render_parameters(ui, pool);
let (width, height) = pool.get_pool().content_size(obj);
ui.separator();
let desired_size = egui::Vec2::new(width as f32, height as f32);
Expand Down
Loading

0 comments on commit 6576caf

Please sign in to comment.