TreeUseCases.cs 6.3 KB

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