diff options
Diffstat (limited to 'Extensions')
| -rw-r--r-- | Extensions/GraphicsExtensions.cs | 218 | ||||
| -rw-r--r-- | Extensions/UdpClientExtensions.cs | 21 |
2 files changed, 239 insertions, 0 deletions
diff --git a/Extensions/GraphicsExtensions.cs b/Extensions/GraphicsExtensions.cs new file mode 100644 index 0000000..acf73cd --- /dev/null +++ b/Extensions/GraphicsExtensions.cs @@ -0,0 +1,218 @@ +using System; +using System.Drawing; +using System.Drawing.Drawing2D; + +namespace Chess.Extensions +{ + public static class GraphicsExtensions + { + private static GraphicsPath GenerateRoundedRectangle( + this Graphics graphics, + RectangleF rectangle, + float radius) + { + float diameter; + GraphicsPath path = new GraphicsPath(); + if (radius <= 0.0F) + { + path.AddRectangle(rectangle); + path.CloseFigure(); + return path; + } + else + { + if (radius >= (Math.Min(rectangle.Width, rectangle.Height)) / 2.0) + return graphics.GenerateCapsule(rectangle); + diameter = radius * 2.0F; + SizeF sizeF = new SizeF(diameter, diameter); + RectangleF arc = new RectangleF(rectangle.Location, sizeF); + path.AddArc(arc, 180, 90); + arc.X = rectangle.Right - diameter; + path.AddArc(arc, 270, 90); + arc.Y = rectangle.Bottom - diameter; + path.AddArc(arc, 0, 90); + arc.X = rectangle.Left; + path.AddArc(arc, 90, 90); + path.CloseFigure(); + } + return path; + } + + private static GraphicsPath GenerateCapsule( + this Graphics graphics, + RectangleF baseRect) + { + float diameter; + RectangleF arc; + GraphicsPath path = new GraphicsPath(); + try + { + if (baseRect.Width > baseRect.Height) + { + diameter = baseRect.Height; + SizeF sizeF = new SizeF(diameter, diameter); + arc = new RectangleF(baseRect.Location, sizeF); + path.AddArc(arc, 90, 180); + arc.X = baseRect.Right - diameter; + path.AddArc(arc, 270, 180); + } + else if (baseRect.Width < baseRect.Height) + { + diameter = baseRect.Width; + SizeF sizeF = new SizeF(diameter, diameter); + arc = new RectangleF(baseRect.Location, sizeF); + path.AddArc(arc, 180, 180); + arc.Y = baseRect.Bottom - diameter; + path.AddArc(arc, 0, 180); + } + else path.AddEllipse(baseRect); + } + catch { path.AddEllipse(baseRect); } + finally { path.CloseFigure(); } + return path; + } + + /// <summary> + /// Draws a rounded rectangle specified by a pair of coordinates, a width, a height and the radius + /// for the arcs that make the rounded edges. + /// </summary> + /// <param name="brush">System.Drawing.Pen that determines the color, width and style of the rectangle.</param> + /// <param name="x">The x-coordinate of the upper-left corner of the rectangle to draw.</param> + /// <param name="y">The y-coordinate of the upper-left corner of the rectangle to draw.</param> + /// <param name="width">Width of the rectangle to draw.</param> + /// <param name="height">Height of the rectangle to draw.</param> + /// <param name="radius">The radius of the arc used for the rounded edges.</param> + public static void DrawRoundedRectangle( + this Graphics graphics, + Pen pen, + float x, + float y, + float width, + float height, + float radius) + { + RectangleF rectangle = new RectangleF(x, y, width, height); + GraphicsPath path = graphics.GenerateRoundedRectangle(rectangle, radius); + SmoothingMode old = graphics.SmoothingMode; + graphics.SmoothingMode = SmoothingMode.AntiAlias; + graphics.DrawPath(pen, path); + graphics.SmoothingMode = old; + } + + + /// <summary> + /// Draws a rounded rectangle specified by a pair of coordinates, a width, a height and the radius + /// for the arcs that make the rounded edges. + /// </summary> + /// <param name="brush">System.Drawing.Pen that determines the color, width and style of the rectangle.</param> + /// <param name="x">The x-coordinate of the upper-left corner of the rectangle to draw.</param> + /// <param name="y">The y-coordinate of the upper-left corner of the rectangle to draw.</param> + /// <param name="width">Width of the rectangle to draw.</param> + /// <param name="height">Height of the rectangle to draw.</param> + /// <param name="radius">The radius of the arc used for the rounded edges.</param> + public static void DrawRoundedRectangle( + this Graphics graphics, + Pen pen, + int x, + int y, + int width, + int height, + int radius) + { + graphics.DrawRoundedRectangle( + pen, + Convert.ToSingle(x), + Convert.ToSingle(y), + Convert.ToSingle(width), + Convert.ToSingle(height), + Convert.ToSingle(radius)); + } + + /// <summary> + /// Fills the interior of a rounded rectangle specified by a pair of coordinates, a width, a height + /// and the radius for the arcs that make the rounded edges. + /// </summary> + /// <param name="brush">System.Drawing.Brush that determines the characteristics of the fill.</param> + /// <param name="x">The x-coordinate of the upper-left corner of the rectangle to fill.</param> + /// <param name="y">The y-coordinate of the upper-left corner of the rectangle to fill.</param> + /// <param name="width">Width of the rectangle to fill.</param> + /// <param name="height">Height of the rectangle to fill.</param> + /// <param name="radius">The radius of the arc used for the rounded edges.</param> + public static void FillRoundedRectangle( + this Graphics graphics, + Brush brush, + float x, + float y, + float width, + float height, + float radius) + { + RectangleF rectangle = new RectangleF(x, y, width, height); + GraphicsPath path = graphics.GenerateRoundedRectangle(rectangle, radius); + SmoothingMode old = graphics.SmoothingMode; + graphics.SmoothingMode = SmoothingMode.AntiAlias; + graphics.FillPath(brush, path); + graphics.SmoothingMode = old; + } + + /// <summary> + /// Fills the interior of a rounded rectangle specified by a pair of coordinates, a width, a height + /// and the radius for the arcs that make the rounded edges. + /// </summary> + /// <param name="brush">System.Drawing.Brush that determines the characteristics of the fill.</param> + /// <param name="x">The x-coordinate of the upper-left corner of the rectangle to fill.</param> + /// <param name="y">The y-coordinate of the upper-left corner of the rectangle to fill.</param> + /// <param name="width">Width of the rectangle to fill.</param> + /// <param name="height">Height of the rectangle to fill.</param> + /// <param name="radius">The radius of the arc used for the rounded edges.</param> + public static void FillRoundedRectangle( + this Graphics graphics, + Brush brush, + int x, + int y, + int width, + int height, + int radius) + { + graphics.FillRoundedRectangle( + brush, + Convert.ToSingle(x), + Convert.ToSingle(y), + Convert.ToSingle(width), + Convert.ToSingle(height), + Convert.ToSingle(radius)); + } + + public static void DrawParagraph(this Graphics graphics, string s, Font font, Brush brush, float x, float y) + { + graphics.DrawParagraph(s, font, brush, x, y, StringFormat.GenericDefault); + } + + public static void DrawParagraph(this Graphics graphics, string s, Font font, Brush brush, float x, float y, StringFormat format) + { + string[] split = s.Split(new string[] { "\n" }, StringSplitOptions.None); + int dy = (int)font.GetHeight(graphics) * 1; + + int offy = 0; + foreach (var str in split) + { + graphics.DrawString(str, font, brush, x, y + offy, format); + offy += dy; + } + } + + public static int ParagraphHeight(this Graphics graphics, string s, Font font) + { + string[] split = s.Split(new string[] { "\n" }, StringSplitOptions.None); + int dy = (int)font.GetHeight(graphics) * 1; + + int offy = 0; + foreach (var str in split) + { + offy += dy; + } + + return offy; + } + } +}
\ No newline at end of file diff --git a/Extensions/UdpClientExtensions.cs b/Extensions/UdpClientExtensions.cs new file mode 100644 index 0000000..23278db --- /dev/null +++ b/Extensions/UdpClientExtensions.cs @@ -0,0 +1,21 @@ +using Chess.Networking; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace Chess.Extensions +{ + public static class UdpClientExtensions + { + private static int Send(this UdpClient client, INetworkMessage message, IPEndPoint ep) + { + var msg = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)); + return client.Send(msg, msg.Length, ep); + } + } +} |
