osu/osu.Game/Graphics/UserInterface/TwoLayerButton.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

252 lines
8.5 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
2017-01-27 12:57:22 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2018-11-20 07:51:59 +00:00
using osuTK;
using osuTK.Graphics;
2017-02-03 04:16:45 +00:00
using osu.Game.Graphics.Sprites;
using osu.Framework.Extensions.Color4Extensions;
using osu.Game.Graphics.Containers;
using osu.Game.Beatmaps.ControlPoints;
using osu.Framework.Audio.Track;
using System;
2021-02-25 06:38:56 +00:00
using osu.Framework.Extensions.EnumExtensions;
2019-04-02 05:51:28 +00:00
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Shapes;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
using osu.Game.Screens.Select;
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
namespace osu.Game.Graphics.UserInterface
{
public partial class TwoLayerButton : OsuClickableContainer
2017-01-27 12:57:22 +00:00
{
2017-05-26 10:46:44 +00:00
private readonly BouncingIcon bouncingIcon;
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
public Box IconLayer;
public Box TextLayer;
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
private const int transform_time = 600;
private const int pulse_length = 250;
2018-04-13 09:19:50 +00:00
2019-08-06 03:57:17 +00:00
private const float shear_width = 5f;
2019-08-07 03:36:00 +00:00
private static readonly Vector2 shear = new Vector2(shear_width / Footer.HEIGHT, 0);
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
public static readonly Vector2 SIZE_EXTENDED = new Vector2(140, 50);
public static readonly Vector2 SIZE_RETRACTED = new Vector2(100, 50);
private readonly SpriteText text;
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
public Color4 HoverColour;
private readonly Container c1;
private readonly Container c2;
2018-04-13 09:19:50 +00:00
public Color4 BackgroundColour
2017-01-27 12:57:22 +00:00
{
set
{
TextLayer.Colour = value;
IconLayer.Colour = value;
}
}
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
public override Anchor Origin
{
get => base.Origin;
2017-01-27 12:57:22 +00:00
set
{
base.Origin = value;
2021-02-25 06:38:56 +00:00
c1.Origin = c1.Anchor = value.HasFlagFast(Anchor.x2) ? Anchor.TopLeft : Anchor.TopRight;
c2.Origin = c2.Anchor = value.HasFlagFast(Anchor.x2) ? Anchor.TopRight : Anchor.TopLeft;
2018-04-13 09:19:50 +00:00
2021-02-25 06:38:56 +00:00
X = value.HasFlagFast(Anchor.x2) ? SIZE_RETRACTED.X * shear.X * 0.5f : 0;
2018-04-13 09:19:50 +00:00
Remove(c1, false);
Remove(c2, false);
2021-02-25 06:38:56 +00:00
c1.Depth = value.HasFlagFast(Anchor.x2) ? 0 : 1;
c2.Depth = value.HasFlagFast(Anchor.x2) ? 1 : 0;
2017-06-18 22:50:26 +00:00
Add(c1);
Add(c2);
2017-01-27 12:57:22 +00:00
}
}
2018-04-13 09:19:50 +00:00
public TwoLayerButton(HoverSampleSet sampleSet = HoverSampleSet.Default)
: base(sampleSet)
2017-01-27 12:57:22 +00:00
{
Size = SIZE_RETRACTED;
2019-08-07 03:36:00 +00:00
Shear = shear;
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
Children = new Drawable[]
{
c2 = new Container
{
RelativeSizeAxes = Axes.Both,
Width = 0.4f,
Children = new Drawable[]
{
2017-06-12 03:48:47 +00:00
new Container
{
2017-01-27 12:57:22 +00:00
RelativeSizeAxes = Axes.Both,
Masking = true,
MaskingSmoothness = 2,
2017-06-12 03:48:47 +00:00
EdgeEffect = new EdgeEffectParameters
{
2017-01-27 12:57:22 +00:00
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.2f),
Offset = new Vector2(2, 0),
Radius = 2,
},
2017-06-12 03:48:47 +00:00
Children = new[]
{
2017-01-27 12:57:22 +00:00
IconLayer = new Box
{
RelativeSizeAxes = Axes.Both,
EdgeSmoothness = new Vector2(2, 0),
2017-01-27 12:57:22 +00:00
},
}
},
2017-05-26 10:46:44 +00:00
bouncingIcon = new BouncingIcon
2017-01-27 12:57:22 +00:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2019-08-07 03:36:00 +00:00
Shear = -shear,
2017-01-27 12:57:22 +00:00
},
}
},
c1 = new Container
{
Origin = Anchor.TopRight,
Anchor = Anchor.TopRight,
RelativeSizeAxes = Axes.Both,
Width = 0.6f,
Children = new Drawable[]
{
2017-06-12 03:48:47 +00:00
new Container
{
2017-01-27 12:57:22 +00:00
RelativeSizeAxes = Axes.Both,
Masking = true,
MaskingSmoothness = 2,
2017-06-12 03:48:47 +00:00
EdgeEffect = new EdgeEffectParameters
{
2017-01-27 12:57:22 +00:00
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.2f),
Offset = new Vector2(2, 0),
Radius = 2,
},
2017-06-12 03:48:47 +00:00
Children = new[]
{
2017-01-27 12:57:22 +00:00
TextLayer = new Box
{
Origin = Anchor.TopLeft,
Anchor = Anchor.TopLeft,
RelativeSizeAxes = Axes.Both,
EdgeSmoothness = new Vector2(2, 0),
2017-01-27 12:57:22 +00:00
},
}
},
text = new OsuSpriteText
2017-01-27 12:57:22 +00:00
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
2019-08-07 03:36:00 +00:00
Shear = -shear,
2017-01-27 12:57:22 +00:00
}
}
},
};
}
2018-04-13 09:19:50 +00:00
public IconUsage Icon
2017-01-27 12:57:22 +00:00
{
set => bouncingIcon.Icon = value;
2017-01-27 12:57:22 +00:00
}
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
public string Text
{
set => text.Text = value;
2017-01-27 12:57:22 +00:00
}
2018-04-13 09:19:50 +00:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => IconLayer.ReceivePositionalInputAt(screenSpacePos) || TextLayer.ReceivePositionalInputAt(screenSpacePos);
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
protected override bool OnHover(HoverEvent e)
2017-01-27 12:57:22 +00:00
{
2017-07-22 18:50:25 +00:00
this.ResizeTo(SIZE_EXTENDED, transform_time, Easing.OutElastic);
2019-06-25 11:23:34 +00:00
IconLayer.FadeColour(HoverColour, transform_time / 2f, Easing.OutQuint);
2018-04-13 09:19:50 +00:00
2017-07-22 18:50:25 +00:00
bouncingIcon.ScaleTo(1.1f, transform_time, Easing.OutElastic);
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
return true;
}
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
protected override void OnHoverLost(HoverLostEvent e)
2017-01-27 12:57:22 +00:00
{
2019-06-25 11:23:34 +00:00
this.ResizeTo(SIZE_RETRACTED, transform_time, Easing.Out);
IconLayer.FadeColour(TextLayer.Colour, transform_time, Easing.Out);
2018-04-13 09:19:50 +00:00
2019-06-25 11:23:34 +00:00
bouncingIcon.ScaleTo(1, transform_time, Easing.Out);
2017-01-27 12:57:22 +00:00
}
2018-04-13 09:19:50 +00:00
2019-06-25 11:23:34 +00:00
protected override bool OnMouseDown(MouseDownEvent e) => true;
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
protected override bool OnClick(ClickEvent e)
2017-01-27 12:57:22 +00:00
{
var flash = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White.Opacity(0.5f),
};
Add(flash);
2018-04-13 09:19:50 +00:00
2017-01-27 12:57:22 +00:00
flash.Alpha = 1;
2017-07-22 18:50:25 +00:00
flash.FadeOut(500, Easing.OutQuint);
2017-01-27 12:57:22 +00:00
flash.Expire();
2018-04-13 09:19:50 +00:00
2018-10-02 03:02:47 +00:00
return base.OnClick(e);
2017-01-27 12:57:22 +00:00
}
2018-04-13 09:19:50 +00:00
2017-05-26 10:46:44 +00:00
private partial class BouncingIcon : BeatSyncedContainer
{
private const double beat_in_time = 60;
2018-04-13 09:19:50 +00:00
private readonly SpriteIcon icon;
2018-04-13 09:19:50 +00:00
public IconUsage Icon
2019-02-28 04:31:40 +00:00
{
set => icon.Icon = value;
}
2018-04-13 09:19:50 +00:00
2017-05-26 10:46:44 +00:00
public BouncingIcon()
{
EarlyActivationMilliseconds = beat_in_time;
AutoSizeAxes = Axes.Both;
2018-04-13 09:19:50 +00:00
Children = new Drawable[]
{
icon = new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(25),
2017-05-26 10:46:44 +00:00
}
};
}
2018-04-13 09:19:50 +00:00
2020-06-23 04:49:18 +00:00
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
{
base.OnNewBeat(beatIndex, timingPoint, effectPoint, amplitudes);
2018-04-13 09:19:50 +00:00
double beatLength = timingPoint.BeatLength;
2018-04-13 09:19:50 +00:00
float amplitudeAdjust = Math.Min(1, 0.4f + amplitudes.Maximum);
2018-04-13 09:19:50 +00:00
if (beatIndex < 0) return;
2018-04-13 09:19:50 +00:00
2017-07-22 18:50:25 +00:00
icon.ScaleTo(1 - 0.1f * amplitudeAdjust, beat_in_time, Easing.Out)
2017-07-16 15:28:20 +00:00
.Then()
2017-07-22 18:50:25 +00:00
.ScaleTo(1, beatLength * 2, Easing.OutQuint);
}
}
2017-01-27 12:57:22 +00:00
}
2018-01-05 11:21:19 +00:00
}