1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 18:32:55 +08:00

Handle scrolling in SectionsContainer.

This commit is contained in:
Huo Yaoyuan 2017-06-25 10:06:54 +08:00
parent 674e2a4395
commit 18295a9b97
3 changed files with 19 additions and 10 deletions

View File

@ -16,7 +16,7 @@ namespace osu.Game.Graphics.Containers
where T : Drawable where T : Drawable
{ {
private Drawable expandableHeader, fixedHeader, footer, headerBackground; private Drawable expandableHeader, fixedHeader, footer, headerBackground;
public readonly ScrollContainer ScrollContainer; private readonly ScrollContainer scrollContainer;
private readonly Container headerBackgroundContainer; private readonly Container headerBackgroundContainer;
private readonly FlowContainer<T> scrollContentContainer; private readonly FlowContainer<T> scrollContentContainer;
@ -62,13 +62,13 @@ namespace osu.Game.Graphics.Containers
if (value == footer) return; if (value == footer) return;
if (footer != null) if (footer != null)
ScrollContainer.Remove(footer); scrollContainer.Remove(footer);
footer = value; footer = value;
if (value == null) return; if (value == null) return;
footer.Anchor |= Anchor.y2; footer.Anchor |= Anchor.y2;
footer.Origin |= Anchor.y2; footer.Origin |= Anchor.y2;
ScrollContainer.Add(footer); scrollContainer.Add(footer);
lastKnownScroll = float.NaN; lastKnownScroll = float.NaN;
} }
} }
@ -122,10 +122,11 @@ namespace osu.Game.Graphics.Containers
public SectionsContainer() public SectionsContainer()
{ {
AddInternal(ScrollContainer = new ScrollContainer() AddInternal(scrollContainer = new ScrollContainer()
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Masking = true, Masking = true,
ScrollbarVisible = false,
Children = new Drawable[] { scrollContentContainer = CreateScrollContentContainer() }, Children = new Drawable[] { scrollContentContainer = CreateScrollContentContainer() },
Depth = float.MaxValue Depth = float.MaxValue
}); });
@ -137,6 +138,15 @@ namespace osu.Game.Graphics.Containers
originalSectionsMargin = scrollContentContainer.Margin; originalSectionsMargin = scrollContentContainer.Margin;
} }
public void ScrollToTop(T section)
{
float pos = scrollContainer.GetChildPosInContent(section);
float current = scrollContainer.Current;
float scrollOffset = FixedHeader?.LayoutSize.Y ?? 0;
if (section == Children.First() && current < pos - scrollOffset) return;
scrollContainer.ScrollTo(pos - scrollOffset);
}
private float lastKnownScroll; private float lastKnownScroll;
protected override void UpdateAfterChildren() protected override void UpdateAfterChildren()
{ {
@ -151,7 +161,7 @@ namespace osu.Game.Graphics.Containers
updateSectionsMargin(); updateSectionsMargin();
} }
float currentScroll = Math.Max(0, ScrollContainer.Current); float currentScroll = Math.Max(0, scrollContainer.Current);
if (currentScroll != lastKnownScroll) if (currentScroll != lastKnownScroll)
{ {
lastKnownScroll = currentScroll; lastKnownScroll = currentScroll;
@ -169,10 +179,11 @@ namespace osu.Game.Graphics.Containers
T bestMatch = null; T bestMatch = null;
float minDiff = float.MaxValue; float minDiff = float.MaxValue;
float scrollOffset = FixedHeader?.LayoutSize.Y ?? 0;
foreach (var section in Children) foreach (var section in Children)
{ {
float diff = Math.Abs(ScrollContainer.GetChildPosInContent(section) - currentScroll); float diff = Math.Abs(scrollContainer.GetChildPosInContent(section) - currentScroll - scrollOffset);
if (diff < minDiff) if (diff < minDiff)
{ {
minDiff = diff; minDiff = diff;

View File

@ -93,7 +93,7 @@ namespace osu.Game.Overlays
new SidebarButton new SidebarButton
{ {
Section = section, Section = section,
Action = sectionsContainer.ScrollContainer.ScrollIntoView, Action = sectionsContainer.ScrollToTop
} }
).ToArray() ).ToArray()
} }
@ -162,7 +162,6 @@ namespace osu.Game.Overlays
public SettingsSectionsContainer() public SettingsSectionsContainer()
{ {
ScrollContainer.ScrollbarVisible = false;
HeaderBackground = new Box HeaderBackground = new Box
{ {
Colour = Color4.Black, Colour = Color4.Black,

View File

@ -112,7 +112,6 @@ namespace osu.Game.Overlays
RelativeSizeAxes = Axes.Both RelativeSizeAxes = Axes.Both
} }
}; };
sectionsContainer.ScrollContainer.ScrollbarVisible = false;
Add(sectionsContainer); Add(sectionsContainer);
sectionsContainer.SelectedSection.ValueChanged += s => sectionsContainer.SelectedSection.ValueChanged += s =>
{ {
@ -135,7 +134,7 @@ namespace osu.Game.Overlays
if (lastSection != s) if (lastSection != s)
{ {
lastSection = s; lastSection = s;
sectionsContainer.ScrollContainer.ScrollIntoView(lastSection); sectionsContainer.ScrollToTop(lastSection);
} }
}; };