// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; namespace osu.Game.Graphics.Containers { /// /// A container that adds the ability to shake its contents. /// public class ShakeContainer : Container { /// /// The length of a single shake. /// public float ShakeDuration = 80; /// /// Total number of shakes. May be shortened if possible. /// public float TotalShakes = 4; /// /// Pixels of displacement per shake. /// public float ShakeMagnitude = 8; /// /// Shake the contents of this container. /// /// The maximum length the shake should last. public void Shake(double? maximumLength = null) { const float shake_amount = 8; // if we don't have enough time, don't bother shaking. if (maximumLength < ShakeDuration * 2) return; var sequence = this.MoveToX(shake_amount, ShakeDuration / 2, Easing.OutSine).Then() .MoveToX(-shake_amount, 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(shake_amount, ShakeDuration, Easing.InOutSine).Then() .MoveToX(-shake_amount, ShakeDuration, Easing.InOutSine).Then(); sequence.MoveToX(0, ShakeDuration / 2, Easing.InSine); } } }