From 1117ce9f13217987195f8004606b26ac72349d3d Mon Sep 17 00:00:00 2001 From: zxq5 Date: Tue, 15 Jul 2025 14:56:28 +0100 Subject: [PATCH] added copy and delete to object and templates --- .../1da3111e-717a-4452-8209-98c5443fc31a.json | 13 ------ .../d1b10e0c-f6b4-4b9d-a96e-cb51213f0243.json | 16 +++---- .../b5745688-3c1c-40de-bc3a-2a3e354dd19d.json | 13 ++++++ src/main.rs | 16 ++++--- src/object.rs | 43 ++++++++++++++++--- src/template.rs | 28 +++++++++--- 6 files changed, 92 insertions(+), 37 deletions(-) delete mode 100644 project/objects/1da3111e-717a-4452-8209-98c5443fc31a.json create mode 100644 project/templates/b5745688-3c1c-40de-bc3a-2a3e354dd19d.json diff --git a/project/objects/1da3111e-717a-4452-8209-98c5443fc31a.json b/project/objects/1da3111e-717a-4452-8209-98c5443fc31a.json deleted file mode 100644 index 88a8990..0000000 --- a/project/objects/1da3111e-717a-4452-8209-98c5443fc31a.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "id": "1da3111e-717a-4452-8209-98c5443fc31a", - "template_id": "c96f5e87-7517-44cc-a5ab-42ffd537801d", - "name": "sword", - "fields": { - "description": { - "value": "idk, it probably looks cool" - }, - "durability": { - "value": "13.1" - } - } -} \ No newline at end of file diff --git a/project/objects/d1b10e0c-f6b4-4b9d-a96e-cb51213f0243.json b/project/objects/d1b10e0c-f6b4-4b9d-a96e-cb51213f0243.json index 7b96f6a..1168151 100644 --- a/project/objects/d1b10e0c-f6b4-4b9d-a96e-cb51213f0243.json +++ b/project/objects/d1b10e0c-f6b4-4b9d-a96e-cb51213f0243.json @@ -3,20 +3,20 @@ "template_id": "a24b3ab7-2572-4af4-8457-df26937fd773", "name": "zxq5", "fields": { - "Appearance": { - "value": "taller than panic" - }, - "Date of Birth": { - "value": "2025-07-15" + "Personality": { + "value": "coder" }, "Portrait / Image": { - "value": "/home/zxq5/Pictures/logos and pfps/YT profile picture background.png" + "value": "/home/fantasypvp/Pictures/logos and pfps/YT profile picture background.png" + }, + "Appearance": { + "value": "taller than panic" }, "Age": { "value": "19" }, - "Personality": { - "value": "coder" + "Date of Birth": { + "value": "2025-07-15" } } } \ No newline at end of file diff --git a/project/templates/b5745688-3c1c-40de-bc3a-2a3e354dd19d.json b/project/templates/b5745688-3c1c-40de-bc3a-2a3e354dd19d.json new file mode 100644 index 0000000..6a3b549 --- /dev/null +++ b/project/templates/b5745688-3c1c-40de-bc3a-2a3e354dd19d.json @@ -0,0 +1,13 @@ +{ + "name": "Location", + "id": "b5745688-3c1c-40de-bc3a-2a3e354dd19d", + "description": "a place", + "fields": [ + { + "name": "description", + "field_type": "MultiLine", + "required": true, + "description": "what is it like?" + } + ] +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f4e96de..7af5d8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,16 +159,23 @@ impl eframe::App for Interface { // Main content area egui::SidePanel::right("templates").show(ctx, |ui| { - let mut new_instance: RightPanelContent = RightPanelContent::None; + let mut new_instance: Option = None; match &mut self.right_panel_content { // an instance of a template RightPanelContent::Object { object } => { // load template from path + + let mut right_panel = None; + let template = Template::load(&object.template_id).unwrap(); ScrollArea::vertical().show(ui, |ui| { - object.ui(ui, &template); + object.ui(ui, &template, &mut right_panel); }); + + if let Some(right_panel) = right_panel { + self.right_panel_content = right_panel; + } } // an editable template @@ -199,9 +206,8 @@ impl eframe::App for Interface { } }; - if let RightPanelContent::None = new_instance { - } else { - self.right_panel_content = new_instance; + if let Some(new) = new_instance { + self.right_panel_content = new; } }); diff --git a/src/object.rs b/src/object.rs index 72fb3c2..730745d 100644 --- a/src/object.rs +++ b/src/object.rs @@ -5,7 +5,7 @@ use egui::{CollapsingHeader, RichText, TextEdit, Ui, vec2}; use serde::{Deserialize, Serialize}; use crate::{ - PROJECT_FOLDER, + PROJECT_FOLDER, RightPanelContent, template::{FieldType, FieldValue, Template}, }; @@ -90,7 +90,13 @@ impl ObjectInstance { Ok(instance) } - pub fn ui(&mut self, ui: &mut Ui, template: &Template) { + pub fn ui( + &mut self, + ui: &mut Ui, + template: &Template, + right_panel: &mut Option, + ) { + let _ = right_panel; if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) { if let Err(e) = self.save() { eprintln!("Failed to save: {e}"); @@ -111,11 +117,36 @@ impl ObjectInstance { }); }); - if ui.button("Save").clicked() { - if let Err(e) = self.save() { - eprintln!("Failed to save: {e}"); + ui.horizontal(|ui| { + if ui.button("Save").clicked() { + if let Err(e) = self.save() { + eprintln!("Failed to save: {e}"); + } } - } + + if ui.button("Create Copy").clicked() { + let mut copy = self.clone(); + copy.id = uuid::Uuid::new_v4().to_string(); + copy.dialog = None; + copy.name = format!("{} (Copy)", self.name); + copy.save().unwrap(); + + *right_panel = Some(RightPanelContent::Object { + object: Box::new(copy), + }); + } + + if ui.button("Delete").clicked() { + std::fs::remove_file( + PROJECT_FOLDER + .join("objects") + .join(format!("{}.json", self.id)), + ) + .unwrap(); + + *right_panel = Some(RightPanelContent::None); + } + }); ui.separator(); diff --git a/src/template.rs b/src/template.rs index de5e17e..6a7861e 100644 --- a/src/template.rs +++ b/src/template.rs @@ -99,7 +99,7 @@ impl Template { pub fn ui( &mut self, ui: &mut egui::Ui, - new_instance: &mut RightPanelContent, + new_instance: &mut Option, new_field_name: &mut String, new_field_type: &mut FieldType, new_field_required: &mut bool, @@ -130,22 +130,40 @@ impl Template { // Save/Cancel buttons ui.horizontal(|ui| { - if ui.button("Save Template").clicked() { + if ui.button("Save").clicked() { if let Err(e) = self.save() { eprintln!("Failed to save: {e}"); } } + if ui.button("Create Copy").clicked() { + let mut copy = self.clone(); + copy.id = uuid::Uuid::new_v4().to_string(); + copy.name = format!("{} (Copy)", self.name); + copy.save().unwrap(); + } + + if ui.button("Delete").clicked() { + std::fs::remove_file( + PROJECT_FOLDER + .join("templates") + .join(format!("{}.json", self.id)), + ) + .unwrap(); + + *new_instance = Some(RightPanelContent::None); + } + if ui.button("Cancel").clicked() { // load default state *self = Self::load(&self.id).unwrap(); } - if ui.button("Create New Instance").clicked() { + if ui.button("Use Template").clicked() { if self.saved { - *new_instance = RightPanelContent::Object { + *new_instance = Some(RightPanelContent::Object { object: Box::new(ObjectInstance::new(self)), - }; + }); } else { self.error = Some(Error::new( "You must save the template before creating a new instance!"