vpin/vpx/gameitem/
select.rs

1use crate::vpx::biff;
2
3// TODO create the read side of this trait
4
5pub trait WriteSharedAttributes {
6    fn write_shared_attributes(&self, writer: &mut biff::BiffWriter);
7    fn read_shared_attribute(&mut self, tag_str: &str, reader: &mut biff::BiffReader) -> bool;
8}
9
10/// Required trait for any type that has shared attributes
11///
12/// TODO we could use a shared struct to implement this trait for all types that have shared attributes. (composition)
13pub trait HasSharedAttributes {
14    fn name(&self) -> &str;
15    fn is_locked(&self) -> bool;
16    /// in 10.8.1 Deprecated and replaced by part groups
17    fn editor_layer(&self) -> Option<u32>;
18    fn editor_layer_name(&self) -> Option<&str>;
19    fn editor_layer_visibility(&self) -> Option<bool>;
20    fn part_group_name(&self) -> Option<&str>;
21
22    fn set_is_locked(&mut self, locked: bool);
23    /// in 10.8.1 Deprecated and replaced by part groups
24    fn set_editor_layer(&mut self, layer: Option<u32>);
25    fn set_editor_layer_name(&mut self, name: Option<String>);
26    fn set_editor_layer_visibility(&mut self, visibility: Option<bool>);
27    fn set_part_group_name(&mut self, name: Option<String>);
28}
29
30pub trait TimerDataRoot {
31    fn is_timer_enabled(&self) -> bool;
32    fn timer_interval(&self) -> i32;
33}
34
35impl<T> WriteSharedAttributes for T
36where
37    T: HasSharedAttributes,
38{
39    fn write_shared_attributes(&self, writer: &mut biff::BiffWriter) {
40        writer.write_tagged_bool("LOCK", self.is_locked());
41        if let Some(layer) = self.editor_layer() {
42            writer.write_tagged_u32("LAYR", layer);
43        }
44        if let Some(name) = self.editor_layer_name() {
45            writer.write_tagged_string("LANR", name);
46        }
47        if let Some(group_name) = self.part_group_name() {
48            writer.write_tagged_string("GRUP", group_name);
49        }
50        if let Some(visibility) = self.editor_layer_visibility() {
51            writer.write_tagged_bool("LVIS", visibility);
52        }
53    }
54
55    fn read_shared_attribute(&mut self, tag: &str, reader: &mut biff::BiffReader) -> bool {
56        match tag {
57            "LOCK" => {
58                self.set_is_locked(reader.get_bool());
59                true
60            }
61            "LAYR" => {
62                self.set_editor_layer(Some(reader.get_u32()));
63                true
64            }
65            "LANR" => {
66                self.set_editor_layer_name(Some(reader.get_string()));
67                true
68            }
69            "LVIS" => {
70                self.set_editor_layer_visibility(Some(reader.get_bool()));
71                true
72            }
73            "GRUP" => {
74                self.set_part_group_name(Some(reader.get_string()));
75                true
76            }
77            _ => false,
78        }
79    }
80}
81
82/// Macro to implement HasSharedAttributes for a given type
83/// Assumes the type has the following fields:
84/// - name: String
85/// - is_locked: bool
86/// - editor_layer: Option<u32>
87/// - editor_layer_name: Option<String>
88/// - editor_layer_visibility: Option<bool>
89/// - part_group_name: Option<String>
90#[macro_export]
91macro_rules! impl_shared_attributes {
92    ($ty:ty) => {
93        impl $crate::vpx::gameitem::select::HasSharedAttributes for $ty {
94            fn name(&self) -> &str {
95                &self.name
96            }
97            fn is_locked(&self) -> bool {
98                self.is_locked
99            }
100            fn editor_layer(&self) -> Option<u32> {
101                self.editor_layer
102            }
103            fn editor_layer_name(&self) -> Option<&str> {
104                self.editor_layer_name.as_deref()
105            }
106            fn editor_layer_visibility(&self) -> Option<bool> {
107                self.editor_layer_visibility
108            }
109            fn part_group_name(&self) -> Option<&str> {
110                self.part_group_name.as_deref()
111            }
112
113            fn set_is_locked(&mut self, locked: bool) {
114                self.is_locked = locked;
115            }
116            fn set_editor_layer(&mut self, layer: Option<u32>) {
117                self.editor_layer = layer;
118            }
119            fn set_editor_layer_name(&mut self, name: Option<String>) {
120                self.editor_layer_name = name;
121            }
122            fn set_editor_layer_visibility(&mut self, visibility: Option<bool>) {
123                self.editor_layer_visibility = visibility;
124            }
125            fn set_part_group_name(&mut self, name: Option<String>) {
126                self.part_group_name = name;
127            }
128        }
129    };
130}