2019-01-24 17:43:03 +09: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.
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2022-06-17 16:37:17 +09:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2019-11-20 13:19:49 +01:00
|
|
|
|
using System;
|
2016-12-06 18:56:20 +09:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2016-09-30 18:45:55 +09:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Input;
|
2018-11-20 16:51:59 +09:00
|
|
|
|
using osuTK;
|
2016-11-08 18:13:20 -05:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 19:04:31 +09:00
|
|
|
|
using osu.Framework.Bindables;
|
2017-02-18 21:34:21 +01:00
|
|
|
|
using osu.Game.Configuration;
|
2020-01-09 13:43:44 +09:00
|
|
|
|
using osu.Framework.Utils;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2016-09-30 18:45:55 +09:00
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
|
{
|
2017-11-01 17:06:55 +09:00
|
|
|
|
public class ParallaxContainer : Container, IRequireHighFrequencyMousePosition
|
2016-09-30 18:45:55 +09:00
|
|
|
|
{
|
2018-02-28 11:42:28 +09:00
|
|
|
|
public const float DEFAULT_PARALLAX_AMOUNT = 0.02f;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-06-21 12:49:07 +09:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The amount of parallax movement. Negative values will reverse the direction of parallax relative to user input.
|
|
|
|
|
/// </summary>
|
2018-02-28 11:42:28 +09:00
|
|
|
|
public float ParallaxAmount = DEFAULT_PARALLAX_AMOUNT;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2017-02-19 17:11:31 +01:00
|
|
|
|
private Bindable<bool> parallaxEnabled;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2021-01-22 15:37:50 +09:00
|
|
|
|
private const float parallax_duration = 100;
|
|
|
|
|
|
|
|
|
|
private bool firstUpdate = true;
|
|
|
|
|
|
2016-10-01 18:40:14 +09:00
|
|
|
|
public ParallaxContainer()
|
2016-09-30 18:45:55 +09:00
|
|
|
|
{
|
2016-10-01 18:40:14 +09:00
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2016-11-10 17:40:42 -05:00
|
|
|
|
AddInternal(content = new Container
|
2016-11-08 18:13:20 -05:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre
|
|
|
|
|
});
|
2016-09-30 18:45:55 +09:00
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2017-03-23 13:41:50 +09:00
|
|
|
|
private readonly Container content;
|
2016-11-24 15:30:55 +09:00
|
|
|
|
private InputManager input;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2016-11-10 17:40:42 -05:00
|
|
|
|
protected override Container<Drawable> Content => content;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2016-11-24 15:30:55 +09:00
|
|
|
|
[BackgroundDependencyLoader]
|
2017-08-15 14:30:50 +09:00
|
|
|
|
private void load(OsuConfigManager config)
|
2016-09-30 18:45:55 +09:00
|
|
|
|
{
|
2017-05-15 10:56:27 +09:00
|
|
|
|
parallaxEnabled = config.GetBindable<bool>(OsuSetting.MenuParallax);
|
2017-02-19 17:11:31 +01:00
|
|
|
|
parallaxEnabled.ValueChanged += delegate
|
2017-02-18 21:34:21 +01:00
|
|
|
|
{
|
2019-02-21 18:56:34 +09:00
|
|
|
|
if (!parallaxEnabled.Value)
|
2017-02-19 13:47:26 +01:00
|
|
|
|
{
|
2017-07-22 20:50:25 +02:00
|
|
|
|
content.MoveTo(Vector2.Zero, firstUpdate ? 0 : 1000, Easing.OutQuint);
|
2019-11-20 14:25:44 +01:00
|
|
|
|
content.Scale = new Vector2(1 + Math.Abs(ParallaxAmount));
|
2017-02-19 13:47:26 +01:00
|
|
|
|
}
|
2017-02-18 21:34:21 +01:00
|
|
|
|
};
|
2016-09-30 18:45:55 +09:00
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2017-08-15 14:30:50 +09:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
input = GetContainingInputManager();
|
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2016-09-30 18:45:55 +09:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2019-02-21 18:56:34 +09:00
|
|
|
|
if (parallaxEnabled.Value)
|
2017-04-06 16:21:18 +08:00
|
|
|
|
{
|
2021-01-22 15:37:50 +09:00
|
|
|
|
Vector2 offset = Vector2.Zero;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2021-01-22 15:37:50 +09:00
|
|
|
|
if (input.CurrentState.Mouse != null)
|
|
|
|
|
{
|
|
|
|
|
var sizeDiv2 = DrawSize / 2;
|
|
|
|
|
|
|
|
|
|
Vector2 relativeAmount = ToLocalSpace(input.CurrentState.Mouse.Position) - sizeDiv2;
|
|
|
|
|
|
|
|
|
|
const float base_factor = 0.999f;
|
|
|
|
|
|
|
|
|
|
relativeAmount.X = (float)(Math.Sign(relativeAmount.X) * Interpolation.Damp(0, 1, base_factor, Math.Abs(relativeAmount.X)));
|
|
|
|
|
relativeAmount.Y = (float)(Math.Sign(relativeAmount.Y) * Interpolation.Damp(0, 1, base_factor, Math.Abs(relativeAmount.Y)));
|
|
|
|
|
|
|
|
|
|
offset = relativeAmount * sizeDiv2 * ParallaxAmount;
|
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2019-11-20 13:19:49 +01:00
|
|
|
|
double elapsed = Math.Clamp(Clock.ElapsedFrameTime, 0, parallax_duration);
|
2019-11-07 09:30:50 +09:00
|
|
|
|
|
|
|
|
|
content.Position = Interpolation.ValueAt(elapsed, content.Position, offset, 0, parallax_duration, Easing.OutQuint);
|
2019-11-20 14:25:44 +01:00
|
|
|
|
content.Scale = Interpolation.ValueAt(elapsed, content.Scale, new Vector2(1 + Math.Abs(ParallaxAmount)), 0, 1000, Easing.OutQuint);
|
2017-02-18 22:00:07 +01:00
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2016-12-01 21:59:32 +09:00
|
|
|
|
firstUpdate = false;
|
2016-09-30 18:45:55 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-08 18:46:08 -05:00
|
|
|
|
}
|