Scene.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Urho.IO;
  4. using Urho.Resources;
  5. namespace Urho
  6. {
  7. partial class Scene
  8. {
  9. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  10. internal static extern bool Scene_LoadXMLFromCache(IntPtr handle, IntPtr cache, string file);
  11. public bool LoadXmlFromCache(ResourceCache cache, string file)
  12. {
  13. Runtime.ValidateRefCounted(this);
  14. return Scene_LoadXMLFromCache(handle, cache.Handle, file);
  15. }
  16. public bool LoadXml(string path)
  17. {
  18. Runtime.ValidateRefCounted(this);
  19. using (var file = new File(Context, path, FileMode.Read))
  20. {
  21. return LoadXml(file);
  22. }
  23. }
  24. public bool SaveXml(string path, string indentation = "\t")
  25. {
  26. Runtime.ValidateRefCounted(this);
  27. using (var file = new File(Context, path, FileMode.Write))
  28. {
  29. return SaveXml(file, indentation);
  30. }
  31. }
  32. unsafe partial void OnSceneCreated()
  33. {
  34. ComponentCloned += Scene_ComponentCloned;
  35. }
  36. void Scene_ComponentCloned(ComponentClonedEventArgs e)
  37. {
  38. if (e.CloneComponent.GetType() != e.Component.GetType())
  39. {
  40. // it means Component to clone is a managed subclass.
  41. // Let's wrap this Handle with that Managed class and re-register in the internal cache.
  42. var clonedManagedComponent = (Component)Activator.CreateInstance(e.Component.GetType(), e.CloneComponent.Handle);
  43. clonedManagedComponent.OnCloned(e.Scene, e.Component);
  44. Runtime.RegisterObject(clonedManagedComponent);
  45. }
  46. }
  47. }
  48. }