Skip to main content

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