Added speech balloon shadow

This commit is contained in:
Jaex 2016-11-29 23:23:34 +03:00
parent 58a6707635
commit 9000cf31a7

View file

@ -90,26 +90,38 @@ public override void Move(int x, int y)
} }
public override void OnDraw(Graphics g) public override void OnDraw(Graphics g)
{
if (Rectangle.Width > 10 && Rectangle.Height > 10)
{ {
DrawSpeechBalloon(g); DrawSpeechBalloon(g);
DrawText(g);
}
} }
protected void DrawSpeechBalloon(Graphics g) protected void DrawSpeechBalloon(Graphics g)
{ {
if (Rectangle.Width > 10 && Rectangle.Height > 10) if (Shadow && IsBorderVisible)
{
DrawSpeechBalloon(g, ShadowColor, BorderSize, Color.Transparent, Rectangle.LocationOffset(ShadowOffset), TailPosition.Add(ShadowOffset));
}
DrawSpeechBalloon(g, BorderColor, BorderSize, FillColor, Rectangle, TailPosition);
}
protected void DrawSpeechBalloon(Graphics g, Color borderColor, int borderSize, Color fillColor, Rectangle rect, Point tailPosition)
{ {
GraphicsPath gpTail = null; GraphicsPath gpTail = null;
if (TailVisible) if (TailVisible)
{ {
gpTail = CreateTailPath(); gpTail = CreateTailPath(rect, tailPosition);
} }
if (FillColor.A > 0) if (fillColor.A > 0)
{ {
using (Brush brush = new SolidBrush(FillColor)) using (Brush brush = new SolidBrush(fillColor))
{ {
g.FillRectangle(brush, Rectangle); g.FillRectangle(brush, rect);
} }
} }
@ -117,11 +129,11 @@ protected void DrawSpeechBalloon(Graphics g)
{ {
g.SmoothingMode = SmoothingMode.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality;
if (FillColor.A > 0) if (fillColor.A > 0)
{ {
g.ExcludeClip(Rectangle); g.ExcludeClip(rect);
using (Brush brush = new SolidBrush(FillColor)) using (Brush brush = new SolidBrush(fillColor))
{ {
g.FillPath(brush, gpTail); g.FillPath(brush, gpTail);
} }
@ -129,11 +141,11 @@ protected void DrawSpeechBalloon(Graphics g)
g.ResetClip(); g.ResetClip();
} }
if (BorderSize > 0 && BorderColor.A > 0) if (borderSize > 0 && borderColor.A > 0)
{ {
g.ExcludeClip(Rectangle.Offset(-1)); g.ExcludeClip(rect.Offset(-1));
using (Pen pen = new Pen(BorderColor, BorderSize)) using (Pen pen = new Pen(borderColor, borderSize))
{ {
g.DrawPath(pen, gpTail); g.DrawPath(pen, gpTail);
} }
@ -144,7 +156,7 @@ protected void DrawSpeechBalloon(Graphics g)
g.SmoothingMode = SmoothingMode.None; g.SmoothingMode = SmoothingMode.None;
} }
if (BorderSize > 0 && BorderColor.A > 0) if (borderSize > 0 && borderColor.A > 0)
{ {
if (gpTail != null) if (gpTail != null)
{ {
@ -154,11 +166,9 @@ protected void DrawSpeechBalloon(Graphics g)
} }
} }
Rectangle rect = Rectangle.Offset(BorderSize - 1); using (Pen pen = new Pen(borderColor, borderSize) { Alignment = PenAlignment.Inset })
using (Pen pen = new Pen(BorderColor, BorderSize) { Alignment = PenAlignment.Inset })
{ {
g.DrawRectangleProper(pen, rect); g.DrawRectangleProper(pen, rect.Offset(borderSize - 1));
} }
g.ResetClip(); g.ResetClip();
@ -168,27 +178,24 @@ protected void DrawSpeechBalloon(Graphics g)
{ {
gpTail.Dispose(); gpTail.Dispose();
} }
DrawText(g);
}
} }
protected GraphicsPath CreateTailPath() protected GraphicsPath CreateTailPath(Rectangle rect, Point tailPosition)
{ {
GraphicsPath gpTail = new GraphicsPath(); GraphicsPath gpTail = new GraphicsPath();
Point center = Rectangle.Center(); Point center = rect.Center();
int rectAverageSize = (Rectangle.Width + Rectangle.Height) / 2; int rectAverageSize = (rect.Width + rect.Height) / 2;
int tailWidth = (int)(TailWidthMultiplier * rectAverageSize); int tailWidth = (int)(TailWidthMultiplier * rectAverageSize);
tailWidth = Math.Min(Math.Min(tailWidth, Rectangle.Width), Rectangle.Height); tailWidth = Math.Min(Math.Min(tailWidth, rect.Width), rect.Height);
int tailOrigin = tailWidth / 2; int tailOrigin = tailWidth / 2;
int tailLength = (int)MathHelpers.Distance(center, TailPosition); int tailLength = (int)MathHelpers.Distance(center, tailPosition);
gpTail.AddLine(0, -tailOrigin, 0, tailOrigin); gpTail.AddLine(0, -tailOrigin, 0, tailOrigin);
gpTail.AddLine(0, tailOrigin, tailLength, 0); gpTail.AddLine(0, tailOrigin, tailLength, 0);
gpTail.CloseFigure(); gpTail.CloseFigure();
using (Matrix matrix = new Matrix()) using (Matrix matrix = new Matrix())
{ {
matrix.Translate(center.X, center.Y); matrix.Translate(center.X, center.Y);
float tailDegree = MathHelpers.LookAtDegree(center, TailPosition); float tailDegree = MathHelpers.LookAtDegree(center, tailPosition);
matrix.Rotate(tailDegree); matrix.Rotate(tailDegree);
gpTail.Transform(matrix); gpTail.Transform(matrix);
} }