From 9a9ed1d630d9aa01ecca4ae03251d3a75124c7b4 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 5 Apr 2018 19:30:54 +0900 Subject: [PATCH] Encapsulate zoom logic into a separate class --- .../Timeline/ScrollingTimelineContainer.cs | 115 ++--------------- .../Timeline/ZoomableScrollContainer.cs | 118 ++++++++++++++++++ 2 files changed, 125 insertions(+), 108 deletions(-) create mode 100644 osu.Game/Screens/Edit/Screens/Compose/Timeline/ZoomableScrollContainer.cs diff --git a/osu.Game/Screens/Edit/Screens/Compose/Timeline/ScrollingTimelineContainer.cs b/osu.Game/Screens/Edit/Screens/Compose/Timeline/ScrollingTimelineContainer.cs index e48b625b8b..96dc54f1e9 100644 --- a/osu.Game/Screens/Edit/Screens/Compose/Timeline/ScrollingTimelineContainer.cs +++ b/osu.Game/Screens/Edit/Screens/Compose/Timeline/ScrollingTimelineContainer.cs @@ -3,40 +3,24 @@ using osu.Framework.Configuration; using osu.Framework.Graphics; -using osu.Framework.Graphics.Containers; -using osu.Framework.Graphics.Transforms; -using osu.Framework.Input; -using osu.Framework.MathUtils; using osu.Game.Beatmaps; using osu.Game.Graphics; -using OpenTK; namespace osu.Game.Screens.Edit.Screens.Compose.Timeline { - public class ScrollingTimelineContainer : ScrollContainer + public class ScrollingTimelineContainer : ZoomableScrollContainer { public readonly Bindable WaveformVisible = new Bindable(); public readonly Bindable Beatmap = new Bindable(); - private readonly Container zoomedContent; - - private float currentZoom = 10; - public ScrollingTimelineContainer() - : base(Direction.Horizontal) { - Masking = true; - BeatmapWaveformGraph waveform; - Child = zoomedContent = new Container + Child = waveform = new BeatmapWaveformGraph { - RelativeSizeAxes = Axes.Y, - Child = waveform = new BeatmapWaveformGraph - { - RelativeSizeAxes = Axes.Both, - Colour = OsuColour.FromHex("222"), - Depth = float.MaxValue - } + RelativeSizeAxes = Axes.Both, + Colour = OsuColour.FromHex("222"), + Depth = float.MaxValue }; waveform.Beatmap.BindTo(Beatmap); @@ -44,97 +28,12 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Timeline WaveformVisible.ValueChanged += visible => waveform.FadeTo(visible ? 1 : 0, 200, Easing.OutQuint); } - /// - /// Gets or sets the content zoom of this . - /// - public int Zoom - { - get => zoomTarget; - set => setZoomTarget(value, ToSpaceOfOtherDrawable(new Vector2(DrawWidth / 2, 0), zoomedContent).X); - } - protected override void Update() { base.Update(); - zoomedContent.Margin = new MarginPadding { Horizontal = DrawWidth / 2 }; - zoomedContent.Width = DrawWidth * currentZoom; - } - - protected override bool OnWheel(InputState state) - { - if (!state.Keyboard.ControlPressed) - return base.OnWheel(state); - - setZoomTarget(zoomTarget + state.Mouse.WheelDelta, zoomedContent.ToLocalSpace(state.Mouse.NativeState.Position).X); - return true; - } - - private int zoomTarget = 10; - private void setZoomTarget(int newZoom, float focusPoint) - { - zoomTarget = MathHelper.Clamp(newZoom, 1, 60); - transformZoomTo(zoomTarget, focusPoint, 200, Easing.OutQuint); - } - - private void transformZoomTo(int newZoom, float focusPoint, double duration = 0, Easing easing = Easing.None) - => this.TransformTo(this.PopulateTransform(new TransformZoom(focusPoint, zoomedContent.DrawWidth), newZoom, duration, easing)); - - private class TransformZoom : Transform - { - /// - /// The focus point in the waveform, in absolute coordinates local to the waveform. - /// - private readonly float focusPoint; - - /// - /// The size of the waveform. - /// - private readonly float waveformSize; - - /// - /// The scroll offset at the start time of the transform/ - /// - private float startScrollOffset; - - /// - /// Transforms to a new value. - /// - /// The focus point in the waveform, in absolute coordinates local to the waveform. - /// The size of the waveform. - public TransformZoom(float focusPoint, float waveformSize) - { - this.focusPoint = focusPoint; - this.waveformSize = waveformSize; - } - - 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(ScrollingTimelineContainer d, double time) - { - float newZoom = valueAt(time); - - float focusOffset = focusPoint - startScrollOffset; - float expectedWidth = d.DrawWidth * newZoom; - float targetOffset = expectedWidth * (focusPoint / waveformSize) - focusOffset; - - d.currentZoom = newZoom; - d.ScrollTo(targetOffset, false); - } - - protected override void ReadIntoStartValue(ScrollingTimelineContainer d) - { - startScrollOffset = d.Current; - StartValue = d.currentZoom; - } + // We want time = 0 to be at the centre of the container when scrolled to the start + Content.Margin = new MarginPadding { Horizontal = DrawWidth / 2 }; } } } diff --git a/osu.Game/Screens/Edit/Screens/Compose/Timeline/ZoomableScrollContainer.cs b/osu.Game/Screens/Edit/Screens/Compose/Timeline/ZoomableScrollContainer.cs new file mode 100644 index 0000000000..05a2882f31 --- /dev/null +++ b/osu.Game/Screens/Edit/Screens/Compose/Timeline/ZoomableScrollContainer.cs @@ -0,0 +1,118 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Transforms; +using osu.Framework.Input; +using osu.Framework.MathUtils; +using OpenTK; + +namespace osu.Game.Screens.Edit.Screens.Compose.Timeline +{ + public class ZoomableScrollContainer : ScrollContainer + { + private readonly Container zoomedContent; + protected override Container Content => zoomedContent; + + private float currentZoom = 10; + + public ZoomableScrollContainer() + : base(Direction.Horizontal) + { + base.Content.Add(zoomedContent = new Container { RelativeSizeAxes = Axes.Y }); + } + + /// + /// Gets or sets the content zoom of this . + /// + public int Zoom + { + get => zoomTarget; + set => setZoomTarget(value, ToSpaceOfOtherDrawable(new Vector2(DrawWidth / 2, 0), zoomedContent).X); + } + + protected override void Update() + { + base.Update(); + + zoomedContent.Width = DrawWidth * currentZoom; + } + + protected override bool OnWheel(InputState state) + { + if (!state.Keyboard.ControlPressed) + return base.OnWheel(state); + + setZoomTarget(zoomTarget + state.Mouse.WheelDelta, zoomedContent.ToLocalSpace(state.Mouse.NativeState.Position).X); + return true; + } + + private int zoomTarget = 10; + private void setZoomTarget(int newZoom, float focusPoint) + { + zoomTarget = MathHelper.Clamp(newZoom, 1, 60); + transformZoomTo(zoomTarget, focusPoint, 200, Easing.OutQuint); + } + + private void transformZoomTo(int newZoom, float focusPoint, double duration = 0, Easing easing = Easing.None) + => this.TransformTo(this.PopulateTransform(new TransformZoom(focusPoint, zoomedContent.DrawWidth), newZoom, duration, easing)); + + private class TransformZoom : Transform + { + /// + /// The focus point in the waveform, in absolute coordinates local to the waveform. + /// + private readonly float focusPoint; + + /// + /// The size of the waveform. + /// + private readonly float waveformSize; + + /// + /// The scroll offset at the start time of the transform/ + /// + private float startScrollOffset; + + /// + /// Transforms to a new value. + /// + /// The focus point in the waveform, in absolute coordinates local to the waveform. + /// The size of the waveform. + public TransformZoom(float focusPoint, float waveformSize) + { + this.focusPoint = focusPoint; + this.waveformSize = waveformSize; + } + + 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 - startScrollOffset; + float expectedWidth = d.DrawWidth * newZoom; + float targetOffset = expectedWidth * (focusPoint / waveformSize) - focusOffset; + + d.currentZoom = newZoom; + d.ScrollTo(targetOffset, false); + } + + protected override void ReadIntoStartValue(ZoomableScrollContainer d) + { + startScrollOffset = d.Current; + StartValue = d.currentZoom; + } + } + } +}