1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-28 04:03:06 +08:00

Merge pull request #9216 from frenzibyte/add-textbox-samples-and-fix-distingushing-passwords

This commit is contained in:
Dean Herbert 2020-07-10 18:44:36 +09:00 committed by GitHub
commit 63f42c5502
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 138 additions and 49 deletions

View File

@ -1,48 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
namespace osu.Game.Tests.Visual.UserInterface
{
[TestFixture]
public class TestSceneNumberBox : OsuTestScene
{
private OsuNumberBox numberBox;
[BackgroundDependencyLoader]
private void load()
{
Child = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.X,
Padding = new MarginPadding { Horizontal = 250 },
Child = numberBox = new OsuNumberBox
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.X,
PlaceholderText = "Insert numbers here"
}
};
clearInput();
AddStep("enter numbers", () => numberBox.Text = "987654321");
expectedValue("987654321");
clearInput();
AddStep("enter text + single number", () => numberBox.Text = "1 hello 2 world 3");
expectedValue("123");
clearInput();
}
private void clearInput() => AddStep("clear input", () => numberBox.Text = null);
private void expectedValue(string value) => AddAssert("expect number", () => numberBox.Text == value);
}
}

View File

@ -0,0 +1,80 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.UserInterface;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Tests.Visual.UserInterface
{
public class TestSceneOsuTextBox : OsuTestScene
{
private readonly OsuNumberBox numberBox;
public TestSceneOsuTextBox()
{
Child = new Container
{
Masking = true,
CornerRadius = 10f,
AutoSizeAxes = Axes.Both,
Padding = new MarginPadding(15f),
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.DarkSlateGray,
Alpha = 0.75f,
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Padding = new MarginPadding(50f),
Spacing = new Vector2(0f, 50f),
Children = new[]
{
new OsuTextBox
{
Width = 500f,
PlaceholderText = "Normal textbox",
},
new OsuPasswordTextBox
{
Width = 500f,
PlaceholderText = "Password textbox",
},
numberBox = new OsuNumberBox
{
Width = 500f,
PlaceholderText = "Number textbox"
}
}
}
}
};
}
[Test]
public void TestNumberBox()
{
clearTextbox(numberBox);
AddStep("enter numbers", () => numberBox.Text = "987654321");
expectedValue(numberBox, "987654321");
clearTextbox(numberBox);
AddStep("enter text + single number", () => numberBox.Text = "1 hello 2 world 3");
expectedValue(numberBox, "123");
clearTextbox(numberBox);
}
private void clearTextbox(OsuTextBox textBox) => AddStep("clear textbox", () => textBox.Text = null);
private void expectedValue(OsuTextBox textBox, string value) => AddAssert("expected textbox value", () => textBox.Text == value);
}
}

View File

@ -24,6 +24,8 @@ namespace osu.Game.Graphics.UserInterface
Child = new PasswordMaskChar(CalculatedTextSize), Child = new PasswordMaskChar(CalculatedTextSize),
}; };
protected override bool AllowUniqueCharacterSamples => false;
protected override bool AllowClipboardExport => false; protected override bool AllowClipboardExport => false;
private readonly CapsWarning warning; private readonly CapsWarning warning;

View File

@ -1,7 +1,10 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Audio.Track; using osu.Framework.Audio.Track;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
@ -11,6 +14,7 @@ using osuTK.Graphics;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Framework.Utils;
using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osuTK; using osuTK;
@ -19,6 +23,18 @@ namespace osu.Game.Graphics.UserInterface
{ {
public class OsuTextBox : BasicTextBox public class OsuTextBox : BasicTextBox
{ {
private readonly SampleChannel[] textAddedSamples = new SampleChannel[4];
private SampleChannel capsTextAddedSample;
private SampleChannel textRemovedSample;
private SampleChannel textCommittedSample;
private SampleChannel caretMovedSample;
/// <summary>
/// Whether to allow playing a different samples based on the type of character.
/// If set to false, the same sample will be used for all characters.
/// </summary>
protected virtual bool AllowUniqueCharacterSamples => true;
protected override float LeftRightPadding => 10; protected override float LeftRightPadding => 10;
protected override float CaretWidth => 3; protected override float CaretWidth => 3;
@ -41,15 +57,54 @@ namespace osu.Game.Graphics.UserInterface
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colour) private void load(OsuColour colour, AudioManager audio)
{ {
BackgroundUnfocused = Color4.Black.Opacity(0.5f); BackgroundUnfocused = Color4.Black.Opacity(0.5f);
BackgroundFocused = OsuColour.Gray(0.3f).Opacity(0.8f); BackgroundFocused = OsuColour.Gray(0.3f).Opacity(0.8f);
BackgroundCommit = BorderColour = colour.Yellow; BackgroundCommit = BorderColour = colour.Yellow;
for (int i = 0; i < textAddedSamples.Length; i++)
textAddedSamples[i] = audio.Samples.Get($@"Keyboard/key-press-{1 + i}");
capsTextAddedSample = audio.Samples.Get(@"Keyboard/key-caps");
textRemovedSample = audio.Samples.Get(@"Keyboard/key-delete");
textCommittedSample = audio.Samples.Get(@"Keyboard/key-confirm");
caretMovedSample = audio.Samples.Get(@"Keyboard/key-movement");
} }
protected override Color4 SelectionColour => new Color4(249, 90, 255, 255); protected override Color4 SelectionColour => new Color4(249, 90, 255, 255);
protected override void OnTextAdded(string added)
{
base.OnTextAdded(added);
if (added.Any(char.IsUpper) && AllowUniqueCharacterSamples)
capsTextAddedSample?.Play();
else
textAddedSamples[RNG.Next(0, 3)]?.Play();
}
protected override void OnTextRemoved(string removed)
{
base.OnTextRemoved(removed);
textRemovedSample?.Play();
}
protected override void OnTextCommitted(bool textChanged)
{
base.OnTextCommitted(textChanged);
textCommittedSample?.Play();
}
protected override void OnCaretMoved(bool selecting)
{
base.OnCaretMoved(selecting);
caretMovedSample?.Play();
}
protected override void OnFocus(FocusEvent e) protected override void OnFocus(FocusEvent e)
{ {
BorderThickness = 3; BorderThickness = 3;