1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 16:42:55 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/Components/Timeline/ZoomableScrollContainer.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

229 lines
7.6 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-05-18 12:05:58 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2018-05-18 16:53:09 +08:00
using System;
using osu.Framework.Allocation;
2018-05-18 12:05:58 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Transforms;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Layout;
using osu.Framework.Timing;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
using osu.Game.Graphics.Containers;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-05-18 12:05:58 +08:00
2018-11-06 17:28:22 +08:00
namespace osu.Game.Screens.Edit.Compose.Components.Timeline
2018-05-18 12:05:58 +08:00
{
public class ZoomableScrollContainer : OsuScrollContainer
2018-05-18 12:05:58 +08:00
{
2018-05-18 16:53:09 +08:00
/// <summary>
/// The time to zoom into/out of a point.
/// All user scroll input will be overwritten during the zoom transform.
/// </summary>
public double ZoomDuration;
/// <summary>
/// The easing with which to transform the zoom.
/// </summary>
public Easing ZoomEasing;
2018-05-18 12:05:58 +08:00
private readonly Container zoomedContent;
protected override Container<Drawable> Content => zoomedContent;
2018-05-18 16:53:09 +08:00
private float currentZoom = 1;
2018-05-18 12:05:58 +08:00
/// <summary>
/// The current zoom level of <see cref="ZoomableScrollContainer" />.
/// It may differ from <see cref="Zoom" /> during transitions.
/// </summary>
public float CurrentZoom => currentZoom;
[Resolved(canBeNull: true)]
private IFrameBasedClock editorClock { get; set; }
private readonly LayoutValue zoomedContentWidthCache = new LayoutValue(Invalidation.DrawSize);
2018-05-18 12:05:58 +08:00
public ZoomableScrollContainer()
: base(Direction.Horizontal)
{
base.Content.Add(zoomedContent = new Container { RelativeSizeAxes = Axes.Y });
AddLayout(zoomedContentWidthCache);
2018-05-18 12:05:58 +08:00
}
private float minZoom = 1;
2018-05-18 16:53:09 +08:00
2018-05-18 12:05:58 +08:00
/// <summary>
2018-05-18 16:53:09 +08:00
/// The minimum zoom level allowed.
/// </summary>
public float MinZoom
2018-05-18 16:53:09 +08:00
{
get => minZoom;
set
{
if (value < 1)
throw new ArgumentException($"{nameof(MinZoom)} must be >= 1.", nameof(value));
2019-02-28 12:31:40 +08:00
2018-05-18 16:53:09 +08:00
minZoom = value;
// ensure zoom range is in valid state before updating zoom.
if (MinZoom < MaxZoom)
updateZoom();
2018-05-18 16:53:09 +08:00
}
}
private float maxZoom = 60;
2018-05-18 16:53:09 +08:00
/// <summary>
/// The maximum zoom level allowed.
/// </summary>
public float MaxZoom
2018-05-18 16:53:09 +08:00
{
get => maxZoom;
set
{
if (value < 1)
throw new ArgumentException($"{nameof(MaxZoom)} must be >= 1.", nameof(value));
2019-02-28 12:31:40 +08:00
2018-05-18 16:53:09 +08:00
maxZoom = value;
// ensure zoom range is in valid state before updating zoom.
if (MaxZoom > MinZoom)
updateZoom();
2018-05-18 16:53:09 +08:00
}
}
/// <summary>
/// Gets or sets the content zoom level of this <see cref="ZoomableScrollContainer"/>.
2018-05-18 12:05:58 +08:00
/// </summary>
public float Zoom
2018-05-18 12:05:58 +08:00
{
get => zoomTarget;
set => updateZoom(value);
}
2018-05-18 16:53:09 +08:00
private void updateZoom(float? value = null)
{
float newZoom = Math.Clamp(value ?? Zoom, MinZoom, MaxZoom);
if (IsLoaded)
setZoomTarget(newZoom, ToSpaceOfOtherDrawable(new Vector2(DrawWidth / 2, 0), zoomedContent).X);
else
currentZoom = zoomTarget = newZoom;
2018-05-18 12:05:58 +08:00
}
protected override void Update()
2019-05-09 12:53:53 +08:00
{
base.Update();
2019-05-09 12:53:53 +08:00
if (!zoomedContentWidthCache.IsValid)
updateZoomedContentWidth();
2019-05-09 12:53:53 +08:00
}
2019-05-09 12:16:56 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnScroll(ScrollEvent e)
2018-05-18 12:05:58 +08:00
{
2020-11-04 04:49:04 +08:00
if (e.AltPressed)
{
// zoom when holding alt.
setZoomTarget(zoomTarget + e.ScrollDelta.Y, zoomedContent.ToLocalSpace(e.ScreenSpaceMousePosition).X);
return true;
}
// can't handle scroll correctly while playing.
// the editor will handle this case for us.
if (editorClock?.IsRunning == true)
return false;
return base.OnScroll(e);
2018-05-18 12:05:58 +08:00
}
private void updateZoomedContentWidth()
{
zoomedContent.Width = DrawWidth * currentZoom;
zoomedContentWidthCache.Validate();
}
private float zoomTarget = 1;
2019-02-28 12:31:40 +08:00
private void setZoomTarget(float newZoom, float focusPoint)
2018-05-18 12:05:58 +08:00
{
zoomTarget = Math.Clamp(newZoom, MinZoom, MaxZoom);
2018-05-18 16:53:09 +08:00
transformZoomTo(zoomTarget, focusPoint, ZoomDuration, ZoomEasing);
OnZoomChanged();
2018-05-18 12:05:58 +08:00
}
private void transformZoomTo(float newZoom, float focusPoint, double duration = 0, Easing easing = Easing.None)
2018-05-18 12:05:58 +08:00
=> this.TransformTo(this.PopulateTransform(new TransformZoom(focusPoint, zoomedContent.DrawWidth, Current), newZoom, duration, easing));
/// <summary>
/// Invoked when <see cref="Zoom"/> has changed.
/// </summary>
protected virtual void OnZoomChanged()
{
}
2018-05-18 12:05:58 +08:00
private class TransformZoom : Transform<float, ZoomableScrollContainer>
{
/// <summary>
/// The focus point in absolute coordinates local to the content.
/// </summary>
private readonly float focusPoint;
/// <summary>
/// The size of the content.
/// </summary>
private readonly float contentSize;
/// <summary>
/// The scroll offset at the start of the transform.
/// </summary>
private readonly float scrollOffset;
/// <summary>
2019-04-25 16:36:17 +08:00
/// Transforms <see cref="ZoomableScrollContainer.currentZoom"/> to a new value.
2018-05-18 12:05:58 +08:00
/// </summary>
/// <param name="focusPoint">The focus point in absolute coordinates local to the content.</param>
/// <param name="contentSize">The size of the content.</param>
/// <param name="scrollOffset">The scroll offset at the start of the transform.</param>
public TransformZoom(float focusPoint, float contentSize, float scrollOffset)
{
this.focusPoint = focusPoint;
this.contentSize = contentSize;
this.scrollOffset = scrollOffset;
}
public override string TargetMember => nameof(currentZoom);
private float valueAt(double time)
{
if (time < StartTime) return StartValue;
if (time >= EndTime) return EndValue;
return Interpolation.ValueAt(time, StartValue, EndValue, StartTime, EndTime, Easing);
}
protected override void Apply(ZoomableScrollContainer d, double time)
{
float newZoom = valueAt(time);
float focusOffset = focusPoint - scrollOffset;
float expectedWidth = d.DrawWidth * newZoom;
float targetOffset = expectedWidth * (focusPoint / contentSize) - focusOffset;
d.currentZoom = newZoom;
2019-05-09 12:16:56 +08:00
d.updateZoomedContentWidth();
// Temporarily here to make sure ScrollTo gets the correct DrawSize for scrollable area.
// TODO: Make sure draw size gets invalidated properly on the framework side, and remove this once it is.
d.Invalidate(Invalidation.DrawSize);
2018-05-18 12:05:58 +08:00
d.ScrollTo(targetOffset, false);
}
protected override void ReadIntoStartValue(ZoomableScrollContainer d) => StartValue = d.currentZoom;
}
}
}