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