TreeUseCases.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Terminal.Gui;
  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. // Init
  14. Application.Init ();
  15. // Setup - Create a top-level application window and configure it.
  16. Toplevel appWindow = new ();
  17. var menu = new MenuBar
  18. {
  19. Menus =
  20. [
  21. new MenuBarItem ("_File", new MenuItem [] { new ("_Quit", "", () => Quit ()) }),
  22. new MenuBarItem (
  23. "_Scenarios",
  24. new MenuItem []
  25. {
  26. new (
  27. "_Simple Nodes",
  28. "",
  29. () => LoadSimpleNodes ()
  30. ),
  31. new ("_Rooms", "", () => LoadRooms ()),
  32. new (
  33. "_Armies With Builder",
  34. "",
  35. () => LoadArmies (false)
  36. ),
  37. new (
  38. "_Armies With Delegate",
  39. "",
  40. () => LoadArmies (true)
  41. )
  42. }
  43. )
  44. ]
  45. };
  46. appWindow.Add (menu);
  47. var statusBar = new StatusBar ([new (Application.QuitKey, "Quit", Quit)]);
  48. appWindow.Add (statusBar);
  49. appWindow.Ready += (sender, args) =>
  50. // Start with the most basic use case
  51. LoadSimpleNodes ();
  52. // Run - Start the application.
  53. Application.Run (appWindow);
  54. appWindow.Dispose ();
  55. // Shutdown - Calling Application.Shutdown is required.
  56. Application.Shutdown ();
  57. }
  58. private void LoadArmies (bool useDelegate)
  59. {
  60. var army1 = new Army
  61. {
  62. Designation = "3rd Infantry",
  63. Units = new List<Unit> { new () { Name = "Orc" }, new () { Name = "Troll" }, new () { Name = "Goblin" } }
  64. };
  65. if (_currentTree != null)
  66. {
  67. Application.Top.Remove (_currentTree);
  68. _currentTree.Dispose ();
  69. }
  70. TreeView<GameObject> tree = new () { X = 0, Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1) };
  71. if (useDelegate)
  72. {
  73. tree.TreeBuilder = new DelegateTreeBuilder<GameObject> (
  74. o =>
  75. o is Army a
  76. ? a.Units
  77. : Enumerable.Empty<GameObject> ()
  78. );
  79. }
  80. else
  81. {
  82. tree.TreeBuilder = new GameObjectTreeBuilder ();
  83. }
  84. Application.Top.Add (tree);
  85. tree.AddObject (army1);
  86. _currentTree = tree;
  87. }
  88. private void LoadRooms ()
  89. {
  90. var myHouse = new House
  91. {
  92. Address = "23 Nowhere Street",
  93. Rooms = new List<Room>
  94. {
  95. new () { Name = "Ballroom" }, new () { Name = "Bedroom 1" }, new () { Name = "Bedroom 2" }
  96. }
  97. };
  98. if (_currentTree != null)
  99. {
  100. Application.Top.Remove (_currentTree);
  101. _currentTree.Dispose ();
  102. }
  103. var tree = new TreeView { X = 0, Y = 1, Width = Dim.Fill(), Height = Dim.Fill (1) };
  104. Application.Top.Add (tree);
  105. tree.AddObject (myHouse);
  106. _currentTree = tree;
  107. }
  108. private void LoadSimpleNodes ()
  109. {
  110. if (_currentTree != null)
  111. {
  112. Application.Top.Remove (_currentTree);
  113. _currentTree.Dispose ();
  114. }
  115. var tree = new TreeView { X = 0, Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1) };
  116. Application.Top.Add (tree);
  117. var root1 = new TreeNode ("Root1");
  118. root1.Children.Add (new TreeNode ("Child1.1"));
  119. root1.Children.Add (new TreeNode ("Child1.2"));
  120. var root2 = new TreeNode ("Root2");
  121. root2.Children.Add (new TreeNode ("Child2.1"));
  122. root2.Children.Add (new TreeNode ("Child2.2"));
  123. tree.AddObject (root1);
  124. tree.AddObject (root2);
  125. _currentTree = tree;
  126. }
  127. private void Quit () { Application.RequestStop (); }
  128. private class Army : GameObject
  129. {
  130. public string Designation { get; set; }
  131. public List<Unit> Units { get; set; }
  132. public override string ToString () { return Designation; }
  133. }
  134. private abstract class GameObject
  135. { }
  136. private class GameObjectTreeBuilder : ITreeBuilder<GameObject>
  137. {
  138. public bool SupportsCanExpand => true;
  139. public bool CanExpand (GameObject model) { return model is Army; }
  140. public IEnumerable<GameObject> GetChildren (GameObject model)
  141. {
  142. if (model is Army a)
  143. {
  144. return a.Units;
  145. }
  146. return Enumerable.Empty<GameObject> ();
  147. }
  148. }
  149. // Your data class
  150. private class House : TreeNode
  151. {
  152. // Your properties
  153. public string Address { get; set; }
  154. // ITreeNode member:
  155. public override IList<ITreeNode> Children => Rooms.Cast<ITreeNode> ().ToList ();
  156. public List<Room> Rooms { get; set; }
  157. public override string Text
  158. {
  159. get => Address;
  160. set => Address = value;
  161. }
  162. }
  163. private class Room : TreeNode
  164. {
  165. public string Name { get; set; }
  166. public override string Text
  167. {
  168. get => Name;
  169. set => Name = value;
  170. }
  171. }
  172. private class Unit : GameObject
  173. {
  174. public string Name { get; set; }
  175. public override string ToString () { return Name; }
  176. }
  177. }