AllTreeViewModel.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel.Composition;
  4. using System.IO;
  5. using Common.Helper;
  6. namespace Modules.BehaviorTreeModule
  7. {
  8. [Export(contractType: typeof (AllTreeViewModel)),
  9. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  10. public class AllTreeViewModel
  11. {
  12. public int MaxNodeId { get; set; }
  13. public int MaxTreeId { get; set; }
  14. private readonly Dictionary<int, TreeViewModel> treeViewModelsDict =
  15. new Dictionary<int, TreeViewModel>();
  16. public readonly ObservableCollection<TreeNodeViewModel> rootList =
  17. new ObservableCollection<TreeNodeViewModel>();
  18. public ObservableCollection<TreeNodeViewModel> RootList
  19. {
  20. get
  21. {
  22. return this.rootList;
  23. }
  24. }
  25. public void Open(string file)
  26. {
  27. this.rootList.Clear();
  28. this.treeViewModelsDict.Clear();
  29. var treeDict = new Dictionary<int, List<TreeNodeData>>();
  30. byte[] bytes = File.ReadAllBytes(file);
  31. var allTreeData = ProtobufHelper.FromBytes<AllTreeData>(bytes);
  32. this.MaxNodeId = 0;
  33. this.MaxTreeId = 0;
  34. foreach (TreeNodeData treeNodeData in allTreeData.TreeNodeDatas)
  35. {
  36. List<TreeNodeData> tree;
  37. treeDict.TryGetValue(treeNodeData.TreeId, out tree);
  38. if (tree == null)
  39. {
  40. tree = new List<TreeNodeData>();
  41. treeDict[treeNodeData.TreeId] = tree;
  42. }
  43. tree.Add(treeNodeData);
  44. if (treeNodeData.Id > this.MaxNodeId)
  45. {
  46. this.MaxNodeId = treeNodeData.Id;
  47. }
  48. if (treeNodeData.TreeId > this.MaxTreeId)
  49. {
  50. this.MaxTreeId = treeNodeData.TreeId;
  51. }
  52. }
  53. foreach (KeyValuePair<int, List<TreeNodeData>> pair in treeDict)
  54. {
  55. var treeViewModel = new TreeViewModel(pair.Value) { AllTreeViewModel = this, TreeId = pair.Key };
  56. this.treeViewModelsDict[pair.Key] = treeViewModel;
  57. this.RootList.Add(treeViewModel.Root);
  58. }
  59. }
  60. public void Save(string file)
  61. {
  62. AllTreeData allTreeData = new AllTreeData();
  63. foreach (var value in this.treeViewModelsDict.Values)
  64. {
  65. List<TreeNodeData> list = value.GetDatas();
  66. allTreeData.TreeNodeDatas.AddRange(list);
  67. }
  68. using (Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write))
  69. {
  70. byte[] bytes = ProtobufHelper.ToBytes(allTreeData);
  71. stream.Write(bytes, 0, bytes.Length);
  72. }
  73. }
  74. public void New(TreeViewModel treeViewModel)
  75. {
  76. }
  77. public void Add(TreeViewModel treeViewModel)
  78. {
  79. this.treeViewModelsDict[treeViewModel.TreeId] = treeViewModel;
  80. this.rootList.Add(treeViewModel.Root);
  81. }
  82. public void Remove(TreeNodeViewModel treeViewModel)
  83. {
  84. this.treeViewModelsDict.Remove(treeViewModel.TreeId);
  85. this.rootList.Remove(treeViewModel);
  86. }
  87. public TreeViewModel Get(int treeId)
  88. {
  89. return this.treeViewModelsDict[treeId];
  90. }
  91. }
  92. }