Window.cs 7.8 KB

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