Refactor to replace existing panels

This commit is contained in:
Andrei Zavatski
2020-03-04 14:58:15 +03:00
parent f8f13a2f28
commit 6631b07442
15 changed files with 175 additions and 560 deletions

View File

@ -3,10 +3,8 @@
using System;
using osuTK;
using osuTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
@ -16,32 +14,18 @@ using osu.Game.Overlays;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.UserInterface;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics.Containers;
using osu.Game.Overlays.Profile.Header.Components;
using osu.Game.Users.Drawables;
using JetBrains.Annotations;
using osu.Framework.Input.Events;
namespace osu.Game.Users
{
public class UserPanel : OsuClickableContainer, IHasContextMenu
public abstract class UserPanel : OsuClickableContainer, IHasContextMenu
{
private const float height = 100;
private const float content_padding = 10;
private const float status_height = 30;
public readonly User User;
[Resolved(canBeNull: true)]
private OsuColour colours { get; set; }
private Container statusBar;
private Box statusBg;
private OsuSpriteText statusMessage;
private Container content;
protected override Container<Drawable> Content => content;
public readonly Bindable<UserStatus> Status = new Bindable<UserStatus>();
public readonly IBindable<UserActivity> Activity = new Bindable<UserActivity>();
@ -50,164 +34,63 @@ namespace osu.Game.Users
protected Action ViewProfile;
public UserPanel(User user)
protected DelayedLoadUnloadWrapper Background;
private SpriteIcon statusIcon;
private OsuSpriteText statusMessage;
private TextFlowContainer lastVisitMessage;
protected UserPanel(User user)
{
if (user == null)
throw new ArgumentNullException(nameof(user));
User = user;
Height = height - status_height;
}
[BackgroundDependencyLoader(permitNulls: true)]
private void load(UserProfileOverlay profile)
[Resolved(canBeNull: true)]
private UserProfileOverlay profileOverlay { get; set; }
[Resolved]
private OsuColour colours { get; set; }
[BackgroundDependencyLoader]
private void load()
{
if (colours == null)
throw new InvalidOperationException($"{nameof(colours)} not initialized!");
Action = () => profileOverlay?.ShowUser(User);
FillFlowContainer infoContainer;
Masking = true;
BorderColour = colours.GreyVioletLighter;
AddInternal(content = new Container
AddRange(new[]
{
RelativeSizeAxes = Axes.Both,
Masking = true,
CornerRadius = 5,
EdgeEffect = new EdgeEffectParameters
new Box
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.25f),
Radius = 4,
RelativeSizeAxes = Axes.Both,
Colour = colours.Gray1
},
Children = new Drawable[]
Background = new DelayedLoadUnloadWrapper(() => new UserCoverBackground
{
new DelayedLoadUnloadWrapper(() => new UserCoverBackground
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
User = User,
}, 300, 5000)
{
RelativeSizeAxes = Axes.Both,
},
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black.Opacity(0.7f),
},
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Top = content_padding, Horizontal = content_padding },
Children = new Drawable[]
{
new UpdateableAvatar
{
Size = new Vector2(height - status_height - content_padding * 2),
User = User,
Masking = true,
CornerRadius = 5,
OpenOnClick = { Value = false },
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.25f),
Radius = 4,
},
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Left = height - status_height - content_padding },
Children = new Drawable[]
{
new OsuSpriteText
{
Text = User.Username,
Font = OsuFont.GetFont(weight: FontWeight.SemiBold, size: 18, italics: true),
},
infoContainer = new FillFlowContainer
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
AutoSizeAxes = Axes.X,
Height = 20f,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5f, 0f),
Children = new Drawable[]
{
new UpdateableFlag(User.Country)
{
Width = 30f,
RelativeSizeAxes = Axes.Y,
},
},
},
},
},
},
},
statusBar = new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
Alpha = 0f,
Children = new Drawable[]
{
statusBg = new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0.5f,
},
new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Spacing = new Vector2(5f, 0f),
Children = new Drawable[]
{
new SpriteIcon
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Icon = FontAwesome.Regular.Circle,
Shadow = true,
Size = new Vector2(14),
},
statusMessage = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(weight: FontWeight.SemiBold),
},
},
},
},
},
}
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
User = User,
}, 300, 5000)
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
RelativeSizeAxes = Axes.Both,
},
CreateLayout()
});
if (User.IsSupporter)
{
infoContainer.Add(new SupporterIcon
{
Height = 20f,
SupportLevel = User.SupportLevel
});
}
Status.ValueChanged += status => displayStatus(status.NewValue, Activity.Value);
Activity.ValueChanged += activity => displayStatus(Status.Value, activity.NewValue);
base.Action = ViewProfile = () =>
{
Action?.Invoke();
profile?.ShowUser(User);
profileOverlay?.ShowUser(User);
};
}
@ -217,33 +100,105 @@ namespace osu.Game.Users
Status.TriggerChange();
}
protected override bool OnHover(HoverEvent e)
{
BorderThickness = 2;
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
BorderThickness = 0;
base.OnHoverLost(e);
}
[NotNull]
protected abstract Drawable CreateLayout();
protected UpdateableAvatar CreateAvatar() => new UpdateableAvatar
{
User = User,
OpenOnClick = { Value = false }
};
protected UpdateableFlag CreateFlag() => new UpdateableFlag(User.Country)
{
Size = new Vector2(39, 26)
};
protected OsuSpriteText CreateUsername() => new OsuSpriteText
{
Font = OsuFont.GetFont(size: 20, weight: FontWeight.Bold, italics: true),
Text = User.Username,
};
protected SpriteIcon CreateStatusIcon() => statusIcon = new SpriteIcon
{
Icon = FontAwesome.Regular.Circle,
Size = new Vector2(25)
};
protected FillFlowContainer CreateStatusMessage(bool rightAlignedChildren)
{
var statusContainer = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical
};
var alignment = rightAlignedChildren ? Anchor.x2 : Anchor.x0;
statusContainer.Add(lastVisitMessage = new TextFlowContainer(t => t.Font = OsuFont.GetFont(size: 15)).With(text =>
{
text.Anchor = Anchor.y1 | alignment;
text.Origin = Anchor.y1 | alignment;
text.AutoSizeAxes = Axes.Both;
text.Alpha = 0;
if (User.LastVisit.HasValue)
{
text.AddText(@"Last seen ");
text.AddText(new DrawableDate(User.LastVisit.Value, italic: false));
}
}));
statusContainer.Add(statusMessage = new OsuSpriteText
{
Anchor = Anchor.y1 | alignment,
Origin = Anchor.y1 | alignment,
Font = OsuFont.GetFont(size: 17)
});
return statusContainer;
}
private void displayStatus(UserStatus status, UserActivity activity = null)
{
const float transition_duration = 500;
if (status != null)
{
if (activity != null && !(status is UserStatusOffline))
{
statusMessage.Text = activity.Status;
statusIcon.FadeColour(activity.GetAppropriateColour(colours), 500, Easing.OutQuint);
}
else
{
lastVisitMessage.FadeTo(status is UserStatusOffline && User.LastVisit.HasValue ? 1 : 0);
statusMessage.Text = status.Message;
statusIcon.FadeColour(status.GetAppropriateColour(colours), 500, Easing.OutQuint);
}
if (status == null)
{
statusBar.ResizeHeightTo(0f, transition_duration, Easing.OutQuint);
statusBar.FadeOut(transition_duration, Easing.OutQuint);
this.ResizeHeightTo(height - status_height, transition_duration, Easing.OutQuint);
}
else
{
statusBar.ResizeHeightTo(status_height, transition_duration, Easing.OutQuint);
statusBar.FadeIn(transition_duration, Easing.OutQuint);
this.ResizeHeightTo(height, transition_duration, Easing.OutQuint);
return;
}
if (status is UserStatusOnline && activity != null)
// Set local status according to web if it's null
if (User.IsOnline)
{
statusMessage.Text = activity.Status;
statusBg.FadeColour(activity.GetAppropriateColour(colours), 500, Easing.OutQuint);
}
else
{
statusMessage.Text = status?.Message;
statusBg.FadeColour(status?.GetAppropriateColour(colours) ?? colours.Gray5, 500, Easing.OutQuint);
Status.Value = new UserStatusOnline();
return;
}
Status.Value = new UserStatusOffline();
}
public MenuItem[] ContextMenuItems => new MenuItem[]