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