// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#if USE_LARGE_WORLDS
using Real = System.Double;
#else
using Real = System.Single;
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using FlaxEditor.Modules;
using FlaxEditor.SceneGraph.Actors;
using FlaxEditor.Windows;
using FlaxEngine;
namespace FlaxEditor.SceneGraph
{
///
/// Base class for all leaf node objects which belong to scene graph used by the Editor.
/// Scene Graph is directional graph without cyclic references. It's a tree.
/// A class is responsible for Scene Graph management.
///
[HideInEditor]
public abstract class SceneGraphNode
{
///
/// The parent node.
///
protected SceneGraphNode parentNode;
///
/// Gets the children list.
///
public List ChildNodes { get; } = new List();
///
/// Initializes a new instance of the class.
///
/// The unique node identifier. Cannot be changed at runtime.
protected SceneGraphNode(Guid id)
{
ID = id;
SceneGraphFactory.Nodes.Add(id, this);
}
///
/// Gets the name.
///
public abstract string Name { get; }
///
/// Gets the identifier. Must be unique and immutable.
///
public Guid ID { get; }
///
/// Gets the parent scene.
///
public abstract SceneNode ParentScene { get; }
///
/// Gets the root node of the scene graph (if has).
///
public virtual RootNode Root => ParentNode?.Root;
///
/// Gets or sets the transform of the node.
///
public abstract Transform Transform { get; set; }
///
/// Gets a value indicating whether this instance can be copied or/and pasted.
///
public virtual bool CanCopyPaste => true;
///
/// Gets a value indicating whether this instance can be duplicated by the user.
///
public virtual bool CanDuplicate => true;
///
/// Gets a value indicating whether this node can be deleted by the user.
///
public virtual bool CanDelete => true;
///
/// Gets a value indicating whether this node can be dragged by the user.
///
public virtual bool CanDrag => true;
///
/// Gets a value indicating whether this node can be transformed by the user.
///
public virtual bool CanTransform => true;
///
/// Gets a value indicating whether this node can be used for the vertex snapping feature.
///
public bool CanVertexSnap
{
get
{
var v = Vector3.Zero;
return OnVertexSnap(ref v, out _);
}
}
///
/// Gets a value indicating whether this is active.
///
public abstract bool IsActive { get; }
///
/// Gets a value indicating whether this is active and all parent nodes are also active.
///
public abstract bool IsActiveInHierarchy { get; }
///
/// Gets or sets order of the node in the parent container.
///
public abstract int OrderInParent { get; set; }
///
/// Gets or sets the parent node.
///
public virtual SceneGraphNode ParentNode
{
get => parentNode;
set
{
if (parentNode != value)
{
parentNode?.ChildNodes.Remove(this);
parentNode = value;
parentNode?.ChildNodes.Add(this);
OnParentChanged();
}
}
}
///
/// Gets the object to edit via properties editor when this node is being selected.
///
public virtual object EditableObject => this;
///
/// Gets the object used to record undo changes.
///
public virtual object UndoRecordObject => EditableObject;
///
/// Determines whether the specified object is in a hierarchy (one of the children or lower).
///
/// The node to check,
/// True if given actor is part of the hierarchy, otherwise false.
public virtual bool ContainsInHierarchy(SceneGraphNode node)
{
if (ChildNodes.Contains(node))
return true;
return ChildNodes.Any(x => x.ContainsInHierarchy(node));
}
///
/// Determines whether the specified object is one of the children.
///
/// The node to check,
/// True if given object is a child, otherwise false.
public virtual bool ContainsChild(SceneGraphNode node)
{
return ChildNodes.Contains(node);
}
///
/// Adds the child node.
///
/// The node.
public void AddChild(SceneGraphNode node)
{
node.ParentNode = this;
}
///
/// The scene graph raycasting data container.
///
[HideInEditor]
public struct RayCastData
{
///
/// The raycasting optional flags.
///
[Flags, HideInEditor]
public enum FlagTypes
{
///
/// The none.
///
None = 0,
///
/// The skip colliders flag. Use it to ignore physics colliders intersections detection.
///
SkipColliders = 1,
///
/// The skip editor primitives. Use it to ignore editor icons and primitives intersections detection.
///
SkipEditorPrimitives = 2,
///
/// The skip trigger colliders flag. Use it to ignore physics trigger colliders intersections detection.
///
SkipTriggers = 4,
}
///
/// The ray (for intersection raycasting).
///
public Ray Ray;
///
/// The camera view ray (camera position and direction).
///
public Ray View;
///
/// The flags.
///
public FlagTypes Flags;
///
/// The list of objects to exclude from tracing against. Null if unused.
///
public List