2019-01-24 16:43:03 +08:00
|
|
|
|
// 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
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2021-07-06 18:07:25 +08:00
|
|
|
|
using System;
|
|
|
|
|
using osu.Framework;
|
2021-07-06 01:22:55 +08:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2017-01-28 02:18:57 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Colour;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-04-02 13:51:28 +08:00
|
|
|
|
using osu.Framework.Graphics.Effects;
|
2021-07-06 01:22:55 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2018-10-02 11:02:47 +08:00
|
|
|
|
using osu.Framework.Input.Events;
|
2021-04-20 16:06:12 +08:00
|
|
|
|
using osu.Framework.Localisation;
|
2021-07-06 01:22:55 +08:00
|
|
|
|
using osu.Game.Graphics.Backgrounds;
|
|
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-27 11:35:13 +08:00
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
2017-01-28 02:18:57 +08:00
|
|
|
|
{
|
2021-07-06 18:07:25 +08:00
|
|
|
|
public class DialogButton : OsuClickableContainer, IStateful<SelectionState>
|
2017-01-27 17:24:49 +08:00
|
|
|
|
{
|
2019-12-06 17:49:03 +08:00
|
|
|
|
private const float idle_width = 0.8f;
|
2017-02-07 15:15:45 +08:00
|
|
|
|
private const float hover_width = 0.9f;
|
2019-12-09 13:11:44 +08:00
|
|
|
|
|
2017-02-07 15:15:45 +08:00
|
|
|
|
private const float hover_duration = 500;
|
|
|
|
|
private const float click_duration = 200;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-07-06 18:07:25 +08:00
|
|
|
|
public event Action<SelectionState> StateChanged;
|
2021-07-06 01:22:55 +08:00
|
|
|
|
|
2021-07-06 18:07:25 +08:00
|
|
|
|
private SelectionState state;
|
|
|
|
|
|
|
|
|
|
public SelectionState State
|
2021-07-06 01:22:55 +08:00
|
|
|
|
{
|
2021-07-06 18:07:25 +08:00
|
|
|
|
get => state;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (state == value)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
state = value;
|
|
|
|
|
StateChanged?.Invoke(value);
|
|
|
|
|
}
|
2021-07-06 01:22:55 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-03-21 15:06:32 +08:00
|
|
|
|
protected readonly Container ColourContainer;
|
|
|
|
|
|
2017-03-23 12:41:50 +08:00
|
|
|
|
private readonly Container backgroundContainer;
|
|
|
|
|
private readonly Container glowContainer;
|
|
|
|
|
private readonly Box leftGlow;
|
|
|
|
|
private readonly Box centerGlow;
|
|
|
|
|
private readonly Box rightGlow;
|
|
|
|
|
private readonly Box background;
|
|
|
|
|
private readonly SpriteText spriteText;
|
2017-02-28 09:30:10 +08:00
|
|
|
|
private Vector2 hoverSpacing => new Vector2(3f, 0f);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-06-01 20:00:14 +08:00
|
|
|
|
public DialogButton(HoverSampleSet sampleSet = HoverSampleSet.Button)
|
|
|
|
|
: base(sampleSet)
|
2017-01-27 17:39:15 +08:00
|
|
|
|
{
|
2017-02-27 11:35:13 +08:00
|
|
|
|
RelativeSizeAxes = Axes.X;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-01-30 21:26:24 +08:00
|
|
|
|
Children = new Drawable[]
|
2017-01-28 02:18:57 +08:00
|
|
|
|
{
|
|
|
|
|
backgroundContainer = new Container
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Width = 1f,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-02-27 11:35:13 +08:00
|
|
|
|
background = new Box
|
2017-01-28 02:18:57 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-02-27 11:35:13 +08:00
|
|
|
|
Colour = backgroundColour,
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-01-28 02:18:57 +08:00
|
|
|
|
},
|
|
|
|
|
glowContainer = new Container
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Width = 1f,
|
|
|
|
|
Alpha = 0f,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-01-30 20:19:44 +08:00
|
|
|
|
leftGlow = new Box
|
2017-01-28 02:18:57 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Origin = Anchor.TopLeft,
|
|
|
|
|
Anchor = Anchor.TopLeft,
|
2017-02-27 11:35:13 +08:00
|
|
|
|
Width = 0.125f,
|
2017-01-29 09:50:06 +08:00
|
|
|
|
},
|
2017-01-30 20:19:44 +08:00
|
|
|
|
centerGlow = new Box
|
2017-01-29 09:50:06 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Anchor = Anchor.Centre,
|
2017-02-27 11:35:13 +08:00
|
|
|
|
Width = 0.75f,
|
2017-01-28 02:18:57 +08:00
|
|
|
|
},
|
2017-01-30 20:19:44 +08:00
|
|
|
|
rightGlow = new Box
|
2017-01-28 02:18:57 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Origin = Anchor.TopRight,
|
|
|
|
|
Anchor = Anchor.TopRight,
|
2017-02-27 11:35:13 +08:00
|
|
|
|
Width = 0.125f,
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-01-28 02:18:57 +08:00
|
|
|
|
},
|
|
|
|
|
new Container
|
|
|
|
|
{
|
2017-01-30 16:43:06 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-01-28 02:18:57 +08:00
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Masking = true,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2022-03-21 15:06:32 +08:00
|
|
|
|
ColourContainer = new Container
|
2017-01-28 02:18:57 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Anchor = Anchor.Centre,
|
2019-12-06 17:49:03 +08:00
|
|
|
|
Width = idle_width,
|
2017-01-28 02:18:57 +08:00
|
|
|
|
Masking = true,
|
2017-02-04 18:06:26 +08:00
|
|
|
|
MaskingSmoothness = 2,
|
2017-06-12 11:48:47 +08:00
|
|
|
|
EdgeEffect = new EdgeEffectParameters
|
2017-01-28 02:18:57 +08:00
|
|
|
|
{
|
|
|
|
|
Type = EdgeEffectType.Shadow,
|
|
|
|
|
Colour = Color4.Black.Opacity(0.2f),
|
2017-02-27 11:35:13 +08:00
|
|
|
|
Radius = 5,
|
2017-01-28 02:18:57 +08:00
|
|
|
|
},
|
2017-02-28 09:38:25 +08:00
|
|
|
|
Colour = ButtonColour,
|
2017-01-30 20:45:10 +08:00
|
|
|
|
Shear = new Vector2(0.2f, 0),
|
2017-01-28 02:18:57 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
new Box
|
|
|
|
|
{
|
|
|
|
|
EdgeSmoothness = new Vector2(2, 0),
|
2017-02-27 11:35:13 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-01-28 02:18:57 +08:00
|
|
|
|
},
|
2017-02-05 23:34:47 +08:00
|
|
|
|
new Container
|
2017-01-28 02:18:57 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-02-05 23:34:47 +08:00
|
|
|
|
Masking = true,
|
|
|
|
|
MaskingSmoothness = 0,
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
|
|
|
|
new Triangles
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
TriangleScale = 4,
|
|
|
|
|
ColourDark = OsuColour.Gray(0.88f),
|
2017-02-27 11:35:13 +08:00
|
|
|
|
Shear = new Vector2(-0.2f, 0),
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-02-05 23:34:47 +08:00
|
|
|
|
},
|
2017-02-27 11:35:13 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-01-28 02:18:57 +08:00
|
|
|
|
},
|
2017-02-15 20:37:43 +08:00
|
|
|
|
spriteText = new OsuSpriteText
|
2017-01-28 02:18:57 +08:00
|
|
|
|
{
|
|
|
|
|
Text = Text,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
2019-02-12 12:04:46 +08:00
|
|
|
|
Font = OsuFont.GetFont(size: 28, weight: FontWeight.Bold),
|
2017-01-28 02:18:57 +08:00
|
|
|
|
Shadow = true,
|
|
|
|
|
ShadowColour = new Color4(0, 0, 0, 0.1f),
|
2017-02-27 11:35:13 +08:00
|
|
|
|
Colour = Color4.White,
|
|
|
|
|
},
|
2017-01-30 21:26:24 +08:00
|
|
|
|
};
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-01-31 21:17:47 +08:00
|
|
|
|
updateGlow();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-07-06 18:07:25 +08:00
|
|
|
|
StateChanged += selectionChanged;
|
2017-12-18 13:47:17 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-12-18 13:47:17 +08:00
|
|
|
|
private Color4 buttonColour;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2017-12-18 13:47:17 +08:00
|
|
|
|
public Color4 ButtonColour
|
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
|
get => buttonColour;
|
2017-12-18 13:47:17 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
buttonColour = value;
|
|
|
|
|
updateGlow();
|
2022-03-21 15:06:32 +08:00
|
|
|
|
ColourContainer.Colour = value;
|
2017-12-18 13:47:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-12-18 13:47:17 +08:00
|
|
|
|
private Color4 backgroundColour = OsuColour.Gray(34);
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2017-12-18 13:47:17 +08:00
|
|
|
|
public Color4 BackgroundColour
|
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
|
get => backgroundColour;
|
2017-12-18 13:47:17 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
backgroundColour = value;
|
|
|
|
|
background.Colour = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-04-20 16:06:12 +08:00
|
|
|
|
private LocalisableString text;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2021-04-20 16:06:12 +08:00
|
|
|
|
public LocalisableString Text
|
2017-12-18 13:47:17 +08:00
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
|
get => text;
|
2017-12-18 13:47:17 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
text = value;
|
|
|
|
|
spriteText.Text = Text;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-12-18 13:47:17 +08:00
|
|
|
|
public float TextSize
|
|
|
|
|
{
|
2019-02-12 12:04:46 +08:00
|
|
|
|
get => spriteText.Font.Size;
|
2019-02-20 18:32:30 +08:00
|
|
|
|
set => spriteText.Font = spriteText.Font.With(size: value);
|
2017-12-18 13:47:17 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-09-26 13:01:15 +08:00
|
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => backgroundContainer.ReceivePositionalInputAt(screenSpacePos);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-12-09 13:11:44 +08:00
|
|
|
|
private bool clickAnimating;
|
2019-12-06 17:49:03 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnClick(ClickEvent e)
|
2017-12-18 13:47:17 +08:00
|
|
|
|
{
|
2019-12-09 13:11:44 +08:00
|
|
|
|
var flash = new Box
|
2017-12-18 13:47:17 +08:00
|
|
|
|
{
|
2019-12-09 13:11:44 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = ButtonColour,
|
|
|
|
|
Blending = BlendingParameters.Additive,
|
|
|
|
|
Alpha = 0.05f
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-21 15:06:32 +08:00
|
|
|
|
ColourContainer.Add(flash);
|
2019-12-09 13:11:44 +08:00
|
|
|
|
flash.FadeOutFromOne(100).Expire();
|
|
|
|
|
|
|
|
|
|
clickAnimating = true;
|
2022-03-21 15:06:32 +08:00
|
|
|
|
ColourContainer.ResizeWidthTo(ColourContainer.Width * 1.05f, 100, Easing.OutQuint)
|
2019-12-09 13:11:44 +08:00
|
|
|
|
.OnComplete(_ =>
|
|
|
|
|
{
|
|
|
|
|
clickAnimating = false;
|
2021-07-06 18:07:25 +08:00
|
|
|
|
StateChanged?.Invoke(State);
|
2019-12-09 13:11:44 +08:00
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
return base.OnClick(e);
|
2017-12-18 13:47:17 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-12-09 13:11:44 +08:00
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
|
|
|
|
{
|
2022-03-21 15:06:32 +08:00
|
|
|
|
ColourContainer.ResizeWidthTo(hover_width * 0.98f, click_duration * 4, Easing.OutQuad);
|
2019-12-09 13:11:44 +08:00
|
|
|
|
return base.OnMouseDown(e);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 17:17:21 +08:00
|
|
|
|
protected override void OnMouseUp(MouseUpEvent e)
|
2019-12-09 13:11:44 +08:00
|
|
|
|
{
|
2021-07-06 18:07:25 +08:00
|
|
|
|
if (State == SelectionState.Selected)
|
2022-03-21 15:06:32 +08:00
|
|
|
|
ColourContainer.ResizeWidthTo(hover_width, click_duration, Easing.In);
|
2020-01-20 17:17:21 +08:00
|
|
|
|
base.OnMouseUp(e);
|
2019-12-09 13:11:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
2017-12-18 13:47:17 +08:00
|
|
|
|
{
|
2018-10-02 11:02:47 +08:00
|
|
|
|
base.OnHover(e);
|
2021-07-06 18:07:25 +08:00
|
|
|
|
State = SelectionState.Selected;
|
2019-12-09 13:11:44 +08:00
|
|
|
|
|
2017-12-18 13:47:17 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
2017-12-18 13:47:17 +08:00
|
|
|
|
{
|
2018-10-02 11:02:47 +08:00
|
|
|
|
base.OnHoverLost(e);
|
2021-07-06 18:07:25 +08:00
|
|
|
|
State = SelectionState.NotSelected;
|
2017-12-18 13:47:17 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-07-06 18:07:25 +08:00
|
|
|
|
private void selectionChanged(SelectionState newState)
|
2017-12-18 13:47:17 +08:00
|
|
|
|
{
|
2019-12-09 13:11:44 +08:00
|
|
|
|
if (clickAnimating)
|
|
|
|
|
return;
|
2019-12-06 17:49:03 +08:00
|
|
|
|
|
2021-07-06 18:07:25 +08:00
|
|
|
|
if (newState == SelectionState.Selected)
|
2017-12-18 13:47:17 +08:00
|
|
|
|
{
|
|
|
|
|
spriteText.TransformSpacingTo(hoverSpacing, hover_duration, Easing.OutElastic);
|
2022-03-21 15:06:32 +08:00
|
|
|
|
ColourContainer.ResizeWidthTo(hover_width, hover_duration, Easing.OutElastic);
|
2019-12-09 13:11:44 +08:00
|
|
|
|
glowContainer.FadeIn(hover_duration, Easing.OutQuint);
|
2017-12-18 13:47:17 +08:00
|
|
|
|
}
|
2017-12-18 15:36:11 +08:00
|
|
|
|
else
|
2017-12-18 13:47:17 +08:00
|
|
|
|
{
|
2022-03-21 15:06:32 +08:00
|
|
|
|
ColourContainer.ResizeWidthTo(idle_width, hover_duration, Easing.OutElastic);
|
2017-12-18 13:47:17 +08:00
|
|
|
|
spriteText.TransformSpacingTo(Vector2.Zero, hover_duration, Easing.OutElastic);
|
2019-12-09 13:11:44 +08:00
|
|
|
|
glowContainer.FadeOut(hover_duration, Easing.OutQuint);
|
2017-12-18 13:47:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-12-18 13:47:17 +08:00
|
|
|
|
private void updateGlow()
|
|
|
|
|
{
|
|
|
|
|
leftGlow.Colour = ColourInfo.GradientHorizontal(new Color4(ButtonColour.R, ButtonColour.G, ButtonColour.B, 0f), ButtonColour);
|
|
|
|
|
centerGlow.Colour = ButtonColour;
|
|
|
|
|
rightGlow.Colour = ColourInfo.GradientHorizontal(ButtonColour, new Color4(ButtonColour.R, ButtonColour.G, ButtonColour.B, 0f));
|
2017-01-28 02:18:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|