Types.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace ConFrames
  4. {
  5. // Character info (matches format used by Windows Console API's)
  6. [StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Unicode)]
  7. public struct CharInfo
  8. {
  9. public char Char;
  10. public ushort Attributes;
  11. }
  12. // Rectangle
  13. public struct Rect
  14. {
  15. public Rect(int left, int top, int width, int height)
  16. {
  17. Left = left;
  18. Top = top;
  19. Width = width;
  20. Height = height;
  21. }
  22. public int Left;
  23. public int Top;
  24. public int Width;
  25. public int Height;
  26. public int Right { get { return Left + Width; } }
  27. public int Bottom { get { return Top + Height; } }
  28. public Size Size { get { return new Size(Width, Height); } }
  29. public bool IntersectsWith(Rect other)
  30. {
  31. // Does it intersect
  32. if (other.Right < Left)
  33. return false;
  34. if (other.Left > Right)
  35. return false;
  36. if (other.Top > Bottom)
  37. return false;
  38. if (other.Bottom < Top)
  39. return false;
  40. return true;
  41. }
  42. public bool Intersect(Rect rectOther)
  43. {
  44. if (!IntersectsWith(rectOther))
  45. {
  46. Width = 0;
  47. Height = 0;
  48. return false;
  49. }
  50. var right = Right;
  51. var bottom = Bottom;
  52. Left = Math.Max(Left, rectOther.Left);
  53. Width = Math.Min(right, rectOther.Right) - Left;
  54. Top = Math.Max(Top, rectOther.Top);
  55. Height = Math.Min(bottom, rectOther.Bottom) - Top;
  56. return true;
  57. }
  58. public override bool Equals(object obj)
  59. {
  60. if (obj == null)
  61. return false;
  62. if (!(obj is Rect))
  63. return false;
  64. return this == (Rect)obj;
  65. }
  66. public override int GetHashCode()
  67. {
  68. return Left.GetHashCode() ^ Top.GetHashCode() ^ Width.GetHashCode() ^ Height.GetHashCode();
  69. }
  70. public static bool operator ==(Rect a, Rect b)
  71. {
  72. return a.Left == b.Left && a.Top == b.Top && a.Width == b.Width && a.Height == b.Height;
  73. }
  74. public static bool operator !=(Rect a, Rect b)
  75. {
  76. return !(a == b);
  77. }
  78. }
  79. // Point
  80. public struct Point
  81. {
  82. public Point(int x, int y)
  83. {
  84. X = x;
  85. Y = y;
  86. }
  87. public int X;
  88. public int Y;
  89. }
  90. // Size
  91. public struct Size
  92. {
  93. public Size(int width, int height)
  94. {
  95. Width = width;
  96. Height = height;
  97. }
  98. public int Width;
  99. public int Height;
  100. public override bool Equals(object obj)
  101. {
  102. if (obj == null)
  103. return false;
  104. if (!(obj is Size))
  105. return false;
  106. return this == (Size)obj;
  107. }
  108. public override int GetHashCode()
  109. {
  110. return Width.GetHashCode() ^ Height.GetHashCode();
  111. }
  112. public static bool operator ==(Size a, Size b)
  113. {
  114. return a.Width == b.Width && a.Height == b.Height;
  115. }
  116. public static bool operator !=(Size a, Size b)
  117. {
  118. return !(a == b);
  119. }
  120. }
  121. // View mode
  122. public enum ViewMode
  123. {
  124. StdOut,
  125. Desktop,
  126. }
  127. // Helper for working with attributes
  128. public static class Attribute
  129. {
  130. public static ushort Make(ConsoleColor foreground, ConsoleColor background)
  131. {
  132. return (ushort)(((byte)background << 4) | (ushort)foreground);
  133. }
  134. }
  135. }