Component.cs 6.5 KB

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