added copy and delete to object and templates

This commit is contained in:
2025-07-15 14:56:28 +01:00
parent 76ec44d4e6
commit 1117ce9f13
6 changed files with 92 additions and 37 deletions
@@ -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"
}
}
}
@@ -3,20 +3,20 @@
"template_id": "a24b3ab7-2572-4af4-8457-df26937fd773", "template_id": "a24b3ab7-2572-4af4-8457-df26937fd773",
"name": "zxq5", "name": "zxq5",
"fields": { "fields": {
"Appearance": { "Personality": {
"value": "taller than panic" "value": "coder"
},
"Date of Birth": {
"value": "2025-07-15"
}, },
"Portrait / Image": { "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": { "Age": {
"value": "19" "value": "19"
}, },
"Personality": { "Date of Birth": {
"value": "coder" "value": "2025-07-15"
} }
} }
} }
@@ -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?"
}
]
}
+11 -5
View File
@@ -159,16 +159,23 @@ impl eframe::App for Interface {
// Main content area // Main content area
egui::SidePanel::right("templates").show(ctx, |ui| { egui::SidePanel::right("templates").show(ctx, |ui| {
let mut new_instance: RightPanelContent = RightPanelContent::None; let mut new_instance: Option<RightPanelContent> = None;
match &mut self.right_panel_content { match &mut self.right_panel_content {
// an instance of a template // an instance of a template
RightPanelContent::Object { object } => { RightPanelContent::Object { object } => {
// load template from path // load template from path
let mut right_panel = None;
let template = Template::load(&object.template_id).unwrap(); let template = Template::load(&object.template_id).unwrap();
ScrollArea::vertical().show(ui, |ui| { 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 // an editable template
@@ -199,9 +206,8 @@ impl eframe::App for Interface {
} }
}; };
if let RightPanelContent::None = new_instance { if let Some(new) = new_instance {
} else { self.right_panel_content = new;
self.right_panel_content = new_instance;
} }
}); });
+37 -6
View File
@@ -5,7 +5,7 @@ use egui::{CollapsingHeader, RichText, TextEdit, Ui, vec2};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
PROJECT_FOLDER, PROJECT_FOLDER, RightPanelContent,
template::{FieldType, FieldValue, Template}, template::{FieldType, FieldValue, Template},
}; };
@@ -90,7 +90,13 @@ impl ObjectInstance {
Ok(instance) 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<RightPanelContent>,
) {
let _ = right_panel;
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) { if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.save() { if let Err(e) = self.save() {
eprintln!("Failed to save: {e}"); eprintln!("Failed to save: {e}");
@@ -111,11 +117,36 @@ impl ObjectInstance {
}); });
}); });
if ui.button("Save").clicked() { ui.horizontal(|ui| {
if let Err(e) = self.save() { if ui.button("Save").clicked() {
eprintln!("Failed to save: {e}"); 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(); ui.separator();
+23 -5
View File
@@ -99,7 +99,7 @@ impl Template {
pub fn ui( pub fn ui(
&mut self, &mut self,
ui: &mut egui::Ui, ui: &mut egui::Ui,
new_instance: &mut RightPanelContent, new_instance: &mut Option<RightPanelContent>,
new_field_name: &mut String, new_field_name: &mut String,
new_field_type: &mut FieldType, new_field_type: &mut FieldType,
new_field_required: &mut bool, new_field_required: &mut bool,
@@ -130,22 +130,40 @@ impl Template {
// Save/Cancel buttons // Save/Cancel buttons
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui.button("Save Template").clicked() { if ui.button("Save").clicked() {
if let Err(e) = self.save() { if let Err(e) = self.save() {
eprintln!("Failed to save: {e}"); 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() { if ui.button("Cancel").clicked() {
// load default state // load default state
*self = Self::load(&self.id).unwrap(); *self = Self::load(&self.id).unwrap();
} }
if ui.button("Create New Instance").clicked() { if ui.button("Use Template").clicked() {
if self.saved { if self.saved {
*new_instance = RightPanelContent::Object { *new_instance = Some(RightPanelContent::Object {
object: Box::new(ObjectInstance::new(self)), object: Box::new(ObjectInstance::new(self)),
}; });
} else { } else {
self.error = Some(Error::new( self.error = Some(Error::new(
"You must save the template before creating a new instance!" "You must save the template before creating a new instance!"