2020-04-03 09:15:24 +00: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 14:27:27 +00:00
|
|
|
using osu.Framework.Bindables;
|
2021-04-29 06:29:25 +00:00
|
|
|
using osu.Framework.Graphics;
|
2021-05-10 13:33:45 +00:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-03-11 14:27:27 +00:00
|
|
|
using osu.Game.Configuration;
|
2021-05-06 06:16:16 +00:00
|
|
|
using osu.Game.Screens.Play.HUD;
|
2021-06-06 10:58:21 +00:00
|
|
|
using osu.Game.Skinning;
|
2021-04-29 06:29:25 +00:00
|
|
|
using osuTK;
|
2020-04-03 09:15:24 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Extensions
|
|
|
|
{
|
|
|
|
public static class DrawableExtensions
|
|
|
|
{
|
2021-09-14 05:44:23 +00:00
|
|
|
/// <summary>
|
2021-09-14 20:50:45 +00:00
|
|
|
/// Shakes this drawable.
|
2021-09-14 05:44:23 +00: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 06:29:25 +00: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 06:16:16 +00:00
|
|
|
|
2021-05-13 04:14:49 +00:00
|
|
|
public static SkinnableInfo CreateSkinnableInfo(this Drawable component) => new SkinnableInfo(component);
|
2021-05-07 10:13:38 +00:00
|
|
|
|
2021-05-13 04:14:49 +00:00
|
|
|
public static void ApplySkinnableInfo(this Drawable component, SkinnableInfo info)
|
2021-05-07 10:13:38 +00: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 04:39:32 +00:00
|
|
|
component.Origin = info.Origin;
|
2021-06-07 04:04:53 +00:00
|
|
|
|
|
|
|
if (component is ISkinnableDrawable skinnable)
|
2022-03-11 14:27:27 +00:00
|
|
|
{
|
2021-06-08 12:22:35 +00:00
|
|
|
skinnable.UsesFixedAnchor = info.UsesFixedAnchor;
|
2021-05-10 13:33:45 +00:00
|
|
|
|
2022-03-11 14:27:27 +00:00
|
|
|
foreach (var (_, property) in component.GetSettingsSourceProperties())
|
|
|
|
{
|
2022-06-20 13:02:15 +00:00
|
|
|
if (!info.Settings.TryGetValue(property.Name.ToSnakeCase(), out object settingValue))
|
2022-03-11 14:27:27 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
skinnable.CopyAdjustedSetting((IBindable)property.GetValue(component), settingValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 13:33:45 +00:00
|
|
|
if (component is Container container)
|
|
|
|
{
|
|
|
|
foreach (var child in info.Children)
|
|
|
|
container.Add(child.CreateInstance());
|
|
|
|
}
|
2021-05-07 10:13:38 +00:00
|
|
|
}
|
2020-04-03 09:15:24 +00:00
|
|
|
}
|
|
|
|
}
|