1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 18:42:56 +08:00
osu-lazer/osu.Game/Graphics/Containers/ParallaxContainer.cs

101 lines
3.3 KiB
C#
Raw Normal View History

// 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 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics;
using osu.Framework.Input;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Game.Configuration;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.Containers
{
2022-11-24 13:32:20 +08:00
public partial class ParallaxContainer : Container, IRequireHighFrequencyMousePosition
2018-04-13 17:19:50 +08:00
{
public const float DEFAULT_PARALLAX_AMOUNT = 0.02f;
/// <summary>
/// The amount of parallax movement. Negative values will reverse the direction of parallax relative to user input.
/// </summary>
2018-04-13 17:19:50 +08:00
public float ParallaxAmount = DEFAULT_PARALLAX_AMOUNT;
private Bindable<bool> parallaxEnabled;
private const float parallax_duration = 100;
private bool firstUpdate = true;
2018-04-13 17:19:50 +08: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 17:56:34 +08:00
if (!parallaxEnabled.Value)
2018-04-13 17:19:50 +08:00
{
content.MoveTo(Vector2.Zero, firstUpdate ? 0 : 1000, Easing.OutQuint);
2019-11-20 21:25:44 +08:00
content.Scale = new Vector2(1 + Math.Abs(ParallaxAmount));
2018-04-13 17:19:50 +08:00
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
input = GetContainingInputManager();
}
protected override void Update()
{
base.Update();
2019-02-21 17:56:34 +08:00
if (parallaxEnabled.Value)
2018-04-13 17:19:50 +08:00
{
Vector2 offset = Vector2.Zero;
2018-04-13 17:19:50 +08: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 17:19:50 +08:00
double elapsed = Math.Clamp(Clock.ElapsedFrameTime, 0, parallax_duration);
content.Position = Interpolation.ValueAt(elapsed, content.Position, offset, 0, parallax_duration, Easing.OutQuint);
2019-11-20 21:25:44 +08:00
content.Scale = Interpolation.ValueAt(elapsed, content.Scale, new Vector2(1 + Math.Abs(ParallaxAmount)), 0, 1000, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
firstUpdate = false;
}
}
}