|
| 1 | +// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. |
| 2 | +// See the LICENCE file in the repository root for full licence text. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using osu.Framework.Allocation; |
| 7 | +using osu.Framework.Bindables; |
| 8 | +using osu.Framework.Extensions; |
| 9 | +using osu.Framework.Graphics; |
| 10 | +using osu.Framework.Graphics.Containers; |
| 11 | +using osu.Framework.Graphics.Cursor; |
| 12 | +using osu.Framework.Graphics.Shapes; |
| 13 | +using osu.Framework.Graphics.Sprites; |
| 14 | +using osu.Framework.Graphics.UserInterface; |
| 15 | +using osu.Framework.Input.Events; |
| 16 | +using osu.Framework.Localisation; |
| 17 | +using osu.Game.Graphics; |
| 18 | +using osu.Game.Graphics.Containers; |
| 19 | +using osu.Game.Graphics.Sprites; |
| 20 | +using osu.Game.Graphics.UserInterfaceV2; |
| 21 | +using osu.Game.Online.API; |
| 22 | +using osu.Game.Online.API.Requests.Responses; |
| 23 | +using osu.Game.Resources.Localisation.Web; |
| 24 | +using osu.Game.Users; |
| 25 | +using osuTK; |
| 26 | + |
| 27 | +namespace osu.Game.Overlays.Profile.Header.Components |
| 28 | +{ |
| 29 | + public partial class UserActionsButton : OsuHoverContainer, IHasPopover |
| 30 | + { |
| 31 | + public readonly Bindable<UserProfileData?> User = new Bindable<UserProfileData?>(); |
| 32 | + |
| 33 | + private Box background = null!; |
| 34 | + |
| 35 | + protected override IEnumerable<Drawable> EffectTargets => [background]; |
| 36 | + |
| 37 | + [Resolved] |
| 38 | + private OverlayColourProvider colourProvider { get; set; } = null!; |
| 39 | + |
| 40 | + [Resolved] |
| 41 | + private IAPIProvider api { get; set; } = null!; |
| 42 | + |
| 43 | + [BackgroundDependencyLoader] |
| 44 | + private void load() |
| 45 | + { |
| 46 | + IdleColour = colourProvider.Background2; |
| 47 | + HoverColour = colourProvider.Background1; |
| 48 | + |
| 49 | + Size = new Vector2(40); |
| 50 | + Masking = true; |
| 51 | + CornerRadius = 20; |
| 52 | + |
| 53 | + Child = new CircularContainer |
| 54 | + { |
| 55 | + Masking = true, |
| 56 | + RelativeSizeAxes = Axes.Both, |
| 57 | + Children = new Drawable[] |
| 58 | + { |
| 59 | + background = new Box |
| 60 | + { |
| 61 | + RelativeSizeAxes = Axes.Both, |
| 62 | + }, |
| 63 | + new SpriteIcon |
| 64 | + { |
| 65 | + Size = new Vector2(12), |
| 66 | + Anchor = Anchor.Centre, |
| 67 | + Origin = Anchor.Centre, |
| 68 | + Icon = FontAwesome.Solid.EllipsisV, |
| 69 | + }, |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + Action = this.ShowPopover; |
| 74 | + } |
| 75 | + |
| 76 | + protected override void LoadComplete() |
| 77 | + { |
| 78 | + base.LoadComplete(); |
| 79 | + |
| 80 | + User.BindValueChanged(_ => Alpha = User.Value?.User.OnlineID == api.LocalUser.Value.OnlineID ? 0 : 1, true); |
| 81 | + } |
| 82 | + |
| 83 | + public Popover GetPopover() => new UserActionPopover(User.Value!.User); |
| 84 | + |
| 85 | + private partial class UserActionPopover : OsuPopover |
| 86 | + { |
| 87 | + private readonly APIUser user; |
| 88 | + |
| 89 | + public UserActionPopover(APIUser user) |
| 90 | + : base(false) |
| 91 | + { |
| 92 | + this.user = user; |
| 93 | + } |
| 94 | + |
| 95 | + [BackgroundDependencyLoader] |
| 96 | + private void load(OverlayColourProvider colourProvider, IAPIProvider api, IDialogOverlay? dialogOverlay) |
| 97 | + { |
| 98 | + Background.Colour = colourProvider.Background6; |
| 99 | + |
| 100 | + bool userBlocked = api.Blocks.Any(b => b.TargetID == user.Id); |
| 101 | + |
| 102 | + AllowableAnchors = [Anchor.BottomCentre, Anchor.TopCentre]; |
| 103 | + |
| 104 | + Child = new FillFlowContainer |
| 105 | + { |
| 106 | + Width = 160, |
| 107 | + AutoSizeAxes = Axes.Y, |
| 108 | + Padding = new MarginPadding { Horizontal = 5, Vertical = 10 }, |
| 109 | + Children = new Drawable[] |
| 110 | + { |
| 111 | + new UserAction(FontAwesome.Solid.Ban, userBlocked ? UsersStrings.BlocksButtonUnblock : UsersStrings.BlocksButtonBlock) |
| 112 | + { |
| 113 | + Action = () => |
| 114 | + { |
| 115 | + dialogOverlay?.Push(userBlocked ? ConfirmBlockActionDialog.Unblock(user) : ConfirmBlockActionDialog.Block(user)); |
| 116 | + this.HidePopover(); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + }; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private partial class UserAction : OsuClickableContainer |
| 125 | + { |
| 126 | + private readonly IconUsage icon; |
| 127 | + private readonly LocalisableString caption; |
| 128 | + |
| 129 | + private Box background = null!; |
| 130 | + private CircularContainer indicator = null!; |
| 131 | + |
| 132 | + public UserAction(IconUsage icon, LocalisableString caption) |
| 133 | + { |
| 134 | + this.icon = icon; |
| 135 | + this.caption = caption; |
| 136 | + } |
| 137 | + |
| 138 | + [BackgroundDependencyLoader] |
| 139 | + private void load(OverlayColourProvider colourProvider) |
| 140 | + { |
| 141 | + RelativeSizeAxes = Content.RelativeSizeAxes = Axes.X; |
| 142 | + AutoSizeAxes = Content.AutoSizeAxes = Axes.Y; |
| 143 | + |
| 144 | + Masking = true; |
| 145 | + CornerRadius = 4; |
| 146 | + Children = new Drawable[] |
| 147 | + { |
| 148 | + background = new Box |
| 149 | + { |
| 150 | + RelativeSizeAxes = Axes.Both, |
| 151 | + Colour = colourProvider.Background5, |
| 152 | + Alpha = 0, |
| 153 | + }, |
| 154 | + indicator = new Circle |
| 155 | + { |
| 156 | + Width = 4, |
| 157 | + Height = 14, |
| 158 | + X = 10, |
| 159 | + Colour = colourProvider.Highlight1, |
| 160 | + Anchor = Anchor.CentreLeft, |
| 161 | + Origin = Anchor.Centre, |
| 162 | + Alpha = 0, |
| 163 | + }, |
| 164 | + new FillFlowContainer |
| 165 | + { |
| 166 | + AutoSizeAxes = Axes.Y, |
| 167 | + RelativeSizeAxes = Axes.X, |
| 168 | + Padding = new MarginPadding { Horizontal = 25, Vertical = 5 }, |
| 169 | + Direction = FillDirection.Horizontal, |
| 170 | + Spacing = new Vector2(5, 0), |
| 171 | + Children = new Drawable[] |
| 172 | + { |
| 173 | + new SpriteIcon |
| 174 | + { |
| 175 | + Icon = icon, |
| 176 | + Size = new Vector2(11), |
| 177 | + Anchor = Anchor.CentreLeft, |
| 178 | + Origin = Anchor.CentreLeft, |
| 179 | + }, |
| 180 | + new OsuSpriteText |
| 181 | + { |
| 182 | + Text = caption, |
| 183 | + Font = OsuFont.Style.Body, |
| 184 | + Anchor = Anchor.CentreLeft, |
| 185 | + Origin = Anchor.CentreLeft, |
| 186 | + UseFullGlyphHeight = false, |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + }; |
| 191 | + } |
| 192 | + |
| 193 | + protected override bool OnHover(HoverEvent e) |
| 194 | + { |
| 195 | + updateState(); |
| 196 | + return true; |
| 197 | + } |
| 198 | + |
| 199 | + protected override void OnHoverLost(HoverLostEvent e) |
| 200 | + { |
| 201 | + updateState(); |
| 202 | + base.OnHoverLost(e); |
| 203 | + } |
| 204 | + |
| 205 | + private void updateState() |
| 206 | + { |
| 207 | + background.Alpha = indicator.Alpha = IsHovered ? 1 : 0; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments