A game framework written with osu! in mind.
at master 103 lines 3.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 osuTK; 5using osuTK.Graphics; 6using osu.Framework.Graphics.Containers; 7using osu.Framework.Graphics.Shapes; 8using osu.Framework.Graphics.Sprites; 9using osu.Framework.Localisation; 10 11namespace osu.Framework.Graphics.UserInterface 12{ 13 /// <summary> 14 /// A basic checkbox for framework internal use and for prototyping UI. 15 /// </summary> 16 public class BasicCheckbox : Checkbox 17 { 18 /// <summary> 19 /// The color of the checkbox when the checkbox is checked. Defaults to White 20 /// </summary> 21 /// <remarks> 22 /// The changes done to this property are only applied when <see cref="Checkbox.Current"/>'s value changes. 23 /// </remarks> 24 public Color4 CheckedColor { get; set; } = FrameworkColour.YellowGreen; 25 26 /// <summary> 27 /// The color of the checkbox when the checkbox is not checked. Default is an white with low opacity. 28 /// </summary> 29 /// <remarks> 30 /// The changes done to this property are only applied when <see cref="Checkbox.Current"/>'s value changes. 31 /// </remarks> 32 public Color4 UncheckedColor { get; set; } = FrameworkColour.Green; 33 34 /// <summary> 35 /// The length of the duration between checked and unchecked. 36 /// </summary> 37 /// <remarks> 38 /// Changes to this property only affect future transitions between checked and unchecked. 39 /// Transitions between checked and unchecked that are already in progress are unaffected. 40 /// </remarks> 41 public int FadeDuration { get; set; } = 50; 42 43 /// <summary> 44 /// The text in the label. 45 /// </summary> 46 public LocalisableString LabelText 47 { 48 get => labelSpriteText.Text; 49 set => labelSpriteText.Text = value; 50 } 51 52 /// <summary> 53 /// The spacing between the checkbox and the label. 54 /// </summary> 55 public float LabelSpacing 56 { 57 get => fillFlowContainer.Spacing.X; 58 set => fillFlowContainer.Spacing = new Vector2(value, 0); 59 } 60 61 /// <summary> 62 /// 63 /// </summary> 64 public bool RightHandedCheckbox 65 { 66 get => fillFlowContainer.GetLayoutPosition(labelSpriteText) < -0.5f; 67 set => fillFlowContainer.SetLayoutPosition(labelSpriteText, value ? -1 : 1); 68 } 69 70 private readonly SpriteText labelSpriteText; 71 private readonly FillFlowContainer fillFlowContainer; 72 73 public BasicCheckbox() 74 { 75 Box box; 76 77 AutoSizeAxes = Axes.Both; 78 79 Child = fillFlowContainer = new FillFlowContainer 80 { 81 Direction = FillDirection.Horizontal, 82 AutoSizeAxes = Axes.Both, 83 Spacing = new Vector2(10, 0), 84 Children = new Drawable[] 85 { 86 box = new Box 87 { 88 Size = new Vector2(30), 89 }, 90 labelSpriteText = new SpriteText 91 { 92 Anchor = Anchor.CentreLeft, 93 Origin = Anchor.CentreLeft, 94 Depth = float.MinValue, 95 Font = FrameworkFont.Condensed 96 }, 97 } 98 }; 99 100 Current.BindValueChanged(e => box.FadeColour(e.NewValue ? CheckedColor : UncheckedColor, FadeDuration), true); 101 } 102 } 103}