GuidStructureItem.cs 4.9 KB

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