osu/osu.Game/Overlays/Dialog/PopupDialog.cs

317 lines
12 KiB
C#
Raw Normal View History

2017-02-24 04:05:37 +00:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-02-27 03:35:13 +00:00
using System;
2017-02-24 04:05:37 +00:00
using System.Linq;
using OpenTK;
using OpenTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2017-02-27 03:35:13 +00:00
using osu.Framework.Graphics.Primitives;
2017-02-24 04:05:37 +00:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Transformations;
using osu.Framework.MathUtils;
using osu.Game.Graphics;
using osu.Game.Graphics.Backgrounds;
namespace osu.Game.Overlays.Dialog
{
public class PopupDialog : OverlayContainer
{
2017-02-27 03:35:13 +00:00
private const float enter_duration = 500;
private const float exit_duration = 200;
2017-02-24 04:05:37 +00:00
private readonly Vector2 ring_size = new Vector2(100f);
private readonly Vector2 ring_minified_size = new Vector2(20f);
2017-02-27 03:35:13 +00:00
private readonly Vector2 buttons_spacing = new Vector2(0f, 50f);
2017-02-24 04:05:37 +00:00
private PopupDialogTriangles triangles;
2017-02-27 03:35:13 +00:00
private Container content, ring;
private FlowContainer<PopupDialogButton> buttonsContainer;
2017-02-24 04:05:37 +00:00
private TextAwesome iconText;
2017-02-27 03:35:13 +00:00
private SpriteText header, body;
2017-02-24 04:05:37 +00:00
public FontAwesome Icon
{
get
{
return iconText.Icon;
}
set
{
iconText.Icon = value;
}
}
public string HeaderText
{
get
{
2017-02-27 03:35:13 +00:00
return header.Text;
2017-02-24 04:05:37 +00:00
}
set
{
2017-02-27 03:35:13 +00:00
header.Text = value;
2017-02-24 04:05:37 +00:00
}
}
public string BodyText
{
get
{
2017-02-27 03:35:13 +00:00
return body.Text;
2017-02-24 04:05:37 +00:00
}
set
{
2017-02-27 03:35:13 +00:00
body.Text = value;
2017-02-24 04:05:37 +00:00
}
}
public PopupDialogButton[] Buttons
{
get
{
2017-02-27 03:35:13 +00:00
return buttonsContainer.Children.ToArray();
2017-02-24 04:05:37 +00:00
}
set
{
buttonsContainer.Children = value;
foreach (PopupDialogButton b in value)
{
var action = b.Action;
b.Action = () =>
{
Hide();
action?.Invoke();
};
}
}
}
protected override void PopIn()
{
2017-02-27 03:35:13 +00:00
// Reset various animations but only if the dialog animation fully completed
if (content.Alpha == 0)
2017-02-24 04:05:37 +00:00
{
2017-02-27 03:35:13 +00:00
buttonsContainer.TransformSpacingTo(buttons_spacing);
buttonsContainer.MoveToY(buttons_spacing.Y);
ring.ResizeTo(ring_minified_size);
2017-02-24 04:05:37 +00:00
}
triangles.SlideIn();
2017-02-27 03:35:13 +00:00
content.FadeIn(enter_duration, EasingTypes.OutQuint);
ring.ResizeTo(ring_size, enter_duration, EasingTypes.OutQuint);
buttonsContainer.TransformSpacingTo(Vector2.Zero, enter_duration, EasingTypes.OutQuint);
buttonsContainer.MoveToY(0, enter_duration, EasingTypes.OutQuint);
2017-02-24 04:05:37 +00:00
}
protected override void PopOut()
{
triangles.SlideOut();
2017-02-27 03:35:13 +00:00
content.FadeOut(exit_duration, EasingTypes.InSine);
2017-02-24 04:05:37 +00:00
}
public PopupDialog()
{
Children = new Drawable[]
{
triangles = new PopupDialogTriangles
{
RelativeSizeAxes = Axes.Both,
2017-02-27 03:35:13 +00:00
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre,
2017-02-24 04:05:37 +00:00
Width = 0.5f,
},
2017-02-27 03:35:13 +00:00
content = new Container
2017-02-24 04:05:37 +00:00
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.BottomCentre,
2017-02-27 03:35:13 +00:00
Origin = Anchor.BottomCentre,
2017-02-24 04:05:37 +00:00
Width = 0.4f,
Children = new Drawable[]
{
2017-02-27 03:35:13 +00:00
new Container
2017-02-24 04:05:37 +00:00
{
RelativeSizeAxes = Axes.Both,
2017-02-27 03:35:13 +00: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 04:05:37 +00:00
},
2017-02-27 03:35:13 +00:00
new FlowContainer
2017-02-24 04:05:37 +00:00
{
2017-02-27 03:35:13 +00: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 04:05:37 +00:00
Children = new Drawable[]
{
new Container
{
2017-02-27 03:35:13 +00:00
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Size = ring_size,
Margin = new MarginPadding
{
Bottom = 30,
},
2017-02-24 04:05:37 +00:00
Children = new Drawable[]
{
2017-02-27 03:35:13 +00:00
ring = new CircularContainer
2017-02-24 04:05:37 +00:00
{
2017-02-27 03:35:13 +00:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
BorderColour = Color4.White,
BorderThickness = 5f,
2017-02-24 04:05:37 +00:00
Children = new Drawable[]
{
2017-02-27 03:35:13 +00:00
new Box
2017-02-24 04:05:37 +00:00
{
2017-02-27 03:35:13 +00:00
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black.Opacity(0),
2017-02-24 04:05:37 +00:00
},
2017-02-27 03:35:13 +00:00
iconText = new TextAwesome
2017-02-24 04:05:37 +00:00
{
2017-02-27 03:35:13 +00:00
Origin = Anchor.Centre,
2017-02-24 04:05:37 +00:00
Anchor = Anchor.Centre,
2017-02-27 03:35:13 +00:00
Icon = FontAwesome.fa_close,
TextSize = 50,
2017-02-24 04:05:37 +00:00
},
},
},
},
},
2017-02-27 03:35:13 +00: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 04:05:37 +00:00
},
},
2017-02-27 03:35:13 +00:00
buttonsContainer = new FlowContainer<PopupDialogButton>
2017-02-24 04:05:37 +00:00
{
2017-02-27 03:35:13 +00:00
Anchor = Anchor.Centre,
Origin = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2017-02-24 04:05:37 +00:00
Direction = FlowDirections.Vertical,
},
},
},
};
}
}
class PopupDialogTriangles : Triangles
{
2017-02-24 20:48:30 +00:00
private const float transition_duration = 500;
2017-02-24 20:41:10 +00:00
private const float triangle_normal_speed = 20000;
2017-02-24 20:48:30 +00:00
private const float triangle_moving_speed = 100;
2017-02-24 20:41:10 +00:00
private float triangleMoveSpeed;
2017-02-24 04:05:37 +00:00
private Color4[] colours;
protected override float SpawnRatio => spawnRatio;
private float spawnRatio = 0f;
2017-02-24 20:41:10 +00:00
protected override Color4 GetTriangleShade() => colours[RNG.Next(0, colours.Length - 1)];
2017-02-24 04:05:37 +00:00
[BackgroundDependencyLoader]
private void load(OsuColour colour)
{
colours = new Color4[]
{
colour.BlueLight,
colour.Blue,
colour.PinkLight,
colour.Pink,
colour.YellowLight,
colour.Yellow,
colour.PurpleLight,
colour.Purple,
};
}
protected override void Update()
{
base.Update();
foreach (Drawable d in Children)
d.Position -= new Vector2(0, (float)(d.Scale.X * (Time.Elapsed / triangleMoveSpeed)));
}
public void SlideIn()
{
2017-02-24 20:48:30 +00:00
triangleMoveSpeed = triangle_moving_speed;
TransformFloatTo(spawnRatio, 1f, transition_duration, EasingTypes.None, new TransformFloatSpawnRatio());
TransformFloatTo(triangleMoveSpeed, triangle_normal_speed, transition_duration, EasingTypes.InExpo, new TransformFloatSpeed());
2017-02-24 04:05:37 +00:00
}
public void SlideOut()
{
2017-02-24 20:48:30 +00:00
TransformFloatTo(spawnRatio, 0f, transition_duration, EasingTypes.None, new TransformFloatSpawnRatio());
TransformFloatTo(triangleMoveSpeed, triangle_moving_speed, transition_duration, EasingTypes.OutExpo, new TransformFloatSpeed());
2017-02-24 04:05:37 +00:00
}
class TransformFloatSpeed : TransformFloat
{
public override void Apply(Drawable d)
{
base.Apply(d);
PopupDialogTriangles t = d as PopupDialogTriangles;
t.triangleMoveSpeed = CurrentValue;
}
}
class TransformFloatSpawnRatio : TransformFloat
{
public override void Apply(Drawable d)
{
base.Apply(d);
PopupDialogTriangles t = d as PopupDialogTriangles;
t.spawnRatio = CurrentValue;
}
}
protected override void LoadComplete()
{
// override so the triangles don't do the initial fill
}
public PopupDialogTriangles()
{
TriangleScale = 2f;
}
}
}