|
| 1 | + // Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. |
| 2 | +// See the LICENCE file in the repository root for full licence text. |
| 3 | + |
| 4 | +using osu.Framework.Graphics; |
| 5 | +using osuTK.Graphics; |
| 6 | +using osu.Framework.Graphics.Containers; |
| 7 | +using osu.Framework.Graphics.Shapes; |
| 8 | +using osu.Framework.Graphics.Lines; |
| 9 | +using osu.Framework.Input.Events; |
| 10 | +using osuTK.Input; |
| 11 | +using osu.Framework.Utils; |
| 12 | +using osuTK; |
| 13 | + |
| 14 | +namespace osu.Framework.Tests.Visual.Drawables |
| 15 | +{ |
| 16 | + [System.ComponentModel.Description("Approximate a hand-drawn path with minimal B-spline control points")] |
| 17 | + public partial class TestSceneInteractivePathDrawing : FrameworkTestScene |
| 18 | + { |
| 19 | + private readonly Path rawDrawnPath; |
| 20 | + private readonly Path approximatedDrawnPath; |
| 21 | + private readonly Container controlPointViz; |
| 22 | + |
| 23 | + private readonly IncrementalBSplineBuilder bSplineBuilder = new IncrementalBSplineBuilder(); |
| 24 | + |
| 25 | + public TestSceneInteractivePathDrawing() |
| 26 | + { |
| 27 | + Child = new Container |
| 28 | + { |
| 29 | + RelativeSizeAxes = Axes.Both, |
| 30 | + Children = new Drawable[] |
| 31 | + { |
| 32 | + rawDrawnPath = new Path |
| 33 | + { |
| 34 | + Colour = Color4.DeepPink, |
| 35 | + PathRadius = 5, |
| 36 | + }, |
| 37 | + approximatedDrawnPath = new Path |
| 38 | + { |
| 39 | + Colour = Color4.Blue, |
| 40 | + PathRadius = 3, |
| 41 | + }, |
| 42 | + controlPointViz = new Container |
| 43 | + { |
| 44 | + RelativeSizeAxes = Axes.Both, |
| 45 | + Alpha = 0.5f, |
| 46 | + }, |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + updateViz(); |
| 51 | + OnUpdate += _ => updateViz(); |
| 52 | + |
| 53 | + AddStep("Reset path", () => |
| 54 | + { |
| 55 | + bSplineBuilder.Clear(); |
| 56 | + }); |
| 57 | + |
| 58 | + AddSliderStep($"{nameof(bSplineBuilder.Degree)}", 1, 5, 3, v => |
| 59 | + { |
| 60 | + bSplineBuilder.Degree = v; |
| 61 | + }); |
| 62 | + AddSliderStep($"{nameof(bSplineBuilder.Tolerance)}", 0f, 1f, 0.1f, v => |
| 63 | + { |
| 64 | + bSplineBuilder.Tolerance = v; |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + private void updateControlPointsViz() |
| 69 | + { |
| 70 | + controlPointViz.Clear(); |
| 71 | + |
| 72 | + foreach (var cp in bSplineBuilder.GetControlPoints()) |
| 73 | + { |
| 74 | + controlPointViz.Add(new Box |
| 75 | + { |
| 76 | + Origin = Anchor.Centre, |
| 77 | + Size = new Vector2(10), |
| 78 | + Position = cp, |
| 79 | + Colour = Color4.LightGreen, |
| 80 | + }); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + protected override bool OnDragStart(DragStartEvent e) |
| 85 | + { |
| 86 | + if (e.Button == MouseButton.Left) |
| 87 | + { |
| 88 | + bSplineBuilder.Clear(); |
| 89 | + bSplineBuilder.AddLinearPoint(rawDrawnPath.ToLocalSpace(ToScreenSpace(e.MousePosition))); |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + private void updateViz() |
| 97 | + { |
| 98 | + rawDrawnPath.Vertices = bSplineBuilder.GetInputPath(); |
| 99 | + approximatedDrawnPath.Vertices = bSplineBuilder.OutputPath; |
| 100 | + |
| 101 | + updateControlPointsViz(); |
| 102 | + } |
| 103 | + |
| 104 | + protected override void OnDrag(DragEvent e) |
| 105 | + { |
| 106 | + bSplineBuilder.AddLinearPoint(rawDrawnPath.ToLocalSpace(ToScreenSpace(e.MousePosition))); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments