1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-09 09:37:42 +08:00
osu-lazer/osu.Game/Overlays/Dialog/PopupDialog.cs

280 lines
11 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.Collections.Generic;
using System.Linq;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-04-02 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Containers;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Dialog
{
2019-09-13 14:27:29 +08:00
public abstract class PopupDialog : VisibilityContainer
2018-04-13 17:19:50 +08:00
{
2019-11-12 20:07:01 +08:00
public const float ENTER_DURATION = 500;
public const float EXIT_DURATION = 200;
2018-04-13 17:19:50 +08:00
private readonly Vector2 ringSize = new Vector2(100f);
private readonly Vector2 ringMinifiedSize = new Vector2(20f);
private readonly Vector2 buttonsEnterSpacing = new Vector2(0f, 50f);
private readonly Container content;
private readonly Container ring;
private readonly FillFlowContainer<PopupDialogButton> buttonsContainer;
private readonly SpriteIcon icon;
private readonly TextFlowContainer header;
2018-04-13 17:19:50 +08:00
private readonly TextFlowContainer body;
private bool actionInvoked;
public IconUsage Icon
2018-04-13 17:19:50 +08:00
{
get => icon.Icon;
set => icon.Icon = value;
2018-04-13 17:19:50 +08:00
}
2020-07-06 21:01:45 +08:00
private string headerText;
2018-04-13 17:19:50 +08:00
public string HeaderText
{
2020-07-06 21:01:45 +08:00
get => headerText;
set
{
2020-07-06 21:01:45 +08:00
if (headerText == value)
return;
2019-02-28 12:31:40 +08:00
2020-07-06 21:01:45 +08:00
headerText = value;
header.Text = value;
}
2018-04-13 17:19:50 +08:00
}
2020-07-06 21:01:45 +08:00
private string bodyText;
2018-04-13 17:19:50 +08:00
public string BodyText
{
2020-07-06 21:01:45 +08:00
get => bodyText;
set
{
if (bodyText == value)
return;
bodyText = value;
body.Text = value;
}
2018-04-13 17:19:50 +08:00
}
public IEnumerable<PopupDialogButton> Buttons
{
get => buttonsContainer.Children;
2018-04-13 17:19:50 +08:00
set
{
buttonsContainer.ChildrenEnumerable = value;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
foreach (PopupDialogButton b in value)
{
var action = b.Action;
b.Action = () =>
{
if (actionInvoked) return;
actionInvoked = true;
2018-04-13 17:19:50 +08:00
action?.Invoke();
Hide();
2018-04-13 17:19:50 +08:00
};
}
}
}
// We always want dialogs to show their appear animation, so we request they start hidden.
// Normally this would not be required, but is here due to the manual Show() call that occurs before LoadComplete().
protected override bool StartHidden => true;
2019-09-13 14:42:36 +08:00
protected PopupDialog()
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both;
Children = new Drawable[]
{
content = new Container
{
RelativeSizeAxes = Axes.Both,
Alpha = 0f,
Children = new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true,
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.5f),
Radius = 8,
},
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4Extensions.FromHex(@"221a21"),
2018-04-13 17:19:50 +08:00
},
new Triangles
{
RelativeSizeAxes = Axes.Both,
ColourLight = Color4Extensions.FromHex(@"271e26"),
ColourDark = Color4Extensions.FromHex(@"1e171e"),
2018-04-13 17:19:50 +08:00
TriangleScale = 4,
},
},
},
new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
2019-06-24 10:43:30 +08:00
Spacing = new Vector2(0f, 10f),
Padding = new MarginPadding { Bottom = 10 },
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
new Container
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Size = ringSize,
Children = new Drawable[]
{
ring = new CircularContainer
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Masking = true,
BorderColour = Color4.White,
BorderThickness = 5f,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black.Opacity(0),
},
icon = new SpriteIcon
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
2019-04-02 18:55:24 +08:00
Icon = FontAwesome.Solid.TimesCircle,
2018-04-13 17:19:50 +08:00
Size = new Vector2(50),
},
},
},
},
},
header = new OsuTextFlowContainer(t => t.Font = t.Font.With(size: 25))
2018-04-13 17:19:50 +08:00
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
TextAnchor = Anchor.TopCentre,
2018-04-13 17:19:50 +08:00
},
body = new OsuTextFlowContainer(t => t.Font = t.Font.With(size: 18))
2018-04-13 17:19:50 +08:00
{
2019-06-24 10:43:30 +08:00
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
TextAnchor = Anchor.TopCentre,
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
},
},
},
buttonsContainer = new FillFlowContainer<PopupDialogButton>
{
Anchor = Anchor.Centre,
Origin = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
},
},
},
};
// It's important we start in a visible state so our state fires on hide, even before load.
// This is used by the DialogOverlay to know when the dialog was dismissed.
Show();
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// Programmatically clicks the first <see cref="PopupDialogOkButton"/>.
/// </summary>
public void PerformOkAction() => Buttons.OfType<PopupDialogOkButton>().First().Click();
2018-10-02 11:02:47 +08:00
protected override bool OnKeyDown(KeyDownEvent e)
{
2018-10-02 11:02:47 +08:00
if (e.Repeat) return false;
// press button at number if 1-9 on number row or keypad are pressed
2018-10-02 11:02:47 +08:00
var k = e.Key;
2019-04-01 11:16:05 +08:00
if (k >= Key.Number1 && k <= Key.Number9)
{
pressButtonAtIndex(k - Key.Number1);
return true;
}
if (k >= Key.Keypad1 && k <= Key.Keypad9)
{
pressButtonAtIndex(k - Key.Keypad1);
return true;
}
2018-10-02 11:02:47 +08:00
return base.OnKeyDown(e);
}
protected override void PopIn()
{
actionInvoked = false;
// Reset various animations but only if the dialog animation fully completed
if (content.Alpha == 0)
{
buttonsContainer.TransformSpacingTo(buttonsEnterSpacing);
buttonsContainer.MoveToY(buttonsEnterSpacing.Y);
ring.ResizeTo(ringMinifiedSize);
}
content.FadeIn(ENTER_DURATION, Easing.OutQuint);
ring.ResizeTo(ringSize, ENTER_DURATION, Easing.OutQuint);
buttonsContainer.TransformSpacingTo(Vector2.Zero, ENTER_DURATION, Easing.OutQuint);
buttonsContainer.MoveToY(0, ENTER_DURATION, Easing.OutQuint);
}
protected override void PopOut()
{
if (!actionInvoked && content.IsPresent)
// In the case a user did not choose an action before a hide was triggered, press the last button.
// This is presumed to always be a sane default "cancel" action.
buttonsContainer.Last().Click();
content.FadeOut(EXIT_DURATION, Easing.InSine);
}
private void pressButtonAtIndex(int index)
{
if (index < Buttons.Count())
Buttons.Skip(index).First().Click();
}
2018-04-13 17:19:50 +08:00
}
}