| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.ComponentModel.Composition;
- using Common.Helper;
- using Microsoft.Practices.Prism.Mvvm;
- using MongoDB.Bson.Serialization.Attributes;
- namespace Modules.BehaviorTreeModule
- {
- [BsonDiscriminator("TreeProto", RootClass = true)]
- [Export(typeof (TreeViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
- public class TreeViewModel: BindableBase, ICloneable, ISupportInitialize
- {
- [BsonId]
- private int id;
- [BsonElement]
- private int maxNodeId;
- [BsonElement]
- private TreeNodeViewModel root;
- [BsonIgnore]
- public ObservableCollection<TreeNodeViewModel> allNodes = new ObservableCollection<TreeNodeViewModel>();
- [BsonIgnore]
- public ObservableCollection<TreeNodeViewModel> AllNodes
- {
- get
- {
- return this.allNodes;
- }
- }
- [BsonIgnore]
- public int Id
- {
- get
- {
- return this.id;
- }
- set
- {
- if (this.id == value)
- {
- return;
- }
- this.id = value;
- this.OnPropertyChanged("id");
- }
- }
- [BsonIgnore]
- public string Comment
- {
- get
- {
- if (this.root == null)
- {
- return "";
- }
- return this.root.Comment;
- }
- set
- {
- this.OnPropertyChanged("Comment");
- }
- }
- [BsonIgnore]
- public int MaxNodeId
- {
- get
- {
- return this.maxNodeId;
- }
- set
- {
- this.maxNodeId = value;
- }
- }
- [BsonIgnore]
- public TreeNodeViewModel copy;
-
- [BsonIgnore]
- public TreeNodeViewModel Root
- {
- get
- {
- return this.root;
- }
- set
- {
- this.root = value;
- }
- }
- public void Add(TreeNodeViewModel treeNode, TreeNodeViewModel parent)
- {
- // 如果父节点是折叠的,需要先展开父节点
- if (parent != null)
- {
- if (parent.IsFold)
- {
- this.UnFold(parent);
- }
- treeNode.Parent = parent;
- parent.Children.Add(treeNode);
- }
- else
- {
- this.root = treeNode;
- }
- treeNode.TreeViewModel = this;
- allNodes.Add(treeNode);
- TreeLayout treeLayout = new TreeLayout(this);
- treeLayout.ExcuteLayout();
- }
- private void GetChildrenIdAndSelf(TreeNodeViewModel treeNodeViewModel, List<TreeNodeViewModel> children)
- {
- children.Add(treeNodeViewModel);
- this.GetAllChildrenId(treeNodeViewModel, children);
- }
- private void GetAllChildrenId(TreeNodeViewModel treeNodeViewModel, List<TreeNodeViewModel> children)
- {
- foreach (TreeNodeViewModel child in treeNodeViewModel.Children)
- {
- children.Add(child);
- this.GetAllChildrenId(child, children);
- }
- }
- public void Remove(TreeNodeViewModel treeNodeViewModel)
- {
- var all = new List<TreeNodeViewModel>();
- this.GetChildrenIdAndSelf(treeNodeViewModel, all);
- foreach (TreeNodeViewModel child in all)
- {
- this.allNodes.Remove(child);
- }
- TreeNodeViewModel parent = treeNodeViewModel.Parent;
- if (parent != null)
- {
- parent.Children.Remove(treeNodeViewModel);
- }
- TreeLayout treeLayout = new TreeLayout(this);
- treeLayout.ExcuteLayout();
- }
- private void RecursionMove(TreeNodeViewModel treeNodeViewModel, double offsetX, double offsetY)
- {
- treeNodeViewModel.XX += offsetX;
- treeNodeViewModel.YY += offsetY;
- foreach (TreeNodeViewModel child in treeNodeViewModel.Children)
- {
- this.RecursionMove(child, offsetX, offsetY);
- }
- }
- public void MoveToPosition(double offsetX, double offsetY)
- {
- this.RecursionMove(this.Root, offsetX, offsetY);
- }
- public void MoveToNode(TreeNodeViewModel from, TreeNodeViewModel to)
- {
- // from节点不能是to节点的父级节点
- TreeNodeViewModel tmpNode = to;
- while (tmpNode != null)
- {
- if (tmpNode.IsRoot)
- {
- break;
- }
- if (tmpNode.Id == from.Id)
- {
- return;
- }
- tmpNode = tmpNode.Parent;
- }
- if (from.IsFold)
- {
- this.UnFold(from);
- }
- if (to.IsFold)
- {
- this.UnFold(to);
- }
- from.Parent.Children.Remove(from);
- to.Children.Add(from);
- from.Parent = to;
- TreeLayout treeLayout = new TreeLayout(this);
- treeLayout.ExcuteLayout();
- }
- /// <summary>
- /// 折叠节点
- /// </summary>
- /// <param name="treeNodeViewModel"></param>
- public void Fold(TreeNodeViewModel treeNodeViewModel)
- {
- var allChild = new List<TreeNodeViewModel>();
- this.GetAllChildrenId(treeNodeViewModel, allChild);
- foreach (TreeNodeViewModel child in allChild)
- {
- this.allNodes.Remove(child);
- }
- treeNodeViewModel.IsFold = true;
- TreeLayout treeLayout = new TreeLayout(this);
- treeLayout.ExcuteLayout();
- }
- /// <summary>
- /// 展开节点,一级一级展开,一次只展开下层子节点,比如下层节点是折叠的,那下下层节点不展开
- /// </summary>
- /// <param name="treeNodeViewModel"></param>
- public void UnFold(TreeNodeViewModel treeNodeViewModel)
- {
- treeNodeViewModel.IsFold = false;
- var allChild = new List<TreeNodeViewModel>();
- this.GetAllChildrenId(treeNodeViewModel, allChild);
- foreach (TreeNodeViewModel child in allChild)
- {
- this.allNodes.Add(child);
- }
- TreeLayout treeLayout = new TreeLayout(this);
- treeLayout.ExcuteLayout();
- }
- public void MoveLeft(TreeNodeViewModel treeNodeViewModel)
- {
- if (treeNodeViewModel.IsRoot)
- {
- return;
- }
- TreeNodeViewModel parent = treeNodeViewModel.Parent;
- int index = parent.Children.IndexOf(treeNodeViewModel);
- if (index == 0)
- {
- return;
- }
- parent.Children.Remove(treeNodeViewModel);
- parent.Children.Insert(index - 1, treeNodeViewModel);
- TreeLayout treeLayout = new TreeLayout(this);
- treeLayout.ExcuteLayout();
- }
- public void MoveRight(TreeNodeViewModel treeNodeViewModel)
- {
- if (treeNodeViewModel.IsRoot)
- {
- return;
- }
- TreeNodeViewModel parent = treeNodeViewModel.Parent;
- int index = parent.Children.IndexOf(treeNodeViewModel);
- if (index == parent.Children.Count - 1)
- {
- return;
- }
- parent.Children.Remove(treeNodeViewModel);
- parent.Children.Insert(index + 1, treeNodeViewModel);
- TreeLayout treeLayout = new TreeLayout(this);
- treeLayout.ExcuteLayout();
- }
- public void Copy(TreeNodeViewModel copyTreeNodeViewModel)
- {
- this.copy = copyTreeNodeViewModel;
- }
- public void Paste(TreeNodeViewModel pasteTreeNodeViewModel)
- {
- if (this.copy == null)
- {
- return;
- }
- TreeNodeViewModel copyTreeNodeViewModel = this.copy;
- // copy节点不能是paste节点的父级节点
- TreeNodeViewModel tmpNode = pasteTreeNodeViewModel;
- while (tmpNode != null)
- {
- if (tmpNode.IsRoot)
- {
- break;
- }
- if (tmpNode.Id == copyTreeNodeViewModel.Id)
- {
- return;
- }
- tmpNode = tmpNode.Parent;
- }
- this.copy = null;
- this.CopyTree(copyTreeNodeViewModel, pasteTreeNodeViewModel);
- }
- private void CopyTree(TreeNodeViewModel copyTreeNodeViewModel, TreeNodeViewModel parent)
- {
- TreeNodeViewModel newTreeNodeViewModel = (TreeNodeViewModel)copyTreeNodeViewModel.Clone();
- newTreeNodeViewModel.Id = ++this.MaxNodeId;
- this.Add(newTreeNodeViewModel, parent);
- foreach (TreeNodeViewModel child in copyTreeNodeViewModel.Children)
- {
- this.CopyTree(child, newTreeNodeViewModel);
- }
- }
- public object Clone()
- {
- return MongoHelper.FromJson<TreeViewModel>(MongoHelper.ToJson(this));
- }
- private void SetChildParent(TreeNodeViewModel node)
- {
- if (node == null)
- {
- return;
- }
- node.TreeViewModel = this;
- allNodes.Add(node);
- foreach (TreeNodeViewModel child in node.Children)
- {
- child.Parent = node;
- SetChildParent(child);
- }
- }
- public void BeginInit()
- {
- }
- public void EndInit()
- {
- SetChildParent(Root);
- this.Root.XX = 450;
- this.Root.YY = 20;
- }
- }
- }
|