UserComponent.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Support for bubbling up to C# the virtual methods calls in LogicComponent
  3. //
  4. // This is done by using an ComponentProxy in C++ that bubbles up
  5. //
  6. using System;
  7. using System.Runtime.InteropServices;
  8. namespace Urho {
  9. /// <summary>
  10. /// Update mask used for UserComponent, to determine which methods to invoke
  11. /// </summary>
  12. [Flags]
  13. public enum ComponentEventMask : byte{
  14. Update = 1,
  15. PostUpdate = 2,
  16. FixedUpdate = 4,
  17. FixedPostUpdate = 8
  18. }
  19. public partial class UserComponent {
  20. [DllImport ("mono-urho", CallingConvention=CallingConvention.Cdecl)]
  21. extern static IntPtr ApplicationProxy_ApplicationProxy (IntPtr contextHandle, Action<IntPtr> setup, Action<IntPtr> start, Action<IntPtr> stop);
  22. public Application (Context context) : base (UrhoObjectFlag.Empty)
  23. {
  24. if (context == null)
  25. throw new ArgumentNullException ("context");
  26. handle = ApplicationProxy_ApplicationProxy (context.Handle, ProxySetup, ProxyStart, ProxyStop);
  27. Runtime.RegisterObject (this);
  28. }
  29. static UserComponent GetComponent (IntPtr handle)
  30. {
  31. return Runtime.LookupObject<UserComponent> (handle);
  32. }
  33. static void ProxyOnSetEnabled (IntPtr handle)
  34. {
  35. GetComponent (handle).OnSetEnabled ();
  36. }
  37. static void ProxyStart() (IntPtr handle)
  38. {
  39. GetComponent (handle).Start ();
  40. }
  41. static void ProxyDelayedStart() (IntPtr handle)
  42. {
  43. GetComponent (handle).DelayedStart ();
  44. }
  45. static void ProxyStop() (IntPtr handle)
  46. {
  47. GetComponent (handle).Stop ();
  48. }
  49. static void ProxyUpdate (IntPtr handle, float timeStep)
  50. {
  51. GetComponent (handle).Update (timeStep);
  52. }
  53. static void ProxyPostUpdate (IntPtr handle, float timeStep)
  54. {
  55. GetComponent (handle).PostUpdate (timeStep);
  56. }
  57. static void ProxyFixedUpdate (IntPtr handle, float timeStep)
  58. {
  59. GetComponent (handle).FixedUpdate (timeStep);
  60. }
  61. static void ProxyFixedPostUpdate (IntPtr handle, float timeStep)
  62. {
  63. GetComponent (handle).FixedPostUpdate (timeStep);
  64. }
  65. static void ProxyOnNodeSet (IntPtr handle, IntPtr node)
  66. {
  67. GetComponent (handle).OnNodeSet (Runtime.LookupObject<Node> (node));
  68. }
  69. /// <summary>Handle enabled/disabled state change. Changes update event subscription.</summary>
  70. public virtual void OnSetEnabled()
  71. {
  72. }
  73. /// <summary>Called when the component is added to a scene node. Other components may not yet exist.</summary>
  74. public virtual void Start()
  75. {
  76. }
  77. /// <summary>Called before the first update. At this
  78. /// point all other components of the node should
  79. /// exist. Will also be called if update events are
  80. /// not wanted; in that case the event is immediately
  81. /// unsubscribed afterward.</summary>
  82. public virtual void DelayedStart()
  83. {
  84. }
  85. /// <summary>Called when the component is detached
  86. /// from a scene node, usually on destruction. Note
  87. /// that you will no longer have access to the node
  88. /// and scene at that point.</summary>
  89. public virtual void Stop()
  90. {
  91. }
  92. /// <summary>Called on scene update, variable timestep.</summary>
  93. public virtual void Update(float timeStep)
  94. {
  95. }
  96. /// <summary>Called on scene post-update, variable timestep.</summary>
  97. public virtual void PostUpdate(float timeStep)
  98. {
  99. }
  100. /// <summary>Called on physics update, fixed timestep.</summary>
  101. public virtual void FixedUpdate(float timeStep)
  102. {
  103. }
  104. /// <summary>Called on physics post-update, fixed timestep.</summary>
  105. public virtual void FixedPostUpdate(float timeStep)
  106. {
  107. }
  108. public ComponentEventMask ComponentEventMask {
  109. get {
  110. return (ComponentEventMask) LogicComponent_GetUpdateEventMask (handle);
  111. }
  112. set {
  113. return LogicComponent_SetUpdateEventMask (handle, (byte) value);
  114. }
  115. }
  116. protected virtual void OnNodeSet (Node node)
  117. {
  118. }
  119. public bool DelayedStartCalled {
  120. get {
  121. return LogicComponent_IsDelayedStartCalled (handle);
  122. }
  123. }
  124. }
  125. }