1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 16:47:24 +08:00
osu-lazer/osu.Game/Graphics/Containers/ParallaxContainer.cs

70 lines
2.2 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// 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;
using osu.Framework.Configuration;
2016-09-30 17:45:55 +08:00
namespace osu.Game.Graphics.Containers
{
internal class ParallaxContainer : Container, IRequireHighFrequencyMousePosition
2016-09-30 17:45:55 +08:00
{
public float ParallaxAmount = 0.02f;
private Bindable<bool> parallaxEnabled;
2016-09-30 17:45:55 +08:00
public ParallaxContainer()
2016-09-30 17:45:55 +08:00
{
AlwaysReceiveInput = true;
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
}
private readonly Container content;
private InputManager input;
2016-11-11 06:40:42 +08:00
protected override Container<Drawable> Content => content;
[BackgroundDependencyLoader]
2017-02-19 04:34:21 +08:00
private void load(UserInputManager input, OsuConfigManager config)
2016-09-30 17:45:55 +08:00
{
this.input = input;
parallaxEnabled = config.GetBindable<bool>(OsuConfig.MenuParallax);
parallaxEnabled.ValueChanged += delegate
2017-02-19 04:34:21 +08:00
{
if (!parallaxEnabled)
{
content.MoveTo(Vector2.Zero, firstUpdate ? 0 : 1000, EasingTypes.OutQuint);
content.Scale = new Vector2(1 + ParallaxAmount);
}
2017-02-19 04:34:21 +08:00
};
2016-09-30 17:45:55 +08:00
}
2017-03-07 09:59:19 +08:00
private bool firstUpdate = true;
2016-09-30 17:45:55 +08:00
protected override void Update()
{
base.Update();
2017-02-19 23:54:00 +08:00
if (parallaxEnabled)
{
Vector2 offset = input.CurrentState.Mouse == null ? Vector2.Zero : ToLocalSpace(input.CurrentState.Mouse.NativeState.Position) - DrawSize / 2;
content.MoveTo(offset * ParallaxAmount, firstUpdate ? 0 : 1000, EasingTypes.OutQuint);
content.Scale = new Vector2(1 + ParallaxAmount);
}
firstUpdate = false;
2016-09-30 17:45:55 +08:00
}
}
}