fyrox_core/visitor/field.rs
1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! A set fundamental data types.
22
23use nalgebra::{Matrix2, Matrix3, Matrix4, UnitComplex, UnitQuaternion, Vector2, Vector3, Vector4};
24use uuid::Uuid;
25
26/// The internal data format of [`crate::visitor::Visitor`]. Fields are limited to being one of these types.
27/// This means that all [`crate::visitor::Visit`] values must be built from some assortment
28/// of these types.
29/// Fields can be accessed from a visitor using [`crate::visitor::Visit::visit`] on a variable with the
30/// same type as the field.
31#[derive(PartialEq, Debug)]
32pub enum FieldKind {
33 /// Boolean value.
34 Bool(bool),
35 /// `u8` value.
36 U8(u8),
37 /// `i8` value.
38 I8(i8),
39 /// `u16` value.
40 U16(u16),
41 /// `i16` value.
42 I16(i16),
43 /// `u32` value.
44 U32(u32),
45 /// `i32` value.
46 I32(i32),
47 /// `u64` value.
48 U64(u64),
49 /// `i64` value.
50 I64(i64),
51 /// `f32` value.
52 F32(f32),
53 /// `f64` value.
54 F64(f64),
55 /// Unit quaternion.
56 UnitQuaternion(UnitQuaternion<f32>),
57 /// 4x4 f32 matrix.
58 Matrix4(Matrix4<f32>),
59 /// A representation of some `Vec<T>` where `T` must be [Copy].
60 /// It is mostly used to store the bytes of string types.
61 BinaryBlob(Vec<u8>),
62 /// 3x3 f32 matrix.
63 Matrix3(Matrix3<f32>),
64 /// Unique id.
65 Uuid(Uuid),
66 /// A complex number.
67 UnitComplex(UnitComplex<f32>),
68 /// A representation for arrays of [`crate::visitor::pod::Pod`] types as a `Vec<u8>`.
69 PodArray {
70 /// A code to identify the Pod type of the elements of the array.
71 /// Taken from [`crate::visitor::pod::Pod::type_id`].
72 type_id: u8,
73 /// The number of bytes in each array element.
74 element_size: u32,
75 /// The bytes that store the data, with unspecified endianness.
76 bytes: Vec<u8>,
77 },
78 /// 2x2 f32 matrix.
79 Matrix2(Matrix2<f32>),
80 /// 2D f32 vector.
81 Vector2F32(Vector2<f32>),
82 /// 3D f32 vector.
83 Vector3F32(Vector3<f32>),
84 /// 4D f32 vector.
85 Vector4F32(Vector4<f32>),
86 /// 2D f64 vector.
87 Vector2F64(Vector2<f64>),
88 /// 3D f64 vector.
89 Vector3F64(Vector3<f64>),
90 /// 4D f64 vector.
91 Vector4F64(Vector4<f64>),
92 /// 2D u8 vector.
93 Vector2U8(Vector2<u8>),
94 /// 3D u8 vector.
95 Vector3U8(Vector3<u8>),
96 /// 4D u8 vector.
97 Vector4U8(Vector4<u8>),
98 /// 2D i8 vector.
99 Vector2I8(Vector2<i8>),
100 /// 3D i8 vector.
101 Vector3I8(Vector3<i8>),
102 /// 4D i8 vector.
103 Vector4I8(Vector4<i8>),
104 /// 2D u16 vector.
105 Vector2U16(Vector2<u16>),
106 /// 3D u16 vector.
107 Vector3U16(Vector3<u16>),
108 /// 4D u16 vector.
109 Vector4U16(Vector4<u16>),
110 /// 2D i16 vector.
111 Vector2I16(Vector2<i16>),
112 /// 3D i16 vector.
113 Vector3I16(Vector3<i16>),
114 /// 4D i16 vector.
115 Vector4I16(Vector4<i16>),
116 /// 2D u32 vector.
117 Vector2U32(Vector2<u32>),
118 /// 3D u32 vector.
119 Vector3U32(Vector3<u32>),
120 /// 4D u32 vector.
121 Vector4U32(Vector4<u32>),
122 /// 2D i32 vector.
123 Vector2I32(Vector2<i32>),
124 /// 3D i32 vector.
125 Vector3I32(Vector3<i32>),
126 /// 4D i32 vector.
127 Vector4I32(Vector4<i32>),
128 /// 2D u64 vector.
129 Vector2U64(Vector2<u64>),
130 /// 3D u64 vector.
131 Vector3U64(Vector3<u64>),
132 /// 4D u64 vector.
133 Vector4U64(Vector4<u64>),
134 /// 2D i64 vector.
135 Vector2I64(Vector2<i64>),
136 /// 3D i64 vector.
137 Vector3I64(Vector3<i64>),
138 /// 4D i64 vector.
139 Vector4I64(Vector4<i64>),
140 /// A unicode string.
141 String(String),
142}
143
144/// Values within a visitor are constructed from Fields. Each Field has a name and a value. The name
145/// is used as a key to access the value within the visitor using the [`crate::Visit::visit`] method,
146/// so each field within a value must have a unique name.
147#[derive(PartialEq, Debug)]
148pub struct Field {
149 /// The key string that allows access to the field.
150 pub name: String,
151 /// The data stored in the visitor for this field.
152 pub kind: FieldKind,
153}
154
155impl Field {
156 /// Creates a new field from the given name and the given field kind.
157 pub fn new(name: &str, kind: FieldKind) -> Self {
158 Self {
159 name: name.to_owned(),
160 kind,
161 }
162 }
163}