Component.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Scene
  8. * @{
  9. */
  10. /// <summary>
  11. /// Base class for all components. Components represent primary logic elements in the scene. They are attached to
  12. /// scene objects.
  13. ///
  14. /// Implementations of <see cref="Component"/> can implement a set of callbacks that will be called by the runtime
  15. /// at specified occassions:
  16. /// void OnInitialize() - Called once when the component is instantiated. Only called when the game is playing.
  17. /// void OnUpdate() - Called every frame while the game is running and the component is enabled.
  18. /// void OnEnable() - Called whenever a component is enabled, or instantiated as enabled in which case it is called
  19. /// after OnInitialize. Only called when the game is playing.
  20. /// void OnDisable() - Called whenever a component is disabled. This includes destruction where it is called before
  21. /// OnDestroy. Only called when the game is playing.
  22. /// void OnDestroy() - Called before the component is destroyed. Destruction is usually delayed until the end of the
  23. /// current frame unless specified otherwise in a call to Destroy.
  24. /// void OnReset() - Called when script assemblies have been refreshed or when the component is initialized. During
  25. /// initialization it is called after OnInitialize but before OnEnable. Only relevant in editor.
  26. /// void OnTransformChanged(TransformChangedFlags) - Called when the transform of the owning scene object changes.
  27. /// When and if this gets triggered depends on
  28. /// <see cref="NotifyFlags"/>.
  29. ///
  30. /// You can also make these callbacks trigger when the game is stopped/paused by using the <see cref="RunInEditor"/>
  31. /// attribute on the component.
  32. /// </summary>
  33. public class Component : GameObject
  34. {
  35. // Internal use only
  36. protected Component()
  37. { }
  38. /// <summary>
  39. /// Returns the scene object this component is attached to.
  40. /// </summary>
  41. public SceneObject SceneObject
  42. {
  43. get { return Internal_GetSceneObject(mCachedPtr); }
  44. }
  45. /// <summary>
  46. /// Determines in which situations will OnTransformChanged be triggered.
  47. /// </summary>
  48. protected TransformChangedFlags NotifyFlags
  49. {
  50. set { Internal_SetNotifyFlags(mCachedPtr, value); }
  51. get { return Internal_GetNotifyFlags(mCachedPtr); }
  52. }
  53. /// <summary>
  54. /// Destroys the component, removing it from its scene object and stopping component updates.
  55. /// </summary>
  56. /// <param name="immediate">If true the component will be fully destroyed immediately. This means that objects
  57. /// that are still referencing this component might fail. Normally destruction is delayed
  58. /// until the end of the frame to give other objects a chance to stop using it.</param>
  59. public void Destroy(bool immediate = false)
  60. {
  61. Internal_Destroy(mCachedPtr, immediate);
  62. }
  63. /// <summary>
  64. /// Calculates bounds of the visible content for this component.
  65. /// </summary>
  66. /// <param name="box">Bounds in world space represented as an axis aligned bounding box.</param>
  67. /// <param name="sphere">Bounds in world space represented as a sphere.</param>
  68. /// <returns>True if the bounds have non-zero volume, false otherwise.</returns>
  69. protected internal virtual bool CalculateBounds(out AABox box, out Sphere sphere)
  70. {
  71. Vector3 pos = SceneObject.Position;
  72. box = new AABox(pos, pos);
  73. sphere = new Sphere(pos, 0.0f);
  74. return false;
  75. }
  76. /// <summary>
  77. /// Calls a parameterless method with the specified name, on the component.
  78. /// </summary>
  79. /// <param name="name">Name of the method to call.</param>
  80. protected internal void Invoke(string name)
  81. {
  82. Internal_Invoke(mCachedPtr, name);
  83. }
  84. [MethodImpl(MethodImplOptions.InternalCall)]
  85. internal static extern Component Internal_AddComponent(SceneObject parent, Type type);
  86. [MethodImpl(MethodImplOptions.InternalCall)]
  87. internal static extern Component Internal_GetComponent(SceneObject parent, Type type);
  88. [MethodImpl(MethodImplOptions.InternalCall)]
  89. internal static extern Component[] Internal_GetComponents(SceneObject parent);
  90. [MethodImpl(MethodImplOptions.InternalCall)]
  91. internal static extern Component[] Internal_GetComponentsPerType(SceneObject parent, Type type);
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. internal static extern Component Internal_RemoveComponent(SceneObject parent, Type type);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. internal static extern SceneObject Internal_GetSceneObject(IntPtr nativeInstance);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. internal static extern TransformChangedFlags Internal_GetNotifyFlags(IntPtr nativeInstance);
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. internal static extern void Internal_SetNotifyFlags(IntPtr nativeInstance, TransformChangedFlags flags);
  100. [MethodImpl(MethodImplOptions.InternalCall)]
  101. internal static extern void Internal_Invoke(IntPtr nativeInstance, string name);
  102. [MethodImpl(MethodImplOptions.InternalCall)]
  103. private static extern void Internal_Destroy(IntPtr nativeInstance, bool immediate);
  104. }
  105. /** @} */
  106. }