public void SetPosition(int x, int y) { if (x < 0 && y == 0) { _LightPosition = LightSourcePosition.Left; transform.position = new Vector3(-_Direction, 10, 0); } else if (x > 0 && y == 0) { _LightPosition = LightSourcePosition.Right; transform.position = new Vector3(_Direction, 10, 0); } else if (y > 0 && x == 0) { _LightPosition = LightSourcePosition.Up; transform.position = new Vector3(0, 10, _Direction); } else if (y < 0 && x == 0) { _LightPosition = LightSourcePosition.Down; transform.position = new Vector3(0, 10, -_Direction); } else if (y == 0 && x == 0) { _LightPosition = LightSourcePosition.Center; transform.position = new Vector3(0, 10, 0); } }
internal static void DrawDropShadow(Graphics graphics, Rectangle shadowSourceRectangle, Color baseColor, int shadowDepth, LightSourcePosition lightSourcePosition, float lightSourceIntensity, bool roundEdges) { if (graphics == null) { throw new ArgumentNullException("graphics"); } if ((shadowSourceRectangle.IsEmpty || (shadowSourceRectangle.Width < 0)) || (shadowSourceRectangle.Height < 0)) { throw new ArgumentException(SR.GetString("Error_InvalidShadowRectangle"), "shadowRectangle"); } if ((shadowDepth < 1) || (shadowDepth > 12)) { throw new ArgumentException(SR.GetString("Error_InvalidShadowDepth"), "shadowDepth"); } if ((lightSourceIntensity <= 0f) || (lightSourceIntensity > 1f)) { throw new ArgumentException(SR.GetString("Error_InvalidLightSource"), "lightSourceIntensity"); } Rectangle rectangle = shadowSourceRectangle; Size empty = Size.Empty; if ((lightSourcePosition & LightSourcePosition.Center) > 0) { rectangle.Inflate(shadowDepth, shadowDepth); } if ((lightSourcePosition & LightSourcePosition.Left) > 0) { empty.Width += shadowDepth + 1; } else if ((lightSourcePosition & LightSourcePosition.Right) > 0) { empty.Width -= shadowDepth + 1; } if ((lightSourcePosition & LightSourcePosition.Top) > 0) { empty.Height += shadowDepth + 1; } else if ((lightSourcePosition & LightSourcePosition.Bottom) > 0) { empty.Height -= shadowDepth + 1; } rectangle.Offset(empty.Width, empty.Height); GraphicsContainer container = graphics.BeginContainer(); GraphicsPath path = new GraphicsPath(); if (roundEdges) { path.AddPath(GetRoundedRectanglePath(shadowSourceRectangle, 8), true); } else { path.AddRectangle(shadowSourceRectangle); } try { using (Region region = new Region(path)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.ExcludeClip(region); Color color = Color.FromArgb(Convert.ToInt32((float) (40f * lightSourceIntensity)), baseColor); int num = Math.Max(40 / shadowDepth, 2); for (int i = 0; i < shadowDepth; i++) { rectangle.Inflate(-1, -1); using (Brush brush = new SolidBrush(color)) { using (GraphicsPath path2 = new GraphicsPath()) { if (roundEdges) { path2.AddPath(GetRoundedRectanglePath(rectangle, 8), true); } else { path2.AddRectangle(rectangle); } graphics.FillPath(brush, path2); } } color = Color.FromArgb(color.A + num, color.R, color.G, color.B); } } } finally { graphics.EndContainer(container); } }
/// <summary> /// Draws the drop shadow using specified base color /// </summary> /// <param name="graphics">Graphics object on which to draw the shadow</param> /// <param name="shadowSourceRectangle">Rectangle around which to draw the shadow</param> /// <param name="baseColor">Base color used to draw the shadow</param> /// <param name="shadowDepth">Depth of the shadow, this has to be between 1 and 12 inclusive</param> /// <param name="lightSourcePosition">Position of the light source determining the way shadow will be drawn</param> /// <param name="lightSourceIntensity">Intensity of the light source, this needs to be inbetween 0.01 and 1</param> /// <param name="roundEdges">Flag indicating whether to round the edges of the shadow</param> internal static void DrawDropShadow(Graphics graphics, Rectangle shadowSourceRectangle, Color baseColor, int shadowDepth, LightSourcePosition lightSourcePosition, float lightSourceIntensity, bool roundEdges) { if (graphics == null) throw new ArgumentNullException("graphics"); if (shadowSourceRectangle.IsEmpty || shadowSourceRectangle.Width < 0 || shadowSourceRectangle.Height < 0) throw new ArgumentException(SR.GetString(SR.Error_InvalidShadowRectangle), "shadowRectangle"); if (shadowDepth < 1 || shadowDepth > 12) throw new ArgumentException(SR.GetString(SR.Error_InvalidShadowDepth), "shadowDepth"); if (lightSourceIntensity <= 0.0f || lightSourceIntensity > 1.0f) throw new ArgumentException(SR.GetString(SR.Error_InvalidLightSource), "lightSourceIntensity"); const int baseAlphaLevel = 40; Rectangle shadowRectangle = shadowSourceRectangle; Size offset = Size.Empty; if ((lightSourcePosition & LightSourcePosition.Center) > 0) shadowRectangle.Inflate(shadowDepth, shadowDepth); if ((lightSourcePosition & LightSourcePosition.Left) > 0) offset.Width += (shadowDepth + 1); else if ((lightSourcePosition & LightSourcePosition.Right) > 0) offset.Width -= (shadowDepth + 1); if ((lightSourcePosition & LightSourcePosition.Top) > 0) offset.Height += (shadowDepth + 1); else if ((lightSourcePosition & LightSourcePosition.Bottom) > 0) offset.Height -= (shadowDepth + 1); shadowRectangle.Offset(offset.Width, offset.Height); GraphicsContainer graphicsState = graphics.BeginContainer(); GraphicsPath excludePath = new GraphicsPath(); if (roundEdges) excludePath.AddPath(GetRoundedRectanglePath(shadowSourceRectangle, AmbientTheme.ArcDiameter), true); else excludePath.AddRectangle(shadowSourceRectangle); try { using (Region clipRegion = new Region(excludePath)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.ExcludeClip(clipRegion); Color shadowColor = Color.FromArgb(Convert.ToInt32((baseAlphaLevel * lightSourceIntensity)), baseColor); int alphaIncreament = Math.Max((baseAlphaLevel / shadowDepth), 2); for (int i = 0; i < shadowDepth; i++) { shadowRectangle.Inflate(-1, -1); using (Brush shadowBrush = new SolidBrush(shadowColor)) using (GraphicsPath shadowPath = new GraphicsPath()) { if (roundEdges) shadowPath.AddPath(GetRoundedRectanglePath(shadowRectangle, AmbientTheme.ArcDiameter), true); else shadowPath.AddRectangle(shadowRectangle); graphics.FillPath(shadowBrush, shadowPath); } shadowColor = Color.FromArgb(shadowColor.A + alphaIncreament, shadowColor.R, shadowColor.G, shadowColor.B); } } } finally { graphics.EndContainer(graphicsState); } }