2019-01-24 08:43:03 +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.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-11-20 12:19:49 +00:00
|
|
|
|
using System;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Input;
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 10:04:31 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Game.Configuration;
|
2020-01-09 04:43:44 +00:00
|
|
|
|
using osu.Framework.Utils;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
|
{
|
|
|
|
|
public class ParallaxContainer : Container, IRequireHighFrequencyMousePosition
|
|
|
|
|
{
|
|
|
|
|
public const float DEFAULT_PARALLAX_AMOUNT = 0.02f;
|
|
|
|
|
|
2018-06-21 03:49:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The amount of parallax movement. Negative values will reverse the direction of parallax relative to user input.
|
|
|
|
|
/// </summary>
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public float ParallaxAmount = DEFAULT_PARALLAX_AMOUNT;
|
|
|
|
|
|
|
|
|
|
private Bindable<bool> parallaxEnabled;
|
|
|
|
|
|
2021-01-22 06:37:50 +00:00
|
|
|
|
private const float parallax_duration = 100;
|
|
|
|
|
|
|
|
|
|
private bool firstUpdate = true;
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public ParallaxContainer()
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
AddInternal(content = new Container
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly Container content;
|
|
|
|
|
private InputManager input;
|
|
|
|
|
|
|
|
|
|
protected override Container<Drawable> Content => content;
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuConfigManager config)
|
|
|
|
|
{
|
|
|
|
|
parallaxEnabled = config.GetBindable<bool>(OsuSetting.MenuParallax);
|
|
|
|
|
parallaxEnabled.ValueChanged += delegate
|
|
|
|
|
{
|
2019-02-21 09:56:34 +00:00
|
|
|
|
if (!parallaxEnabled.Value)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
content.MoveTo(Vector2.Zero, firstUpdate ? 0 : 1000, Easing.OutQuint);
|
2019-11-20 13:25:44 +00:00
|
|
|
|
content.Scale = new Vector2(1 + Math.Abs(ParallaxAmount));
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
input = GetContainingInputManager();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2019-02-21 09:56:34 +00:00
|
|
|
|
if (parallaxEnabled.Value)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-01-22 06:37:50 +00:00
|
|
|
|
Vector2 offset = Vector2.Zero;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-01-22 06:37:50 +00: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 09:19:50 +00:00
|
|
|
|
|
2019-11-20 12:19:49 +00:00
|
|
|
|
double elapsed = Math.Clamp(Clock.ElapsedFrameTime, 0, parallax_duration);
|
2019-11-07 00:30:50 +00:00
|
|
|
|
|
|
|
|
|
content.Position = Interpolation.ValueAt(elapsed, content.Position, offset, 0, parallax_duration, Easing.OutQuint);
|
2019-11-20 13:25:44 +00:00
|
|
|
|
content.Scale = Interpolation.ValueAt(elapsed, content.Scale, new Vector2(1 + Math.Abs(ParallaxAmount)), 0, 1000, Easing.OutQuint);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
firstUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|