1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 15:27:25 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/DrawableCarouselItem.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

170 lines
4.8 KiB
C#
Raw Normal View History

// 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 17:19:50 +08:00
2020-10-12 14:36:03 +08:00
using System.Diagnostics;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-10-12 14:36:03 +08:00
using osu.Framework.Graphics.Pooling;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osuTK;
2018-04-13 17:19:50 +08:00
2017-12-12 16:48:38 +08:00
namespace osu.Game.Screens.Select.Carousel
{
2020-10-12 14:36:03 +08:00
public abstract partial class DrawableCarouselItem : PoolableDrawable
{
public const float MAX_HEIGHT = 80;
2018-04-13 17:19:50 +08:00
2020-10-12 14:36:03 +08:00
public override bool IsPresent => base.IsPresent || Item?.Visible == true;
2018-04-13 17:19:50 +08:00
public override bool HandlePositionalInput => Item?.Visible == true;
public override bool PropagatePositionalInputSubTree => Item?.Visible == true;
public readonly CarouselHeader Header;
2020-10-13 14:32:37 +08:00
/// <summary>
/// Optional content which sits below the header.
/// </summary>
protected readonly Container<Drawable> Content;
protected readonly Container MovementContainer;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) =>
Header.ReceivePositionalInputAt(screenSpacePos);
2023-01-09 02:02:48 +08:00
private CarouselItem? item;
2023-01-09 02:02:48 +08:00
public CarouselItem? Item
2020-10-12 14:36:03 +08:00
{
get => item;
set
{
if (item == value)
return;
if (item != null)
{
item.Filtered.ValueChanged -= onStateChange;
item.State.ValueChanged -= onStateChange;
Header.State.UnbindFrom(item.State);
if (item is CarouselGroup group)
{
foreach (var c in group.Items)
c.Filtered.ValueChanged -= onStateChange;
}
2020-10-12 14:36:03 +08:00
}
2018-04-13 17:19:50 +08:00
2020-10-12 14:36:03 +08:00
item = value;
if (IsLoaded)
UpdateItem();
}
}
2018-04-13 17:19:50 +08:00
protected DrawableCarouselItem()
{
RelativeSizeAxes = Axes.X;
Alpha = 0;
InternalChildren = new Drawable[]
{
MovementContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
Header = new CarouselHeader(),
Content = new Container
{
RelativeSizeAxes = Axes.Both,
}
}
},
2017-12-18 06:58:48 +08:00
};
}
public void SetMultiplicativeAlpha(float alpha) => Header.BorderContainer.Alpha = alpha;
protected override void LoadComplete()
{
base.LoadComplete();
2018-04-13 17:19:50 +08:00
UpdateItem();
}
protected override void Update()
{
base.Update();
Content.Y = Header.Height;
}
2020-10-12 14:36:03 +08:00
protected virtual void UpdateItem()
{
if (Item == null)
2020-10-12 14:36:03 +08:00
return;
2020-10-12 17:13:25 +08:00
Scheduler.AddOnce(ApplyState);
2020-10-12 14:36:03 +08:00
Item.Filtered.ValueChanged += onStateChange;
Item.State.ValueChanged += onStateChange;
Header.State.BindTo(Item.State);
if (Item is CarouselGroup group)
{
foreach (var c in group.Items)
c.Filtered.ValueChanged += onStateChange;
}
}
2018-04-13 17:19:50 +08:00
2020-10-12 17:13:25 +08:00
private void onStateChange(ValueChangedEvent<CarouselItemState> obj) => Scheduler.AddOnce(ApplyState);
2020-10-12 14:36:03 +08:00
2020-10-12 17:13:25 +08:00
private void onStateChange(ValueChangedEvent<bool> _) => Scheduler.AddOnce(ApplyState);
2020-10-12 14:36:03 +08:00
2017-12-12 16:48:38 +08:00
protected virtual void ApplyState()
{
2023-01-10 16:52:28 +08:00
Debug.Assert(Item != null);
// Use the fact that we know the precise height of the item from the model to avoid the need for AutoSize overhead.
// Additionally, AutoSize doesn't work well due to content starting off-screen and being masked away.
Height = Item.TotalHeight;
2018-04-13 17:19:50 +08:00
switch (Item.State.Value)
{
case CarouselItemState.NotSelected:
Deselected();
break;
case CarouselItemState.Selected:
Selected();
break;
}
2018-04-13 17:19:50 +08:00
2017-12-12 16:48:38 +08:00
if (!Item.Visible)
this.FadeOut(300, Easing.OutQuint);
else
this.FadeIn(250);
}
2018-04-13 17:19:50 +08:00
protected virtual void Selected()
{
2020-10-12 14:36:03 +08:00
Debug.Assert(Item != null);
}
2018-04-13 17:19:50 +08:00
protected virtual void Deselected()
{
}
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
2016-11-02 05:57:11 +08:00
{
Debug.Assert(Item != null);
Item.State.Value = CarouselItemState.Selected;
2016-11-02 05:57:11 +08:00
return true;
}
}
}