1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:17:23 +08:00

Implemented the new IHasHoverSounds interface in a private "ChatHoverContainer" class which is now used for ChatLink instances.

Also moved the overhead for finding all sprites in the same line that reference the same URL to the LoadComplete (used to be every hover, now only once).
This commit is contained in:
FreezyLemon 2017-12-07 13:17:40 +01:00
parent a8599a1b75
commit 8ba66015f4

View File

@ -8,7 +8,11 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Input;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Chat;
using System.Collections.Generic;
using System.Linq;
namespace osu.Game.Online.Chat
@ -20,17 +24,34 @@ namespace osu.Game.Online.Chat
private Color4 hoverColour;
private Color4 urlColour;
private readonly ChatHoverContainer content;
protected IEnumerable<ChatLink> SameLinkSprites { get; private set; }
protected override Container<Drawable> Content => content ?? base.Content;
public string TooltipText => LinkId != -1 ? Url : null;
public ChatLink()
{
AddInternal(content = new ChatHoverContainer
{
AutoSizeAxes = Axes.Both,
});
OnLoadComplete = d =>
{
// All sprites in the same chatline that represent the same URL
SameLinkSprites = (d.Parent as Container<Drawable>).Children.Where(child => (child as ChatLink)?.LinkId == LinkId && !d.Equals(child)).Cast<ChatLink>();
};
}
protected override bool OnHover(InputState state)
{
// Every word is one sprite in chat (for word wrap) so we need to find all other sprites that display the same link
var otherSpritesWithSameLink = ((Container<Drawable>)Parent).Children.Where(child => (child as ChatLink)?.LinkId == LinkId && !Equals(child));
var hoverResult = base.OnHover(state);
if (!otherSpritesWithSameLink.Any(sprite => sprite.IsHovered))
foreach (ChatLink sprite in otherSpritesWithSameLink)
if (!SameLinkSprites.Any(sprite => sprite.IsHovered))
foreach (ChatLink sprite in SameLinkSprites)
sprite.TriggerOnHover(state);
Content.FadeColour(hoverColour, 500, Easing.OutQuint);
@ -40,16 +61,14 @@ namespace osu.Game.Online.Chat
protected override void OnHoverLost(InputState state)
{
var spritesWithSameLink = ((Container<Drawable>)Parent).Children.Where(child => (child as ChatLink)?.LinkId == LinkId);
if (spritesWithSameLink.Any(sprite => sprite.IsHovered))
if (SameLinkSprites.Any(sprite => sprite.IsHovered))
{
// We have to do this so this sprite does not fade its colour back
Content.FadeColour(hoverColour, 500, Easing.OutQuint);
return;
}
foreach (ChatLink sprite in spritesWithSameLink)
foreach (ChatLink sprite in SameLinkSprites)
sprite.Content.FadeColour(urlColour, 500, Easing.OutQuint);
base.OnHoverLost(state);
@ -63,5 +82,10 @@ namespace osu.Game.Online.Chat
if (LinkId != -1)
Content.Colour = urlColour;
}
private class ChatHoverContainer : OsuHoverContainer, IHasHoverSounds
{
public bool ShouldPlayHoverSound => ((ChatLink)Parent).SameLinkSprites.All(sprite => !sprite.IsHovered);
}
}
}