TreeUseCases.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.Ready += (sender, args) =>
  64. {
  65. // Start with the most basic use case
  66. LoadSimpleNodes ();
  67. };
  68. Application.Run (appWindow);
  69. appWindow.Dispose ();
  70. Application.Shutdown ();
  71. }
  72. private void LoadArmies (bool useDelegate)
  73. {
  74. Army army1 = new ()
  75. {
  76. Designation = "3rd Infantry",
  77. Units = [new () { Name = "Orc" }, new () { Name = "Troll" }, new () { Name = "Goblin" }]
  78. };
  79. if (_currentTree is { })
  80. {
  81. if (Application.TopRunnable is { })
  82. {
  83. Application.TopRunnable.Remove (_currentTree);
  84. }
  85. _currentTree.Dispose ();
  86. }
  87. TreeView<GameObject> tree = new () { X = 0, Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1) };
  88. if (useDelegate)
  89. {
  90. tree.TreeBuilder = new DelegateTreeBuilder<GameObject> (
  91. o =>
  92. o is Army a && a.Units is { }
  93. ? a.Units
  94. : Enumerable.Empty<GameObject> ()
  95. );
  96. }
  97. else
  98. {
  99. tree.TreeBuilder = new GameObjectTreeBuilder ();
  100. }
  101. if (Application.TopRunnable is { })
  102. {
  103. Application.TopRunnable.Add (tree);
  104. }
  105. tree.AddObject (army1);
  106. _currentTree = tree;
  107. }
  108. private void LoadRooms ()
  109. {
  110. House myHouse = new ()
  111. {
  112. Address = "23 Nowhere Street",
  113. Rooms =
  114. [
  115. new () { Name = "Ballroom" },
  116. new () { Name = "Bedroom 1" },
  117. new () { Name = "Bedroom 2" }
  118. ]
  119. };
  120. if (_currentTree is { })
  121. {
  122. if (Application.TopRunnable is { })
  123. {
  124. Application.TopRunnable.Remove (_currentTree);
  125. }
  126. _currentTree.Dispose ();
  127. }
  128. TreeView tree = new () { X = 0, Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1) };
  129. if (Application.TopRunnable is { })
  130. {
  131. Application.TopRunnable.Add (tree);
  132. }
  133. tree.AddObject (myHouse);
  134. _currentTree = tree;
  135. }
  136. private void LoadSimpleNodes ()
  137. {
  138. if (_currentTree is { })
  139. {
  140. if (Application.TopRunnable is { })
  141. {
  142. Application.TopRunnable.Remove (_currentTree);
  143. }
  144. _currentTree.Dispose ();
  145. }
  146. TreeView tree = new () { X = 0, Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1) };
  147. if (Application.TopRunnable is { })
  148. {
  149. Application.TopRunnable.Add (tree);
  150. }
  151. TreeNode root1 = new ("Root1");
  152. root1.Children.Add (new TreeNode ("Child1.1"));
  153. root1.Children.Add (new TreeNode ("Child1.2"));
  154. TreeNode root2 = new ("Root2");
  155. root2.Children.Add (new TreeNode ("Child2.1"));
  156. root2.Children.Add (new TreeNode ("Child2.2"));
  157. tree.AddObject (root1);
  158. tree.AddObject (root2);
  159. _currentTree = tree;
  160. }
  161. private void Quit ()
  162. {
  163. Application.RequestStop ();
  164. }
  165. private class Army : GameObject
  166. {
  167. public string Designation { get; set; } = string.Empty;
  168. public List<Unit> Units { get; set; } = [];
  169. public override string ToString () { return Designation; }
  170. }
  171. private abstract class GameObject
  172. {
  173. }
  174. private class GameObjectTreeBuilder : ITreeBuilder<GameObject>
  175. {
  176. public bool SupportsCanExpand => true;
  177. public bool CanExpand (GameObject model) { return model is Army; }
  178. public IEnumerable<GameObject> GetChildren (GameObject model)
  179. {
  180. if (model is Army a && a.Units is { })
  181. {
  182. return a.Units;
  183. }
  184. return Enumerable.Empty<GameObject> ();
  185. }
  186. }
  187. private class House : TreeNode
  188. {
  189. public string Address { get; set; } = string.Empty;
  190. public override IList<ITreeNode> Children => Rooms.Cast<ITreeNode> ().ToList ();
  191. public List<Room> Rooms { get; set; } = [];
  192. public override string Text
  193. {
  194. get => Address;
  195. set => Address = value;
  196. }
  197. }
  198. private class Room : TreeNode
  199. {
  200. public string Name { get; set; } = string.Empty;
  201. public override string Text
  202. {
  203. get => Name;
  204. set => Name = value;
  205. }
  206. }
  207. private class Unit : GameObject
  208. {
  209. public string Name { get; set; } = string.Empty;
  210. public override string ToString () { return Name; }
  211. }
  212. }