| | 1 | | /* |
| | 2 | | * Copyright (c) 2024 Willy Alberto Kuster |
| | 3 | | * |
| | 4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| | 5 | | * of this software and associated documentation files (the "Software"), to deal |
| | 6 | | * in the Software without restriction, including without limitation the rights |
| | 7 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| | 8 | | * copies of the Software, and to permit persons to whom the Software is |
| | 9 | | * furnished to do so, subject to the following conditions: |
| | 10 | | * |
| | 11 | | * The above copyright notice and this permission notice shall be included in |
| | 12 | | * all copies or substantial portions of the Software. |
| | 13 | | * |
| | 14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| | 15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| | 16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| | 17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| | 18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| | 19 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| | 20 | | * THE SOFTWARE. |
| | 21 | | */ |
| | 22 | | using System; |
| | 23 | | using System.Collections.Generic; |
| | 24 | | using System.Linq; |
| | 25 | | using UnityEngine; |
| | 26 | |
|
| | 27 | | namespace Willykc.Templ.Editor.Scaffold |
| | 28 | | { |
| | 29 | | [Serializable] |
| | 30 | | internal abstract class TemplScaffoldNode |
| | 31 | | { |
| | 32 | | internal const string NameOfName = nameof(name); |
| | 33 | | internal const string NameOfChildren = nameof(children); |
| | 34 | |
|
| | 35 | | [SerializeField] |
| | 36 | | internal string name; |
| | 37 | | [SerializeReference] |
| 364 | 38 | | private List<TemplScaffoldNode> children = new List<TemplScaffoldNode>(); |
| | 39 | | [SerializeReference] |
| | 40 | | private TemplScaffoldNode parent; |
| | 41 | |
|
| 335 | 42 | | internal IReadOnlyList<TemplScaffoldNode> Children => children; |
| 101 | 43 | | internal TemplScaffoldNode Parent => parent; |
| 56 | 44 | | internal string NodePath => parent == null ? name : $"{parent.NodePath}/{name}"; |
| 245 | 45 | | internal int NodeCount => children.Sum(c => c.NodeCount) + 1; |
| 316 | 46 | | internal string RenderedName { get; set; } |
| | 47 | |
|
| | 48 | | internal bool IsValid => |
| 34 | 49 | | IsValidNode && |
| | 50 | | !IsNameDuplicated && |
| | 51 | | name.IsValidFileName() && |
| 3 | 52 | | children.All(c => c.IsValid); |
| | 53 | |
|
| 31 | 54 | | protected virtual bool IsValidNode => true; |
| | 55 | |
|
| | 56 | | private bool IsNameDuplicated => |
| 35 | 57 | | parent?.children.Any(c => c != this && c.name == name) ?? false; |
| | 58 | |
|
| | 59 | | internal void RemoveChild(TemplScaffoldNode node) => |
| 4 | 60 | | children.Remove(node); |
| | 61 | |
|
| | 62 | | internal void InsertChildrenRange(int insertIndex, IEnumerable<TemplScaffoldNode> nodes) |
| 2 | 63 | | { |
| 10 | 64 | | foreach (var child in nodes) |
| 2 | 65 | | { |
| 2 | 66 | | child.SwitchParent(this); |
| 2 | 67 | | } |
| | 68 | |
|
| 2 | 69 | | children.InsertRange(insertIndex, nodes); |
| 2 | 70 | | } |
| | 71 | |
|
| | 72 | | internal void AddChild(TemplScaffoldNode node) |
| 80 | 73 | | { |
| 80 | 74 | | if (!IsValidChild(node)) |
| 0 | 75 | | { |
| 0 | 76 | | throw new InvalidOperationException( |
| | 77 | | $"{node.GetType().Name} can not be a child of {GetType().Name}"); |
| | 78 | | } |
| | 79 | |
|
| 80 | 80 | | children.Add(node); |
| 80 | 81 | | node.parent = this; |
| 80 | 82 | | } |
| | 83 | |
|
| | 84 | | internal void AddChildrenRange(IEnumerable<TemplScaffoldNode> children) |
| 32 | 85 | | { |
| 186 | 86 | | foreach (var child in children) |
| 45 | 87 | | { |
| 45 | 88 | | AddChild(child); |
| 45 | 89 | | } |
| 32 | 90 | | } |
| | 91 | |
|
| | 92 | | internal TemplScaffoldNode Clone() |
| 2 | 93 | | { |
| 2 | 94 | | var clone = CloneRecursive(this, parent); |
| 2 | 95 | | parent?.children.Add(clone); |
| 2 | 96 | | return clone; |
| 2 | 97 | | } |
| | 98 | |
|
| | 99 | | internal bool ContainsTemplate(ScribanAsset template) |
| 2 | 100 | | { |
| 2 | 101 | | if (this is TemplScaffoldFile fileNode && fileNode.Template == template) |
| 1 | 102 | | { |
| 1 | 103 | | return true; |
| | 104 | | } |
| | 105 | |
|
| 2 | 106 | | return children.Any(c => c.ContainsTemplate(template)); |
| 2 | 107 | | } |
| | 108 | |
|
| | 109 | | protected abstract bool IsValidChild(TemplScaffoldNode value); |
| | 110 | | protected abstract TemplScaffoldNode DoClone(); |
| | 111 | |
|
| | 112 | | private void SwitchParent(TemplScaffoldNode newParent) |
| 2 | 113 | | { |
| 2 | 114 | | if (!newParent.IsValidChild(this)) |
| 0 | 115 | | { |
| 0 | 116 | | throw new InvalidOperationException( |
| | 117 | | $"{GetType().Name} can not be a child of {newParent.GetType().Name}"); |
| | 118 | | } |
| | 119 | |
|
| 2 | 120 | | parent.RemoveChild(this); |
| 2 | 121 | | parent = newParent; |
| 2 | 122 | | } |
| | 123 | |
|
| | 124 | | private TemplScaffoldNode CloneRecursive( |
| | 125 | | TemplScaffoldNode original, |
| | 126 | | TemplScaffoldNode parent) |
| 3 | 127 | | { |
| 3 | 128 | | var clone = DoClone(); |
| 3 | 129 | | clone.name = original.name; |
| 3 | 130 | | clone.parent = parent; |
| 4 | 131 | | clone.children.AddRange(original.children.Select(c => c.CloneRecursive(c, clone))); |
| 3 | 132 | | return clone; |
| 3 | 133 | | } |
| | 134 | | } |
| | 135 | | } |