A game framework written with osu! in mind.
at master 8.3 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.Colour; 6using osu.Framework.Graphics.Textures; 7using osu.Framework.Graphics.UserInterface; 8using osuTK.Graphics; 9using SixLabors.ImageSharp; 10using SixLabors.ImageSharp.PixelFormats; 11 12namespace osu.Framework.Tests.Visual.UserInterface 13{ 14 public class TestSceneCircularProgress : FrameworkTestScene 15 { 16 private readonly CircularProgress clock; 17 18 private int rotateMode; 19 private const double period = 4000; 20 private const double transition_period = 2000; 21 22 private readonly Texture gradientTextureHorizontal; 23 private readonly Texture gradientTextureVertical; 24 private readonly Texture gradientTextureBoth; 25 26 public TestSceneCircularProgress() 27 { 28 const int width = 128; 29 30 var image = new Image<Rgba32>(width, 1); 31 32 gradientTextureHorizontal = new Texture(width, 1, true); 33 34 for (int i = 0; i < width; ++i) 35 { 36 float brightness = (float)i / (width - 1); 37 image[i, 0] = new Rgba32((byte)(128 + (1 - brightness) * 127), (byte)(128 + brightness * 127), 128, 255); 38 } 39 40 gradientTextureHorizontal.SetData(new TextureUpload(image)); 41 42 image = new Image<Rgba32>(width, 1); 43 44 gradientTextureVertical = new Texture(1, width, true); 45 46 for (int i = 0; i < width; ++i) 47 { 48 float brightness = (float)i / (width - 1); 49 image[i, 0] = new Rgba32((byte)(128 + (1 - brightness) * 127), (byte)(128 + brightness * 127), 128, 255); 50 } 51 52 gradientTextureVertical.SetData(new TextureUpload(image)); 53 54 image = new Image<Rgba32>(width, width); 55 56 gradientTextureBoth = new Texture(width, width, true); 57 58 for (int i = 0; i < width; ++i) 59 { 60 for (int j = 0; j < width; ++j) 61 { 62 float brightness = (float)i / (width - 1); 63 float brightness2 = (float)j / (width - 1); 64 image[i, j] = new Rgba32( 65 (byte)(128 + (1 + brightness - brightness2) / 2 * 127), 66 (byte)(128 + (1 + brightness2 - brightness) / 2 * 127), 67 (byte)(128 + (brightness + brightness2) / 2 * 127), 68 255); 69 } 70 } 71 72 gradientTextureBoth.SetData(new TextureUpload(image)); 73 74 Children = new Drawable[] 75 { 76 clock = new CircularProgress 77 { 78 Width = 0.8f, 79 Height = 0.8f, 80 RelativeSizeAxes = Axes.Both, 81 Anchor = Anchor.Centre, 82 Origin = Anchor.Centre, 83 }, 84 }; 85 86 AddStep("Forward", delegate { setRotationMode(1); }); 87 AddStep("Backward", delegate { setRotationMode(2); }); 88 AddStep("Transition Focus", delegate { setRotationMode(3); }); 89 AddStep("Transition Focus 2", delegate { setRotationMode(4); }); 90 AddStep("Forward/Backward", delegate { setRotationMode(0); }); 91 92 AddStep("Horizontal Gradient Texture", delegate { setTexture(1); }); 93 AddStep("Vertical Gradient Texture", delegate { setTexture(2); }); 94 AddStep("2D Graident Texture", delegate { setTexture(3); }); 95 AddStep("White Texture", delegate { setTexture(0); }); 96 97 AddStep("Red Colour", delegate { setColour(1); }); 98 AddStep("Horzontal Gradient Colour", delegate { setColour(2); }); 99 AddStep("Vertical Gradient Colour", delegate { setColour(3); }); 100 AddStep("2D Gradient Colour", delegate { setColour(4); }); 101 AddStep("White Colour", delegate { setColour(0); }); 102 103 AddStep("Forward Transform", delegate { transform(0); }); 104 AddStep("Backward Transform", delegate { transform(1); }); 105 AddStep("Fwd/Bwd Transform", delegate { transform(2); }); 106 AddStep("Easing Transform", delegate { transform(3); }); 107 108 AddSliderStep("Fill", 0, 10, 10, fill => clock.InnerRadius = fill / 10f); 109 } 110 111 protected override void Update() 112 { 113 base.Update(); 114 115 switch (rotateMode) 116 { 117 case 0: 118 clock.Current.Value = Time.Current % (period * 2) / period - 1; 119 break; 120 121 case 1: 122 clock.Current.Value = Time.Current % period / period; 123 break; 124 125 case 2: 126 clock.Current.Value = Time.Current % period / period - 1; 127 break; 128 129 case 3: 130 clock.Current.Value = Time.Current % transition_period / transition_period / 5 - 0.1f; 131 break; 132 133 case 4: 134 clock.Current.Value = (Time.Current % transition_period / transition_period / 5 - 0.1f + 2) % 2 - 1; 135 break; 136 } 137 } 138 139 private void setRotationMode(int mode) 140 { 141 clock.ClearTransforms(); 142 rotateMode = mode; 143 } 144 145 private void setTexture(int textureMode) 146 { 147 switch (textureMode) 148 { 149 case 0: 150 clock.Texture = Texture.WhitePixel; 151 break; 152 153 case 1: 154 clock.Texture = gradientTextureHorizontal; 155 break; 156 157 case 2: 158 clock.Texture = gradientTextureVertical; 159 break; 160 161 case 3: 162 clock.Texture = gradientTextureBoth; 163 break; 164 } 165 } 166 167 private void setColour(int colourMode) 168 { 169 switch (colourMode) 170 { 171 case 0: 172 clock.Colour = new Color4(255, 255, 255, 255); 173 break; 174 175 case 1: 176 clock.Colour = new Color4(255, 88, 88, 255); 177 break; 178 179 case 2: 180 clock.Colour = new ColourInfo 181 { 182 TopLeft = new Color4(255, 128, 128, 255), 183 TopRight = new Color4(128, 255, 128, 255), 184 BottomLeft = new Color4(255, 128, 128, 255), 185 BottomRight = new Color4(128, 255, 128, 255), 186 }; 187 break; 188 189 case 3: 190 clock.Colour = new ColourInfo 191 { 192 TopLeft = new Color4(255, 128, 128, 255), 193 TopRight = new Color4(255, 128, 128, 255), 194 BottomLeft = new Color4(128, 255, 128, 255), 195 BottomRight = new Color4(128, 255, 128, 255), 196 }; 197 break; 198 199 case 4: 200 clock.Colour = new ColourInfo 201 { 202 TopLeft = new Color4(255, 128, 128, 255), 203 TopRight = new Color4(128, 255, 128, 255), 204 BottomLeft = new Color4(128, 128, 255, 255), 205 BottomRight = new Color4(255, 255, 255, 255), 206 }; 207 break; 208 } 209 } 210 211 private void transform(int tf) 212 { 213 setRotationMode(-1); 214 215 switch (tf) 216 { 217 case 0: 218 clock.FillTo(0).Then().FillTo(1, 1000).Loop(); 219 break; 220 221 case 1: 222 clock.FillTo(1).Then().FillTo(0, 1000).Loop(); 223 break; 224 225 case 2: 226 clock.FillTo(0, 1000).Then().FillTo(1, 1000).Loop(); 227 break; 228 229 case 3: 230 clock.FillTo(0).Then().FillTo(1, 1000, Easing.InOutQuart).Loop(); 231 break; 232 } 233 } 234 } 235}