1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 21:42:56 +08:00
osu-lazer/osu.Game/Overlays/News/Sidebar/MonthSection.cs

186 lines
6.0 KiB
C#
Raw Normal View History

2021-05-10 13:47:00 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2022-06-17 15:37:17 +08:00
#nullable disable
2021-05-10 13:47:00 +08:00
using System;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Graphics.Containers;
using osuTK;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics;
using System.Linq;
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Sprites;
using System.Diagnostics;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Game.Online.Chat;
2021-05-10 13:47:00 +08:00
namespace osu.Game.Overlays.News.Sidebar
{
2022-11-24 13:32:20 +08:00
public partial class MonthSection : CompositeDrawable
2021-05-10 13:47:00 +08:00
{
public int Year { get; private set; }
public int Month { get; private set; }
public readonly BindableBool Expanded = new BindableBool();
2021-05-11 19:43:23 +08:00
private const int animation_duration = 250;
private Sample sampleOpen;
private Sample sampleClose;
2021-05-11 19:43:23 +08:00
2021-05-17 15:02:21 +08:00
public MonthSection(int month, int year, IEnumerable<APINewsPost> posts)
2021-05-10 13:47:00 +08:00
{
Debug.Assert(posts.All(p => p.PublishedAt.Month == month && p.PublishedAt.Year == year));
Year = year;
Month = month;
2021-05-11 21:51:59 +08:00
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2021-05-11 19:43:23 +08:00
Masking = true;
2021-05-19 14:48:31 +08:00
2021-05-10 13:47:00 +08:00
InternalChild = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
new DropdownHeader(month, year)
2021-05-10 13:47:00 +08:00
{
2021-05-19 14:48:31 +08:00
Expanded = { BindTarget = Expanded }
2021-05-10 13:47:00 +08:00
},
new PostsContainer
2021-05-10 13:47:00 +08:00
{
2021-05-19 14:48:31 +08:00
Expanded = { BindTarget = Expanded },
2023-05-21 07:38:56 +08:00
Children = posts.Select(p => new PostLink(p)).ToArray()
2021-05-10 13:47:00 +08:00
}
}
};
Expanded.ValueChanged += expanded =>
{
if (expanded.NewValue)
sampleOpen?.Play();
else
sampleClose?.Play();
};
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleOpen = audio.Samples.Get(@"UI/dropdown-open");
sampleClose = audio.Samples.Get(@"UI/dropdown-close");
2021-05-10 13:47:00 +08:00
}
2022-11-24 13:32:20 +08:00
private partial class DropdownHeader : OsuClickableContainer
2021-05-11 19:43:23 +08:00
{
2021-05-19 14:48:31 +08:00
public readonly BindableBool Expanded = new BindableBool();
2021-05-10 13:47:00 +08:00
private readonly SpriteIcon icon;
public DropdownHeader(int month, int year)
2021-05-10 13:47:00 +08:00
{
var date = new DateTime(year, month, 1);
RelativeSizeAxes = Axes.X;
Height = 15;
2021-05-19 14:48:31 +08:00
Action = Expanded.Toggle;
2021-05-10 13:47:00 +08:00
Children = new Drawable[]
{
new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
Text = date.ToLocalisableString(@"MMM yyyy")
2021-05-10 13:47:00 +08:00
},
icon = new SpriteIcon
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Size = new Vector2(10),
Icon = FontAwesome.Solid.ChevronDown
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
2021-05-19 14:48:31 +08:00
Expanded.BindValueChanged(open =>
2021-05-10 13:47:00 +08:00
{
icon.Scale = new Vector2(1, open.NewValue ? -1 : 1);
}, true);
}
}
2023-05-21 07:38:56 +08:00
private partial class PostLink : LinkFlowContainer
2021-05-10 13:47:00 +08:00
{
2023-05-21 07:38:56 +08:00
public PostLink(APINewsPost post)
: base(t => t.Font = OsuFont.GetFont(size: 12))
2021-05-10 13:47:00 +08:00
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
AddLink(post.Title, LinkAction.External, @"/home/news/" + post.Slug, "view in browser");
2021-05-10 13:47:00 +08:00
}
}
2022-11-24 13:32:20 +08:00
private partial class PostsContainer : Container
{
2021-05-19 14:48:31 +08:00
public readonly BindableBool Expanded = new BindableBool();
2021-05-19 14:45:24 +08:00
protected override Container<Drawable> Content { get; }
public PostsContainer()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
AutoSizeDuration = animation_duration;
AutoSizeEasing = Easing.Out;
2021-05-19 14:45:24 +08:00
InternalChild = Content = new FillFlowContainer
{
Margin = new MarginPadding { Top = 5 },
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 5),
Alpha = 0
};
}
protected override void LoadComplete()
{
base.LoadComplete();
2021-05-19 14:48:31 +08:00
Expanded.BindValueChanged(updateState, true);
}
2021-05-19 14:48:31 +08:00
private void updateState(ValueChangedEvent<bool> expanded)
{
ClearTransforms(true);
2021-05-19 14:48:31 +08:00
if (expanded.NewValue)
{
AutoSizeAxes = Axes.Y;
2021-05-19 14:45:24 +08:00
Content.FadeIn(animation_duration, Easing.OutQuint);
}
else
{
AutoSizeAxes = Axes.None;
this.ResizeHeightTo(0, animation_duration, Easing.OutQuint);
2021-05-19 14:45:24 +08:00
Content.FadeOut(animation_duration, Easing.OutQuint);
}
}
}
2021-05-10 13:47:00 +08:00
}
}