1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 10:07:25 +08:00
osu-lazer/osu.Game/Graphics/Containers/SectionsContainer.cs

219 lines
6.8 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
using System.Linq;
using JetBrains.Annotations;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-02-26 14:06:30 +08:00
using osu.Framework.Layout;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.Containers
{
/// <summary>
/// A container that can scroll to each section inside it.
/// </summary>
public class SectionsContainer<T> : Container<T>
where T : Drawable
{
public Bindable<T> SelectedSection { get; } = new Bindable<T>();
2018-04-13 17:19:50 +08:00
public Drawable ExpandableHeader
{
get => expandableHeader;
2018-04-13 17:19:50 +08:00
set
{
if (value == expandableHeader) return;
expandableHeader?.Expire();
expandableHeader = value;
if (value == null) return;
AddInternal(expandableHeader);
lastKnownScroll = float.NaN;
}
}
public Drawable FixedHeader
{
get => fixedHeader;
2018-04-13 17:19:50 +08:00
set
{
if (value == fixedHeader) return;
fixedHeader?.Expire();
fixedHeader = value;
if (value == null) return;
AddInternal(fixedHeader);
lastKnownScroll = float.NaN;
}
}
public Drawable Footer
{
get => footer;
2018-04-13 17:19:50 +08:00
set
{
if (value == footer) return;
if (footer != null)
scrollContainer.Remove(footer);
footer = value;
if (value == null) return;
footer.Anchor |= Anchor.y2;
footer.Origin |= Anchor.y2;
scrollContainer.Add(footer);
lastKnownScroll = float.NaN;
}
}
public Drawable HeaderBackground
{
get => headerBackground;
2018-04-13 17:19:50 +08:00
set
{
if (value == headerBackground) return;
headerBackgroundContainer.Clear();
headerBackground = value;
2018-04-13 17:19:50 +08:00
if (value == null) return;
headerBackgroundContainer.Add(headerBackground);
lastKnownScroll = float.NaN;
}
}
protected override Container<T> Content => scrollContentContainer;
2018-04-13 17:19:50 +08:00
private readonly OsuScrollContainer scrollContainer;
private readonly Container headerBackgroundContainer;
2018-04-13 17:19:50 +08:00
private readonly MarginPadding originalSectionsMargin;
private Drawable expandableHeader, fixedHeader, footer, headerBackground;
private FlowContainer<T> scrollContentContainer;
2019-02-28 12:31:40 +08:00
private float headerHeight, footerHeight;
2018-04-13 17:19:50 +08:00
private float lastKnownScroll;
2018-04-13 17:19:50 +08:00
public SectionsContainer()
{
AddRangeInternal(new Drawable[]
2018-04-13 17:19:50 +08:00
{
scrollContainer = CreateScrollContainer().With(s =>
{
s.RelativeSizeAxes = Axes.Both;
s.Masking = true;
s.ScrollbarVisible = false;
s.Child = scrollContentContainer = CreateScrollContentContainer();
}),
headerBackgroundContainer = new Container
{
RelativeSizeAxes = Axes.X
}
2018-04-13 17:19:50 +08:00
});
2018-04-13 17:19:50 +08:00
originalSectionsMargin = scrollContentContainer.Margin;
}
public override void Add(T drawable)
{
base.Add(drawable);
lastKnownScroll = float.NaN;
headerHeight = float.NaN;
footerHeight = float.NaN;
}
public void ScrollTo(Drawable section) =>
scrollContainer.ScrollTo(scrollContainer.GetChildPosInContent(section) - (FixedHeader?.BoundingBox.Height ?? 0));
2018-04-13 17:19:50 +08:00
public void ScrollToTop() => scrollContainer.ScrollTo(0);
[NotNull]
protected virtual OsuScrollContainer CreateScrollContainer() => new OsuScrollContainer();
[NotNull]
protected virtual FlowContainer<T> CreateScrollContentContainer() =>
new FillFlowContainer<T>
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
};
2020-02-26 14:06:30 +08:00
protected override bool OnInvalidate(Invalidation invalidation, InvalidationSource source)
2018-12-23 04:50:25 +08:00
{
2020-02-26 14:06:30 +08:00
var result = base.OnInvalidate(invalidation, source);
2018-12-23 04:50:25 +08:00
2020-02-26 14:06:30 +08:00
if (source == InvalidationSource.Child && (invalidation & Invalidation.DrawSize) != 0)
2018-12-23 04:50:25 +08:00
{
2020-02-26 14:06:30 +08:00
lastKnownScroll = -1;
result = true;
2018-12-23 04:50:25 +08:00
}
2020-02-26 14:06:30 +08:00
return result;
2018-12-23 04:50:25 +08:00
}
2018-04-13 17:19:50 +08:00
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
float headerH = (ExpandableHeader?.LayoutSize.Y ?? 0) + (FixedHeader?.LayoutSize.Y ?? 0);
float footerH = Footer?.LayoutSize.Y ?? 0;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
if (headerH != headerHeight || footerH != footerHeight)
{
headerHeight = headerH;
footerHeight = footerH;
updateSectionsMargin();
}
float currentScroll = scrollContainer.Current;
if (currentScroll != lastKnownScroll)
{
lastKnownScroll = currentScroll;
if (ExpandableHeader != null && FixedHeader != null)
{
float offset = Math.Min(ExpandableHeader.LayoutSize.Y, currentScroll);
ExpandableHeader.Y = -offset;
FixedHeader.Y = -offset + ExpandableHeader.LayoutSize.Y;
}
headerBackgroundContainer.Height = (ExpandableHeader?.LayoutSize.Y ?? 0) + (FixedHeader?.LayoutSize.Y ?? 0);
headerBackgroundContainer.Y = ExpandableHeader?.Y ?? 0;
float scrollOffset = FixedHeader?.LayoutSize.Y ?? 0;
Func<T, float> diff = section => scrollContainer.GetChildPosInContent(section) - currentScroll - scrollOffset;
2018-04-13 17:19:50 +08:00
if (scrollContainer.IsScrolledToEnd())
2018-04-13 17:19:50 +08:00
{
SelectedSection.Value = Children.LastOrDefault();
}
else
{
SelectedSection.Value = Children.TakeWhile(section => diff(section) <= 0).LastOrDefault()
2020-05-15 20:31:05 +08:00
?? Children.FirstOrDefault();
2018-04-13 17:19:50 +08:00
}
}
}
private void updateSectionsMargin()
{
if (!Children.Any()) return;
var newMargin = originalSectionsMargin;
newMargin.Top += headerHeight;
newMargin.Bottom += footerHeight;
scrollContentContainer.Margin = newMargin;
}
2018-04-13 17:19:50 +08:00
}
}