TreeUseCases.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("Tree View", "Simple tree view examples.")]
  6. [ScenarioCategory ("Controls")]
  7. [ScenarioCategory ("TreeView")]
  8. public class TreeUseCases : Scenario
  9. {
  10. private View? _currentTree;
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. Window appWindow = new ();
  15. // MenuBar
  16. MenuBar menu = new ();
  17. menu.Add (
  18. new MenuBarItem (
  19. "_File",
  20. [
  21. new MenuItem
  22. {
  23. Title = "_Quit",
  24. Action = Quit
  25. }
  26. ]
  27. )
  28. );
  29. menu.Add (
  30. new MenuBarItem (
  31. "_Scenarios",
  32. [
  33. new MenuItem
  34. {
  35. Title = "_Simple Nodes",
  36. Action = LoadSimpleNodes
  37. },
  38. new MenuItem
  39. {
  40. Title = "_Rooms",
  41. Action = LoadRooms
  42. },
  43. new MenuItem
  44. {
  45. Title = "_Armies With Builder",
  46. Action = () => LoadArmies (false)
  47. },
  48. new MenuItem
  49. {
  50. Title = "_Armies With Delegate",
  51. Action = () => LoadArmies (true)
  52. }
  53. ]
  54. )
  55. );
  56. // StatusBar
  57. StatusBar statusBar = new (
  58. [
  59. new (Application.QuitKey, "Quit", Quit)
  60. ]
  61. );
  62. appWindow.Add (menu, statusBar);
  63. appWindow.IsModalChanged += (sender, args) =>
  64. {
  65. if (args.Value)
  66. {
  67. // Start with the most basic use case
  68. LoadSimpleNodes ();
  69. }
  70. };
  71. Application.Run (appWindow);
  72. appWindow.Dispose ();
  73. Application.Shutdown ();
  74. }
  75. private void LoadArmies (bool useDelegate)
  76. {
  77. Army army1 = new ()
  78. {
  79. Designation = "3rd Infantry",
  80. Units = [new () { Name = "Orc" }, new () { Name = "Troll" }, new () { Name = "Goblin" }]
  81. };
  82. if (_currentTree is { })
  83. {
  84. if (Application.TopRunnableView is { })
  85. {
  86. Application.TopRunnableView.Remove (_currentTree);
  87. }
  88. _currentTree.Dispose ();
  89. }
  90. TreeView<GameObject> tree = new () { X = 0, Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1) };
  91. if (useDelegate)
  92. {
  93. tree.TreeBuilder = new DelegateTreeBuilder<GameObject> (
  94. o =>
  95. o is Army a && a.Units is { }
  96. ? a.Units
  97. : Enumerable.Empty<GameObject> ()
  98. );
  99. }
  100. else
  101. {
  102. tree.TreeBuilder = new GameObjectTreeBuilder ();
  103. }
  104. if (Application.TopRunnableView is { })
  105. {
  106. Application.TopRunnableView.Add (tree);
  107. }
  108. tree.AddObject (army1);
  109. _currentTree = tree;
  110. }
  111. private void LoadRooms ()
  112. {
  113. House myHouse = new ()
  114. {
  115. Address = "23 Nowhere Street",
  116. Rooms =
  117. [
  118. new () { Name = "Ballroom" },
  119. new () { Name = "Bedroom 1" },
  120. new () { Name = "Bedroom 2" }
  121. ]
  122. };
  123. if (_currentTree is { })
  124. {
  125. if (Application.TopRunnableView is { })
  126. {
  127. Application.TopRunnableView.Remove (_currentTree);
  128. }
  129. _currentTree.Dispose ();
  130. }
  131. TreeView tree = new () { X = 0, Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1) };
  132. if (Application.TopRunnableView is { })
  133. {
  134. Application.TopRunnableView.Add (tree);
  135. }
  136. tree.AddObject (myHouse);
  137. _currentTree = tree;
  138. }
  139. private void LoadSimpleNodes ()
  140. {
  141. if (_currentTree is { })
  142. {
  143. if (Application.TopRunnableView is { })
  144. {
  145. Application.TopRunnableView.Remove (_currentTree);
  146. }
  147. _currentTree.Dispose ();
  148. }
  149. TreeView tree = new () { X = 0, Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1) };
  150. if (Application.TopRunnableView is { })
  151. {
  152. Application.TopRunnableView.Add (tree);
  153. }
  154. TreeNode root1 = new ("Root1");
  155. root1.Children.Add (new TreeNode ("Child1.1"));
  156. root1.Children.Add (new TreeNode ("Child1.2"));
  157. TreeNode root2 = new ("Root2");
  158. root2.Children.Add (new TreeNode ("Child2.1"));
  159. root2.Children.Add (new TreeNode ("Child2.2"));
  160. tree.AddObject (root1);
  161. tree.AddObject (root2);
  162. _currentTree = tree;
  163. }
  164. private void Quit ()
  165. {
  166. Application.RequestStop ();
  167. }
  168. private class Army : GameObject
  169. {
  170. public string Designation { get; set; } = string.Empty;
  171. public List<Unit> Units { get; set; } = [];
  172. public override string ToString () { return Designation; }
  173. }
  174. private abstract class GameObject
  175. {
  176. }
  177. private class GameObjectTreeBuilder : ITreeBuilder<GameObject>
  178. {
  179. public bool SupportsCanExpand => true;
  180. public bool CanExpand (GameObject model) { return model is Army; }
  181. public IEnumerable<GameObject> GetChildren (GameObject model)
  182. {
  183. if (model is Army a && a.Units is { })
  184. {
  185. return a.Units;
  186. }
  187. return Enumerable.Empty<GameObject> ();
  188. }
  189. }
  190. private class House : TreeNode
  191. {
  192. public string Address { get; set; } = string.Empty;
  193. public override IList<ITreeNode> Children => Rooms.Cast<ITreeNode> ().ToList ();
  194. public List<Room> Rooms { get; set; } = [];
  195. public override string Text
  196. {
  197. get => Address;
  198. set => Address = value;
  199. }
  200. }
  201. private class Room : TreeNode
  202. {
  203. public string Name { get; set; } = string.Empty;
  204. public override string Text
  205. {
  206. get => Name;
  207. set => Name = value;
  208. }
  209. }
  210. private class Unit : GameObject
  211. {
  212. public string Name { get; set; } = string.Empty;
  213. public override string ToString () { return Name; }
  214. }
  215. }