2020-04-03 17:15:24 +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.
|
|
|
|
|
2022-03-11 22:27:27 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-04-29 14:29:25 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-05-10 21:33:45 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-03-11 22:27:27 +08:00
|
|
|
using osu.Game.Configuration;
|
2021-05-06 14:16:16 +08:00
|
|
|
using osu.Game.Screens.Play.HUD;
|
2021-06-06 18:58:21 +08:00
|
|
|
using osu.Game.Skinning;
|
2021-04-29 14:29:25 +08:00
|
|
|
using osuTK;
|
2020-04-03 17:15:24 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Extensions
|
|
|
|
{
|
|
|
|
public static class DrawableExtensions
|
|
|
|
{
|
2021-09-14 13:44:23 +08:00
|
|
|
/// <summary>
|
2021-09-15 04:50:45 +08:00
|
|
|
/// Shakes this drawable.
|
2021-09-14 13:44:23 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="target">The target to shake.</param>
|
|
|
|
/// <param name="shakeDuration">The length of a single shake.</param>
|
|
|
|
/// <param name="shakeMagnitude">Pixels of displacement per shake.</param>
|
|
|
|
/// <param name="maximumLength">The maximum length the shake should last.</param>
|
|
|
|
public static void Shake(this Drawable target, double shakeDuration = 80, float shakeMagnitude = 8, double? maximumLength = null)
|
|
|
|
{
|
|
|
|
// if we don't have enough time, don't bother shaking.
|
|
|
|
if (maximumLength < shakeDuration * 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var sequence = target.MoveToX(shakeMagnitude, shakeDuration / 2, Easing.OutSine).Then()
|
|
|
|
.MoveToX(-shakeMagnitude, shakeDuration, Easing.InOutSine).Then();
|
|
|
|
|
|
|
|
// if we don't have enough time for the second shake, skip it.
|
|
|
|
if (!maximumLength.HasValue || maximumLength >= shakeDuration * 4)
|
|
|
|
{
|
|
|
|
sequence = sequence
|
|
|
|
.MoveToX(shakeMagnitude, shakeDuration, Easing.InOutSine).Then()
|
|
|
|
.MoveToX(-shakeMagnitude, shakeDuration, Easing.InOutSine).Then();
|
|
|
|
}
|
|
|
|
|
|
|
|
sequence.MoveToX(0, shakeDuration / 2, Easing.InSine);
|
|
|
|
}
|
|
|
|
|
2021-04-29 14:29:25 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Accepts a delta vector in screen-space coordinates and converts it to one which can be applied to this drawable's position.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="drawable">The drawable.</param>
|
|
|
|
/// <param name="delta">A delta in screen-space coordinates.</param>
|
|
|
|
/// <returns>The delta vector in Parent's coordinates.</returns>
|
|
|
|
public static Vector2 ScreenSpaceDeltaToParentSpace(this Drawable drawable, Vector2 delta) =>
|
|
|
|
drawable.Parent.ToLocalSpace(drawable.Parent.ToScreenSpace(Vector2.Zero) + delta);
|
2021-05-06 14:16:16 +08:00
|
|
|
|
2021-05-13 12:14:49 +08:00
|
|
|
public static SkinnableInfo CreateSkinnableInfo(this Drawable component) => new SkinnableInfo(component);
|
2021-05-07 18:13:38 +08:00
|
|
|
|
2021-05-13 12:14:49 +08:00
|
|
|
public static void ApplySkinnableInfo(this Drawable component, SkinnableInfo info)
|
2021-05-07 18:13:38 +08:00
|
|
|
{
|
|
|
|
// todo: can probably make this better via deserialisation directly using a common interface.
|
|
|
|
component.Position = info.Position;
|
|
|
|
component.Rotation = info.Rotation;
|
|
|
|
component.Scale = info.Scale;
|
|
|
|
component.Anchor = info.Anchor;
|
2021-05-11 12:39:32 +08:00
|
|
|
component.Origin = info.Origin;
|
2021-06-07 12:04:53 +08:00
|
|
|
|
|
|
|
if (component is ISkinnableDrawable skinnable)
|
2022-03-11 22:27:27 +08:00
|
|
|
{
|
2021-06-08 20:22:35 +08:00
|
|
|
skinnable.UsesFixedAnchor = info.UsesFixedAnchor;
|
2021-05-10 21:33:45 +08:00
|
|
|
|
2022-03-11 22:27:27 +08:00
|
|
|
foreach (var (_, property) in component.GetSettingsSourceProperties())
|
|
|
|
{
|
2022-06-20 21:02:15 +08:00
|
|
|
if (!info.Settings.TryGetValue(property.Name.ToSnakeCase(), out object settingValue))
|
2022-03-11 22:27:27 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
skinnable.CopyAdjustedSetting((IBindable)property.GetValue(component), settingValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 21:33:45 +08:00
|
|
|
if (component is Container container)
|
|
|
|
{
|
|
|
|
foreach (var child in info.Children)
|
|
|
|
container.Add(child.CreateInstance());
|
|
|
|
}
|
2021-05-07 18:13:38 +08:00
|
|
|
}
|
2020-04-03 17:15:24 +08:00
|
|
|
}
|
|
|
|
}
|