2
0

Component.cs 5.5 KB

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