1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 19:22:54 +08:00

Add audio feedback for text selection

This commit is contained in:
Jamie Taylor 2022-08-19 15:51:27 +09:00
parent bc851c174b
commit 41408a3106
No known key found for this signature in database
GPG Key ID: 2ACFA8B6370B8C8C

View File

@ -46,8 +46,16 @@ namespace osu.Game.Graphics.UserInterface
private Sample? textCommittedSample;
private Sample? caretMovedSample;
private Sample? selectCharSample;
private Sample? selectWordSample;
private Sample? selectAllSample;
private Sample? deselectSample;
private OsuCaret? caret;
private bool selectionStarted;
private double sampleLastPlaybackTime;
public OsuTextBox()
{
Height = 40;
@ -78,6 +86,11 @@ namespace osu.Game.Graphics.UserInterface
textRemovedSample = audio.Samples.Get(@"Keyboard/key-delete");
textCommittedSample = audio.Samples.Get(@"Keyboard/key-confirm");
caretMovedSample = audio.Samples.Get(@"Keyboard/key-movement");
selectCharSample = audio.Samples.Get(@"Keyboard/select-char");
selectWordSample = audio.Samples.Get(@"Keyboard/select-word");
selectAllSample = audio.Samples.Get(@"Keyboard/select-all");
deselectSample = audio.Samples.Get(@"Keyboard/deselect");
}
private Color4 selectionColour;
@ -112,7 +125,72 @@ namespace osu.Game.Graphics.UserInterface
{
base.OnCaretMoved(selecting);
caretMovedSample?.Play();
if (!selecting)
caretMovedSample?.Play();
}
protected override void OnTextSelectionChanged(TextSelectionType selectionType)
{
base.OnTextSelectionChanged(selectionType);
if (selectionType == TextSelectionType.Word)
{
if (!selectionStarted)
playSelectSample(selectionType);
else
playSelectSample();
}
else
{
playSelectSample(selectionType);
}
selectionStarted = true;
}
protected override void OnTextDeselected()
{
if (selectionStarted)
playSelectSample(TextSelectionType.Deselect);
selectionStarted = false;
base.OnTextDeselected();
}
private void playSelectSample(TextSelectionType selectionType = TextSelectionType.Character)
{
if (Time.Current < sampleLastPlaybackTime + 15) return;
SampleChannel? channel;
double pitch = 0.98 + RNG.NextDouble(0.04);
switch (selectionType)
{
case TextSelectionType.All:
channel = selectAllSample?.GetChannel();
break;
case TextSelectionType.Word:
channel = selectWordSample?.GetChannel();
break;
case TextSelectionType.Deselect:
channel = deselectSample?.GetChannel();
break;
default:
channel = selectCharSample?.GetChannel();
pitch += (SelectedText.Length / (float)Text.Length) * 0.15f;
break;
}
if (channel == null) return;
channel.Frequency.Value = pitch;
channel.Play();
sampleLastPlaybackTime = Time.Current;
}
protected override void OnImeComposition(string newComposition, int removedTextLength, int addedTextLength, bool caretMoved)