123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.Linq;
- using PixiEditor.Helpers;
- using PixiEditor.ViewModels;
- namespace PixiEditor.Models.Layers
- {
- [DebuggerDisplay("{Name} - {GroupGuid}")]
- public class GuidStructureItem : NotifyableObject, ICloneable
- {
- public event EventHandler<GroupChangedEventArgs> GroupsChanged;
- public Guid GroupGuid { get; init; }
- private string name;
- public string Name
- {
- get => name;
- set
- {
- name = value;
- RaisePropertyChanged(nameof(Name));
- }
- }
- private Guid startLayerGuid;
- public Guid StartLayerGuid
- {
- get => startLayerGuid;
- set
- {
- startLayerGuid = value;
- RaisePropertyChanged(nameof(StartLayerGuid));
- }
- }
- private Guid endLayerGuid;
- public Guid EndLayerGuid
- {
- get => endLayerGuid;
- set
- {
- endLayerGuid = value;
- RaisePropertyChanged(nameof(EndLayerGuid));
- }
- }
- public ObservableCollection<GuidStructureItem> Subgroups { get; set; } = new ObservableCollection<GuidStructureItem>();
- public GuidStructureItem Parent { get; set; }
- private bool isExpanded;
- public bool IsExpanded
- {
- get => isExpanded;
- set => SetProperty(ref isExpanded, value);
- }
- private bool isRenaming = false;
- public bool IsRenaming
- {
- get => isRenaming;
- set => SetProperty(ref isRenaming, value);
- }
- private bool isVisible = true;
- public bool IsVisible
- {
- get => isVisible;
- set
- {
- if (SetProperty(ref isVisible, value))
- {
- ViewModelMain.Current.ToolsSubViewModel.TriggerCacheOutdated();
- }
- }
- }
- private float opacity = 1;
- public float Opacity
- {
- get => opacity;
- set
- {
- if (SetProperty(ref opacity, value))
- {
- ViewModelMain.Current.ToolsSubViewModel.TriggerCacheOutdated();
- }
- }
- }
- public GuidStructureItem(
- string name,
- Guid startLayerGuid,
- Guid endLayerGuid,
- IEnumerable<GuidStructureItem> subgroups,
- GuidStructureItem parent)
- {
- Name = name;
- Subgroups = new ObservableCollection<GuidStructureItem>(subgroups);
- GroupGuid = Guid.NewGuid();
- Parent = parent;
- StartLayerGuid = startLayerGuid;
- EndLayerGuid = endLayerGuid;
- Subgroups.CollectionChanged += Subgroups_CollectionChanged;
- }
- public GuidStructureItem(string name, Guid layer)
- {
- Name = name;
- GroupGuid = Guid.NewGuid();
- Parent = null;
- StartLayerGuid = layer;
- EndLayerGuid = layer;
- Subgroups.CollectionChanged += Subgroups_CollectionChanged;
- }
- public override int GetHashCode()
- {
- HashCode hc = default;
- hc.Add(GroupGuid);
- hc.Add(EndLayerGuid);
- hc.Add(StartLayerGuid);
- hc.Add(Opacity);
- hc.Add(IsVisible);
- hc.Add(IsExpanded);
- hc.Add(IsRenaming);
- hc.Add(Parent);
- hc.Add(Subgroups);
- return hc.ToHashCode();
- }
- public GuidStructureItem CloneGroup()
- {
- GuidStructureItem item = new(Name, StartLayerGuid, EndLayerGuid, Array.Empty<GuidStructureItem>(), null)
- {
- GroupGuid = GroupGuid,
- IsExpanded = isExpanded,
- IsRenaming = isRenaming,
- IsVisible = isVisible,
- Opacity = opacity
- };
- if(Subgroups.Count > 0)
- {
- item.Subgroups = new ObservableCollection<GuidStructureItem>();
- for (int i = 0; i < Subgroups.Count; i++)
- {
- item.Subgroups.Add(Subgroups[i].CloneGroup());
- item.Subgroups[^1].Parent = item;
- }
- }
- return item;
- }
- public object Clone()
- {
- return CloneGroup();
- }
- private void Subgroups_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- var args = new GroupChangedEventArgs(e.NewItems != null ? e.NewItems.Cast<GuidStructureItem>().ToList() : new List<GuidStructureItem>());
- GroupsChanged?.Invoke(this, args);
- Parent?.GroupsChanged?.Invoke(this, args);
- }
- }
- }
|