// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using Humanizer; 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; namespace osu.Game.Extensions { public static class DrawableExtensions { /// /// Shakes this drawable. /// /// The target to shake. /// The length of a single shake. /// Pixels of displacement per shake. /// The maximum length the shake should last. 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); } /// /// Accepts a delta vector in screen-space coordinates and converts it to one which can be applied to this drawable's position. /// /// The drawable. /// A delta in screen-space coordinates. /// The delta vector in Parent's coordinates. 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.Underscore(), 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()); } } } }