using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Globalization; namespace ScreenConnect; public struct CoreRect { public int X { get; set; } public int Y { get; set; } public int Width { get; set; } public int Height { get; set; } public CorePoint Location => new CorePoint(X, Y); public CoreSize Size => new CoreSize(Width, Height); public int Left => X; public int Top => Y; public int Bottom => Y + Height; public int Right => X + Width; public bool HasArea { get { if (Width > 0) { return Height > 0; } return false; } } public CoreRect(CorePoint location, CoreSize size) { this = default(CoreRect); X = location.X; Y = location.Y; Width = size.Width; Height = size.Height; } public CoreRect(int x, int y, int width, int height) { this = default(CoreRect); X = x; Y = y; Width = width; Height = height; } public static CoreRect FromLTRB(int left, int top, int right, int bottom) { return new CoreRect(left, top, right - left, bottom - top); } public static CoreRect RoundToContaining(float left, float top, float right, float bottom) { return FromLTRB((int)Math.Floor(left), (int)Math.Floor(top), (int)Math.Ceiling(right), (int)Math.Ceiling(bottom)); } public static CoreRect operator +(CoreRect rect, CorePoint point) { return new CoreRect(rect.X + point.X, rect.Y + point.Y, rect.Width, rect.Height); } public static CoreRect operator -(CoreRect rect, CorePoint point) { return new CoreRect(rect.X - point.X, rect.Y - point.Y, rect.Width, rect.Height); } public static CoreRect operator +(CoreRect rect, CoreSize size) { return new CoreRect(rect.X, rect.Y, rect.Width + size.Width, rect.Height + size.Height); } public static implicit operator CoreRect(CoreSize size) { return new CoreRect(default(CorePoint), size); } public bool Contains(int x, int y) { if (Left <= x && x < Right && Top <= y) { return y < Bottom; } return false; } public bool Contains(CorePoint pt) { return Contains(pt.X, pt.Y); } public bool Contains(CoreRect rect) { if (X <= rect.X && Y <= rect.Y && Bottom >= rect.Bottom) { return Right >= rect.Right; } return false; } public static bool Intersect(CoreRect a, CoreRect b, out CoreRect intersection) { int num = Math.Max(a.X, b.X); int num2 = Math.Min(a.X + a.Width, b.X + b.Width); int num3 = Math.Max(a.Y, b.Y); int num4 = Math.Min(a.Y + a.Height, b.Y + b.Height); if (num > num2 || num3 > num4) { intersection = default(CoreRect); return false; } intersection = new CoreRect(num, num3, num2 - num, num4 - num3); return true; } public static CoreRect Intersect(CoreRect a, CoreRect b) { if (!Intersect(a, b, out var intersection)) { return default(CoreRect); } return intersection; } public bool IntersectsWith(CoreRect rect) { CoreRect intersection; return Intersect(this, rect, out intersection); } public static CoreRect Union(CoreRect a, CoreRect b) { int num = Math.Min(a.X, b.X); int num2 = Math.Max(a.X + a.Width, b.X + b.Width); int num3 = Math.Min(a.Y, b.Y); int num4 = Math.Max(a.Y + a.Height, b.Y + b.Height); return new CoreRect(num, num3, num2 - num, num4 - num3); } public static CoreRect UnionUnlessNoArea(CoreRect a, CoreRect b) { if (a.Width == 0 || a.Height == 0) { return b; } if (b.Width == 0 || b.Height == 0) { return a; } return Union(a, b); } public override int GetHashCode() { return X ^ ((Y << 13) | (Y >> 19)) ^ ((Width << 26) | (Width >> 6)) ^ ((Height << 7) | (Height >> 25)); } public override bool Equals(object obj) { if (!(obj is CoreRect coreRect)) { return false; } if (coreRect.X == X && coreRect.Y == Y && coreRect.Width == Width) { return coreRect.Height == Height; } return false; } public static bool operator ==(CoreRect left, CoreRect right) { if (left.X == right.X && left.Y == right.Y && left.Width == right.Width) { return left.Height == right.Height; } return false; } public static bool operator !=(CoreRect left, CoreRect right) { return !(left == right); } public override string ToString() { return "{X=" + X.ToString(CultureInfo.CurrentCulture) + ",Y=" + Y.ToString(CultureInfo.CurrentCulture) + ",Width=" + Width.ToString(CultureInfo.CurrentCulture) + ",Height=" + Height.ToString(CultureInfo.CurrentCulture) + "}"; } public int CompareXThenY(CoreRect second) { int num = X.CompareTo(second.X); if (num == 0) { return Y.CompareTo(second.Y); } return num; } public static string Serialize(CoreRect coreRect) { return new KeyValuePair[4] { Extensions.CreateKeyValuePair("X", coreRect.X.ToString()), Extensions.CreateKeyValuePair("Y", coreRect.Y.ToString()), Extensions.CreateKeyValuePair("Width", coreRect.Width.ToString()), Extensions.CreateKeyValuePair("Height", coreRect.Height.ToString()) }.Pipe(Extensions.EncodeQueryString); } public static CoreRect Deserialize(string coreRectString) { return Extensions.DecodeQueryString(coreRectString).Pipe((NameValueCollection it) => new CoreRect { X = int.Parse(it["X"]), Y = int.Parse(it["Y"]), Width = int.Parse(it["Width"]), Height = int.Parse(it["Height"]) }); } }