0
|
1 using UnityEngine;
|
|
2 using System.Collections;
|
|
3 using System.Collections.Generic;
|
|
4 public class DefaultTreeEditor : TreeEditor {
|
|
5 private Traverser traverser;
|
|
6 public DefaultTreeEditor(Traverser traverser){
|
|
7 this.traverser = traverser;
|
|
8 }
|
|
9
|
|
10 public Either<Error,LoggingNode> edit(TreeNode root,NodePath path, NodeEditor editor){
|
|
11 DefaultEvaluator e = new DefaultEvaluator (path);
|
|
12 Either<Error, Traversal> either = traverser.traverse (root, e);
|
|
13
|
|
14 if (either.isA ()) {
|
|
15 return DefaultEither<Error, LoggingNode>.newA (either.a ());
|
|
16 }
|
|
17 Traversal t = either.b ();
|
|
18 return clone (t, editor);
|
|
19 }
|
|
20
|
|
21 private Either<Error, LoggingNode> clone(Traversal t, NodeEditor editor){
|
|
22 List<Direction<TreeNode>> path = new List<Direction<TreeNode>> ();
|
|
23
|
|
24 foreach (Direction<TreeNode> direction in t) {
|
|
25 path = path.addLast (direction);
|
|
26 }
|
|
27
|
|
28 Direction<TreeNode> targetDirection = path.headList ();
|
|
29 TreeNode target = targetDirection.getTarget ();
|
|
30 Either<Error, LoggingNode> either = editor.edit (target);
|
|
31 if (either.isA ()) {
|
|
32 return DefaultEither<Error, LoggingNode>.newA (either.a ());
|
|
33 }
|
|
34
|
|
35 LoggingNode newWrap = either.b ();
|
|
36
|
|
37 int pos = targetDirection.getPosition ();
|
|
38 TreeNode child = newWrap.getWrap ();
|
|
39
|
|
40 foreach (Direction<TreeNode> parentDirection in path.deleteHead()) {
|
|
41 TreeNodeChildren chs = parentDirection.getTarget ().getChildren ();
|
|
42
|
|
43 Either<Error, TreeNode> ret = chs.replaceNode (pos, child);
|
|
44 if (ret.isA ()) {
|
|
45 return DefaultEither<Error, LoggingNode>.newA (ret.a ());
|
|
46 }
|
|
47
|
|
48 child = ret.b ();
|
|
49 pos = parentDirection.getPosition ();
|
|
50 }
|
|
51
|
|
52 TreeNode newRoot = child;
|
|
53 LoggingNode logNode = editor.wrap (newRoot, newWrap.getOperationLog ());
|
|
54 return DefaultEither<Error, LoggingNode>.newB (logNode);
|
|
55
|
|
56 }
|
|
57
|
|
58 }
|