A game framework written with osu! in mind.
at master 9.6 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 osu.Framework.Input.Events; 9using osu.Framework.Layout; 10using osu.Framework.Testing; 11using osuTK; 12using osuTK.Graphics; 13 14namespace osu.Framework.Tests.Visual.Containers 15{ 16 public class TestScenePadding : GridTestScene 17 { 18 public TestScenePadding() 19 : base(2, 2) 20 { 21 Cell(0).AddRange(new Drawable[] 22 { 23 new SpriteText { Text = @"Padding - 20 All Sides" }, 24 new Container 25 { 26 AutoSizeAxes = Axes.Both, 27 Anchor = Anchor.Centre, 28 Origin = Anchor.Centre, 29 Children = new Drawable[] 30 { 31 new Box 32 { 33 RelativeSizeAxes = Axes.Both, 34 Colour = Color4.White, 35 }, 36 new PaddedBox(Color4.Blue) 37 { 38 Padding = new MarginPadding(20), 39 Size = new Vector2(200), 40 Origin = Anchor.Centre, 41 Anchor = Anchor.Centre, 42 Masking = true, 43 Children = new Drawable[] 44 { 45 new PaddedBox(Color4.DarkSeaGreen) 46 { 47 Padding = new MarginPadding(40), 48 RelativeSizeAxes = Axes.Both, 49 Origin = Anchor.Centre, 50 Anchor = Anchor.Centre 51 } 52 } 53 } 54 } 55 } 56 }); 57 58 Cell(1).AddRange(new Drawable[] 59 { 60 new SpriteText { Text = @"Padding - 20 Top, Left" }, 61 new Container 62 { 63 AutoSizeAxes = Axes.Both, 64 Anchor = Anchor.Centre, 65 Origin = Anchor.Centre, 66 Children = new Drawable[] 67 { 68 new Box 69 { 70 RelativeSizeAxes = Axes.Both, 71 Colour = Color4.White, 72 }, 73 new PaddedBox(Color4.Blue) 74 { 75 Padding = new MarginPadding 76 { 77 Top = 20, 78 Left = 20, 79 }, 80 Size = new Vector2(200), 81 Origin = Anchor.Centre, 82 Anchor = Anchor.Centre, 83 Masking = true, 84 Children = new Drawable[] 85 { 86 new PaddedBox(Color4.DarkSeaGreen) 87 { 88 Padding = new MarginPadding(40), 89 RelativeSizeAxes = Axes.Both, 90 Origin = Anchor.Centre, 91 Anchor = Anchor.Centre 92 } 93 } 94 } 95 } 96 } 97 }); 98 99 Cell(2).AddRange(new Drawable[] 100 { 101 new SpriteText { Text = @"Margin - 20 All Sides" }, 102 new Container 103 { 104 AutoSizeAxes = Axes.Both, 105 Anchor = Anchor.Centre, 106 Origin = Anchor.Centre, 107 Children = new Drawable[] 108 { 109 new Box 110 { 111 RelativeSizeAxes = Axes.Both, 112 Colour = Color4.White, 113 }, 114 new PaddedBox(Color4.Blue) 115 { 116 Margin = new MarginPadding(20), 117 Size = new Vector2(200), 118 Origin = Anchor.Centre, 119 Anchor = Anchor.Centre, 120 Masking = true, 121 Children = new Drawable[] 122 { 123 new PaddedBox(Color4.DarkSeaGreen) 124 { 125 Padding = new MarginPadding(20), 126 RelativeSizeAxes = Axes.Both, 127 Origin = Anchor.Centre, 128 Anchor = Anchor.Centre 129 } 130 } 131 } 132 } 133 } 134 }); 135 136 Cell(3).AddRange(new Drawable[] 137 { 138 new SpriteText { Text = @"Margin - 20 Top, Left" }, 139 new Container 140 { 141 AutoSizeAxes = Axes.Both, 142 Anchor = Anchor.Centre, 143 Origin = Anchor.Centre, 144 Children = new Drawable[] 145 { 146 new Box 147 { 148 RelativeSizeAxes = Axes.Both, 149 Colour = Color4.White, 150 }, 151 new PaddedBox(Color4.Blue) 152 { 153 Margin = new MarginPadding 154 { 155 Top = 20, 156 Left = 20, 157 }, 158 Size = new Vector2(200), 159 Origin = Anchor.Centre, 160 Anchor = Anchor.Centre, 161 Masking = true, 162 Children = new Drawable[] 163 { 164 new PaddedBox(Color4.DarkSeaGreen) 165 { 166 Padding = new MarginPadding(40), 167 RelativeSizeAxes = Axes.Both, 168 Origin = Anchor.Centre, 169 Anchor = Anchor.Centre 170 } 171 } 172 } 173 } 174 } 175 }); 176 } 177 178 private class PaddedBox : Container 179 { 180 private readonly SpriteText t1; 181 private readonly SpriteText t2; 182 private readonly SpriteText t3; 183 private readonly SpriteText t4; 184 185 private readonly Container content; 186 187 protected override Container<Drawable> Content => content; 188 189 public PaddedBox(Color4 colour) 190 { 191 AddRangeInternal(new Drawable[] 192 { 193 new Box 194 { 195 RelativeSizeAxes = Axes.Both, 196 Colour = colour, 197 }, 198 content = new Container 199 { 200 RelativeSizeAxes = Axes.Both, 201 }, 202 t1 = new SpriteText 203 { 204 Anchor = Anchor.TopCentre, 205 Origin = Anchor.TopCentre 206 }, 207 t2 = new SpriteText 208 { 209 Rotation = 90, 210 Anchor = Anchor.CentreRight, 211 Origin = Anchor.TopCentre 212 }, 213 t3 = new SpriteText 214 { 215 Anchor = Anchor.BottomCentre, 216 Origin = Anchor.BottomCentre 217 }, 218 t4 = new SpriteText 219 { 220 Rotation = -90, 221 Anchor = Anchor.CentreLeft, 222 Origin = Anchor.TopCentre 223 } 224 }); 225 226 Masking = true; 227 } 228 229 protected override bool OnInvalidate(Invalidation invalidation, InvalidationSource source) 230 { 231 base.OnInvalidate(invalidation, source); 232 233 t1.Text = (Padding.Top > 0 ? $"p{Padding.Top}" : string.Empty) + (Margin.Top > 0 ? $"m{Margin.Top}" : string.Empty); 234 t2.Text = (Padding.Right > 0 ? $"p{Padding.Right}" : string.Empty) + (Margin.Right > 0 ? $"m{Margin.Right}" : string.Empty); 235 t3.Text = (Padding.Bottom > 0 ? $"p{Padding.Bottom}" : string.Empty) + (Margin.Bottom > 0 ? $"m{Margin.Bottom}" : string.Empty); 236 t4.Text = (Padding.Left > 0 ? $"p{Padding.Left}" : string.Empty) + (Margin.Left > 0 ? $"m{Margin.Left}" : string.Empty); 237 238 return true; 239 } 240 241 protected override void OnDrag(DragEvent e) 242 { 243 Position += e.Delta; 244 } 245 246 protected override bool OnDragStart(DragStartEvent e) => true; 247 } 248 } 249}