osu/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs

136 lines
4.4 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 09:19:50 +00:00
using osu.Framework.Allocation;
using osu.Framework.Caching;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-04-02 05:51:28 +00:00
using osu.Framework.Graphics.Effects;
2018-11-20 07:51:59 +00:00
using osuTK;
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.Shapes;
using osu.Game.Rulesets;
2019-06-08 15:27:40 +00:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Bindables;
using osu.Framework.Input.Events;
using osuTK.Input;
using System.Linq;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Overlays.Toolbar
{
2019-06-08 15:27:40 +00:00
public class ToolbarRulesetSelector : TabControl<RulesetInfo>
2018-04-13 09:19:50 +00:00
{
private const float padding = 10;
private readonly Drawable modeButtonLine;
private RulesetStore rulesets;
2019-06-08 15:27:40 +00:00
public override bool HandleNonPositionalInput => !Current.Disabled && base.HandleNonPositionalInput;
public override bool HandlePositionalInput => !Current.Disabled && base.HandlePositionalInput;
public override bool PropagatePositionalInputSubTree => !Current.Disabled && base.PropagatePositionalInputSubTree;
private void disabledChanged(bool isDisabled) => this.FadeColour(isDisabled ? Color4.Gray : Color4.White, 300);
protected override Dropdown<RulesetInfo> CreateDropdown() => null;
protected override TabItem<RulesetInfo> CreateTabItem(RulesetInfo value) => new ToolbarRulesetButton(value);
2018-04-13 09:19:50 +00:00
2018-07-09 16:20:21 +00:00
public ToolbarRulesetSelector()
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Y;
AutoSizeAxes = Axes.X;
2018-04-13 09:19:50 +00:00
2019-06-10 00:35:00 +00:00
AddRangeInternal(new[]
2018-04-13 09:19:50 +00:00
{
2019-06-08 15:27:40 +00:00
new OpaqueBackground
2018-04-13 09:19:50 +00:00
{
2019-06-08 15:27:40 +00:00
Depth = 1,
2018-04-13 09:19:50 +00:00
},
modeButtonLine = new Container
{
2019-06-10 00:35:00 +00:00
Size = new Vector2(padding * 2 + ToolbarButton.WIDTH, 3),
2018-04-13 09:19:50 +00:00
Anchor = Anchor.BottomLeft,
Origin = Anchor.TopLeft,
Masking = true,
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Glow,
Colour = new Color4(255, 194, 224, 100),
Radius = 15,
Roundness = 15,
},
2019-06-08 15:27:40 +00:00
Child = new Box
2018-04-13 09:19:50 +00:00
{
2019-06-08 15:27:40 +00:00
RelativeSizeAxes = Axes.Both,
2018-04-13 09:19:50 +00:00
}
}
2019-06-08 15:27:40 +00:00
});
2018-04-13 09:19:50 +00:00
}
2019-06-08 15:27:40 +00:00
protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Direction = FillDirection.Horizontal,
Padding = new MarginPadding { Left = padding, Right = padding },
};
[BackgroundDependencyLoader]
private void load(RulesetStore rulesets, Bindable<RulesetInfo> parentRuleset)
2018-04-13 09:19:50 +00:00
{
this.rulesets = rulesets;
2019-04-01 03:16:05 +00:00
2018-04-13 09:19:50 +00:00
foreach (var r in rulesets.AvailableRulesets)
{
2019-06-08 15:27:40 +00:00
AddItem(r);
2018-04-13 09:19:50 +00:00
}
2019-06-08 15:27:40 +00:00
Current.BindTo(parentRuleset);
Current.DisabledChanged += disabledChanged;
Current.BindValueChanged(rulesetChanged);
}
2018-10-02 03:02:47 +00:00
protected override bool OnKeyDown(KeyDownEvent e)
{
2018-10-02 03:02:47 +00:00
base.OnKeyDown(e);
if (e.ControlPressed && !e.Repeat && e.Key >= Key.Number1 && e.Key <= Key.Number9)
{
2018-10-02 03:02:47 +00:00
int requested = e.Key - Key.Number1;
RulesetInfo found = rulesets.AvailableRulesets.Skip(requested).FirstOrDefault();
if (found != null)
2019-06-08 15:27:40 +00:00
Current.Value = found;
return true;
}
return false;
2018-04-13 09:19:50 +00:00
}
2019-06-08 15:27:40 +00:00
private readonly Cached activeMode = new Cached();
2018-04-13 09:19:50 +00:00
2019-02-21 09:56:34 +00:00
private void rulesetChanged(ValueChangedEvent<RulesetInfo> e)
2018-04-13 09:19:50 +00:00
{
activeMode.Invalidate();
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
if (!activeMode.IsValid)
{
2019-06-08 15:27:40 +00:00
foreach (TabItem<RulesetInfo> tabItem in TabContainer)
{
if (tabItem.Value == Current.Value)
{
modeButtonLine.MoveToX(tabItem.DrawPosition.X, 200, Easing.OutQuint);
activeMode.Validate();
return;
}
}
2018-04-13 09:19:50 +00:00
}
}
}
}