GuidStructureItem.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using PixiEditor.Helpers;
  7. namespace PixiEditor.Models.Layers
  8. {
  9. [DebuggerDisplay("{Name} - {GroupGuid}")]
  10. public class GuidStructureItem : NotifyableObject, ICloneable
  11. {
  12. public event EventHandler<GroupChangedEventArgs> GroupsChanged;
  13. public Guid GroupGuid { get; init; }
  14. private string name;
  15. public string Name
  16. {
  17. get => name;
  18. set
  19. {
  20. name = value;
  21. RaisePropertyChanged(nameof(Name));
  22. }
  23. }
  24. private Guid startLayerGuid;
  25. public Guid StartLayerGuid
  26. {
  27. get => startLayerGuid;
  28. set
  29. {
  30. startLayerGuid = value;
  31. RaisePropertyChanged(nameof(StartLayerGuid));
  32. }
  33. }
  34. private Guid endLayerGuid;
  35. public Guid EndLayerGuid
  36. {
  37. get => endLayerGuid;
  38. set
  39. {
  40. endLayerGuid = value;
  41. RaisePropertyChanged(nameof(EndLayerGuid));
  42. }
  43. }
  44. public ObservableCollection<GuidStructureItem> Subgroups { get; set; } = new ObservableCollection<GuidStructureItem>();
  45. public GuidStructureItem Parent { get; set; }
  46. private bool isExpanded;
  47. public bool IsExpanded
  48. {
  49. get => isExpanded;
  50. set
  51. {
  52. isExpanded = value;
  53. RaisePropertyChanged(nameof(IsExpanded));
  54. }
  55. }
  56. private bool isRenaming = false;
  57. public bool IsRenaming
  58. {
  59. get => isRenaming;
  60. set => SetProperty(ref isRenaming, value);
  61. }
  62. private bool isVisible = true;
  63. public bool IsVisible
  64. {
  65. get => isVisible;
  66. set => SetProperty(ref isVisible, value);
  67. }
  68. private float opacity = 1;
  69. public float Opacity
  70. {
  71. get => opacity;
  72. set => SetProperty(ref opacity, value);
  73. }
  74. public GuidStructureItem(
  75. string name,
  76. Guid startLayerGuid,
  77. Guid endLayerGuid,
  78. IEnumerable<GuidStructureItem> subgroups,
  79. GuidStructureItem parent)
  80. {
  81. Name = name;
  82. Subgroups = new ObservableCollection<GuidStructureItem>(subgroups);
  83. GroupGuid = Guid.NewGuid();
  84. Parent = parent;
  85. StartLayerGuid = startLayerGuid;
  86. EndLayerGuid = endLayerGuid;
  87. Subgroups.CollectionChanged += Subgroups_CollectionChanged;
  88. }
  89. public GuidStructureItem(string name, Guid layer)
  90. {
  91. Name = name;
  92. GroupGuid = Guid.NewGuid();
  93. Parent = null;
  94. StartLayerGuid = layer;
  95. EndLayerGuid = layer;
  96. Subgroups.CollectionChanged += Subgroups_CollectionChanged;
  97. }
  98. public override int GetHashCode()
  99. {
  100. return GroupGuid.GetHashCode();
  101. }
  102. public GuidStructureItem CloneGroup()
  103. {
  104. GuidStructureItem item = new(Name, StartLayerGuid, EndLayerGuid, Array.Empty<GuidStructureItem>(), null)
  105. {
  106. GroupGuid = GroupGuid,
  107. IsExpanded = isExpanded,
  108. IsRenaming = isRenaming
  109. };
  110. if(Subgroups.Count > 0)
  111. {
  112. item.Subgroups = new ObservableCollection<GuidStructureItem>();
  113. for (int i = 0; i < Subgroups.Count; i++)
  114. {
  115. item.Subgroups.Add(Subgroups[i].CloneGroup());
  116. item.Subgroups[^1].Parent = item;
  117. }
  118. }
  119. return item;
  120. }
  121. public object Clone()
  122. {
  123. return CloneGroup();
  124. }
  125. private void Subgroups_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  126. {
  127. var args = new GroupChangedEventArgs(e.NewItems != null ? e.NewItems.Cast<GuidStructureItem>().ToList() : new List<GuidStructureItem>());
  128. GroupsChanged?.Invoke(this, args);
  129. Parent?.GroupsChanged?.Invoke(this, args);
  130. }
  131. }
  132. }