From 777996d8841835c0d23f5aa3cebc3955a72ac54a Mon Sep 17 00:00:00 2001 From: smoogipooo Date: Fri, 3 Mar 2017 15:43:16 +0900 Subject: [PATCH] Rewrite VisualiserLine + add VisualiserContainer. --- .../Components/VisualiserContainer.cs | 208 +++++++++++++++++ .../Tournament/Components/VisualiserLine.cs | 209 ------------------ osu.Game/Screens/Tournament/Drawings.cs | 19 +- osu.Game/osu.Game.csproj | 2 +- 4 files changed, 213 insertions(+), 225 deletions(-) create mode 100644 osu.Game/Screens/Tournament/Components/VisualiserContainer.cs delete mode 100644 osu.Game/Screens/Tournament/Components/VisualiserLine.cs diff --git a/osu.Game/Screens/Tournament/Components/VisualiserContainer.cs b/osu.Game/Screens/Tournament/Components/VisualiserContainer.cs new file mode 100644 index 0000000000..53ade4de77 --- /dev/null +++ b/osu.Game/Screens/Tournament/Components/VisualiserContainer.cs @@ -0,0 +1,208 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using OpenTK; +using osu.Framework.Allocation; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Batches; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.OpenGL; +using osu.Framework.Graphics.Primitives; +using osu.Framework.Graphics.Shaders; +using osu.Framework.Graphics.Transforms; +using osu.Framework.MathUtils; +using osu.Framework.Timing; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace osu.Game.Screens.Tournament.Components +{ + class VisualiserContainer : Container + { + /// + /// Number of lines in the visualiser. + /// + public int Lines + { + get { return allLines.Count; } + set + { + while (value > allLines.Count) + addLine(); + + while (value < allLines.Count) + removeLine(); + } + } + + private List allLines = new List(); + + private float offset; + + private void addLine() + { + VisualiserLine newLine = new VisualiserLine() + { + RelativeSizeAxes = Axes.Both, + + PeriodOffset = offset, + Period = 2 * (float)Math.PI, + CycleTime = RNG.Next(10000, 12000) + }; + + allLines.Add(newLine); + Add(newLine); + + offset += (float)Math.PI / 6f; + } + + private void removeLine() + { + if (allLines.Count == 0) + return; + + Remove(allLines.First()); + allLines.Remove(allLines.First()); + } + + class VisualiserLine : Drawable + { + /// + /// Width of the line strokes. + /// + public float StrokeWidth = 1f; + + /// + /// Height of the line strokes. + /// + public float StrokeHeight = 1f; + + /// + /// Separation between strokes in the line. + /// + public float Separation = 0; + + /// + /// Period offset of the line. + /// + public float PeriodOffset; + + /// + /// Period of the line. + /// + public float Period; + + /// + /// The time to cycle one period of the line in milliseconds. + /// + public double CycleTime; + + private Shader shader; + + private VisualiserLineDrawNodeSharedData visualiserLineDrawNodeSharedData => new VisualiserLineDrawNodeSharedData(); + + private float runningPeriodOffset; + + protected override void Update() + { + base.Update(); + + if (CycleTime != 0) + { + runningPeriodOffset += (float)(Time.Elapsed / CycleTime) * Period; + Invalidate(Invalidation.DrawNode, shallPropagate: false); + } + } + + protected override DrawNode CreateDrawNode() => new VisualiserLineDrawNode(); + + [BackgroundDependencyLoader] + private void load(ShaderManager shaders) + { + shader = shaders?.Load(VertexShaderDescriptor.Colour, @"DottedLine"); + } + + protected override void ApplyDrawNode(DrawNode node) + { + base.ApplyDrawNode(node); + + VisualiserLineDrawNode vNode = node as VisualiserLineDrawNode; + vNode.Shader = shader; + vNode.Shared = visualiserLineDrawNodeSharedData; + vNode.ScreenSpaceDrawQuad = ScreenSpaceDrawQuad; + + vNode.Period = Period; + vNode.PeriodOffset = PeriodOffset + runningPeriodOffset; + vNode.StrokeWidth = StrokeWidth; + vNode.StrokeHeight = StrokeHeight; + vNode.Separation = Separation; + } + + class VisualiserLineDrawNodeSharedData + { + public QuadBatch QuadBatch = new QuadBatch(1, 1); + } + + class VisualiserLineDrawNode : DrawNode + { + public Shader Shader; + public VisualiserLineDrawNodeSharedData Shared; + + public Quad ScreenSpaceDrawQuad; + + public float Period; + public float PeriodOffset; + + public float StrokeWidth; + public float StrokeHeight; + public float Separation; + + public override void Draw(Action vertexAction) + { + base.Draw(vertexAction); + + Shader.Bind(); + + Shader.GetUniform(@"g_Position").Value = ScreenSpaceDrawQuad.TopLeft; + Shader.GetUniform(@"g_Size").Value = ScreenSpaceDrawQuad.Size; + + Shader.GetUniform(@"g_Period").Value = Period; + Shader.GetUniform(@"g_PeriodOffset").Value = PeriodOffset; + + Shader.GetUniform(@"g_StrokeWidth").Value = StrokeWidth; + Shader.GetUniform(@"g_StrokeHeight").Value = StrokeHeight; + Shader.GetUniform(@"g_Separation").Value = Separation; + + Shared.QuadBatch.Add(new Vertex2D() + { + Position = ScreenSpaceDrawQuad.BottomLeft, + Colour = DrawInfo.Colour.BottomLeft.Linear + }); + + Shared.QuadBatch.Add(new Vertex2D() + { + Position = ScreenSpaceDrawQuad.BottomRight, + Colour = DrawInfo.Colour.BottomRight.Linear + }); + + Shared.QuadBatch.Add(new Vertex2D() + { + Position = ScreenSpaceDrawQuad.TopRight, + Colour = DrawInfo.Colour.TopRight.Linear + }); + + Shared.QuadBatch.Add(new Vertex2D() + { + Position = ScreenSpaceDrawQuad.TopLeft, + Colour = DrawInfo.Colour.TopLeft.Linear + }); + + Shader.Unbind(); + } + } + } + } +} diff --git a/osu.Game/Screens/Tournament/Components/VisualiserLine.cs b/osu.Game/Screens/Tournament/Components/VisualiserLine.cs deleted file mode 100644 index 3acc9c918f..0000000000 --- a/osu.Game/Screens/Tournament/Components/VisualiserLine.cs +++ /dev/null @@ -1,209 +0,0 @@ -// Copyright (c) 2007-2017 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE - -using OpenTK; -using osu.Framework.Allocation; -using osu.Framework.Graphics; -using osu.Framework.Graphics.Shaders; -using System; -using osu.Framework.Graphics.OpenGL; -using osu.Framework.Graphics.Batches; -using osu.Framework.Graphics.Primitives; -using osu.Framework.Timing; -using osu.Framework.Graphics.Transforms; - -namespace osu.Game.Screens.Tournament.Components -{ - class VisualiserLine : Drawable - { - private float strokeWidth = 1; - public float StrokeWidth - { - get { return strokeWidth; } - set - { - if (strokeWidth == value) - return; - - strokeWidth = value; - Invalidate(Invalidation.DrawNode, shallPropagate: false); - } - } - - private float strokeHeight = 1; - public float StrokeHeight - { - get { return strokeHeight; } - set - { - if (strokeHeight == value) - return; - - strokeHeight = value; - Invalidate(Invalidation.DrawNode, shallPropagate: false); - } - } - - private float separation = 0; - public float Separation - { - get { return separation; } - set - { - if (separation == value) - return; - - separation = value; - Invalidate(Invalidation.DrawNode, shallPropagate: false); - } - } - - private Shader shader; - - private VisualiserLineDrawNodeSharedData visualiserLineDrawNodeSharedData => new VisualiserLineDrawNodeSharedData(); - - /// - /// The period of this visualiser line, in radians. - /// - private float period; - - /// - /// The period offset this line was constructed with, in radians. - /// - private readonly float initialPeriodOffset; - /// - /// The rolling period offset (by transformation), in radians. - /// - private float _periodOffset; - /// - /// The final period offset, in radians. - /// - private float periodOffset - { - get { return initialPeriodOffset + _periodOffset; } - set - { - if (_periodOffset == value) - return; - - _periodOffset = value; - Invalidate(Invalidation.DrawNode, shallPropagate: false); - } - } - - /// - /// Constructs a new Visualiser Line. - /// - /// The period of the line, in radians. - /// The offset to the period of the line, in radians. - /// The time to cycle the line. - public VisualiserLine(float period, float periodOffset = 0, int cycleTime = 0) - { - this.period = period; - this.initialPeriodOffset = periodOffset; - - Clock = new FramedClock(); - - if (cycleTime > 0) - TransformFloatTo(0, period, cycleTime, EasingTypes.None, new TransformVisualiserOffset()); - - Loop(); - } - - protected override DrawNode CreateDrawNode() => new VisualiserLineDrawNode(); - - [BackgroundDependencyLoader] - private void load(ShaderManager shaders) - { - shader = shaders?.Load(VertexShaderDescriptor.Colour, @"DottedLine"); - } - - protected override void ApplyDrawNode(DrawNode node) - { - base.ApplyDrawNode(node); - - VisualiserLineDrawNode vNode = node as VisualiserLineDrawNode; - vNode.Shader = shader; - vNode.Shared = visualiserLineDrawNodeSharedData; - vNode.ScreenSpaceDrawQuad = ScreenSpaceDrawQuad; - - vNode.Period = period; - vNode.PeriodOffset = periodOffset; - vNode.StrokeWidth = StrokeWidth; - vNode.StrokeHeight = StrokeHeight; - vNode.Separation = Separation; - } - - class VisualiserLineDrawNodeSharedData - { - public QuadBatch QuadBatch = new QuadBatch(1, 1); - } - - class VisualiserLineDrawNode : DrawNode - { - public Shader Shader; - public VisualiserLineDrawNodeSharedData Shared; - - public Quad ScreenSpaceDrawQuad; - - public float Period; - public float PeriodOffset; - - public float StrokeWidth; - public float StrokeHeight; - public float Separation; - - public override void Draw(Action vertexAction) - { - base.Draw(vertexAction); - - Shader.Bind(); - - Shader.GetUniform(@"g_Position").Value = ScreenSpaceDrawQuad.TopLeft; - Shader.GetUniform(@"g_Size").Value = ScreenSpaceDrawQuad.Size; - - Shader.GetUniform(@"g_Period").Value = Period; - Shader.GetUniform(@"g_PeriodOffset").Value = PeriodOffset; - - Shader.GetUniform(@"g_StrokeWidth").Value = StrokeWidth; - Shader.GetUniform(@"g_StrokeHeight").Value = StrokeHeight; - Shader.GetUniform(@"g_Separation").Value = Separation; - - Shared.QuadBatch.Add(new Vertex2D() - { - Position = ScreenSpaceDrawQuad.BottomLeft, - Colour = DrawInfo.Colour.BottomLeft.Linear - }); - - Shared.QuadBatch.Add(new Vertex2D() - { - Position = ScreenSpaceDrawQuad.BottomRight, - Colour = DrawInfo.Colour.BottomRight.Linear - }); - - Shared.QuadBatch.Add(new Vertex2D() - { - Position = ScreenSpaceDrawQuad.TopRight, - Colour = DrawInfo.Colour.TopRight.Linear - }); - - Shared.QuadBatch.Add(new Vertex2D() - { - Position = ScreenSpaceDrawQuad.TopLeft, - Colour = DrawInfo.Colour.TopLeft.Linear - }); - - Shader.Unbind(); - } - } - - class TransformVisualiserOffset : TransformFloat - { - public override void Apply(Drawable d) - { - base.Apply(d); - (d as VisualiserLine).periodOffset = CurrentValue; - } - } - } -} diff --git a/osu.Game/Screens/Tournament/Drawings.cs b/osu.Game/Screens/Tournament/Drawings.cs index 2b26229934..67bd96cbb3 100644 --- a/osu.Game/Screens/Tournament/Drawings.cs +++ b/osu.Game/Screens/Tournament/Drawings.cs @@ -60,8 +60,6 @@ namespace osu.Game.Screens.Tournament drawingsConfig = new DrawingsConfigManager(storage); - Container visualiserContainer; - Children = new Drawable[] { new Box() @@ -90,7 +88,7 @@ namespace osu.Game.Screens.Tournament Children = new Drawable[] { // Visualiser - visualiserContainer = new Container() + new VisualiserContainer() { Anchor = Anchor.Centre, Origin = Anchor.Centre, @@ -98,7 +96,9 @@ namespace osu.Game.Screens.Tournament RelativeSizeAxes = Axes.X, Size = new Vector2(1, 10), - Colour = new Color4(255, 204, 34, 255) + Colour = new Color4(255, 204, 34, 255), + + Lines = 6 }, // Groups groupsContainer = new GroupsContainer(drawingsConfig.Get(DrawingsConfig.Groups), drawingsConfig.Get(DrawingsConfig.TeamsPerGroup)) @@ -230,17 +230,6 @@ namespace osu.Game.Screens.Tournament } }; - float offset = 0; - for (int i = 0; i < 6; i++) - { - visualiserContainer.Add(new VisualiserLine(2 * (float)Math.PI, offset, RNG.Next(10000, 12000)) - { - RelativeSizeAxes = Axes.Both, - }); - - offset += (float)Math.PI / 6f; - } - teamsContainer.OnSelected += onTeamSelected; teamsContainer.OnScrollStarted += () => fullTeamNameText.FadeOut(200); diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 6b67aaf7e1..0354103d09 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -212,7 +212,7 @@ - +