@@ -9,70 +9,6 @@ use crate::builtin::meta::{ConvertError, FromGodot, FromGodotError, GodotConvert
99use crate :: builtin:: { real, Vector2 , Vector2i , Vector3 , Vector3i , Vector4 , Vector4i } ;
1010use crate :: obj:: EngineEnum ;
1111
12- /// Access vector components in different order.
13- ///
14- /// Allows to rearrange components, as well as to create higher- or lower-order vectors.
15- ///
16- /// This macro supports all vector types (2D, 3D, 4D; both integer and float). The resulting vector
17- /// type is deduced from the number and types of components.
18- ///
19- /// To repeat a single component, check out the `splat` method on specific vector types.
20- ///
21- /// # Examples
22- ///
23- /// Reorder or duplicate fields:
24- /// ```
25- /// use godot::prelude::*;
26- ///
27- /// let vec3 = Vector3i::new(1, 2, 3);
28- /// let xzx = swizzle!(vec3 => x, z, x); // Vector3i
29- ///
30- /// assert_eq!(xzx, Vector3i::new(1, 3, 1));
31- /// ```
32- ///
33- /// Create lower-order vector:
34- /// ```
35- /// # use godot::prelude::*;
36- /// let vec4 = Vector4::new(1.0, 2.0, 3.0, 4.0);
37- /// let yw = swizzle!(vec4 => y, w); // Vector2
38- ///
39- /// assert_eq!(yw, Vector2::new(2.0, 4.0));
40- /// ```
41- ///
42- /// Create higher-order vector:
43- /// ```
44- /// # use godot::prelude::*;
45- /// let vec3 = Vector3i::new(1, 2, 3);
46- /// let xyyz = swizzle!(vec3 => x, y, y, z); // Vector4i
47- ///
48- /// assert_eq!(xyyz, Vector4i::new(1, 2, 2, 3));
49- /// ```
50- #[ macro_export]
51- macro_rules! swizzle {
52- ( $vec: expr => $a: ident, $b: ident) => { {
53- let expr = $vec;
54- $crate:: builtin:: ToVector :: to_vector( ( expr. $a, expr. $b) )
55- } } ;
56- ( $vec: expr => $a: ident, $b: ident, $c: ident) => { {
57- let expr = $vec;
58- $crate:: builtin:: ToVector :: to_vector( ( expr. $a, expr. $b, expr. $c) )
59- } } ;
60- ( $vec: expr => $a: ident, $b: ident, $c: ident, $d: ident) => { {
61- let expr = $vec;
62- $crate:: builtin:: ToVector :: to_vector( ( expr. $a, expr. $b, expr. $c, expr. $d) )
63- } } ;
64- }
65-
66- // ----------------------------------------------------------------------------------------------------------------------------------------------
67-
68- /// Trait that allows conversion from tuples to vectors.
69- ///
70- /// Is implemented instead of `From`/`Into` because it provides type inference.
71- pub trait ToVector : Sized {
72- type Output ;
73- fn to_vector ( self ) -> Self :: Output ;
74- }
75-
7612macro_rules! impl_vector_axis_enum {
7713 ( $Vector: ident, $AxisEnum: ident, ( $( $axis: ident) ,+) ) => {
7814 #[ doc = concat!( "Enumerates the axes in a [`" , stringify!( $Vector) , "`]." ) ]
@@ -135,63 +71,3 @@ impl_vector_index!(Vector3i, i32, (x, y, z), Vector3Axis, (X, Y, Z));
13571
13672impl_vector_index ! ( Vector4 , real, ( x, y, z, w) , Vector4Axis , ( X , Y , Z , W ) ) ;
13773impl_vector_index ! ( Vector4i , i32 , ( x, y, z, w) , Vector4Axis , ( X , Y , Z , W ) ) ;
138-
139- // ----------------------------------------------------------------------------------------------------------------------------------------------
140-
141- #[ cfg( test) ]
142- mod test {
143- use crate :: assert_eq_approx;
144- use crate :: builtin:: * ;
145-
146- #[ test]
147- fn test_vector_swizzle ( ) {
148- // * VectorN swizzle
149- let vector2 = Vector2 :: new ( 1.0 , 2.0 ) ;
150- let vector3 = Vector3 :: new ( 1.0 , 2.0 , 3.0 ) ;
151- let vector4 = Vector4 :: new ( 1.0 , 2.0 , 3.0 , 4.0 ) ;
152-
153- // VectorN to Vector2
154- let vec2swiz2: Vector2 = swizzle ! ( vector2 => y, x) ;
155- let vec3swiz2: Vector2 = swizzle ! ( vector3 => y, x) ;
156- let vec4swiz2: Vector2 = swizzle ! ( vector4 => y, x) ;
157- assert_eq_approx ! ( vec2swiz2, Vector2 :: new( 2.0 , 1.0 ) ) ;
158- assert_eq_approx ! ( vec3swiz2, Vector2 :: new( 2.0 , 1.0 ) ) ;
159- assert_eq_approx ! ( vec4swiz2, Vector2 :: new( 2.0 , 1.0 ) ) ;
160-
161- // VectorN to Vector3
162- let vec2swiz3: Vector3 = swizzle ! ( vector2 => y, x, x) ;
163- let vec3swiz3: Vector3 = swizzle ! ( vector3 => y, x, z) ;
164- let vec4swiz3: Vector3 = swizzle ! ( vector4 => y, x, z) ;
165- assert_eq_approx ! ( vec2swiz3, Vector3 :: new( 2.0 , 1.0 , 1.0 ) , ) ;
166- assert_eq_approx ! ( vec3swiz3, Vector3 :: new( 2.0 , 1.0 , 3.0 ) , ) ;
167- assert_eq_approx ! ( vec4swiz3, Vector3 :: new( 2.0 , 1.0 , 3.0 ) , ) ;
168-
169- // VectorN to Vector4
170- let vec2swiz4: Vector4 = swizzle ! ( vector2 => y, x, x, y) ;
171- let vec3swiz4: Vector4 = swizzle ! ( vector3 => y, x, z, y) ;
172- let vec4swiz4: Vector4 = swizzle ! ( vector4 => y, x, z, w) ;
173- assert_eq_approx ! ( vec2swiz4, Vector4 :: new( 2.0 , 1.0 , 1.0 , 2.0 ) , ) ;
174- assert_eq_approx ! ( vec3swiz4, Vector4 :: new( 2.0 , 1.0 , 3.0 , 2.0 ) , ) ;
175- assert_eq_approx ! ( vec4swiz4, Vector4 :: new( 2.0 , 1.0 , 3.0 , 4.0 ) , ) ;
176-
177- // * VectorNi swizzle
178- let vector2i = Vector2i :: new ( 1 , 2 ) ;
179- let vector3i = Vector3i :: new ( 1 , 2 , 3 ) ;
180- let vector4i = Vector4i :: new ( 1 , 2 , 3 , 4 ) ;
181-
182- // VectorNi to Vector2i
183- assert_eq ! ( Vector2i :: new( 2 , 1 ) , swizzle!( vector2i => y, x) ) ;
184- assert_eq ! ( swizzle!( vector3i => y, x) , Vector2i :: new( 2 , 1 ) ) ;
185- assert_eq ! ( swizzle!( vector4i => y, x) , Vector2i :: new( 2 , 1 ) ) ;
186-
187- // VectorNi to Vector3i
188- assert_eq ! ( swizzle!( vector2i => y, x, x) , Vector3i :: new( 2 , 1 , 1 ) ) ;
189- assert_eq ! ( swizzle!( vector3i => y, x, z) , Vector3i :: new( 2 , 1 , 3 ) ) ;
190- assert_eq ! ( swizzle!( vector4i => y, x, z) , Vector3i :: new( 2 , 1 , 3 ) ) ;
191-
192- // VectorNi to Vector4i
193- assert_eq ! ( swizzle!( vector2i => y, x, x, y) , Vector4i :: new( 2 , 1 , 1 , 2 ) ) ;
194- assert_eq ! ( swizzle!( vector3i => y, x, z, y) , Vector4i :: new( 2 , 1 , 3 , 2 ) ) ;
195- assert_eq ! ( swizzle!( vector4i => y, x, z, w) , Vector4i :: new( 2 , 1 , 3 , 4 ) ) ;
196- }
197- }
0 commit comments