A game framework written with osu! in mind.
at master 107 lines 4.1 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using osu.Framework.Graphics; 5using osu.Framework.Graphics.Containers; 6using osu.Framework.Graphics.Shapes; 7using osu.Framework.Graphics.Sprites; 8using osuTK; 9using osuTK.Graphics; 10 11namespace osu.Framework.Tests.Visual.Sprites 12{ 13 public class TestSceneSpriteTextTruncate : FrameworkTestScene 14 { 15 private readonly FillFlowContainer flow; 16 17 public TestSceneSpriteTextTruncate() 18 { 19 Children = new Drawable[] 20 { 21 new BasicScrollContainer 22 { 23 RelativeSizeAxes = Axes.Both, 24 Children = new[] 25 { 26 flow = new FillFlowContainer 27 { 28 Anchor = Anchor.TopLeft, 29 AutoSizeAxes = Axes.Y, 30 Direction = FillDirection.Vertical, 31 } 32 } 33 } 34 }; 35 } 36 37 protected override void LoadComplete() 38 { 39 base.LoadComplete(); 40 41 const string text = "A really really really really long text passage"; 42 43 flow.AddRange(new Drawable[] 44 { 45 new ExampleText(text, false, false), 46 new ExampleText(text, false, true), 47 new ExampleText(text, false, true, spacing: new Vector2(30)), 48 new ExampleText(text, false, true, "…"), 49 new ExampleText(text, false, true, "…", spacing: new Vector2(30)), 50 new ExampleText(text, false, true, "--"), 51 new ExampleText(text, false, true, "--", true), 52 new ExampleText(text, false, true, "--", true, new Vector2(30)), 53 new ExampleText(text, true, false), 54 new ExampleText(text, true, true), 55 new ExampleText(text, true, true, spacing: new Vector2(30)), 56 new ExampleText(text, true, true, "…"), 57 new ExampleText(text, true, true, "…", spacing: new Vector2(30)), 58 new ExampleText(text, true, true, "--"), 59 new ExampleText(text, true, true, "--", true), 60 new ExampleText(text, true, true, "--", true, new Vector2(30)), 61 }); 62 63 const float start_range = 10; 64 const float end_range = 500; 65 66 flow.Width = start_range; 67 flow.ResizeWidthTo(end_range, 10000).Then().ResizeWidthTo(start_range, 10000).Loop(); 68 } 69 70 private class ExampleText : Container 71 { 72 public ExampleText(string text, bool fixedWidth, bool truncate, string ellipsisString = "", bool runtimeChange = false, Vector2 spacing = new Vector2()) 73 { 74 AutoSizeAxes = Axes.Y; 75 RelativeSizeAxes = Axes.X; 76 Children = new Drawable[] 77 { 78 new Box 79 { 80 Colour = Color4.DarkMagenta, 81 RelativeSizeAxes = Axes.Both, 82 }, 83 new CustomEllipsisSpriteText(ellipsisString, runtimeChange) 84 { 85 Text = text, 86 Truncate = truncate, 87 Spacing = spacing, 88 Font = new FontUsage(size: 20, fixedWidth: fixedWidth), 89 RelativeSizeAxes = Axes.X, 90 AllowMultiline = false 91 } 92 }; 93 } 94 } 95 96 private class CustomEllipsisSpriteText : SpriteText 97 { 98 public CustomEllipsisSpriteText(string customEllipsis, bool runtimeChange) 99 { 100 EllipsisString = customEllipsis; 101 102 if (runtimeChange) 103 Scheduler.AddDelayed(() => EllipsisString = customEllipsis == EllipsisString ? string.Empty : customEllipsis, 500, true); 104 } 105 } 106 } 107}