PaintContext.cs 8.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 PaintContext
  23. {
  24. public PaintContext(CharInfo[] buf, Size bufSize, Rect drawRect)
  25. {
  26. _x = 0;
  27. _y = 0;
  28. _buf = buf;
  29. _bufSize = bufSize;
  30. _drawRect = drawRect;
  31. _clipRect = drawRect;
  32. _clipRect.Intersect(new Rect(0, 0, bufSize.Width, bufSize.Height));
  33. _baseOffset = _clipRect.Top * bufSize.Width + _clipRect.Left;
  34. }
  35. public PaintContext(PaintContext underlying, Rect drawRect)
  36. {
  37. _buf = underlying._buf;
  38. _bufSize = underlying._bufSize;
  39. _drawRect = drawRect;
  40. _clipRect = drawRect;
  41. _clipRect.Intersect(underlying._clipRect);
  42. _clipRect.Left += underlying._clipRect.Left;
  43. _clipRect.Top += underlying._clipRect.Top;
  44. _clipRect.Intersect(new Rect(0, 0, _bufSize.Width, _bufSize.Height));
  45. _baseOffset = _clipRect.Top * _bufSize.Width + _clipRect.Left;
  46. }
  47. CharInfo[] _buf; // The buffer to draw to
  48. Size _bufSize; // Size of the buffer
  49. Rect _drawRect; // Drawing rectangle
  50. Rect _clipRect; // Clip rectangle
  51. int _baseOffset; // Origin of first character
  52. int _x; // Current output position
  53. int _y; // Current output position
  54. // Left margin for indent text writes
  55. public int LeftMargin
  56. {
  57. get;
  58. set;
  59. }
  60. // Whether to word wrap output text or not
  61. public bool WordWrap
  62. {
  63. get;
  64. set;
  65. }
  66. // Foreground color
  67. ushort _attributes;
  68. public ConsoleColor ForegroundColor
  69. {
  70. get { return (ConsoleColor)(_attributes & 0x0F); }
  71. set { _attributes = (ushort)((_attributes & 0xF0) | (ushort)value); }
  72. }
  73. // Background color
  74. public ConsoleColor BackgroundColor
  75. {
  76. get { return (ConsoleColor)((_attributes & 0xF0) >> 4); }
  77. set { _attributes = (ushort)((_attributes & 0x0F) | ((ushort)value) << 4); }
  78. }
  79. // Current fore/back color attributes
  80. public ushort Attributes
  81. {
  82. get
  83. {
  84. return _attributes;
  85. }
  86. set
  87. {
  88. _attributes = value;
  89. }
  90. }
  91. // Clear to end of line when outputting a carriage return
  92. bool _clearLineOnReturn;
  93. public bool ClearLineOnReturn
  94. {
  95. get { return _clearLineOnReturn; }
  96. set { _clearLineOnReturn = value; }
  97. }
  98. // Write text at the current position (and update position)
  99. public void Write(string str)
  100. {
  101. for (int i = 0; i < str.Length; i++)
  102. {
  103. if (str[i] == '\r')
  104. {
  105. // Clear to eol
  106. if (_clearLineOnReturn)
  107. {
  108. if (_y>=0 && _y<_clipRect.Height)
  109. {
  110. int pos = _baseOffset + _bufSize.Width * _y + _x;
  111. for (int x = _x; x < _clipRect.Width; x++, pos++)
  112. {
  113. _buf[pos].Char = ' ';
  114. _buf[pos].Attributes = _attributes;
  115. }
  116. }
  117. }
  118. _x = LeftMargin;
  119. }
  120. else if (str[i] == '\n')
  121. {
  122. _y++;
  123. }
  124. else
  125. {
  126. if (_x >= 0 && _y >= 0 && _x < _clipRect.Width && _y < _clipRect.Height)
  127. {
  128. int pos = _baseOffset + _bufSize.Width * _y + _x;
  129. _buf[pos].Char = str[i];
  130. _buf[pos].Attributes = _attributes;
  131. }
  132. _x++;
  133. if (WordWrap && _x >= _clipRect.Width)
  134. {
  135. _x = LeftMargin;
  136. _y++;
  137. }
  138. }
  139. }
  140. }
  141. // Write string and a carriage return
  142. public void WriteLine(string str="")
  143. {
  144. Write(str);
  145. Write("\r\n");
  146. }
  147. // Write formatted text
  148. public void Write(string str, params object[] args)
  149. {
  150. Write(string.Format(str, args));
  151. }
  152. // Write formatted line
  153. public void WriteLine(string str, params object[] args)
  154. {
  155. WriteLine(string.Format(str, args));
  156. }
  157. // Current position
  158. public Point Position
  159. {
  160. get { return new Point(_x, _y); }
  161. set { _x = value.X; _y = value.Y; }
  162. }
  163. // Clear the entire paint context
  164. public void Clear()
  165. {
  166. // Clear buffer
  167. for (int y = 0; y<_clipRect.Height; y++)
  168. {
  169. int pos = _baseOffset + _bufSize.Width * y;
  170. for (int x = 0; x < _clipRect.Width; x++, pos++)
  171. {
  172. _buf[pos].Attributes = _attributes;
  173. _buf[pos].Char = ' ';
  174. }
  175. }
  176. }
  177. // Set a specific character
  178. public void SetChar(int x, int y, char ch)
  179. {
  180. if (x >= 0 && x < _clipRect.Width &&
  181. y >= 0 && y < _clipRect.Height)
  182. {
  183. var pos = _baseOffset + _bufSize.Width * y + x;
  184. _buf[pos].Char = ch;
  185. _buf[pos].Attributes = _attributes;
  186. }
  187. }
  188. // Set a character but don't set attributes
  189. public void SetCharNoAttr(int x, int y, char ch)
  190. {
  191. if (x >= 0 && x < _clipRect.Width &&
  192. y >= 0 && y < _clipRect.Height)
  193. {
  194. var pos = _baseOffset + _bufSize.Width * y + x;
  195. _buf[pos].Char = ch;
  196. }
  197. }
  198. // Set a character with supplied attributes
  199. public void SetChar(int x, int y, char ch, ushort attributes)
  200. {
  201. if (x >= 0 && x < _clipRect.Width &&
  202. y >= 0 && y < _clipRect.Height)
  203. {
  204. var pos = _baseOffset + _bufSize.Width * y + x;
  205. _buf[pos].Char = ch;
  206. _buf[pos].Attributes = attributes;
  207. }
  208. }
  209. // Draw a box
  210. public void DrawBox(Rect rect, bool doubleLine)
  211. {
  212. // Draw the box
  213. var offsBottom = (rect.Height - 1) * rect.Width;
  214. var offsRight = rect.Width - 1;
  215. // Work out character set
  216. char[] boxDraw;
  217. if (doubleLine)
  218. {
  219. boxDraw = new char[] { '╔', '╗', '╚', '╝', '═', '═', '║', '║' };
  220. }
  221. else
  222. {
  223. boxDraw = new char[] { '┌', '┐', '└', '┘', '─', '─', '│', '│' };
  224. }
  225. // Corners
  226. SetChar(rect.Left, rect.Top, boxDraw[0]);
  227. SetChar(rect.Right - 1, rect.Top, boxDraw[1]);
  228. SetChar(rect.Left, rect.Bottom - 1, boxDraw[2]);
  229. SetChar(rect.Right - 1, rect.Bottom - 1, boxDraw[3]);
  230. // Top/bottom
  231. for (int i = 1; i < rect.Width - 1; i++)
  232. {
  233. SetChar(i, rect.Top, boxDraw[4]);
  234. SetChar(i, rect.Bottom - 1, boxDraw[5]);
  235. }
  236. // Left/Right
  237. for (int i = 1; i < rect.Height - 1; i++)
  238. {
  239. SetChar(rect.Left, i, boxDraw[6]);
  240. SetChar(rect.Right - 1, i, boxDraw[7]);
  241. }
  242. }
  243. }
  244. }