From d7fede96ef98c1139960c5e15e9bb885e0a039ba Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sat, 5 Aug 2017 11:09:34 +0900 Subject: [PATCH] Fix shadow on SpriteIcon being a bit off --- osu.Game/Graphics/SpriteIcon.cs | 46 +++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/osu.Game/Graphics/SpriteIcon.cs b/osu.Game/Graphics/SpriteIcon.cs index 345c6e7639..4bf4af922a 100644 --- a/osu.Game/Graphics/SpriteIcon.cs +++ b/osu.Game/Graphics/SpriteIcon.cs @@ -1,6 +1,7 @@ // Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE +using System; using osu.Framework.Allocation; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; @@ -18,18 +19,19 @@ namespace osu.Game.Graphics public SpriteIcon() { + spriteShadow = new Sprite + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + RelativeSizeAxes = Axes.Both, + FillMode = FillMode.Fit, + Depth = 2, + Y = 2, + Colour = new Color4(0f, 0f, 0f, 0.2f), + }; + InternalChildren = new[] { - spriteShadow = new Sprite - { - Anchor = Anchor.Centre, - Origin = Anchor.Centre, - RelativeSizeAxes = Axes.Both, - FillMode = FillMode.Fit, - Position = new Vector2(0, 0.06f), - Colour = new Color4(0f, 0f, 0f, 0.2f), - Alpha = 0 - }, spriteMain = new Sprite { Anchor = Anchor.Centre, @@ -60,10 +62,32 @@ namespace osu.Game.Graphics Size = new Vector2(texture?.DisplayWidth ?? 0, texture?.DisplayHeight ?? 0); } + public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true) + { + if ((invalidation & Invalidation.Colour) > 0) + { + //adjust shadow alpha based on highest component intensity to avoid muddy display of darker text. + //squared result for quadratic fall-off seems to give the best result. + var avgColour = (Color4)DrawInfo.Colour.AverageColour; + + spriteShadow.Alpha = (float)Math.Pow(Math.Max(Math.Max(avgColour.R, avgColour.G), avgColour.B), 2); + } + + return base.Invalidate(invalidation, source, shallPropagate); + } + public bool Shadow { get { return spriteShadow.IsPresent; } - set { spriteShadow.Alpha = value ? 1 : 0; } + set + { + if (value == (spriteShadow.IsAlive && spriteShadow.IsLoaded)) return; + + if (value) + AddInternal(spriteShadow); + else + RemoveInternal(spriteShadow); + } } private FontAwesome icon;