1use crate::prelude::*;
2use codegridfx::Module;
3pub use rusterix::map::*;
4use theframework::prelude::*;
5
6fn default_editing_look_at_3d() -> Vec3<f32> {
7 Vec3::new(2.0, 0.0, 0.0)
8}
9
10#[derive(Serialize, Deserialize, Clone, Debug)]
11pub struct Region {
12 pub id: Uuid,
13
14 pub name: String,
15 pub map: Map,
16
17 #[serde(default)]
18 pub config: String,
19
20 #[serde(default = "default_region_module")]
21 pub module: Module,
22
23 #[serde(default)]
24 pub source: String,
25
26 #[serde(default)]
27 pub source_debug: String,
28
29 pub characters: IndexMap<Uuid, Character>,
30 pub items: IndexMap<Uuid, Item>,
31
32 pub editing_position_3d: Vec3<f32>,
33 #[serde(default = "default_editing_look_at_3d")]
34 pub editing_look_at_3d: Vec3<f32>,
35
36 #[serde(default)]
38 pub editing_position_iso_3d: Option<Vec3<f32>>,
39 #[serde(default)]
40 pub editing_look_at_iso_3d: Option<Vec3<f32>>,
41 #[serde(default)]
42 pub editing_position_orbit_3d: Option<Vec3<f32>>,
43 #[serde(default)]
44 pub editing_look_at_orbit_3d: Option<Vec3<f32>>,
45 #[serde(default)]
46 pub editing_position_firstp_3d: Option<Vec3<f32>>,
47 #[serde(default)]
48 pub editing_look_at_firstp_3d: Option<Vec3<f32>>,
49 #[serde(default)]
50 pub editing_iso_scale: Option<f32>,
51 #[serde(default)]
52 pub editing_orbit_distance: Option<f32>,
53}
54
55impl Default for Region {
56 fn default() -> Self {
57 Self::new()
58 }
59}
60
61impl PartialEq for Region {
62 fn eq(&self, other: &Self) -> bool {
63 self.id == other.id
64 }
65}
66
67impl Region {
68 pub fn new() -> Self {
69 Self {
70 id: Uuid::new_v4(),
71 name: "New Region".to_string(),
72
73 map: Map::default(),
74 config: String::new(),
75 module: default_region_module(),
76 source: String::new(),
77 source_debug: String::new(),
78
79 characters: IndexMap::default(),
80 items: IndexMap::default(),
81
82 editing_position_3d: Vec3::zero(),
83 editing_look_at_3d: Vec3::zero(),
84 editing_position_iso_3d: None,
85 editing_look_at_iso_3d: None,
86 editing_position_orbit_3d: None,
87 editing_look_at_orbit_3d: None,
88 editing_position_firstp_3d: None,
89 editing_look_at_firstp_3d: None,
90 editing_iso_scale: None,
91 editing_orbit_distance: None,
92 }
93 }
94
95 pub fn from_json(json: &str) -> Self {
97 serde_json::from_str(json).unwrap_or(Region::new())
98 }
99
100 pub fn to_json(&self) -> String {
102 serde_json::to_string(&self).unwrap_or_default()
103 }
104}
105
106fn default_region_module() -> Module {
107 Module::as_type(codegridfx::ModuleType::Region)
108}