system for extra data such as grids atop nodes in structure stringtree

This commit is contained in:
Lillian Vixe 2024-05-28 15:19:18 -07:00
parent 0455167d78
commit 1bb0987120
2 changed files with 79 additions and 5 deletions

View file

@ -213,6 +213,7 @@ pub fn startup_system_edit_ui(
let asset_test = StructureAsset {
layout: shared_ui_state.structure.clone(),
default_pose_positions: HashMap::<String, Transform>::new(),
element_data: Default::default(),
};
s_structure_asset::serialization::structures::write_latest_version(
&"test.vvs".to_string(),

View file

@ -1,5 +1,5 @@
use bevy::{
asset::{Asset, AssetLoader, AsyncReadExt},
asset::{Asset, AssetLoader, AssetServer, AsyncReadExt, Handle},
reflect::TypePath,
transform::components::Transform,
utils::{hashbrown::HashMap, thiserror},
@ -9,11 +9,71 @@ use s_string_tree::StringTree;
use crate::vvedit::s_string_tree;
use thiserror::Error;
use super::s_oct_asset::OctTreeAsset;
#[derive(Asset, TypePath)]
pub struct StructureAsset {
pub layout: StringTree,
pub default_pose_positions: HashMap<String, Transform>,
pub element_data: HashMap<String, String>,
pub element_data: HashMap<String, ElementData>,
}
#[derive(Clone)]
pub enum ElementData {
None,
Unprocessed(String),
MeshAsset(Handle<OctTreeAsset>, String), //store path
}
impl ElementData {
pub fn is_none(&self) -> bool {
match self {
ElementData::None => true,
_ => false,
}
}
pub fn to_save_format(&self) -> Option<String> {
match self {
ElementData::None => None,
ElementData::Unprocessed(asset) => Some(asset.clone()),
ElementData::MeshAsset(_, path) => Some(format!("grid|{path}")),
}
}
pub fn process_from_save_format(&mut self, server: &mut AssetServer) -> Option<Self> {
let mut result_change = ElementData::None;
match self {
ElementData::Unprocessed(contents) => {
let mut sides = contents.split("|");
let left = sides.next();
if left.is_none() {
return None;
}
let left = left.unwrap().trim();
match left.to_lowercase().as_str() {
"grid" => {
let right = sides.next();
if right.is_none() {
return None;
}
let right = right.unwrap().trim();
result_change = ElementData::MeshAsset(
server.load(right.to_string()),
right.to_string(),
);
}
_ => {
return None;
}
}
}
_ => {
println!("why are you processing structure asset element data that was already processed or does not exist?");
}
}
if result_change.is_none() {
None
} else {
Some(result_change)
}
}
}
#[derive(Default)]
@ -113,7 +173,8 @@ pub mod serialization {
// EACH LINE DEFINES ONE NODE TO ITS CHILDREN
// FIRST LINE IS ROOT LEVEL NODES AFTER THAT EACH CHILD NODE OF ROOT NODES IN ORDER
// PAUSE BETWEEN STRING TREE AND POSITIONS
// LIST OF NODE IDS BY STRING NAME, FOLLOWED BY TRANSFORM DATA, FOLLOWED BY NONSTANDARD DATA (MESH BEING RENDERED AT THIS TRANSFORM)
// LIST OF NODE IDS BY STRING NAME, FOLLOWED BY TRANSFORM DATA
// NODE ID FOLLOWED BY NONSTANDARD DATA (MESH BEING RENDERED AT THIS TRANSFORM)
let mut data: String = "".into();
// v STRING TREE (tree that defines shape/structure of it)
@ -168,7 +229,7 @@ pub mod serialization {
// ^ STRING TREE (tree that defines shape/structure of it)
data += NEWLINE;
data += "||";
// v NODE IDS BY STRING NAME, FOLLOWED BY TRANSFORM DATA, FOLLOWED BY NONSTANDARD DATA
// v NODE IDS BY STRING NAME, FOLLOWED BY TRANSFORM DATA
{
// error if bool is true
fn transform_to_file(pos: &Transform, data: &mut String) -> bool {
@ -188,9 +249,21 @@ pub mod serialization {
}
}
}
// ^ NODE IDS BY STRING NAME, FOLLOWED BY TRANSFORM DATA, FOLLOWED BY NONSTANDARD DATA
// ^ NODE IDS BY STRING NAME, FOLLOWED BY TRANSFORM DATA
data += NEWLINE;
data += "||";
// v NODE DATA SUCH AS RELIANT GRIDS
for (name, element_data) in &asset.element_data {
data += NEWLINE;
data += name.as_str();
data += "|";
let save_formatted = element_data.to_save_format();
if save_formatted.is_none() {
return None;
}
data += save_formatted.unwrap().as_str();
}
// ^ NODE DATA SUCH AS RELIANT GRIDS
Some(data.as_bytes().to_vec())
}
}