Types.cs 4.6 KB

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