2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-02-07 12:59:30 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-12-06 17:56:20 +08:00
|
|
|
|
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2016-09-30 17:45:55 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using OpenTK;
|
2016-11-09 07:13:20 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2017-02-19 04:34:21 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2017-02-20 00:11:31 +08:00
|
|
|
|
using osu.Framework.Configuration;
|
2018-02-25 04:49:45 +08:00
|
|
|
|
using osu.Framework.MathUtils;
|
2016-09-30 17:45:55 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
|
{
|
2017-11-01 16:06:55 +08:00
|
|
|
|
public class ParallaxContainer : Container, IRequireHighFrequencyMousePosition
|
2016-09-30 17:45:55 +08:00
|
|
|
|
{
|
2018-02-28 10:42:28 +08:00
|
|
|
|
public const float DEFAULT_PARALLAX_AMOUNT = 0.02f;
|
|
|
|
|
|
|
|
|
|
public float ParallaxAmount = DEFAULT_PARALLAX_AMOUNT;
|
2017-02-19 05:26:48 +08:00
|
|
|
|
|
2017-02-20 00:11:31 +08:00
|
|
|
|
private Bindable<bool> parallaxEnabled;
|
2016-09-30 17:45:55 +08:00
|
|
|
|
|
2016-10-01 17:40:14 +08:00
|
|
|
|
public ParallaxContainer()
|
2016-09-30 17:45:55 +08:00
|
|
|
|
{
|
2016-10-01 17:40:14 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2016-11-11 06:40:42 +08:00
|
|
|
|
AddInternal(content = new Container
|
2016-11-09 07:13:20 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre
|
|
|
|
|
});
|
2016-09-30 17:45:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 12:41:50 +08:00
|
|
|
|
private readonly Container content;
|
2016-11-24 14:30:55 +08:00
|
|
|
|
private InputManager input;
|
2016-11-11 06:40:42 +08:00
|
|
|
|
|
|
|
|
|
protected override Container<Drawable> Content => content;
|
|
|
|
|
|
2016-11-24 14:30:55 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2017-08-15 13:30:50 +08:00
|
|
|
|
private void load(OsuConfigManager config)
|
2016-09-30 17:45:55 +08:00
|
|
|
|
{
|
2017-05-15 09:56:27 +08:00
|
|
|
|
parallaxEnabled = config.GetBindable<bool>(OsuSetting.MenuParallax);
|
2017-02-20 00:11:31 +08:00
|
|
|
|
parallaxEnabled.ValueChanged += delegate
|
2017-02-19 04:34:21 +08:00
|
|
|
|
{
|
2017-02-19 20:47:26 +08:00
|
|
|
|
if (!parallaxEnabled)
|
|
|
|
|
{
|
2017-07-23 02:50:25 +08:00
|
|
|
|
content.MoveTo(Vector2.Zero, firstUpdate ? 0 : 1000, Easing.OutQuint);
|
2017-02-20 00:11:31 +08:00
|
|
|
|
content.Scale = new Vector2(1 + ParallaxAmount);
|
2017-02-19 20:47:26 +08:00
|
|
|
|
}
|
2017-02-19 04:34:21 +08:00
|
|
|
|
};
|
2016-09-30 17:45:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 13:30:50 +08:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
input = GetContainingInputManager();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 09:59:19 +08:00
|
|
|
|
private bool firstUpdate = true;
|
2016-12-01 20:59:32 +08:00
|
|
|
|
|
2016-09-30 17:45:55 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
2016-12-01 20:59:32 +08:00
|
|
|
|
|
2017-04-06 16:21:18 +08:00
|
|
|
|
if (parallaxEnabled)
|
|
|
|
|
{
|
2018-02-25 04:49:45 +08:00
|
|
|
|
Vector2 offset = (input.CurrentState.Mouse == null ? Vector2.Zero : ToLocalSpace(input.CurrentState.Mouse.NativeState.Position) - DrawSize / 2) * ParallaxAmount;
|
|
|
|
|
|
|
|
|
|
content.Position = Interpolation.ValueAt(Clock.ElapsedFrameTime, content.Position, offset, 0, 1000, Easing.OutQuint);
|
2017-02-19 05:00:07 +08:00
|
|
|
|
content.Scale = new Vector2(1 + ParallaxAmount);
|
|
|
|
|
}
|
2016-12-01 20:59:32 +08:00
|
|
|
|
|
|
|
|
|
firstUpdate = false;
|
2016-09-30 17:45:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-09 07:46:08 +08:00
|
|
|
|
}
|