2019-01-24 08:43:03 +00: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.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
2021-01-21 05:07:02 +00:00
|
|
|
|
using System.Diagnostics;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using System.Linq;
|
2020-04-13 09:23:28 +00:00
|
|
|
|
using JetBrains.Annotations;
|
2020-11-23 04:49:14 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 10:04:31 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-02-26 06:06:30 +00:00
|
|
|
|
using osu.Framework.Layout;
|
2020-12-22 15:51:12 +00:00
|
|
|
|
using osu.Framework.Utils;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A container that can scroll to each section inside it.
|
|
|
|
|
/// </summary>
|
2020-11-23 04:49:14 +00:00
|
|
|
|
[Cached]
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public class SectionsContainer<T> : Container<T>
|
|
|
|
|
where T : Drawable
|
|
|
|
|
{
|
2020-04-13 11:12:51 +00:00
|
|
|
|
public Bindable<T> SelectedSection { get; } = new Bindable<T>();
|
2020-12-22 15:51:12 +00:00
|
|
|
|
private Drawable lastClickedSection;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
public Drawable ExpandableHeader
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => expandableHeader;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == expandableHeader) return;
|
|
|
|
|
|
2020-09-03 08:11:34 +00:00
|
|
|
|
if (expandableHeader != null)
|
|
|
|
|
RemoveInternal(expandableHeader);
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
expandableHeader = value;
|
2020-09-03 08:11:34 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
if (value == null) return;
|
|
|
|
|
|
|
|
|
|
AddInternal(expandableHeader);
|
2021-01-21 05:31:31 +00:00
|
|
|
|
lastKnownScroll = null;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Drawable FixedHeader
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => fixedHeader;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == fixedHeader) return;
|
|
|
|
|
|
|
|
|
|
fixedHeader?.Expire();
|
|
|
|
|
fixedHeader = value;
|
|
|
|
|
if (value == null) return;
|
|
|
|
|
|
|
|
|
|
AddInternal(fixedHeader);
|
2021-01-21 05:31:31 +00:00
|
|
|
|
lastKnownScroll = null;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Drawable Footer
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => footer;
|
2018-04-13 09:19:50 +00: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);
|
2021-01-21 05:31:31 +00:00
|
|
|
|
lastKnownScroll = null;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Drawable HeaderBackground
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => headerBackground;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == headerBackground) return;
|
|
|
|
|
|
|
|
|
|
headerBackgroundContainer.Clear();
|
|
|
|
|
headerBackground = value;
|
2020-04-13 11:12:51 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
if (value == null) return;
|
|
|
|
|
|
|
|
|
|
headerBackgroundContainer.Add(headerBackground);
|
|
|
|
|
|
2021-01-21 05:31:31 +00:00
|
|
|
|
lastKnownScroll = null;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 11:12:51 +00:00
|
|
|
|
protected override Container<T> Content => scrollContentContainer;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-12-22 15:36:44 +00:00
|
|
|
|
private readonly UserTrackingScrollContainer scrollContainer;
|
2020-04-13 11:12:51 +00:00
|
|
|
|
private readonly Container headerBackgroundContainer;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
private readonly MarginPadding originalSectionsMargin;
|
2020-04-13 11:12:51 +00:00
|
|
|
|
private Drawable expandableHeader, fixedHeader, footer, headerBackground;
|
|
|
|
|
private FlowContainer<T> scrollContentContainer;
|
2019-02-28 04:31:40 +00:00
|
|
|
|
|
2021-01-21 05:31:31 +00:00
|
|
|
|
private float? headerHeight, footerHeight;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-01-21 05:31:31 +00:00
|
|
|
|
private float? lastKnownScroll;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-01-21 06:08:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The percentage of the container to consider the centre-point for deciding the active section (and scrolling to a requested section).
|
2021-01-22 07:53:31 +00:00
|
|
|
|
/// </summary>
|
2021-01-22 07:50:22 +00:00
|
|
|
|
private const float scroll_y_centre = 0.1f;
|
2021-01-21 05:30:22 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public SectionsContainer()
|
|
|
|
|
{
|
2020-04-13 09:23:28 +00:00
|
|
|
|
AddRangeInternal(new Drawable[]
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-04-13 09:23:28 +00:00
|
|
|
|
scrollContainer = CreateScrollContainer().With(s =>
|
|
|
|
|
{
|
|
|
|
|
s.RelativeSizeAxes = Axes.Both;
|
|
|
|
|
s.Masking = true;
|
|
|
|
|
s.ScrollbarVisible = false;
|
2020-04-13 11:12:51 +00:00
|
|
|
|
s.Child = scrollContentContainer = CreateScrollContentContainer();
|
2020-04-13 09:23:28 +00:00
|
|
|
|
}),
|
|
|
|
|
headerBackgroundContainer = new Container
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
});
|
2020-04-13 11:12:51 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
originalSectionsMargin = scrollContentContainer.Margin;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 11:12:51 +00:00
|
|
|
|
public override void Add(T drawable)
|
|
|
|
|
{
|
|
|
|
|
base.Add(drawable);
|
2021-01-21 05:07:02 +00:00
|
|
|
|
|
|
|
|
|
Debug.Assert(drawable != null);
|
|
|
|
|
|
2021-01-21 05:31:31 +00:00
|
|
|
|
lastKnownScroll = null;
|
|
|
|
|
headerHeight = null;
|
|
|
|
|
footerHeight = null;
|
2020-04-13 11:12:51 +00:00
|
|
|
|
}
|
2020-04-13 09:23:28 +00:00
|
|
|
|
|
2020-12-22 15:51:12 +00:00
|
|
|
|
public void ScrollTo(Drawable section)
|
|
|
|
|
{
|
|
|
|
|
lastClickedSection = section;
|
2021-01-21 06:08:36 +00:00
|
|
|
|
scrollContainer.ScrollTo(scrollContainer.GetChildPosInContent(section) - scrollContainer.DisplayableContent * scroll_y_centre - (FixedHeader?.BoundingBox.Height ?? 0));
|
2020-12-22 15:51:12 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
public void ScrollToTop() => scrollContainer.ScrollTo(0);
|
|
|
|
|
|
2020-04-13 11:12:51 +00:00
|
|
|
|
[NotNull]
|
2020-12-22 15:36:44 +00:00
|
|
|
|
protected virtual UserTrackingScrollContainer CreateScrollContainer() => new UserTrackingScrollContainer();
|
2020-04-13 11:12:51 +00:00
|
|
|
|
|
|
|
|
|
[NotNull]
|
|
|
|
|
protected virtual FlowContainer<T> CreateScrollContentContainer() =>
|
|
|
|
|
new FillFlowContainer<T>
|
|
|
|
|
{
|
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-26 06:06:30 +00:00
|
|
|
|
protected override bool OnInvalidate(Invalidation invalidation, InvalidationSource source)
|
2018-12-22 20:50:25 +00:00
|
|
|
|
{
|
2020-02-26 06:06:30 +00:00
|
|
|
|
var result = base.OnInvalidate(invalidation, source);
|
2018-12-22 20:50:25 +00:00
|
|
|
|
|
2020-02-26 06:06:30 +00:00
|
|
|
|
if (source == InvalidationSource.Child && (invalidation & Invalidation.DrawSize) != 0)
|
2018-12-22 20:50:25 +00:00
|
|
|
|
{
|
2021-01-21 05:31:31 +00:00
|
|
|
|
lastKnownScroll = null;
|
2020-02-26 06:06:30 +00:00
|
|
|
|
result = true;
|
2018-12-22 20:50:25 +00:00
|
|
|
|
}
|
2020-02-26 06:06:30 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
2018-12-22 20:50:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
|
{
|
|
|
|
|
base.UpdateAfterChildren();
|
|
|
|
|
|
2021-01-21 05:52:41 +00:00
|
|
|
|
float fixedHeaderSize = (FixedHeader?.LayoutSize.Y ?? 0);
|
|
|
|
|
float expandableHeaderSize = ExpandableHeader?.LayoutSize.Y ?? 0;
|
|
|
|
|
|
|
|
|
|
float headerH = expandableHeaderSize + fixedHeaderSize;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
float footerH = Footer?.LayoutSize.Y ?? 0;
|
2019-04-01 03:16:05 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
if (headerH != headerHeight || footerH != footerHeight)
|
|
|
|
|
{
|
|
|
|
|
headerHeight = headerH;
|
|
|
|
|
footerHeight = footerH;
|
|
|
|
|
updateSectionsMargin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float currentScroll = scrollContainer.Current;
|
|
|
|
|
|
|
|
|
|
if (currentScroll != lastKnownScroll)
|
|
|
|
|
{
|
|
|
|
|
lastKnownScroll = currentScroll;
|
|
|
|
|
|
2020-12-22 15:51:12 +00:00
|
|
|
|
// reset last clicked section because user started scrolling themselves
|
|
|
|
|
if (scrollContainer.UserScrolling)
|
|
|
|
|
lastClickedSection = null;
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
if (ExpandableHeader != null && FixedHeader != null)
|
|
|
|
|
{
|
2021-01-21 05:52:41 +00:00
|
|
|
|
float offset = Math.Min(expandableHeaderSize, currentScroll);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
ExpandableHeader.Y = -offset;
|
2021-01-21 05:52:41 +00:00
|
|
|
|
FixedHeader.Y = -offset + expandableHeaderSize;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 05:52:41 +00:00
|
|
|
|
headerBackgroundContainer.Height = expandableHeaderSize + fixedHeaderSize;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
headerBackgroundContainer.Y = ExpandableHeader?.Y ?? 0;
|
|
|
|
|
|
2021-01-21 05:40:55 +00:00
|
|
|
|
var smallestSectionHeight = Children.Count > 0 ? Children.Min(d => d.Height) : 0;
|
|
|
|
|
|
2020-12-22 15:51:12 +00:00
|
|
|
|
// scroll offset is our fixed header height if we have it plus 20% of content height
|
|
|
|
|
// plus 5% to fix floating point errors and to not have a section instantly unselect when scrolling upwards
|
|
|
|
|
// but the 5% can't be bigger than our smallest section height, otherwise it won't get selected correctly
|
2021-01-21 06:08:36 +00:00
|
|
|
|
float selectionLenienceAboveSection = Math.Min(smallestSectionHeight / 2.0f, scrollContainer.DisplayableContent * 0.05f);
|
2021-01-21 05:44:47 +00:00
|
|
|
|
|
2021-01-21 06:08:36 +00:00
|
|
|
|
float scrollCentre = fixedHeaderSize + scrollContainer.DisplayableContent * scroll_y_centre + selectionLenienceAboveSection;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-12-22 15:51:12 +00:00
|
|
|
|
if (Precision.AlmostBigger(0, scrollContainer.Current))
|
|
|
|
|
SelectedSection.Value = lastClickedSection as T ?? Children.FirstOrDefault();
|
2021-01-21 05:44:47 +00:00
|
|
|
|
else if (Precision.AlmostBigger(scrollContainer.Current, scrollContainer.ScrollableExtent))
|
2020-12-22 15:51:12 +00:00
|
|
|
|
SelectedSection.Value = lastClickedSection as T ?? Children.LastOrDefault();
|
2021-01-21 05:44:47 +00:00
|
|
|
|
else
|
2021-01-21 05:46:35 +00:00
|
|
|
|
{
|
|
|
|
|
SelectedSection.Value = Children
|
2021-01-21 06:08:36 +00:00
|
|
|
|
.TakeWhile(section => scrollContainer.GetChildPosInContent(section) - currentScroll - scrollCentre <= 0)
|
2021-01-21 05:46:35 +00:00
|
|
|
|
.LastOrDefault() ?? Children.FirstOrDefault();
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-13 11:12:51 +00:00
|
|
|
|
|
|
|
|
|
private void updateSectionsMargin()
|
|
|
|
|
{
|
|
|
|
|
if (!Children.Any()) return;
|
|
|
|
|
|
|
|
|
|
var newMargin = originalSectionsMargin;
|
2021-01-21 05:31:31 +00:00
|
|
|
|
|
|
|
|
|
newMargin.Top += (headerHeight ?? 0);
|
|
|
|
|
newMargin.Bottom += (footerHeight ?? 0);
|
2020-04-13 11:12:51 +00:00
|
|
|
|
|
|
|
|
|
scrollContentContainer.Margin = newMargin;
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|