// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Textures; using osu.Framework.Input.Events; using osu.Game.Graphics.Containers; namespace osu.Game.Users.Drawables { public class ClickableAvatar : Container { private const string default_tooltip_text = "view profile"; /// /// Whether to open the user's profile when clicked. /// public bool OpenOnClick { set => clickableArea.Enabled.Value = value; } /// /// By default, the tooltip will show "view profile" as avatars are usually displayed next to a username. /// Setting this to true exposes the username via tooltip for special cases where this is not true. /// public bool ShowUsernameTooltip { set => clickableArea.TooltipText = value ? (user?.Username ?? string.Empty) : default_tooltip_text; } private readonly User user; [Resolved(CanBeNull = true)] private OsuGame game { get; set; } private readonly ClickableArea clickableArea; /// /// A clickable avatar for the specified user, with UI sounds included. /// If is true, clicking will open the user's profile. /// /// The user. A null value will get a placeholder avatar. public ClickableAvatar(User user = null) { this.user = user; Add(clickableArea = new ClickableArea { RelativeSizeAxes = Axes.Both, Action = openProfile }); } [BackgroundDependencyLoader] private void load(LargeTextureStore textures) { LoadComponentAsync(new DrawableAvatar(user), clickableArea.Add); } private void openProfile() { if (user?.Id > 1) game?.ShowUser(user.Id); } private class ClickableArea : OsuClickableContainer { private string tooltip = default_tooltip_text; public override string TooltipText { get => Enabled.Value ? tooltip : null; set => tooltip = value; } protected override bool OnClick(ClickEvent e) { if (!Enabled.Value) return false; return base.OnClick(e); } } } }