1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-07 00:27:26 +08:00
osu-lazer/osu.Game/Graphics/Containers/SectionsContainer.cs

172 lines
5.5 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
namespace osu.Game.Graphics.Containers
{
/// <summary>
/// A container that can scroll to each section inside it.
/// </summary>
public class SectionsContainer : Container
{
2017-05-21 03:44:03 +08:00
private Drawable expandableHeader, fixedHeader, footer;
public readonly ScrollContainer ScrollContainer;
2017-05-20 07:20:46 +08:00
private readonly Container<Drawable> sectionsContainer;
public Drawable ExpandableHeader
{
get { return expandableHeader; }
set
{
if (value == expandableHeader) return;
2017-05-20 07:07:27 +08:00
if (expandableHeader != null)
Remove(expandableHeader);
expandableHeader = value;
2017-05-21 02:11:55 +08:00
if (value == null) return;
Add(expandableHeader);
lastKnownScroll = float.NaN;
}
}
public Drawable FixedHeader
{
get { return fixedHeader; }
set
{
if (value == fixedHeader) return;
2017-05-20 07:07:27 +08:00
if (fixedHeader != null)
Remove(fixedHeader);
fixedHeader = value;
2017-05-21 02:11:55 +08:00
if (value == null) return;
Add(fixedHeader);
lastKnownScroll = float.NaN;
}
}
2017-05-21 03:44:03 +08:00
public Drawable Footer
{
get { return footer; }
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 Bindable<Drawable> SelectedSection { get; } = new Bindable<Drawable>();
2017-05-21 03:02:51 +08:00
protected virtual Container<Drawable> CreateScrollContentContainer()
2017-05-20 07:20:46 +08:00
=> new FillFlowContainer
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Both
};
private List<Drawable> sections = new List<Drawable>();
public IEnumerable<Drawable> Sections
{
get { return sections; }
set
{
foreach (var section in sections)
2017-05-20 07:20:46 +08:00
sectionsContainer.Remove(section);
sections = value.ToList();
if (sections.Count == 0) return;
2017-05-20 07:20:46 +08:00
sectionsContainer.Add(sections);
SelectedSection.Value = sections[0];
lastKnownScroll = float.NaN;
}
}
2017-05-21 03:44:03 +08:00
private float headerHeight, footerHeight;
2017-05-21 04:48:43 +08:00
private readonly MarginPadding originalSectionsMargin;
2017-05-21 03:44:03 +08:00
private void updateSectionsMargin()
{
if (sections.Count == 0) return;
2017-05-21 03:44:03 +08:00
var newMargin = originalSectionsMargin;
2017-05-21 02:11:55 +08:00
newMargin.Top += headerHeight;
2017-05-21 03:44:03 +08:00
newMargin.Bottom += footerHeight;
2017-05-21 03:44:03 +08:00
sectionsContainer.Margin = newMargin;
}
public SectionsContainer()
{
Add(ScrollContainer = new ScrollContainer()
{
2017-05-20 07:20:46 +08:00
RelativeSizeAxes = Axes.Both,
Masking = false,
2017-05-21 03:02:51 +08:00
Children = new Drawable[] { sectionsContainer = CreateScrollContentContainer() }
});
2017-05-21 03:44:03 +08:00
originalSectionsMargin = sectionsContainer.Margin;
}
2017-05-21 04:48:43 +08:00
private float lastKnownScroll;
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
2017-05-21 04:48:43 +08:00
float headerH = (ExpandableHeader?.LayoutSize.Y ?? 0) + (FixedHeader?.LayoutSize.Y ?? 0);
float footerH = Footer?.LayoutSize.Y ?? 0;
if (headerH != headerHeight || footerH != footerHeight)
2017-05-21 02:11:55 +08:00
{
2017-05-21 04:48:43 +08:00
headerHeight = headerH;
footerHeight = footerH;
2017-05-21 03:44:03 +08:00
updateSectionsMargin();
2017-05-21 02:11:55 +08:00
}
float currentScroll = Math.Max(0, ScrollContainer.Current);
if (currentScroll != lastKnownScroll)
{
lastKnownScroll = currentScroll;
if (expandableHeader != null && fixedHeader != null)
2017-05-21 02:11:55 +08:00
{
float offset = Math.Min(expandableHeader.LayoutSize.Y, currentScroll);
2017-05-21 02:11:55 +08:00
expandableHeader.Y = -offset;
fixedHeader.Y = -offset + expandableHeader.LayoutSize.Y;
2017-05-21 02:11:55 +08:00
}
Drawable bestMatch = null;
float minDiff = float.MaxValue;
foreach (var section in sections)
{
float diff = Math.Abs(ScrollContainer.GetChildPosInContent(section) - currentScroll);
if (diff < minDiff)
{
minDiff = diff;
bestMatch = section;
}
}
if (bestMatch != null)
SelectedSection.Value = bestMatch;
}
}
}
}