1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-22 05:13:21 +08:00

Refactor SectionsContainer to generic.

This commit is contained in:
Huo Yaoyuan 2017-06-09 16:24:19 +08:00
parent c60ef2449f
commit 798d8711b8
3 changed files with 33 additions and 41 deletions

View File

@ -2,7 +2,6 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -13,11 +12,15 @@ namespace osu.Game.Graphics.Containers
/// <summary> /// <summary>
/// A container that can scroll to each section inside it. /// A container that can scroll to each section inside it.
/// </summary> /// </summary>
public class SectionsContainer : Container public class SectionsContainer<T> : Container<T>
where T : Drawable
{ {
private Drawable expandableHeader, fixedHeader, footer, headerBackground; private Drawable expandableHeader, fixedHeader, footer, headerBackground;
public readonly ScrollContainer ScrollContainer; public readonly ScrollContainer ScrollContainer;
private readonly Container<Drawable> sectionsContainer, headerBackgroundContainer; private readonly Container headerBackgroundContainer;
private readonly FlowContainer<T> sectionsContainer;
protected override Container<T> Content => sectionsContainer;
public Drawable ExpandableHeader public Drawable ExpandableHeader
{ {
@ -26,12 +29,11 @@ namespace osu.Game.Graphics.Containers
{ {
if (value == expandableHeader) return; if (value == expandableHeader) return;
if (expandableHeader != null) expandableHeader?.Expire();
Remove(expandableHeader);
expandableHeader = value; expandableHeader = value;
if (value == null) return; if (value == null) return;
Add(expandableHeader); AddInternal(expandableHeader);
lastKnownScroll = float.NaN; lastKnownScroll = float.NaN;
} }
} }
@ -43,12 +45,11 @@ namespace osu.Game.Graphics.Containers
{ {
if (value == fixedHeader) return; if (value == fixedHeader) return;
if (fixedHeader != null) fixedHeader?.Expire();
Remove(fixedHeader);
fixedHeader = value; fixedHeader = value;
if (value == null) return; if (value == null) return;
Add(fixedHeader); AddInternal(fixedHeader);
lastKnownScroll = float.NaN; lastKnownScroll = float.NaN;
} }
} }
@ -88,39 +89,27 @@ namespace osu.Game.Graphics.Containers
} }
} }
public Bindable<Drawable> SelectedSection { get; } = new Bindable<Drawable>(); public Bindable<T> SelectedSection { get; } = new Bindable<T>();
protected virtual Container<Drawable> CreateScrollContentContainer() protected virtual FlowContainer<T> CreateScrollContentContainer()
=> new FillFlowContainer => new FillFlowContainer<T>
{ {
Direction = FillDirection.Vertical, Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Y, AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
}; };
private List<Drawable> sections = new List<Drawable>(); public override void Add(T drawable)
public IEnumerable<Drawable> Sections
{ {
get { return sections; } base.Add(drawable);
set
{
foreach (var section in sections)
sectionsContainer.Remove(section);
sections = value.ToList();
if (sections.Count == 0) return;
sectionsContainer.Add(sections);
SelectedSection.Value = sections[0];
lastKnownScroll = float.NaN; lastKnownScroll = float.NaN;
} }
}
private float headerHeight, footerHeight; private float headerHeight, footerHeight;
private readonly MarginPadding originalSectionsMargin; private readonly MarginPadding originalSectionsMargin;
private void updateSectionsMargin() private void updateSectionsMargin()
{ {
if (sections.Count == 0) return; if (!Children.Any()) return;
var newMargin = originalSectionsMargin; var newMargin = originalSectionsMargin;
newMargin.Top += headerHeight; newMargin.Top += headerHeight;
@ -131,14 +120,14 @@ namespace osu.Game.Graphics.Containers
public SectionsContainer() public SectionsContainer()
{ {
Add(ScrollContainer = new ScrollContainer() AddInternal(ScrollContainer = new ScrollContainer()
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Masking = true, Masking = true,
Children = new Drawable[] { sectionsContainer = CreateScrollContentContainer() }, Children = new Drawable[] { sectionsContainer = CreateScrollContentContainer() },
Depth = float.MaxValue Depth = float.MaxValue
}); });
Add(headerBackgroundContainer = new Container<Drawable> AddInternal(headerBackgroundContainer = new Container
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Depth = float.MaxValue / 2 Depth = float.MaxValue / 2
@ -176,10 +165,10 @@ namespace osu.Game.Graphics.Containers
headerBackgroundContainer.Height = (ExpandableHeader?.LayoutSize.Y ?? 0) + (FixedHeader?.LayoutSize.Y ?? 0); headerBackgroundContainer.Height = (ExpandableHeader?.LayoutSize.Y ?? 0) + (FixedHeader?.LayoutSize.Y ?? 0);
headerBackgroundContainer.Y = ExpandableHeader?.Y ?? 0; headerBackgroundContainer.Y = ExpandableHeader?.Y ?? 0;
Drawable bestMatch = null; T bestMatch = null;
float minDiff = float.MaxValue; float minDiff = float.MaxValue;
foreach (var section in sections) foreach (var section in Children)
{ {
float diff = Math.Abs(ScrollContainer.GetChildPosInContent(section) - currentScroll); float diff = Math.Abs(ScrollContainer.GetChildPosInContent(section) - currentScroll);
if (diff < minDiff) if (diff < minDiff)

View File

@ -83,7 +83,7 @@ namespace osu.Game.Overlays
}, },
Exit = Hide, Exit = Hide,
}, },
Sections = sections, Children = sections,
Footer = new SettingsFooter() Footer = new SettingsFooter()
}, },
sidebar = new Sidebar sidebar = new Sidebar
@ -148,12 +148,12 @@ namespace osu.Game.Overlays
base.OnFocus(state); base.OnFocus(state);
} }
private class SettingsSectionsContainer : SectionsContainer private class SettingsSectionsContainer : SectionsContainer<SettingsSection>
{ {
public SearchContainer SearchContainer; public SearchContainer<SettingsSection> SearchContainer;
protected override Container<Drawable> CreateScrollContentContainer() protected override FlowContainer<SettingsSection> CreateScrollContentContainer()
=> SearchContainer = new SearchContainer => SearchContainer = new SearchContainer<SettingsSection>
{ {
AutoSizeAxes = Axes.Y, AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,

View File

@ -1,6 +1,7 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>. // Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Linq;
using OpenTK; using OpenTK;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions; using osu.Framework.Extensions.IEnumerableExtensions;
@ -51,7 +52,7 @@ namespace osu.Game.Users
Colour = OsuColour.Gray(0.2f) Colour = OsuColour.Gray(0.2f)
}); });
var sectionsContainer = new SectionsContainer var sectionsContainer = new SectionsContainer<ProfileSection>
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
ExpandableHeader = new ProfileHeader(user), ExpandableHeader = new ProfileHeader(user),
@ -61,7 +62,7 @@ namespace osu.Game.Users
Colour = OsuColour.Gray(34), Colour = OsuColour.Gray(34),
RelativeSizeAxes = Axes.Both RelativeSizeAxes = Axes.Both
}, },
Sections = sections Children = sections
}; };
Add(sectionsContainer); Add(sectionsContainer);
@ -69,7 +70,7 @@ namespace osu.Game.Users
{ {
if (lastSection != s) if (lastSection != s)
{ {
lastSection = s as ProfileSection; lastSection = s;
tabs.Current.Value = lastSection; tabs.Current.Value = lastSection;
} }
}; };
@ -78,7 +79,9 @@ namespace osu.Game.Users
{ {
if (lastSection == null) if (lastSection == null)
{ {
lastSection = tabs.Current.Value = sections[0]; lastSection = sectionsContainer.Children.FirstOrDefault();
if (lastSection != null)
tabs.Current.Value = lastSection;
return; return;
} }
if (lastSection != s) if (lastSection != s)