1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 10:33:07 +08:00

Adjust SettingsItem to accommodate new default value indicator

This commit is contained in:
Bartłomiej Dach 2021-10-15 22:44:28 +02:00
parent 818f35c35f
commit f422ebb281
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
4 changed files with 90 additions and 23 deletions

View File

@ -4,6 +4,7 @@
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Game.Overlays.Settings;
using osu.Game.Overlays;
@ -29,9 +30,10 @@ namespace osu.Game.Tests.Visual.Settings
Value = "test"
}
};
restoreDefaultValueButton = textBox.ChildrenOfType<RestoreDefaultValueButton<string>>().Single();
});
AddUntilStep("wait for loaded", () => textBox.IsLoaded);
AddStep("retrieve restore default button", () => restoreDefaultValueButton = textBox.ChildrenOfType<RestoreDefaultValueButton<string>>().Single());
AddAssert("restore button hidden", () => restoreDefaultValueButton.Alpha == 0);
AddStep("change value from default", () => textBox.Current.Value = "non-default");
@ -41,6 +43,41 @@ namespace osu.Game.Tests.Visual.Settings
AddUntilStep("restore button hidden", () => restoreDefaultValueButton.Alpha == 0);
}
[Test]
public void TestSetAndClearLabelText()
{
SettingsTextBox textBox = null;
GridContainer settingsItemGrid = null;
RestoreDefaultValueButton<string> restoreDefaultValueButton = null;
AddStep("create settings item", () =>
{
Child = textBox = new SettingsTextBox
{
Current = new Bindable<string>
{
Default = "test",
Value = "test"
}
};
});
AddUntilStep("wait for loaded", () => textBox.IsLoaded);
AddStep("retrieve components", () =>
{
settingsItemGrid = textBox.ChildrenOfType<GridContainer>().Single();
restoreDefaultValueButton = textBox.ChildrenOfType<RestoreDefaultValueButton<string>>().Single();
});
AddStep("set non-default value", () => restoreDefaultValueButton.Current.Value = "non-default");
AddAssert("default value button next to control", () => settingsItemGrid.Content[1][0] == restoreDefaultValueButton);
AddStep("set label", () => textBox.LabelText = "label text");
AddAssert("default value button next to label", () => settingsItemGrid.Content[0][0] == restoreDefaultValueButton);
AddStep("clear label", () => textBox.LabelText = default);
AddAssert("default value button next to control", () => settingsItemGrid.Content[1][0] == restoreDefaultValueButton);
}
/// <summary>
/// Ensures that the reset to default button uses the correct implementation of IsDefault to determine whether it should be shown or not.
/// Values have been chosen so that after being set, Value != Default (but they are close enough that the difference is negligible compared to Precision).
@ -64,9 +101,9 @@ namespace osu.Game.Tests.Visual.Settings
Precision = 0.1f,
}
};
restoreDefaultValueButton = sliderBar.ChildrenOfType<RestoreDefaultValueButton<float>>().Single();
});
AddUntilStep("wait for loaded", () => sliderBar.IsLoaded);
AddStep("retrieve restore default button", () => restoreDefaultValueButton = sliderBar.ChildrenOfType<RestoreDefaultValueButton<float>>().Single());
AddAssert("restore button hidden", () => restoreDefaultValueButton.Alpha == 0);

View File

@ -6,7 +6,6 @@ using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Graphics.UserInterface;
using osuTK;
namespace osu.Game.Overlays.Settings
{
@ -28,11 +27,6 @@ namespace osu.Game.Overlays.Settings
public override IEnumerable<string> FilterTerms => base.FilterTerms.Concat(Control.Items.Select(i => i.ToString()));
public SettingsDropdown()
{
FlowContent.Spacing = new Vector2(0, 10);
}
protected sealed override Drawable CreateControl() => CreateDropdown();
protected virtual OsuDropdown<T> CreateDropdown() => new DropdownControl();

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
@ -14,6 +15,7 @@ using osu.Framework.Localisation;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.Containers;
using osuTK;
namespace osu.Game.Overlays.Settings
{
@ -30,6 +32,10 @@ namespace osu.Game.Overlays.Settings
protected readonly FillFlowContainer FlowContent;
private SpriteText labelText;
private readonly GridContainer gridContainer;
[CanBeNull]
private RestoreDefaultValueButton<T> defaultValueButton;
private OsuTextFlowContainer warningText;
@ -48,12 +54,13 @@ namespace osu.Game.Overlays.Settings
if (labelText == null)
{
// construct lazily for cases where the label is not needed (may be provided by the Control).
FlowContent.Insert(-1, labelText = new OsuSpriteText());
gridContainer.Content[0][1] = labelText = new OsuSpriteText();
updateDisabled();
}
labelText.Text = value;
updateLayout();
}
}
@ -106,18 +113,33 @@ namespace osu.Game.Overlays.Settings
AutoSizeAxes = Axes.Y;
Padding = new MarginPadding { Right = SettingsPanel.CONTENT_MARGINS };
InternalChildren = new Drawable[]
FlowContent = new FillFlowContainer
{
FlowContent = new FillFlowContainer
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(0, 10),
Child = Control = CreateControl(),
};
InternalChild = gridContainer = new GridContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
RowDimensions = new[]
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Left = SettingsPanel.CONTENT_MARGINS },
Children = new[]
{
Control = CreateControl(),
},
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.AutoSize)
},
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, SettingsPanel.CONTENT_MARGINS),
new Dimension()
},
Content = new[]
{
new Drawable[2],
new Drawable[] { null, FlowContent }
}
};
// IMPORTANT: all bindable logic is in constructor intentionally to support "CreateSettingsControls" being used in a context it is
@ -135,13 +157,28 @@ namespace osu.Game.Overlays.Settings
// intentionally done before LoadComplete to avoid overhead.
if (ShowsDefaultIndicator)
{
AddInternal(new RestoreDefaultValueButton<T>
defaultValueButton = new RestoreDefaultValueButton<T>
{
Current = controlWithCurrent.Current,
});
Anchor = Anchor.Centre,
Origin = Anchor.Centre
};
updateLayout();
}
}
private void updateLayout()
{
bool hasLabel = !string.IsNullOrEmpty(labelText?.Text.ToString());
gridContainer.Content[0][0] = null;
gridContainer.Content[1][0] = null;
gridContainer.Content[hasLabel ? 0 : 1][0] = defaultValueButton;
FlowContent.Margin = new MarginPadding { Top = hasLabel ? 10 : 0 };
}
private void updateDisabled()
{
if (labelText != null)

View File

@ -19,7 +19,6 @@ namespace osu.Game.Overlays.Settings
{
protected override Drawable CreateControl() => new TSlider
{
Margin = new MarginPadding { Vertical = 10 },
RelativeSizeAxes = Axes.X
};