From 18295a9b97cc84d7f87a3c39d5f6b7897007ebc4 Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Sun, 25 Jun 2017 10:06:54 +0800 Subject: [PATCH] Handle scrolling in SectionsContainer. --- .../Graphics/Containers/SectionsContainer.cs | 23 ++++++++++++++----- osu.Game/Overlays/SettingsOverlay.cs | 3 +-- osu.Game/Overlays/UserProfileOverlay.cs | 3 +-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/osu.Game/Graphics/Containers/SectionsContainer.cs b/osu.Game/Graphics/Containers/SectionsContainer.cs index 63cc6f1a85..8221c5d3a6 100644 --- a/osu.Game/Graphics/Containers/SectionsContainer.cs +++ b/osu.Game/Graphics/Containers/SectionsContainer.cs @@ -16,7 +16,7 @@ namespace osu.Game.Graphics.Containers where T : Drawable { private Drawable expandableHeader, fixedHeader, footer, headerBackground; - public readonly ScrollContainer ScrollContainer; + private readonly ScrollContainer scrollContainer; private readonly Container headerBackgroundContainer; private readonly FlowContainer scrollContentContainer; @@ -62,13 +62,13 @@ namespace osu.Game.Graphics.Containers if (value == footer) return; if (footer != null) - ScrollContainer.Remove(footer); + scrollContainer.Remove(footer); footer = value; if (value == null) return; footer.Anchor |= Anchor.y2; footer.Origin |= Anchor.y2; - ScrollContainer.Add(footer); + scrollContainer.Add(footer); lastKnownScroll = float.NaN; } } @@ -122,10 +122,11 @@ namespace osu.Game.Graphics.Containers public SectionsContainer() { - AddInternal(ScrollContainer = new ScrollContainer() + AddInternal(scrollContainer = new ScrollContainer() { RelativeSizeAxes = Axes.Both, Masking = true, + ScrollbarVisible = false, Children = new Drawable[] { scrollContentContainer = CreateScrollContentContainer() }, Depth = float.MaxValue }); @@ -137,6 +138,15 @@ namespace osu.Game.Graphics.Containers 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; protected override void UpdateAfterChildren() { @@ -151,7 +161,7 @@ namespace osu.Game.Graphics.Containers updateSectionsMargin(); } - float currentScroll = Math.Max(0, ScrollContainer.Current); + float currentScroll = Math.Max(0, scrollContainer.Current); if (currentScroll != lastKnownScroll) { lastKnownScroll = currentScroll; @@ -169,10 +179,11 @@ namespace osu.Game.Graphics.Containers T bestMatch = null; float minDiff = float.MaxValue; + float scrollOffset = FixedHeader?.LayoutSize.Y ?? 0; 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) { minDiff = diff; diff --git a/osu.Game/Overlays/SettingsOverlay.cs b/osu.Game/Overlays/SettingsOverlay.cs index 267489fc79..2734aa642d 100644 --- a/osu.Game/Overlays/SettingsOverlay.cs +++ b/osu.Game/Overlays/SettingsOverlay.cs @@ -93,7 +93,7 @@ namespace osu.Game.Overlays new SidebarButton { Section = section, - Action = sectionsContainer.ScrollContainer.ScrollIntoView, + Action = sectionsContainer.ScrollToTop } ).ToArray() } @@ -162,7 +162,6 @@ namespace osu.Game.Overlays public SettingsSectionsContainer() { - ScrollContainer.ScrollbarVisible = false; HeaderBackground = new Box { Colour = Color4.Black, diff --git a/osu.Game/Overlays/UserProfileOverlay.cs b/osu.Game/Overlays/UserProfileOverlay.cs index fc6a050d3e..1cc11bc4e9 100644 --- a/osu.Game/Overlays/UserProfileOverlay.cs +++ b/osu.Game/Overlays/UserProfileOverlay.cs @@ -112,7 +112,6 @@ namespace osu.Game.Overlays RelativeSizeAxes = Axes.Both } }; - sectionsContainer.ScrollContainer.ScrollbarVisible = false; Add(sectionsContainer); sectionsContainer.SelectedSection.ValueChanged += s => { @@ -135,7 +134,7 @@ namespace osu.Game.Overlays if (lastSection != s) { lastSection = s; - sectionsContainer.ScrollContainer.ScrollIntoView(lastSection); + sectionsContainer.ScrollToTop(lastSection); } };