fyrox_impl/scene/
pivot.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 simplest possible node which represents point in space.
22use crate::scene::node::constructor::NodeConstructor;
23use crate::{
24    core::{
25        math::aabb::AxisAlignedBoundingBox,
26        pool::Handle,
27        reflect::prelude::*,
28        type_traits::prelude::*,
29        uuid::{uuid, Uuid},
30        visitor::prelude::*,
31    },
32    scene::{
33        base::{Base, BaseBuilder},
34        graph::Graph,
35        node::{Node, NodeTrait},
36    },
37};
38use fyrox_graph::constructor::ConstructorProvider;
39use fyrox_graph::BaseSceneGraph;
40use std::ops::{Deref, DerefMut};
41
42/// A simplest possible node which represents point in space.
43#[derive(Clone, Reflect, Default, Debug, ComponentProvider)]
44pub struct Pivot {
45    base: Base,
46}
47
48impl Visit for Pivot {
49    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
50        self.base.visit(name, visitor)
51    }
52}
53
54impl Deref for Pivot {
55    type Target = Base;
56
57    fn deref(&self) -> &Self::Target {
58        &self.base
59    }
60}
61
62impl TypeUuidProvider for Pivot {
63    fn type_uuid() -> Uuid {
64        uuid!("dd2ecb96-b1f4-4ee0-943b-2a4d1844e3bb")
65    }
66}
67
68impl DerefMut for Pivot {
69    fn deref_mut(&mut self) -> &mut Self::Target {
70        &mut self.base
71    }
72}
73
74impl ConstructorProvider<Node, Graph> for Pivot {
75    fn constructor() -> NodeConstructor {
76        NodeConstructor::new::<Self>().with_variant("Pivot", |_| {
77            PivotBuilder::new(BaseBuilder::new().with_name("Pivot"))
78                .build_node()
79                .into()
80        })
81    }
82}
83
84impl NodeTrait for Pivot {
85    fn local_bounding_box(&self) -> AxisAlignedBoundingBox {
86        self.base.local_bounding_box()
87    }
88
89    fn world_bounding_box(&self) -> AxisAlignedBoundingBox {
90        self.base.world_bounding_box()
91    }
92
93    fn id(&self) -> Uuid {
94        Self::type_uuid()
95    }
96}
97
98/// Allows you to create pivot node in declarative manner.
99pub struct PivotBuilder {
100    base_builder: BaseBuilder,
101}
102
103impl PivotBuilder {
104    /// Creates new pivot builder.
105    pub fn new(base_builder: BaseBuilder) -> Self {
106        Self { base_builder }
107    }
108
109    /// Creates new Pivot node.
110    pub fn build_node(self) -> Node {
111        Node::new(Pivot {
112            base: self.base_builder.build_base(),
113        })
114    }
115
116    /// Creates new Pivot node and adds it to the graph.
117    pub fn build(self, graph: &mut Graph) -> Handle<Node> {
118        graph.add_node(self.build_node())
119    }
120}