Skip to content

Commit

Permalink
insert attribute in separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
bertiqwerty committed Aug 13, 2024
1 parent 10877c2 commit 8093bf0
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 67 deletions.
10 changes: 5 additions & 5 deletions rvimage/src/rvlib/menu/tools_menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ pub fn attributes_menu(
egui::ComboBox::from_label("")
.selected_text(format!(
"{:?}",
match data.new_attr_type {
match data.new_attr_val {
AttrVal::Float(_) => FLOAT_LABEL,
AttrVal::Int(_) => INT_LABEL,
AttrVal::Str(_) => TEXT_LABEL,
Expand All @@ -490,21 +490,21 @@ pub fn attributes_menu(
))
.show_ui(ui, |ui| {
ui.selectable_value(
&mut data.new_attr_type,
&mut data.new_attr_val,
AttrVal::Float(TPtF::default()),
FLOAT_LABEL,
);
ui.selectable_value(
&mut data.new_attr_type,
&mut data.new_attr_val,
AttrVal::Int(TPtI::default()),
INT_LABEL,
);
ui.selectable_value(
&mut data.new_attr_type,
&mut data.new_attr_val,
AttrVal::Str(String::new()),
TEXT_LABEL,
);
ui.selectable_value(&mut data.new_attr_type, AttrVal::Bool(false), BOOL_LABEL);
ui.selectable_value(&mut data.new_attr_val, AttrVal::Bool(false), BOOL_LABEL);
});
if ui.button("Add").clicked() {
if data.attr_names().contains(&data.new_attr) {
Expand Down
2 changes: 1 addition & 1 deletion rvimage/src/rvlib/tools/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Manipulate for Attributes {

if let (Some(mut attr_map_tmp), Some(data)) = (attr_map_tmp, data) {
let new_attr = data.new_attr.clone();
let new_attr_type = data.new_attr_type.clone();
let new_attr_type = data.new_attr_val.clone();
for (_, (val_map, _)) in data.anno_iter_mut() {
set_attrmap_val(val_map, &new_attr, &new_attr_type);
}
Expand Down
143 changes: 83 additions & 60 deletions rvimage/src/rvlib/tools/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,84 @@ pub(super) fn make_track_changes_str(actor: &'static str) -> String {
format!("{actor}_TRACK_CHANGE")
}

pub(super) fn insert_attribute(
mut world: World,
name: &str,
value: AttrVal,
default_value: AttrVal,
filepath: Option<&str>,
) -> World {
let mut old_attr_name = String::new();
let mut old_attr_type = AttrVal::Bool(false);

if let Ok(attr_data) =
tools_data::get_mut(&mut world, attributes::ACTOR_NAME, "Attr data missing")
{
// does the new attribute already exist?
let populate_new_attr = attr_data.specifics.attributes().map(|a| {
a.attr_names()
.iter()
.any(|attr_name| attr_name.as_str() == name)
}) != Ok(true);

// set the attribute data to addtion
trace_ok_err(attr_data.specifics.attributes_mut().map(|d| {
let attr_options = attributes_data::Options {
is_export_triggered: false,
is_addition_triggered: populate_new_attr,
is_update_triggered: false,
removal_idx: None,
};
old_attr_name.clone_from(&d.new_attr);
old_attr_type.clone_from(&d.new_attr_val);
d.new_attr = name.to_string();
d.new_attr_val = default_value;
d.options = attr_options;
}));
}

// actually add the attribute
(world, _) = attributes::Attributes {}.events_tf(world, History::default(), &Events::default());

// insert the attribute's value to the attribute map of the current file
if let Ok(attr_data) =
tools_data::get_mut(&mut world, attributes::ACTOR_NAME, "Attr data missing")
{
let attr_options = attributes_data::Options {
is_export_triggered: false,
is_addition_triggered: false,
is_update_triggered: true,
removal_idx: None,
};
trace_ok_err(attr_data.specifics.attributes_mut().map(|d| {
d.options = attr_options;
let attr_map = if let Some(filepath) = filepath {
d.attr_map(filepath)
} else {
d.current_attr_map.as_mut()
};
if let Some(attr_map) = attr_map {
attr_map.insert(name.to_string(), value);
} else {
warn!("no attrmap found");
}
}));
}
(world, _) = attributes::Attributes {}.events_tf(world, History::default(), &Events::default());

if let Ok(attr_data) =
tools_data::get_mut(&mut world, attributes::ACTOR_NAME, "Attr data missing")
{
// reset the state of the attribute data
trace_ok_err(attr_data.specifics.attributes_mut().map(|d| {
d.new_attr = old_attr_name;
d.new_attr_val = old_attr_type;
}));
}

world
}

pub(super) fn change_annos<T>(
world: &mut World,
track_change_str: &str,
Expand All @@ -29,68 +107,13 @@ pub(super) fn change_annos<T>(
f_change(annos);
}
if track_changes {
let mut old_attr_name = String::new();
let mut old_attr_type = AttrVal::Bool(false);
let track_change_str = track_change_str.to_string();
if let Ok(attr_data) =
tools_data::get_mut(world, attributes::ACTOR_NAME, "Attr data missing")
{
let populate_new_attr = attr_data
.specifics
.attributes()
.map(|a| a.attr_names().contains(&track_change_str))
!= Ok(true);

trace_ok_err(attr_data.specifics.attributes_mut().map(|d| {
let attr_options = attributes_data::Options {
is_export_triggered: false,
is_addition_triggered: populate_new_attr,
is_update_triggered: false,
removal_idx: None,
};
old_attr_name.clone_from(&d.new_attr);
old_attr_type.clone_from(&d.new_attr_type);
d.new_attr = track_change_str.to_string();
d.new_attr_type = AttrVal::Bool(false);
d.options = attr_options;
}));
}
(*world, _) = attributes::Attributes {}.events_tf(
mem::take(world),
History::default(),
&Events::default(),
);
if let Ok(attr_data) =
tools_data::get_mut(world, attributes::ACTOR_NAME, "Attr data missing")
{
let attr_options = attributes_data::Options {
is_export_triggered: false,
is_addition_triggered: false,
is_update_triggered: true,
removal_idx: None,
};
trace_ok_err(attr_data.specifics.attributes_mut().map(|d| {
d.options = attr_options;
if let Some(attr_map) = &mut d.current_attr_map {
attr_map.insert(track_change_str, AttrVal::Bool(true));
} else {
warn!("no attrmap found");
}
}));
}
(*world, _) = attributes::Attributes {}.events_tf(
*world = insert_attribute(
mem::take(world),
History::default(),
&Events::default(),
track_change_str,
AttrVal::Bool(true),
AttrVal::Bool(false),
None,
);
if let Ok(attr_data) =
tools_data::get_mut(world, attributes::ACTOR_NAME, "Attr data missing")
{
trace_ok_err(attr_data.specifics.attributes_mut().map(|d| {
d.new_attr = old_attr_name;
d.new_attr_type = old_attr_type;
}));
}
}
}
pub(super) fn check_trigger_redraw(
Expand Down
7 changes: 6 additions & 1 deletion rvimage/src/rvlib/tools_data/attributes_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub struct AttributesToolData {
attr_names: Vec<String>,
attr_types: Vec<AttrVal>,
pub new_attr: String,
pub new_attr_type: AttrVal,
pub new_attr_val: AttrVal,
new_attr_buffers: Vec<String>,
// maps the filename to the number of rotations
annotations_map: AttrAnnotationsMap,
Expand Down Expand Up @@ -160,5 +160,10 @@ impl AttributesToolData {
self.annotations_map = map;
Ok(())
}
pub fn attr_map(&mut self, filename: &str) -> Option<&mut AttrMap> {
self.annotations_map
.get_mut(filename)
.map(|(attr_map, _)| attr_map)
}
}
implement_annotate!(AttributesToolData);

0 comments on commit 8093bf0

Please sign in to comment.