| | 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 | | [CreateAssetMenu(fileName = NewPrefix + nameof(TemplScaffold), menuName = MenuName, order = 2)] |
| | 30 | | public class TemplScaffold : ScriptableObject |
| | 31 | | { |
| | 32 | | internal const string NameOfDefaultInput = nameof(defaultInput); |
| | 33 | | internal const string NameOfRoot = nameof(root); |
| | 34 | |
|
| | 35 | | protected const string NewPrefix = "New"; |
| | 36 | |
|
| | 37 | | private const string MenuName = "Templ/Scaffold Definition"; |
| | 38 | | private const string DefaultFileName = NewPrefix + "File"; |
| | 39 | | private const string DefaultDirectoryName = NewPrefix + "Directory"; |
| | 40 | |
|
| 1 | 41 | | private static readonly List<TemplScaffoldNode> EmptyList = new List<TemplScaffoldNode>(0); |
| | 42 | |
|
| | 43 | | internal event Action AfterReset; |
| | 44 | | internal event Action<IReadOnlyList<TemplScaffoldNode>> Changed; |
| | 45 | |
|
| | 46 | | [SerializeField] |
| | 47 | | protected ScriptableObject defaultInput; |
| | 48 | | [SerializeReference] |
| 130 | 49 | | private TemplScaffoldRoot root = GetNewRoot(); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Default input instance. |
| | 53 | | /// </summary> |
| 23 | 54 | | public ScriptableObject DefaultInput => defaultInput; |
| | 55 | |
|
| 124 | 56 | | internal virtual TemplScaffoldRoot Root => root; |
| 31 | 57 | | internal virtual bool IsValid => root.IsValid; |
| | 58 | |
|
| | 59 | | protected void Reset() |
| 121 | 60 | | { |
| 121 | 61 | | root = GetNewRoot(); |
| 121 | 62 | | defaultInput = null; |
| 121 | 63 | | AfterReset?.Invoke(); |
| 121 | 64 | | } |
| | 65 | |
|
| | 66 | | internal void AddScaffoldFileNodes(TemplScaffoldNode[] nodes) |
| 16 | 67 | | { |
| 16 | 68 | | nodes = nodes ?? throw new ArgumentNullException(nameof(nodes)); |
| 16 | 69 | | if (nodes.Length == 0) |
| 10 | 70 | | { |
| 10 | 71 | | nodes = new[] { root }; |
| 10 | 72 | | } |
| 16 | 73 | | var newNodes = nodes |
| 16 | 74 | | .Select(n => AddNode<TemplScaffoldFile>(n, DefaultFileName)) |
| | 75 | | .ToList(); |
| 16 | 76 | | Changed?.Invoke(newNodes); |
| 16 | 77 | | } |
| | 78 | |
|
| | 79 | | internal void AddScaffoldDirectoryNodes(TemplScaffoldNode[] nodes) |
| 19 | 80 | | { |
| 19 | 81 | | nodes = nodes ?? throw new ArgumentNullException(nameof(nodes)); |
| 19 | 82 | | if (nodes.Length == 0) |
| 18 | 83 | | { |
| 18 | 84 | | nodes = new[] { root }; |
| 18 | 85 | | } |
| 19 | 86 | | var newNodes = nodes |
| 19 | 87 | | .Select(n => AddNode<TemplScaffoldDirectory>(n, DefaultDirectoryName)) |
| | 88 | | .ToList(); |
| 19 | 89 | | Changed?.Invoke(newNodes); |
| 19 | 90 | | } |
| | 91 | |
|
| | 92 | | internal void RemoveScaffoldNodes(TemplScaffoldNode[] nodes) |
| 2 | 93 | | { |
| 2 | 94 | | nodes = nodes ?? throw new ArgumentNullException(nameof(nodes)); |
| 10 | 95 | | foreach (var node in nodes) |
| 2 | 96 | | { |
| 2 | 97 | | node.Parent?.RemoveChild(node); |
| 2 | 98 | | } |
| 2 | 99 | | Changed?.Invoke(EmptyList); |
| 2 | 100 | | } |
| | 101 | |
|
| | 102 | | internal void MoveScaffoldNodes( |
| | 103 | | TemplScaffoldNode parent, |
| | 104 | | int insertIndex, |
| | 105 | | TemplScaffoldNode[] draggedNodes) |
| 7 | 106 | | { |
| 7 | 107 | | parent ??= root; |
| 7 | 108 | | if (draggedNodes == null || draggedNodes.Length == 0) |
| 1 | 109 | | { |
| 1 | 110 | | return; |
| | 111 | | } |
| | 112 | |
|
| 6 | 113 | | if (insertIndex < 0) |
| 1 | 114 | | { |
| 1 | 115 | | throw new ArgumentException($"Invalid input: {nameof(insertIndex)} " + |
| | 116 | | "must not be negative"); |
| | 117 | | } |
| | 118 | |
|
| 5 | 119 | | var isRootDragged = draggedNodes |
| 5 | 120 | | .Any(n => n is TemplScaffoldRoot); |
| 5 | 121 | | if (isRootDragged) |
| 1 | 122 | | { |
| 1 | 123 | | throw new InvalidOperationException("Scaffold root node can not be reparented"); |
| | 124 | | } |
| | 125 | |
|
| 4 | 126 | | if (parent is TemplScaffoldFile) |
| 1 | 127 | | { |
| 1 | 128 | | throw new InvalidOperationException("Scaffold File nodes can not be parent nodes"); |
| | 129 | | } |
| | 130 | |
|
| 3 | 131 | | if (insertIndex > 0) |
| 2 | 132 | | { |
| 2 | 133 | | var subSet = new TemplScaffoldNode[insertIndex]; |
| 2 | 134 | | Array.Copy(parent.Children.ToArray(), subSet, insertIndex); |
| 1 | 135 | | insertIndex -= subSet.Count(draggedNodes.Contains); |
| 1 | 136 | | } |
| | 137 | |
|
| 2 | 138 | | parent.InsertChildrenRange(insertIndex, draggedNodes); |
| 2 | 139 | | Changed?.Invoke(draggedNodes); |
| 3 | 140 | | } |
| | 141 | |
|
| | 142 | | internal void CloneScaffoldNodes(TemplScaffoldNode[] nodes) |
| 4 | 143 | | { |
| 4 | 144 | | if (nodes == null || nodes.Length == 0) |
| 1 | 145 | | { |
| 1 | 146 | | return; |
| | 147 | | } |
| 3 | 148 | | var clones = nodes |
| 3 | 149 | | .Where(n => !(n is TemplScaffoldRoot)) |
| 2 | 150 | | .Select(n => n.Clone()) |
| | 151 | | .ToList(); |
| 3 | 152 | | Changed?.Invoke(clones); |
| 4 | 153 | | } |
| | 154 | |
|
| | 155 | | internal virtual bool ContainsTemplate(ScribanAsset template) => |
| 3 | 156 | | root.Children.Any(c => c.ContainsTemplate(template)); |
| | 157 | |
|
| | 158 | | private static TemplScaffoldRoot GetNewRoot() => |
| 251 | 159 | | new TemplScaffoldRoot() { name = nameof(Root) }; |
| | 160 | |
|
| | 161 | | private static TemplScaffoldNode AddNode<T>(TemplScaffoldNode node, string name) |
| | 162 | | where T : TemplScaffoldNode, new() |
| 35 | 163 | | { |
| 35 | 164 | | var current = node; |
| 35 | 165 | | if (node is TemplScaffoldFile) |
| 1 | 166 | | { |
| 1 | 167 | | current = node.Parent; |
| 1 | 168 | | } |
| | 169 | |
|
| 35 | 170 | | var newNode = new T() |
| | 171 | | { |
| | 172 | | name = name |
| | 173 | | }; |
| | 174 | |
|
| 35 | 175 | | current.AddChild(newNode); |
| 35 | 176 | | return newNode; |
| 35 | 177 | | } |
| | 178 | | } |
| | 179 | | } |