mirror of
https://github.com/osukey/osukey.git
synced 2025-05-23 22:47:26 +09:00
126 lines
5.3 KiB
C#
126 lines
5.3 KiB
C#
// 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.
|
|
|
|
using osu.Framework.Bindables;
|
|
using osu.Framework.Graphics;
|
|
using osu.Framework.Graphics.Containers;
|
|
using osu.Game.Configuration;
|
|
using osu.Game.Screens.Play.HUD;
|
|
using osu.Game.Skinning;
|
|
using osuTK;
|
|
using System;
|
|
|
|
namespace osu.Game.Extensions
|
|
{
|
|
public static class DrawableExtensions
|
|
{
|
|
/// <summary>
|
|
/// Shakes this drawable.
|
|
/// </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);
|
|
}
|
|
|
|
/// <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);
|
|
|
|
public static SkinnableInfo CreateSkinnableInfo(this Drawable component) => new SkinnableInfo(component);
|
|
|
|
public static void ApplySkinnableInfo(this Drawable component, SkinnableInfo info)
|
|
{
|
|
// 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;
|
|
component.Origin = info.Origin;
|
|
|
|
if (component is ISkinnableDrawable skinnable)
|
|
{
|
|
skinnable.UsesFixedAnchor = info.UsesFixedAnchor;
|
|
|
|
foreach (var (_, property) in component.GetSettingsSourceProperties())
|
|
{
|
|
if (!info.Settings.TryGetValue(property.Name.ToSnakeCase(), out object settingValue))
|
|
continue;
|
|
|
|
skinnable.CopyAdjustedSetting((IBindable)property.GetValue(component), settingValue);
|
|
}
|
|
}
|
|
|
|
if (component is Container container)
|
|
{
|
|
foreach (var child in info.Children)
|
|
container.Add(child.CreateInstance());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Keeps the drawable upright and prevents it from being scaled or flipped with its Parent.
|
|
/// </summary>
|
|
/// <param name="drawable">The drawable.</param>
|
|
public static void KeepUprightAndUnstretched(this Drawable drawable)
|
|
{
|
|
// Fix the rotation
|
|
var result = drawable.Parent.DrawInfo;
|
|
var scale = result.Matrix.ExtractScale();
|
|
var rotation = new Matrix3(
|
|
result.Matrix.Row0 / scale.X,
|
|
result.Matrix.Row1 / scale.Y,
|
|
new Vector3(0.0f, 0.0f, 1.0f)
|
|
);
|
|
rotation.Invert();
|
|
float angle = MathF.Atan(rotation.M12 / rotation.M11);
|
|
angle *= (360 / (2 * MathF.PI));
|
|
drawable.Rotation = angle;
|
|
|
|
// Fix the scale (includes flip)
|
|
var containerOriginToSpaceOrigin = new Matrix3(
|
|
new Vector3(1.0f, 0.0f, 0.0f),
|
|
new Vector3(0.0f, 1.0f, 0.0f),
|
|
new Vector3(drawable.DrawSize.X / 2, drawable.DrawSize.Y / 2, 1.0f)
|
|
);
|
|
var containerOriginToSpaceOriginInverse = containerOriginToSpaceOrigin;
|
|
containerOriginToSpaceOriginInverse.Invert();
|
|
Matrix3 rotatedBack = (containerOriginToSpaceOriginInverse * (rotation * (containerOriginToSpaceOrigin * result.Matrix)));
|
|
|
|
bool xFliped = rotation.M11 < 0;
|
|
bool yFliped = rotation.M22 < 0;
|
|
|
|
var rotatedBackScale = rotatedBack.ExtractScale();
|
|
|
|
drawable.Scale = new Vector2(
|
|
(xFliped ? -1 : 1) / rotatedBackScale.X,
|
|
(yFliped ? -1 : 1) / rotatedBackScale.Y
|
|
);
|
|
}
|
|
|
|
}
|
|
}
|