Component.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 Urho.Resources;
  11. namespace Urho
  12. {
  13. public partial class Component
  14. {
  15. bool subscribedToSceneUpdate;
  16. protected bool ReceiveSceneUpdates { get; set; }
  17. public T GetComponent<T> () where T : Component
  18. {
  19. return (T)Node.Components.FirstOrDefault(c => c is T);
  20. }
  21. public Application Application => Application.Current;
  22. public virtual void OnSerialize(IComponentSerializer serializer) { }
  23. public virtual void OnDeserialize(IComponentDeserializer deserializer) { }
  24. public virtual void OnAttachedToNode(Node node)
  25. {
  26. if (!subscribedToSceneUpdate && ReceiveSceneUpdates)
  27. {
  28. subscribedToSceneUpdate = true;
  29. Application.Update += HandleUpdate;
  30. }
  31. }
  32. protected override void OnDeleted()
  33. {
  34. if (subscribedToSceneUpdate)
  35. {
  36. Application.Update -= HandleUpdate;
  37. }
  38. base.OnDeleted();
  39. }
  40. /// <summary>
  41. /// Make sure you set SubscribeToSceneUpdate property to true in order to receive Update events
  42. /// </summary>
  43. protected virtual void OnUpdate(float timeStep) { }
  44. void HandleUpdate(UpdateEventArgs args)
  45. {
  46. OnUpdate(args.TimeStep);
  47. }
  48. }
  49. }