Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. using System;
  2.  
  3. namespace GUIapp
  4. {
  5.  
  6. public abstract class GuiMenuCreator
  7. {
  8. public abstract GuiManager Instantiate(string option, System.Action exit);
  9. }
  10.  
  11. public class GuiConstructor : GuiMenuCreator
  12. {
  13. public override GuiManager Instantiate(string option, System.Action exit)
  14. {
  15. GuiManager guiManager = new GUIapp.GuiManager();
  16. switch (option)
  17. {
  18. default:
  19. {
  20. GuiElementCreator buttonConstructor = new ButtonConstructor();
  21. GuiElementCreator labelConstructor = new LabelConstructor();
  22. guiManager.elements = new List<GuiElement>();
  23. guiManager.elements.Add(new Clickable(labelConstructor.Instantiate("Click me new", new Point(0, 200), 10, Colour.Black), 100, 20, () => exit()));
  24. guiManager.elements.Add(new Clickable(labelConstructor.Instantiate("Click me", new Point(0, 100), 10, Colour.Black
  25. ), 100, 20, () => {
  26. guiManager.elements = new List<GuiElement>();
  27. guiManager.elements.Add(new Clickable(labelConstructor.Instantiate("Exit ExitExitExitExitExitExit", new Point(0, 0), 10, Colour.Black), 250, 20, () => {
  28. exit();
  29. }));
  30. }));
  31.  
  32. // guiManager.elements.Add(buttonConstructor.Instantiate("Click me", new Point(0, 100), 10, Colour.Black, 100, 30,
  33. // () => {
  34. // guiManager.elements = new List<GuiElement>();
  35. // guiManager.elements.Add(buttonConstructor.Instantiate("Exit", new Point(0, 0), 10, Colour.Black, 100, 30,
  36. // () => {
  37. // exit();
  38. // }
  39. // ));
  40. // }
  41. // ));
  42. break;
  43. }
  44. }
  45. return guiManager;
  46. }
  47. }
  48.  
  49. public abstract class GuiElementCreator
  50. {
  51. public abstract GuiElement Instantiate(string content, Point top_left_corner, int size, Colour color);
  52. public abstract GuiElement Instantiate(string text, Point top_left_corner, int size, Colour color, float width, float height, Action action);
  53. }
  54.  
  55. public interface GuiElement : Drawable, Updateable {
  56. Point Position();
  57. }
  58.  
  59. public abstract class GuiDecorator : GuiElement {
  60. protected GuiElement element;
  61.  
  62. public GuiDecorator(GuiElement element) {
  63. this.element = element;
  64. }
  65.  
  66. public Point Position() {
  67. return element.Position();
  68. }
  69.  
  70. public virtual void Draw(DrawVisitor visitor)
  71. {
  72. element.Draw(visitor);
  73. }
  74. public virtual void Update(UpdateVisitor visitor, float dt)
  75. {
  76. element.Update(visitor, dt);
  77. }
  78. }
  79.  
  80. public class Clickable : GuiDecorator {
  81. private GuiElementCreator buttonConstructor = new ButtonConstructor();
  82. private GuiElement button;
  83.  
  84. public Clickable(GuiElement element, float width, float height, Action action): base(element) {
  85.  
  86. this.button = buttonConstructor.Instantiate("", element.Position(), 10, Colour.Black, width, height, action);
  87. }
  88.  
  89.  
  90. public new Point Position() {
  91. return element.Position();
  92. }
  93. public override void Draw(DrawVisitor visitor)
  94. {
  95. button.Draw(visitor);
  96. element.Draw(visitor);
  97. }
  98. public override void Update(UpdateVisitor visitor, float dt)
  99. {
  100. button.Update(visitor, dt);
  101. element.Update(visitor, dt);
  102. }
  103. }
  104.  
  105. public class ButtonConstructor : GuiElementCreator
  106. {
  107. public override GuiElement Instantiate(string text, Point top_left_corner, int size, Colour color)
  108. {
  109. return new Button(text, top_left_corner, size, color, 40, 40, () => { });
  110. }
  111.  
  112. public override GuiElement Instantiate(string text, Point top_left_corner, int size, Colour color, float width, float height, Action action)
  113. {
  114. return new Button(text, top_left_corner, size, color, width, height, action);
  115. }
  116. }
  117. public class LabelConstructor : GuiElementCreator
  118. {
  119. public override GuiElement Instantiate(string text, Point top_left_corner, int size, Colour color)
  120. {
  121. return new Label(text, top_left_corner, size, color);
  122. }
  123.  
  124. public override GuiElement Instantiate(string text, Point top_left_corner, int size, Colour color, float width, float height, Action action)
  125. {
  126. return new Label(text, top_left_corner, size, color);
  127. }
  128. }
  129. public class Label : GuiElement
  130. {
  131. public string content;
  132.  
  133. public int size;
  134. public Colour color;
  135. public Point p;
  136. public Label(string content, Point top_left_corner, int size, Colour color)
  137. {
  138. this.size = size;
  139. this.color = color;
  140. this.content = content;
  141. this.p = top_left_corner;
  142. }
  143.  
  144. public Point Position() {
  145. return p;
  146. }
  147.  
  148. public void Draw(DrawVisitor visitor)
  149. {
  150. visitor.DrawLabel(this);
  151. }
  152. public void Update(UpdateVisitor visitor, float dt)
  153. {
  154. visitor.UpdateLabel(this, dt);
  155. }
  156.  
  157.  
  158. }
  159. public class Button : GuiElement
  160. {
  161. public float width, height;
  162. public Action action;
  163. public Colour color;
  164.  
  165. public Point p;
  166. public Button(string text, Point top_left_corner, int size, Colour color, float width, float height, Action action)
  167. {
  168. this.action = action;
  169. this.width = width;
  170. this.height = height;
  171. this.color = color;
  172. this.p = top_left_corner;
  173. }
  174.  
  175. public Point Position() {
  176. return this.p;
  177. }
  178. public void Draw(DrawVisitor visitor)
  179. {
  180. visitor.DrawButton(this);
  181. }
  182. public bool is_intersecting(Point point)
  183. {
  184. return point.X > p.X && point.Y > p.Y &&
  185. point.X < p.X + width && point.Y < p.Y + height;
  186. }
  187. public void Update(UpdateVisitor visitor, float dt)
  188. {
  189. visitor.UpdateButton(this, dt);
  190. }
  191.  
  192.  
  193. }
  194.  
  195.  
  196. public class GuiManager : Updateable, Drawable
  197. {
  198. public List<GuiElement> elements;
  199.  
  200. public void Draw(DrawVisitor visitor)
  201. {
  202. visitor.DrawGui(this);
  203. }
  204.  
  205. public void Update(UpdateVisitor visitor, float dt)
  206. {
  207. visitor.UpdateGui(this, dt);
  208. }
  209. }
  210.  
  211.  
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement