//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System;
using System.Runtime.CompilerServices;
namespace BansheeEngine
{
/** @addtogroup Scene
* @{
*/
///
/// Base class for all components. Components represent primary logic elements in the scene. They are attached to
/// scene objects.
///
public class Component : GameObject
{
// Internal use only
protected internal Component()
{ }
///
/// Returns the scene object this component is attached to.
///
public SceneObject SceneObject
{
get { return Internal_GetSceneObject(mCachedPtr); }
}
///
/// Determines in which situations will OnTransformChanged be triggered.
///
protected TransformChangedFlags NotifyFlags
{
set { Internal_SetNotifyFlags(mCachedPtr, value); }
get { return Internal_GetNotifyFlags(mCachedPtr); }
}
///
/// Destroys the component, removing it from its scene object and stopping component updates.
///
/// If true the component will be fully destroyed immediately. This means that objects
/// that are still referencing this component might fail. Normally destruction is delayed
/// until the end of the frame to give other objects a chance to stop using it.
public void Destroy(bool immediate = false)
{
Internal_Destroy(mCachedPtr, immediate);
}
///
/// Calculates bounds of the visible content for this component.
///
/// Bounds in world space represented as an axis aligned bounding box.
/// Bounds in world space represented as a sphere.
/// True if the bounds have non-zero volume, false otherwise.
protected internal virtual bool CalculateBounds(out AABox box, out Sphere sphere)
{
Vector3 pos = SceneObject.Position;
box = new AABox(pos, pos);
sphere = new Sphere(pos, 0.0f);
return false;
}
///
/// Calls a parameterless method with the specified name, on the component.
///
/// Name of the method to call.
protected internal virtual void Invoke(string name)
{ }
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Component Internal_AddComponent(SceneObject parent, Type type);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Component Internal_GetComponent(SceneObject parent, Type type);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Component[] Internal_GetComponents(SceneObject parent);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Component[] Internal_GetComponentsPerType(SceneObject parent, Type type);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Component Internal_RemoveComponent(SceneObject parent, Type type);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern SceneObject Internal_GetSceneObject(IntPtr nativeInstance);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern TransformChangedFlags Internal_GetNotifyFlags(IntPtr nativeInstance);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Internal_SetNotifyFlags(IntPtr nativeInstance, TransformChangedFlags flags);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_Destroy(IntPtr nativeInstance, bool immediate);
}
///
/// Base class for custom component implementations.
///
/// Implementations of can implement a set of callbacks that will be called by the
/// runtime at specified occassions:
/// void OnCreate() - Called once when the component is instantiated.
/// void OnInitialize() - Called once when the component is first enabled. In case this is during instantiation, it is
/// called after OnCreate. Only called when the game is playing.
/// void OnUpdate() - Called every frame while the game is running and the component is enabled.
/// void OnEnable() - Called whenever a component is enabled, or instantiated as enabled in which case it is called
/// after OnInitialize. Only called when the game is playing.
/// void OnDisable() - Called whenever a component is disabled. This includes destruction where it is called before
/// OnDestroy. Only called when the game is playing.
/// void OnDestroy() - Called before the component is destroyed. Destruction is usually delayed until the end of the
/// current frame unless specified otherwise in a call to Destroy.
/// void OnReset() - Called when script assemblies have been refreshed or when the component is initialized. During
/// initialization it is called after OnInitialize but before OnEnable. Only relevant in editor.
/// void OnTransformChanged(TransformChangedFlags) - Called when the transform of the owning scene object changes.
/// When and if this gets triggered depends on
/// . Only called while game is
/// playing.
///
/// You can also make these callbacks trigger when the game is stopped/paused by using the
/// attribute on the component.
///
public class ManagedComponent : Component
{
protected ManagedComponent()
{ }
///
protected internal override void Invoke(string name)
{
Internal_Invoke(mCachedPtr, name);
}
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Internal_Invoke(IntPtr nativeInstance, string name);
}
/** @} */
}