1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 02:07:24 +08:00
osu-lazer/osu.Game/Overlays/Dialog/PopupDialog.cs

274 lines
10 KiB
C#
Raw Normal View History

2017-02-24 12:05:37 +08:00
// 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.Collections.Generic;
2017-02-24 12:05:37 +08:00
using System.Linq;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
2017-02-24 12:05:37 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2017-02-27 11:35:13 +08:00
using osu.Framework.Graphics.Primitives;
2017-02-24 12:05:37 +08:00
using osu.Framework.Graphics.Sprites;
2017-02-28 04:41:45 +08:00
using osu.Framework.Graphics.Transforms;
2017-02-24 12:05:37 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Backgrounds;
namespace osu.Game.Overlays.Dialog
{
public class PopupDialog : FocusedOverlayContainer
2017-02-24 12:05:37 +08:00
{
public static readonly float ENTER_DURATION = 500;
public static readonly float EXIT_DURATION = 200;
2017-02-28 08:55:10 +08:00
private readonly Vector2 ringSize = new Vector2(100f);
private readonly Vector2 ringMinifiedSize = new Vector2(20f);
private readonly Vector2 buttonsEnterSpacing = new Vector2(0f, 50f);
2017-02-24 12:05:37 +08:00
2017-02-27 11:35:13 +08:00
private Container content, ring;
private FlowContainer<PopupDialogButton> buttonsContainer;
2017-02-24 12:05:37 +08:00
private TextAwesome iconText;
2017-02-27 11:35:13 +08:00
private SpriteText header, body;
2017-02-24 12:05:37 +08:00
public FontAwesome Icon
{
get
{
return iconText.Icon;
}
set
{
iconText.Icon = value;
}
}
public string HeaderText
{
get
{
2017-02-27 11:35:13 +08:00
return header.Text;
2017-02-24 12:05:37 +08:00
}
set
{
2017-02-27 11:35:13 +08:00
header.Text = value;
2017-02-24 12:05:37 +08:00
}
}
public string BodyText
{
get
{
2017-02-27 11:35:13 +08:00
return body.Text;
2017-02-24 12:05:37 +08:00
}
set
{
2017-02-27 11:35:13 +08:00
body.Text = value;
2017-02-24 12:05:37 +08:00
}
}
public PopupDialogButton[] Buttons
{
get
{
2017-02-27 11:35:13 +08:00
return buttonsContainer.Children.ToArray();
2017-02-24 12:05:37 +08:00
}
set
{
buttonsContainer.Children = value;
foreach (PopupDialogButton b in value)
{
var action = b.Action;
b.Action = () =>
{
Hide();
action?.Invoke();
};
}
}
}
private void pressButtonAtIndex(int index)
{
if (index < Buttons.Length)
Buttons[index].TriggerClick();
}
private bool triggeredButton = false; // used to make it so the user can't press multiple buttons at once with the keyboard
protected override bool OnKeyDown(Framework.Input.InputState state, Framework.Input.KeyDownEventArgs args)
{
if (args.Repeat) return false;
if (!triggeredButton)
{
if (args.Key == Key.Enter)
{
Buttons.OfType<PopupDialogOkButton>()?.FirstOrDefault()?.TriggerClick();
triggeredButton = true;
return true;
}
// press button at number if 1-9 on number row or keypad are pressed
var k = args.Key;
if (k >= Key.Number1 && k <= Key.Number9)
{
pressButtonAtIndex(k - Key.Number1);
triggeredButton = true;
return true;
}
else if (k >= Key.Keypad1 && k <= Key.Keypad9)
{
pressButtonAtIndex(k - Key.Keypad1);
triggeredButton = true;
return true;
}
2017-02-28 10:32:30 +08:00
}
return base.OnKeyDown(state, args);
}
2017-02-24 12:05:37 +08:00
protected override void PopIn()
{
base.PopIn();
triggeredButton = false;
2017-02-27 11:35:13 +08:00
// Reset various animations but only if the dialog animation fully completed
if (content.Alpha == 0)
2017-02-24 12:05:37 +08:00
{
2017-02-28 08:55:10 +08:00
buttonsContainer.TransformSpacingTo(buttonsEnterSpacing);
buttonsContainer.MoveToY(buttonsEnterSpacing.Y);
ring.ResizeTo(ringMinifiedSize);
2017-02-24 12:05:37 +08:00
}
content.FadeIn(ENTER_DURATION, EasingTypes.OutQuint);
ring.ResizeTo(ringSize, ENTER_DURATION, EasingTypes.OutQuint);
buttonsContainer.TransformSpacingTo(Vector2.Zero, ENTER_DURATION, EasingTypes.OutQuint);
buttonsContainer.MoveToY(0, ENTER_DURATION, EasingTypes.OutQuint);
2017-02-24 12:05:37 +08:00
}
protected override void PopOut()
{
base.PopOut();
content.FadeOut(EXIT_DURATION, EasingTypes.InSine);
2017-02-24 12:05:37 +08:00
}
public PopupDialog()
{
RelativeSizeAxes = Axes.Both;
2017-02-24 12:05:37 +08:00
Children = new Drawable[]
{
2017-02-27 11:35:13 +08:00
content = new Container
2017-02-24 12:05:37 +08:00
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.BottomCentre,
2017-02-27 11:35:13 +08:00
Origin = Anchor.BottomCentre,
2017-02-24 12:05:37 +08:00
Width = 0.4f,
Alpha = 0f,
2017-02-24 12:05:37 +08:00
Children = new Drawable[]
{
2017-02-27 11:35:13 +08:00
new Container
2017-02-24 12:05:37 +08:00
{
RelativeSizeAxes = Axes.Both,
2017-02-27 11:35:13 +08:00
Masking = true,
EdgeEffect = new EdgeEffect
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.5f),
Radius = 8,
},
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.FromHex(@"221a21"),
},
new Triangles
{
RelativeSizeAxes = Axes.Both,
ColourLight = OsuColour.FromHex(@"271e26"),
ColourDark = OsuColour.FromHex(@"1e171e"),
TriangleScale = 4,
},
},
2017-02-24 12:05:37 +08:00
},
2017-02-27 11:35:13 +08:00
new FlowContainer
2017-02-24 12:05:37 +08:00
{
2017-02-27 11:35:13 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.BottomCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Position = new Vector2(0f, -50f),
Direction = FlowDirections.Vertical,
Spacing = new Vector2(0f, 10f),
2017-02-24 12:05:37 +08:00
Children = new Drawable[]
{
new Container
{
2017-02-27 11:35:13 +08:00
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
2017-02-28 08:55:10 +08:00
Size = ringSize,
2017-02-27 11:35:13 +08:00
Margin = new MarginPadding
{
Bottom = 30,
},
2017-02-24 12:05:37 +08:00
Children = new Drawable[]
{
2017-02-27 11:35:13 +08:00
ring = new CircularContainer
2017-02-24 12:05:37 +08:00
{
2017-02-27 11:35:13 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
BorderColour = Color4.White,
BorderThickness = 5f,
2017-02-24 12:05:37 +08:00
Children = new Drawable[]
{
2017-02-27 11:35:13 +08:00
new Box
2017-02-24 12:05:37 +08:00
{
2017-02-27 11:35:13 +08:00
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black.Opacity(0),
2017-02-24 12:05:37 +08:00
},
2017-02-27 11:35:13 +08:00
iconText = new TextAwesome
2017-02-24 12:05:37 +08:00
{
2017-02-27 11:35:13 +08:00
Origin = Anchor.Centre,
2017-02-24 12:05:37 +08:00
Anchor = Anchor.Centre,
2017-02-27 11:35:13 +08:00
Icon = FontAwesome.fa_close,
TextSize = 50,
2017-02-24 12:05:37 +08:00
},
},
},
},
},
2017-02-27 11:35:13 +08:00
header = new SpriteText
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Text = @"Header",
TextSize = 25,
Shadow = true,
},
body = new SpriteText
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Text = @"Body",
TextSize = 18,
Shadow = true,
},
2017-02-24 12:05:37 +08:00
},
},
2017-02-27 11:35:13 +08:00
buttonsContainer = new FlowContainer<PopupDialogButton>
2017-02-24 12:05:37 +08:00
{
2017-02-27 11:35:13 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2017-02-24 12:05:37 +08:00
Direction = FlowDirections.Vertical,
},
},
},
};
}
}
}