1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 14:12:54 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/Toolbar.cs

202 lines
7.0 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
using System;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Game.Rulesets;
using osu.Framework.Input.Bindings;
using osu.Game.Input.Bindings;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Toolbar
{
2021-09-01 03:39:18 +08:00
public class Toolbar : OverlayContainer, IKeyBindingHandler<GlobalAction>
2018-04-13 17:19:50 +08:00
{
public const float HEIGHT = 40;
public const float TOOLTIP_HEIGHT = 30;
2020-11-10 07:16:35 +08:00
/// <summary>
/// Whether the user hid this <see cref="Toolbar"/> with <see cref="GlobalAction.ToggleToolbar"/>.
2021-03-13 22:29:47 +08:00
/// In this state, automatic toggles should not occur, respecting the user's preference to have no toolbar.
2020-11-10 07:16:35 +08:00
/// </summary>
private bool hiddenByUser;
2018-04-13 17:19:50 +08:00
public Action OnHome;
2019-03-28 13:01:06 +08:00
private ToolbarUserButton userButton;
private ToolbarRulesetSelector rulesetSelector;
2018-04-13 17:19:50 +08:00
private const double transition_time = 500;
2020-08-28 02:07:24 +08:00
protected readonly IBindable<OverlayActivation> OverlayActivationMode = new Bindable<OverlayActivation>(OverlayActivation.All);
// Toolbar and its components need keyboard input even when hidden.
public override bool PropagateNonPositionalInputSubTree => true;
2021-09-01 04:40:13 +08:00
protected override bool BlockScrollInput => false;
2019-03-08 14:14:07 +08:00
public Toolbar()
{
RelativeSizeAxes = Axes.X;
Size = new Vector2(1, HEIGHT);
AlwaysPresent = true;
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
// this only needed to be set for the initial LoadComplete/Update, so layout completes and gets buttons in a state they can correctly handle keyboard input for hotkeys.
AlwaysPresent = false;
2019-03-08 14:14:07 +08:00
}
2019-03-08 11:01:40 +08:00
[BackgroundDependencyLoader(true)]
private void load(OsuGame osuGame, Bindable<RulesetInfo> parentRuleset)
2018-04-13 17:19:50 +08:00
{
Children = new Drawable[]
{
new ToolbarBackground(),
new FillFlowContainer
{
Direction = FillDirection.Horizontal,
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Children = new Drawable[]
{
new ToolbarSettingsButton(),
new ToolbarHomeButton
{
Action = () => OnHome?.Invoke()
},
rulesetSelector = new ToolbarRulesetSelector()
2018-04-13 17:19:50 +08:00
}
},
new FillFlowContainer
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Direction = FillDirection.Horizontal,
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Children = new Drawable[]
{
2020-07-16 19:48:40 +08:00
new ToolbarNewsButton(),
2019-05-13 16:01:17 +08:00
new ToolbarChangelogButton(),
2020-02-13 18:39:33 +08:00
new ToolbarRankingsButton(),
new ToolbarBeatmapListingButton(),
2018-04-13 17:19:50 +08:00
new ToolbarChatButton(),
new ToolbarSocialButton(),
2021-04-22 17:16:12 +08:00
new ToolbarWikiButton(),
2018-04-13 17:19:50 +08:00
new ToolbarMusicButton(),
//new ToolbarButton
//{
2019-04-02 18:55:24 +08:00
// Icon = FontAwesome.Solid.search
2018-04-13 17:19:50 +08:00
//},
2019-03-28 13:01:06 +08:00
userButton = new ToolbarUserButton(),
2018-04-13 17:19:50 +08:00
new ToolbarNotificationButton(),
}
}
};
2019-06-26 16:52:25 +08:00
// Bound after the selector is added to the hierarchy to give it a chance to load the available rulesets
rulesetSelector.Current.BindTo(parentRuleset);
if (osuGame != null)
OverlayActivationMode.BindTo(osuGame.OverlayActivationMode);
}
2018-04-13 17:19:50 +08:00
public class ToolbarBackground : Container
{
private readonly Box gradientBackground;
public ToolbarBackground()
{
RelativeSizeAxes = Axes.Both;
Children = new Drawable[]
{
new Box
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.Gray(0.1f),
},
gradientBackground = new Box
{
RelativeSizeAxes = Axes.X,
Anchor = Anchor.BottomLeft,
Alpha = 0,
Height = 100,
2018-04-13 17:19:50 +08:00
Colour = ColourInfo.GradientVertical(
OsuColour.Gray(0).Opacity(0.9f), OsuColour.Gray(0).Opacity(0)),
2018-04-13 17:19:50 +08:00
},
};
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
gradientBackground.FadeIn(transition_time, Easing.OutQuint);
return true;
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
gradientBackground.FadeOut(transition_time, Easing.OutQuint);
}
}
protected override void UpdateState(ValueChangedEvent<Visibility> state)
{
2021-03-13 22:29:01 +08:00
bool blockShow = hiddenByUser || OverlayActivationMode.Value == OverlayActivation.Disabled;
2021-03-13 22:29:01 +08:00
if (state.NewValue == Visibility.Visible && blockShow)
{
State.Value = Visibility.Hidden;
return;
}
base.UpdateState(state);
}
2018-04-13 17:19:50 +08:00
protected override void PopIn()
{
this.MoveToY(0, transition_time, Easing.OutQuint);
2020-07-08 14:06:40 +08:00
this.FadeIn(transition_time / 4, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
protected override void PopOut()
{
userButton.StateContainer?.Hide();
2018-04-13 17:19:50 +08:00
this.MoveToY(-DrawSize.Y, transition_time, Easing.OutQuint);
2020-07-08 14:06:40 +08:00
this.FadeOut(transition_time, Easing.InQuint);
2018-04-13 17:19:50 +08:00
}
2021-09-16 17:26:12 +08:00
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (OverlayActivationMode.Value == OverlayActivation.Disabled)
return false;
2021-09-16 17:26:12 +08:00
switch (e.Action)
{
case GlobalAction.ToggleToolbar:
2021-03-13 22:29:01 +08:00
hiddenByUser = State.Value == Visibility.Visible; // set before toggling to allow the operation to always succeed.
ToggleVisibility();
return true;
}
return false;
}
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
2018-04-13 17:19:50 +08:00
}
}