// Copyright (c) 2012-2020 Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using FlaxEditor.Modules;
using FlaxEditor.SceneGraph.Actors;
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 : ITransformable
{
///
/// 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;
///
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 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 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)
{
if (parentNode != null)
{
parentNode.ChildNodes.Remove(this);
}
parentNode = value;
if (parentNode != null)
{
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.
///
public struct RayCastData
{
///
/// The raycasting optional flags.
///
[Flags]
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 ray.
///
public Ray Ray;
///
/// The camera view ray.
///
public Ray View;
///
/// The flags.
///
public FlagTypes Flags;
}
///
/// Performs raycasting over child nodes hierarchy trying to get the closest object hit by the given ray.
///
/// The ray casting data.
/// The result distance.
/// The result intersection surface normal vector.
/// Hit object or null if there is no intersection at all.
public virtual SceneGraphNode RayCastChildren(ref RayCastData ray, out float distance, out Vector3 normal)
{
if (!IsActive)
{
distance = 0;
normal = Vector3.Up;
return null;
}
SceneGraphNode minTarget = null;
float minDistance = float.MaxValue;
Vector3 minDistanceNormal = Vector3.Up;
// Check all children
for (int i = 0; i < ChildNodes.Count; i++)
{
var hit = ChildNodes[i].RayCast(ref ray, out distance, out normal);
if (hit != null && distance <= minDistance)
{
minTarget = hit;
minDistance = distance;
minDistanceNormal = normal;
}
}
// Return result
distance = minDistance;
normal = minDistanceNormal;
return minTarget;
}
///
/// Performs raycasting over nodes hierarchy trying to get the closest object hit by the given ray.
///
/// The ray casting data.
/// The result distance.
/// The result intersection surface normal vector.
/// Hit object or null if there is no intersection at all.
public virtual SceneGraphNode RayCast(ref RayCastData ray, out float distance, out Vector3 normal)
{
if (!IsActive)
{
distance = 0;
normal = Vector3.Up;
return null;
}
// Check itself
SceneGraphNode minTarget = null;
float minDistance = float.MaxValue;
Vector3 minDistanceNormal = Vector3.Up;
if (RayCastSelf(ref ray, out distance, out normal))
{
minTarget = this;
minDistance = distance;
minDistanceNormal = normal;
}
// Check all children
for (int i = 0; i < ChildNodes.Count; i++)
{
var hit = ChildNodes[i].RayCast(ref ray, out distance, out normal);
if (hit != null && distance <= minDistance)
{
minTarget = hit;
minDistance = distance;
minDistanceNormal = normal;
}
}
// Return result
distance = minDistance;
normal = minDistanceNormal;
return minTarget;
}
///
/// Checks if given ray intersects with the node.
///
/// The ray casting data.
/// The distance.
/// The result intersection surface normal vector.
/// True ray hits this node, otherwise false.
public virtual bool RayCastSelf(ref RayCastData ray, out float distance, out Vector3 normal)
{
distance = 0;
normal = Vector3.Up;
return false;
}
///
/// Called when selected nodes should draw debug shapes using interface.
///
/// The debug draw data.
public virtual void OnDebugDraw(ViewportDebugDrawData data)
{
}
///
/// Deletes object represented by this node eg. actor.
///
public virtual void Delete()
{
}
///
/// Releases the node and the child tree. Disposed all GUI parts and used resources.
///
public virtual void Dispose()
{
OnDispose();
// Unlink from the parent
if (parentNode != null)
{
parentNode.ChildNodes.Remove(this);
parentNode = null;
}
}
///
/// Called when node or parent node is disposing.
///
public virtual void OnDispose()
{
// Call deeper
for (int i = 0; i < ChildNodes.Count; i++)
{
ChildNodes[i].OnDispose();
}
SceneGraphFactory.Nodes.Remove(ID);
}
///
/// Called when parent node gets changed.
///
protected virtual void OnParentChanged()
{
}
}
}