Component.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // Component C# sugar
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // Copyrigh 2015 Xamarin INc
  8. //
  9. using System.Linq;
  10. using System.Reflection;
  11. using Urho.Resources;
  12. namespace Urho
  13. {
  14. public partial class Component
  15. {
  16. bool subscribedToSceneUpdate;
  17. protected bool ReceiveSceneUpdates { get; set; }
  18. public T GetComponent<T> () where T : Component
  19. {
  20. return (T)Node.Components.FirstOrDefault(c => c is T);
  21. }
  22. public Application Application => Application.Current;
  23. public virtual void OnSerialize(IComponentSerializer serializer) { }
  24. public virtual void OnDeserialize(IComponentDeserializer deserializer) { }
  25. public virtual void OnAttachedToNode(Node node)
  26. {
  27. if (!subscribedToSceneUpdate && ReceiveSceneUpdates)
  28. {
  29. subscribedToSceneUpdate = true;
  30. Application.Update += HandleUpdate;
  31. }
  32. }
  33. protected override void OnDeleted()
  34. {
  35. if (subscribedToSceneUpdate)
  36. {
  37. Application.Update -= HandleUpdate;
  38. }
  39. base.OnDeleted();
  40. }
  41. /// <summary>
  42. /// Make sure you set SubscribeToSceneUpdate property to true in order to receive Update events
  43. /// </summary>
  44. protected virtual void OnUpdate(float timeStep) { }
  45. internal static bool IsDefinedInManagedCode<T>() => typeof(T).GetRuntimeProperty("TypeStatic") == null;
  46. void HandleUpdate(UpdateEventArgs args)
  47. {
  48. OnUpdate(args.TimeStep);
  49. }
  50. }
  51. }