From 4042a9e71edf1bf93c12fdc3c1b9d58ac2ace423 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 6 Dec 2016 18:54:32 +0900 Subject: [PATCH] Add preliminary (correctly) textured sliders; split out components into their own files. --- .../Objects/Drawables/DrawableOsuHitObject.cs | 11 +- .../Objects/Drawables/DrawableSlider.cs | 274 ++---------------- .../Objects/Drawables/Pieces/SliderBall.cs | 107 +++++++ .../Objects/Drawables/Pieces/SliderBody.cs | 204 +++++++++++++ osu.Game.Mode.Osu/osu.Game.Modes.Osu.csproj | 2 + 5 files changed, 347 insertions(+), 251 deletions(-) create mode 100644 osu.Game.Mode.Osu/Objects/Drawables/Pieces/SliderBall.cs create mode 100644 osu.Game.Mode.Osu/Objects/Drawables/Pieces/SliderBody.cs diff --git a/osu.Game.Mode.Osu/Objects/Drawables/DrawableOsuHitObject.cs b/osu.Game.Mode.Osu/Objects/Drawables/DrawableOsuHitObject.cs index 80e1f2bb7f..dca9ac9b28 100644 --- a/osu.Game.Mode.Osu/Objects/Drawables/DrawableOsuHitObject.cs +++ b/osu.Game.Mode.Osu/Objects/Drawables/DrawableOsuHitObject.cs @@ -1,4 +1,7 @@ -using System; +//Copyright (c) 2007-2016 ppy Pty Ltd . +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -11,9 +14,9 @@ namespace osu.Game.Modes.Osu.Objects.Drawables { public class DrawableOsuHitObject : DrawableHitObject { - protected const float TIME_PREEMPT = 600; - protected const float TIME_FADEIN = 400; - protected const float TIME_FADEOUT = 500; + public const float TIME_PREEMPT = 600; + public const float TIME_FADEIN = 400; + public const float TIME_FADEOUT = 500; public DrawableOsuHitObject(OsuHitObject hitObject) : base(hitObject) diff --git a/osu.Game.Mode.Osu/Objects/Drawables/DrawableSlider.cs b/osu.Game.Mode.Osu/Objects/Drawables/DrawableSlider.cs index f41b88b21f..e93ebba366 100644 --- a/osu.Game.Mode.Osu/Objects/Drawables/DrawableSlider.cs +++ b/osu.Game.Mode.Osu/Objects/Drawables/DrawableSlider.cs @@ -1,21 +1,11 @@ -// Copyright (c) 2007-2016 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE +//Copyright (c) 2007-2016 ppy Pty Ltd . +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System.Collections.Generic; using osu.Framework.Graphics; -using osu.Framework.Graphics.Containers; using osu.Game.Modes.Objects.Drawables; +using osu.Game.Modes.Osu.Objects.Drawables.Pieces; using OpenTK; -using osu.Framework.Graphics.Sprites; -using osu.Framework.Graphics.Transformations; -using OpenTK.Graphics; -using osu.Framework.Input; -using OpenTK.Graphics.ES30; -using osu.Framework.Allocation; -using osu.Framework.Graphics.Textures; -using osu.Game.Configuration; -using osu.Framework.Configuration; -using System; namespace osu.Game.Modes.Osu.Objects.Drawables { @@ -24,11 +14,14 @@ namespace osu.Game.Modes.Osu.Objects.Drawables private Slider slider; private DrawableHitCircle startCircle; - private Container ball; - private Body body; + + private List components = new List(); public DrawableSlider(Slider s) : base(s) { + SliderBody body; + SliderBall ball; + slider = s; Origin = Anchor.TopLeft; @@ -37,11 +30,12 @@ namespace osu.Game.Modes.Osu.Objects.Drawables Children = new Drawable[] { - body = new Body(s) + body = new SliderBody(s) { Position = s.Position, + PathWidth = 36, }, - ball = new Ball(), + ball = new SliderBall(slider), startCircle = new DrawableHitCircle(new HitCircle { StartTime = s.StartTime, @@ -52,75 +46,24 @@ namespace osu.Game.Modes.Osu.Objects.Drawables Depth = -1 //override time-based depth. }, }; - } - private Bindable snakingIn; - private Bindable snakingOut; - - [BackgroundDependencyLoader] - private void load(OsuConfigManager config) - { - snakingIn = config.GetBindable(OsuConfig.SnakingInSliders); - snakingOut = config.GetBindable(OsuConfig.SnakingOutSliders); - } - - protected override void LoadComplete() - { - base.LoadComplete(); - - - body.PathWidth = 32; - } - - private void computeProgress(out int repeat, out double progress) - { - progress = MathHelper.Clamp((Time.Current - slider.StartTime) / slider.Duration, 0, 1); - - repeat = (int)(progress * slider.RepeatCount); - progress = (progress * slider.RepeatCount) % 1; - - if (repeat % 2 == 1) - progress = 1 - progress; - } - - private void updateBall(double progress) - { - ball.Alpha = Time.Current >= slider.StartTime && Time.Current <= slider.EndTime ? 1 : 0; - ball.Position = slider.Curve.PositionAt(progress); - } - - private void updateBody(int repeat, double progress) - { - double start = 0; - double end = snakingIn ? MathHelper.Clamp((Time.Current - (slider.StartTime - TIME_PREEMPT)) / TIME_FADEIN, 0, 1) : 1; - - if (repeat >= slider.RepeatCount - 1) - { - if (Math.Min(repeat, slider.RepeatCount - 1) % 2 == 1) - { - start = 0; - end = snakingOut ? progress : 1; - } - else - { - start = snakingOut ? progress : 0; - end = 1; - } - } - - body.SetRange(start, end); + components.Add(body); + components.Add(ball); } protected override void Update() { base.Update(); - double progress; - int repeat; - computeProgress(out repeat, out progress); + double progress = MathHelper.Clamp((Time.Current - slider.StartTime) / slider.Duration, 0, 1); - updateBall(progress); - updateBody(repeat, progress); + int repeat = (int)(progress * slider.RepeatCount); + progress = (progress * slider.RepeatCount) % 1; + + if (repeat % 2 == 1) + progress = 1 - progress; + + components.ForEach(c => c.UpdateProgress(progress, repeat)); } protected override void CheckJudgement(bool userTriggered) @@ -142,173 +85,10 @@ namespace osu.Game.Modes.Osu.Objects.Drawables Delay(HitObject.Duration); FadeOut(300); } - - private class Ball : Container - { - private Box follow; - - public Ball() - { - Masking = true; - AutoSizeAxes = Axes.Both; - BlendingMode = BlendingMode.Additive; - Origin = Anchor.Centre; - - Children = new Drawable[] - { - follow = new Box - { - Origin = Anchor.Centre, - Anchor = Anchor.Centre, - Colour = Color4.Orange, - Width = 64, - Height = 64, - }, - new Container - { - Masking = true, - AutoSizeAxes = Axes.Both, - Origin = Anchor.Centre, - Anchor = Anchor.Centre, - Colour = Color4.Cyan, - CornerRadius = 32, - Children = new[] - { - new Box - { - - Width = 64, - Height = 64, - }, - } - } - - }; - } - - private InputState lastState; - - protected override bool OnMouseDown(InputState state, MouseDownEventArgs args) - { - lastState = state; - return base.OnMouseDown(state, args); - } - - protected override bool OnMouseUp(InputState state, MouseUpEventArgs args) - { - lastState = state; - return base.OnMouseUp(state, args); - } - - protected override bool OnMouseMove(InputState state) - { - lastState = state; - return base.OnMouseMove(state); - } - - bool tracking; - protected bool Tracking - { - get { return tracking; } - set - { - if (value == tracking) return; - - tracking = value; - - follow.ScaleTo(tracking ? 2.4f : 1, 140, EasingTypes.Out); - follow.FadeTo(tracking ? 0.8f : 0, 140, EasingTypes.Out); - } - } - - protected override void Update() - { - base.Update(); - - CornerRadius = DrawWidth / 2; - Tracking = lastState != null && Contains(lastState.Mouse.NativeState.Position) && lastState.Mouse.HasMainButtonPressed; - } - } - - private class Body : Container - { - private Path path; - private BufferedContainer container; - - public float PathWidth - { - get { return path.PathWidth; } - set { path.PathWidth = value; } - } - - private double? drawnProgressStart; - private double? drawnProgressEnd; - - private Slider slider; - public Body(Slider s) - { - slider = s; - - Children = new Drawable[] - { - container = new BufferedContainer - { - CacheDrawnFrameBuffer = true, - Children = new Drawable[] - { - path = new Path - { - Colour = s.Colour, - BlendingMode = BlendingMode.None, - }, - } - } - }; - - container.Attach(RenderbufferInternalFormat.DepthComponent16); - } - - [BackgroundDependencyLoader] - private void load(TextureStore textures) - { - // Surprisingly, this looks somewhat okay and works well as a test for self-overlaps. - // TODO: Don't do this. - path.Texture = textures.Get(@"Menu/logo"); - } - - public void SetRange(double p0, double p1) - { - if (p0 > p1) - MathHelper.Swap(ref p0, ref p1); - - if (updateSnaking(p0, p1)) - { - // Autosizing does not give us the desired behaviour here. - // We want the container to have the same size as the slider, - // and to be positioned such that the slider head is at (0,0). - container.Size = path.Size; - container.Position = -path.PositionInBoundingBox(slider.Curve.PositionAt(0) - currentCurve[0]); - - container.ForceRedraw(); - } - } - - private List currentCurve = new List(); - private bool updateSnaking(double p0, double p1) - { - if (drawnProgressStart == p0 && drawnProgressEnd == p1) return false; - - drawnProgressStart = p0; - drawnProgressEnd = p1; - - slider.Curve.GetPathToProgress(currentCurve, p0, p1); - - path.ClearVertices(); - foreach (Vector2 p in currentCurve) - path.AddVertex(p - currentCurve[0]); - - return true; - } - } } -} + + internal interface ISliderProgress + { + void UpdateProgress(double progress, int repeat); + } +} \ No newline at end of file diff --git a/osu.Game.Mode.Osu/Objects/Drawables/Pieces/SliderBall.cs b/osu.Game.Mode.Osu/Objects/Drawables/Pieces/SliderBall.cs new file mode 100644 index 0000000000..f7765a5057 --- /dev/null +++ b/osu.Game.Mode.Osu/Objects/Drawables/Pieces/SliderBall.cs @@ -0,0 +1,107 @@ +//Copyright (c) 2007-2016 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.Sprites; +using osu.Framework.Graphics.Transformations; +using osu.Framework.Input; +using OpenTK.Graphics; + +namespace osu.Game.Modes.Osu.Objects.Drawables.Pieces +{ + public class SliderBall : Container, ISliderProgress + { + private readonly Slider slider; + private Box follow; + + public SliderBall(Slider slider) + { + this.slider = slider; + Masking = true; + AutoSizeAxes = Axes.Both; + BlendingMode = BlendingMode.Additive; + Origin = Anchor.Centre; + + Children = new Drawable[] + { + follow = new Box + { + Origin = Anchor.Centre, + Anchor = Anchor.Centre, + Colour = Color4.Orange, + Width = 64, + Height = 64, + }, + new Container + { + Masking = true, + AutoSizeAxes = Axes.Both, + Origin = Anchor.Centre, + Anchor = Anchor.Centre, + Colour = Color4.Cyan, + CornerRadius = 32, + Children = new[] + { + new Box + { + + Width = 64, + Height = 64, + }, + } + } + + }; + } + + private InputState lastState; + + protected override bool OnMouseDown(InputState state, MouseDownEventArgs args) + { + lastState = state; + return base.OnMouseDown(state, args); + } + + protected override bool OnMouseUp(InputState state, MouseUpEventArgs args) + { + lastState = state; + return base.OnMouseUp(state, args); + } + + protected override bool OnMouseMove(InputState state) + { + lastState = state; + return base.OnMouseMove(state); + } + + bool tracking; + protected bool Tracking + { + get { return tracking; } + set + { + if (value == tracking) return; + + tracking = value; + + follow.ScaleTo(tracking ? 2.4f : 1, 140, EasingTypes.Out); + follow.FadeTo(tracking ? 0.8f : 0, 140, EasingTypes.Out); + } + } + + protected override void Update() + { + base.Update(); + + CornerRadius = DrawWidth / 2; + Tracking = lastState != null && Contains(lastState.Mouse.NativeState.Position) && lastState.Mouse.HasMainButtonPressed; + } + + public void UpdateProgress(double progress, int repeat) + { + Alpha = Time.Current >= slider.StartTime && Time.Current <= slider.EndTime ? 1 : 0; + Position = slider.Curve.PositionAt(progress); + } + } +} \ No newline at end of file diff --git a/osu.Game.Mode.Osu/Objects/Drawables/Pieces/SliderBody.cs b/osu.Game.Mode.Osu/Objects/Drawables/Pieces/SliderBody.cs new file mode 100644 index 0000000000..354bf80619 --- /dev/null +++ b/osu.Game.Mode.Osu/Objects/Drawables/Pieces/SliderBody.cs @@ -0,0 +1,204 @@ +//Copyright (c) 2007-2016 ppy Pty Ltd . +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using System.Collections.Generic; +using osu.Framework.Allocation; +using osu.Framework.Configuration; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Colour; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Sprites; +using osu.Framework.Graphics.Textures; +using osu.Game.Configuration; +using OpenTK; +using OpenTK.Graphics; +using OpenTK.Graphics.ES30; + +namespace osu.Game.Modes.Osu.Objects.Drawables.Pieces +{ + public class SliderBody : Container, ISliderProgress + { + private Path path; + private BodyTexture pathTexture; + private BufferedContainer container; + + public float PathWidth + { + get { return path.PathWidth; } + set + { + path.PathWidth = value; + pathTexture.Size = new Vector2(value, 1); + } + } + + private double? drawnProgressStart; + private double? drawnProgressEnd; + + private Slider slider; + public SliderBody(Slider s) + { + slider = s; + + Children = new Drawable[] + { + pathTexture = new BodyTexture + { + MainColour = s.Colour + }, + container = new BufferedContainer + { + CacheDrawnFrameBuffer = true, + Children = new Drawable[] + { + path = new Path + { + BlendingMode = BlendingMode.None, + }, + } + }, + + }; + + container.Attach(RenderbufferInternalFormat.DepthComponent16); + } + + public void SetRange(double p0, double p1) + { + if (p0 > p1) + MathHelper.Swap(ref p0, ref p1); + + if (updateSnaking(p0, p1)) + { + // Autosizing does not give us the desired behaviour here. + // We want the container to have the same size as the slider, + // and to be positioned such that the slider head is at (0,0). + container.Size = path.Size; + container.Position = -path.PositionInBoundingBox(slider.Curve.PositionAt(0) - currentCurve[0]); + + container.ForceRedraw(); + } + } + + private Bindable snakingIn; + private Bindable snakingOut; + + [BackgroundDependencyLoader] + private void load(OsuConfigManager config) + { + snakingIn = config.GetBindable(OsuConfig.SnakingInSliders); + snakingOut = config.GetBindable(OsuConfig.SnakingOutSliders); + } + + private List currentCurve = new List(); + private bool updateSnaking(double p0, double p1) + { + if (drawnProgressStart == p0 && drawnProgressEnd == p1) return false; + + drawnProgressStart = p0; + drawnProgressEnd = p1; + + slider.Curve.GetPathToProgress(currentCurve, p0, p1); + + path.ClearVertices(); + foreach (Vector2 p in currentCurve) + path.AddVertex(p - currentCurve[0]); + + return true; + } + + protected override void Update() + { + base.Update(); + + var texture = pathTexture.Texture; + if (texture != null) + { + path.Texture = texture; + pathTexture.Alpha = 0; + } + } + + public void UpdateProgress(double progress, int repeat) + { + double start = 0; + double end = snakingIn ? MathHelper.Clamp((Time.Current - (slider.StartTime - DrawableOsuHitObject.TIME_PREEMPT)) / DrawableOsuHitObject.TIME_FADEIN, 0, 1) : 1; + + if (repeat >= slider.RepeatCount - 1) + { + if (Math.Min(repeat, slider.RepeatCount - 1) % 2 == 1) + { + start = 0; + end = snakingOut ? progress : 1; + } + else + { + start = snakingOut ? progress : 0; + } + } + + SetRange(start, end); + } + + public class BodyTexture : BufferedContainer + { + private Box gradientPortion; + private BufferedContainerDrawNode lastNode; + public Texture Texture => lastNode?.FrameBuffers[0].Texture != null ? new Texture(lastNode.FrameBuffers[0].Texture) : null; + + protected override void ApplyDrawNode(DrawNode node) + { + base.ApplyDrawNode(node); + lastNode = node as BufferedContainerDrawNode; + } + + public Color4 MainColour + { + set + { + gradientPortion.ColourInfo = ColourInfo.GradientHorizontal( + value, + new Color4(value.R, value.G, value.B, value.A * 0.4f) + ); + } + } + + public BodyTexture() + { + Children = new[] + { + new FlowContainer + { + RelativeSizeAxes = Axes.Both, + Direction = FlowDirection.HorizontalOnly, + Masking = true, + Children = new[] + { + new Box + { + RelativeSizeAxes = Axes.Both, + ColourInfo = ColourInfo.GradientHorizontal( + new Color4(255, 255, 255, 0), + Color4.White + ), + Size = new Vector2(0.02f, 1), + }, + new Box + { + RelativeSizeAxes = Axes.Both, + Colour = Color4.White, + Size = new Vector2(0.13f, 1), + }, + gradientPortion = new Box + { + RelativeSizeAxes = Axes.Both, + Size = new Vector2(0.85f, 1), + }, + }, + } + }; + } + } + } +} \ No newline at end of file diff --git a/osu.Game.Mode.Osu/osu.Game.Modes.Osu.csproj b/osu.Game.Mode.Osu/osu.Game.Modes.Osu.csproj index ba6e714e0a..1f53c706cc 100644 --- a/osu.Game.Mode.Osu/osu.Game.Modes.Osu.csproj +++ b/osu.Game.Mode.Osu/osu.Game.Modes.Osu.csproj @@ -53,6 +53,8 @@ + +