Window.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConFrames
  7. {
  8. public class Window
  9. {
  10. public Window(string title, Rect frameRectangle)
  11. {
  12. Title = title;
  13. FrameRectangle = frameRectangle;
  14. _clearAttributes = Attribute.Make(ConsoleColor.White, ConsoleColor.Blue);
  15. _needsFrameRender = true;
  16. _needsClientRender = true;
  17. }
  18. public int CursorX;
  19. public int CursorY;
  20. bool _needsFrameRender;
  21. bool _needsClientRender;
  22. bool _frameRenderedActive;
  23. Rect _frame;
  24. Desktop _manager;
  25. CharInfo[] _buf;
  26. // Current cursor position when this window is active
  27. public Point CursorPosition
  28. {
  29. get { return new Point(CursorX, CursorY); }
  30. set { CursorX = value.X; CursorY = value.Y; }
  31. }
  32. // Show the cursor?
  33. public bool CursorVisible
  34. {
  35. get;
  36. set;
  37. }
  38. // Default attributes to clear paint contexts with
  39. ushort _clearAttributes;
  40. public ushort ClearAttributes
  41. {
  42. get
  43. {
  44. return _clearAttributes;
  45. }
  46. set
  47. {
  48. _clearAttributes = value;
  49. }
  50. }
  51. // Window title
  52. string _title;
  53. public string Title
  54. {
  55. get
  56. {
  57. return _title;
  58. }
  59. set
  60. {
  61. _title = value;
  62. _needsFrameRender = true;
  63. if (_manager != null)
  64. _manager.Invalidate(this);
  65. }
  66. }
  67. // Window frame
  68. public Rect FrameRectangle
  69. {
  70. get
  71. {
  72. return _frame;
  73. }
  74. set
  75. {
  76. if (_frame == value)
  77. return;
  78. // Did it change size?
  79. if (_frame.Size != value.Size)
  80. {
  81. _needsClientRender = true;
  82. _needsFrameRender = true;
  83. _buf = null;
  84. }
  85. // Store new frame
  86. _frame = value;
  87. // Re-render desktop
  88. if (_manager != null)
  89. {
  90. _manager.InvalidateDesktop();
  91. _manager.Invalidate(this);
  92. }
  93. }
  94. }
  95. // Client size
  96. public Size ClientSize
  97. {
  98. get
  99. {
  100. return new Size(FrameRectangle.Width - 2, FrameRectangle.Height - 2);
  101. }
  102. }
  103. // Open the window and add to the desktop manager
  104. public void Open(ConFrames.Desktop manager)
  105. {
  106. if (_manager != null)
  107. throw new InvalidOperationException();
  108. _manager = manager;
  109. _manager.AddWindow(this);
  110. }
  111. // Close this window and remove from desktop
  112. public void Close()
  113. {
  114. if (_manager != null)
  115. {
  116. _manager.RemoveWindow(this);
  117. _manager = null;
  118. }
  119. }
  120. // Override to paint the context of this window
  121. public virtual void OnPaint(PaintContext ctx)
  122. {
  123. }
  124. // Override receive key events
  125. public virtual bool OnKey(ConsoleKeyInfo key)
  126. {
  127. return false;
  128. }
  129. // Force this window to repaint
  130. public void Invalidate()
  131. {
  132. _needsClientRender = true;
  133. if (_manager != null)
  134. _manager.Invalidate(this);
  135. }
  136. // Is this window active?
  137. public bool IsActive
  138. {
  139. get
  140. {
  141. if (_manager == null)
  142. return false;
  143. return _manager.ActiveWindow == this;
  144. }
  145. }
  146. // Activate this window
  147. public void Activate()
  148. {
  149. if (_manager == null)
  150. return;
  151. _manager.ActiveWindow = this;
  152. }
  153. // Get a paint context for this window
  154. public PaintContext GetPaintContext()
  155. {
  156. // Create buffer?
  157. if (_buf == null)
  158. {
  159. _buf = new CharInfo[FrameRectangle.Width * FrameRectangle.Height];
  160. }
  161. var ctx = new PaintContext(_buf, FrameRectangle.Size, new Rect(1, 1, FrameRectangle.Width - 2, FrameRectangle.Height - 2));
  162. ctx.Attributes = _clearAttributes;
  163. _manager.Invalidate(this);
  164. return ctx;
  165. }
  166. // Called by manager to draw this window - including frame
  167. public virtual CharInfo[] Draw()
  168. {
  169. // Create buffer?
  170. if (_buf == null)
  171. {
  172. _buf = new CharInfo[FrameRectangle.Width * FrameRectangle.Height];
  173. _needsFrameRender = true;
  174. _needsClientRender = true;
  175. }
  176. // Create paint context for the frame
  177. var px = new PaintContext(_buf, new Size(FrameRectangle.Width, FrameRectangle.Height), new Rect(0, 0, FrameRectangle.Width, FrameRectangle.Height));
  178. // Paint frame?
  179. if (_needsFrameRender || _frameRenderedActive != IsActive)
  180. {
  181. _frameRenderedActive = IsActive;
  182. // Draw border
  183. if (IsActive)
  184. {
  185. px.ForegroundColor = _manager.ActiveBorderLineColor;
  186. px.BackgroundColor = _manager.ActiveBorderBackgroundColor;
  187. }
  188. else
  189. {
  190. px.ForegroundColor = _manager.InactiveBorderLineColor;
  191. px.BackgroundColor = _manager.InactiveBorderBackgroundColor;
  192. }
  193. px.DrawBox(new Rect(0, 0, FrameRectangle.Width, FrameRectangle.Height), IsActive);
  194. // Draw title
  195. if (!string.IsNullOrEmpty(Title))
  196. {
  197. var title = " " + Title + " ";
  198. int titleX = (FrameRectangle.Width - title.Length) / 2;
  199. if (titleX < 2)
  200. titleX = 2;
  201. for (int i = 0; i < title.Length && i < FrameRectangle.Width - 4; i++)
  202. {
  203. _buf[titleX + i].Char = title[i];
  204. }
  205. }
  206. }
  207. // Paint client area
  208. if (_needsClientRender)
  209. {
  210. var ctx = new PaintContext(_buf, FrameRectangle.Size, new Rect(1, 1, FrameRectangle.Width - 2, FrameRectangle.Height - 2));
  211. ctx.Attributes = _clearAttributes;
  212. ctx.Clear();
  213. OnPaint(ctx);
  214. }
  215. _needsClientRender = false;
  216. _needsFrameRender = false;
  217. return _buf;
  218. }
  219. }
  220. }