Skip to content

Commit

Permalink
feat: attach extra info struct to object's in the pool
Browse files Browse the repository at this point in the history
- fixes collapsing tree view when object changes
- also passed around 'project design' more instead of it's variables
  • Loading branch information
GwnDaan committed Dec 22, 2024
1 parent 6abb94e commit d7ac773
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 405 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ rfd = "0.13"
image = "0.25.1"
getrandom = { version = "0.2.11", features = ["js"] }
log = "0.4"
uuid = { version = "1.11.0", features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
] }

# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
29 changes: 27 additions & 2 deletions src/editor_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
//! SPDX-License-Identifier: GPL-3.0-or-later
//! Authors: Daan Steenbergen
use std::cell::RefCell;
use std::{cell::RefCell, collections::HashMap};

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

use crate::ObjectInfo;

const MAX_UNDO_REDO_POOL: usize = 10;
const MAX_UNDO_REDO_SELECTED: usize = 20;
Expand All @@ -21,6 +23,7 @@ pub struct EditorProject {
redo_selected_history: Vec<NullableObjectId>,
pub mask_size: u16,
soft_key_size: (u16, u16),
object_info: RefCell<HashMap<ObjectId, ObjectInfo>>,
}

impl From<ObjectPool> for EditorProject {
Expand All @@ -37,6 +40,7 @@ impl From<ObjectPool> for EditorProject {
redo_selected_history: Default::default(),
mask_size,
soft_key_size,
object_info: RefCell::new(HashMap::new()),
}
}
}
Expand Down Expand Up @@ -151,4 +155,25 @@ impl EditorProject {
self.mut_selected_object.replace(selected);
}
}

/// Change an object id in the object info hashmap
pub fn update_object_id_for_info(&self, old_id: ObjectId, new_id: ObjectId) {
let mut object_info = self.object_info.borrow_mut();
if let Some(info) = object_info.remove(&old_id) {
object_info.insert(new_id, info);
}
}

/// Get the object info for an object id
/// If the object id is not mapped, we insert the default object info
pub fn get_object_info(&self, object: &Object) -> ObjectInfo {
if let Some(info) = self.object_info.borrow().get(&object.id()) {
return info.clone();
}
let info = ObjectInfo::new(object);
self.object_info
.borrow_mut()
.insert(object.id(), info.clone());
info
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ mod allowed_object_relationships;
mod editor_project;
mod object_configuring;
mod object_defaults;
mod object_info;
mod object_rendering;
mod possible_events;

pub use editor_project::EditorProject;
pub use object_configuring::ConfigurableObject;
pub use object_defaults::default_object;
pub use object_info::ObjectInfo;
pub use object_rendering::RenderableObject;
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ impl<'a> egui::Widget for ObjectWrapper<'a> {
}

fn render_selectable_object(ui: &mut egui::Ui, object: &Object, project: &EditorProject) {
let name: String = format!("{:?}: {:?}", u16::from(object.id()), object.object_type());
let name = project.get_object_info(object).get_name(object);
let is_selected = project.get_selected() == object.id().into();
let response = ui.selectable_label(is_selected, name.clone());
let response = ui.selectable_label(is_selected, name);

if response.clicked() {
project
Expand All @@ -221,7 +221,7 @@ fn render_object_hierarchy(
render_selectable_object(ui, object, project);
});
} else {
let id = parent_id.with(object.id().value());
let id = parent_id.with(project.get_object_info(object).get_unique_id());
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, false)
.show_header(ui, |ui| {
render_selectable_object(ui, object, project);
Expand Down Expand Up @@ -491,7 +491,7 @@ impl eframe::App for DesignerApp {
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 d7ac773

Please sign in to comment.