1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 13:53:10 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuTextBox.cs

386 lines
13 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2020-02-08 04:42:47 +08:00
using osu.Framework.Audio.Track;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.Sprites;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.Color4Extensions;
2020-02-09 03:25:16 +08:00
using osu.Framework.Graphics.Shapes;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Utils;
2020-02-08 04:42:47 +08:00
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Graphics.Containers;
using osu.Game.Overlays;
2020-02-08 04:42:47 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.UserInterface
{
2019-12-24 13:21:16 +08:00
public class OsuTextBox : BasicTextBox
2018-04-13 17:19:50 +08:00
{
/// <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;
2018-04-13 17:19:50 +08:00
protected override float LeftRightPadding => 10;
2019-12-24 17:23:09 +08:00
protected override float CaretWidth => 3;
2018-04-13 17:19:50 +08:00
protected override SpriteText CreatePlaceholder() => new OsuSpriteText
{
2019-02-20 15:52:36 +08:00
Font = OsuFont.GetFont(italics: true),
2018-04-13 17:19:50 +08:00
Margin = new MarginPadding { Left = 2 },
};
private OsuCaret? caret;
2022-08-19 14:51:27 +08:00
private bool selectionStarted;
private double sampleLastPlaybackTime;
protected enum FeedbackSampleType
2022-08-24 21:19:32 +08:00
{
TextAdd,
TextAddCaps,
TextRemove,
TextConfirm,
TextInvalid,
CaretMove,
SelectCharacter,
SelectWord,
SelectAll,
2022-08-24 21:19:32 +08:00
Deselect
}
private Dictionary<FeedbackSampleType, Sample?[]> sampleMap = new Dictionary<FeedbackSampleType, Sample?[]>();
2018-04-13 17:19:50 +08:00
public OsuTextBox()
{
Height = 40;
TextContainer.Height = 0.5f;
CornerRadius = 5;
LengthLimit = 1000;
2018-04-13 17:19:50 +08:00
2018-06-27 15:06:26 +08:00
Current.DisabledChanged += disabled => { Alpha = disabled ? 0.3f : 1; };
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader(true)]
private void load(OverlayColourProvider? colourProvider, OsuColour colour, AudioManager audio)
2018-04-13 17:19:50 +08:00
{
BackgroundUnfocused = colourProvider?.Background5 ?? Color4.Black.Opacity(0.5f);
BackgroundFocused = colourProvider?.Background4 ?? OsuColour.Gray(0.3f).Opacity(0.8f);
BackgroundCommit = BorderColour = colourProvider?.Highlight1 ?? colour.Yellow;
selectionColour = colourProvider?.Background1 ?? new Color4(249, 90, 255, 255);
if (caret != null)
caret.SelectionColour = selectionColour;
Placeholder.Colour = colourProvider?.Foreground1 ?? new Color4(180, 180, 180, 255);
var textAddedSamples = new Sample?[4];
for (int i = 0; i < textAddedSamples.Length; i++)
textAddedSamples[i] = audio.Samples.Get($@"Keyboard/key-press-{1 + i}");
sampleMap = new Dictionary<FeedbackSampleType, Sample?[]>
{
{ FeedbackSampleType.TextAdd, textAddedSamples },
{ FeedbackSampleType.TextAddCaps, new[] { audio.Samples.Get(@"Keyboard/key-caps") } },
{ FeedbackSampleType.TextRemove, new[] { audio.Samples.Get(@"Keyboard/key-delete") } },
{ FeedbackSampleType.TextConfirm, new[] { audio.Samples.Get(@"Keyboard/key-confirm") } },
{ FeedbackSampleType.TextInvalid, new[] { audio.Samples.Get(@"Keyboard/key-invalid") } },
{ FeedbackSampleType.CaretMove, new[] { audio.Samples.Get(@"Keyboard/key-movement") } },
{ FeedbackSampleType.SelectCharacter, new[] { audio.Samples.Get(@"Keyboard/select-char") } },
{ FeedbackSampleType.SelectWord, new[] { audio.Samples.Get(@"Keyboard/select-word") } },
{ FeedbackSampleType.SelectAll, new[] { audio.Samples.Get(@"Keyboard/select-all") } },
{ FeedbackSampleType.Deselect, new[] { audio.Samples.Get(@"Keyboard/deselect") } }
};
2018-04-13 17:19:50 +08:00
}
private Color4 selectionColour;
protected override Color4 SelectionColour => selectionColour;
2019-12-24 13:21:16 +08:00
2020-08-19 21:10:58 +08:00
protected override void OnUserTextAdded(string added)
{
2020-08-19 21:10:58 +08:00
base.OnUserTextAdded(added);
if (!added.Any(CanAddCharacter))
return;
if (added.Any(char.IsUpper) && AllowUniqueCharacterSamples)
PlayFeedbackSample(FeedbackSampleType.TextAddCaps);
else
PlayFeedbackSample(FeedbackSampleType.TextAdd);
}
2020-08-19 21:10:58 +08:00
protected override void OnUserTextRemoved(string removed)
{
2020-08-19 21:10:58 +08:00
base.OnUserTextRemoved(removed);
PlayFeedbackSample(FeedbackSampleType.TextRemove);
}
protected override void NotifyInputError()
{
base.NotifyInputError();
PlayFeedbackSample(FeedbackSampleType.TextInvalid);
}
protected override void OnTextCommitted(bool textChanged)
{
base.OnTextCommitted(textChanged);
PlayFeedbackSample(FeedbackSampleType.TextConfirm);
}
protected override void OnCaretMoved(bool selecting)
{
base.OnCaretMoved(selecting);
2022-08-19 14:51:27 +08:00
if (!selecting)
PlayFeedbackSample(FeedbackSampleType.CaretMove);
2022-08-19 14:51:27 +08:00
}
protected override void OnTextSelectionChanged(TextSelectionType selectionType)
{
base.OnTextSelectionChanged(selectionType);
2022-08-24 21:19:32 +08:00
switch (selectionType)
2022-08-19 14:51:27 +08:00
{
2022-08-24 21:19:32 +08:00
case TextSelectionType.Character:
PlayFeedbackSample(FeedbackSampleType.SelectCharacter);
2022-08-24 21:19:32 +08:00
break;
case TextSelectionType.Word:
PlayFeedbackSample(selectionStarted ? FeedbackSampleType.SelectCharacter : FeedbackSampleType.SelectWord);
2022-08-24 21:19:32 +08:00
break;
case TextSelectionType.All:
PlayFeedbackSample(FeedbackSampleType.SelectAll);
2022-08-24 21:19:32 +08:00
break;
2022-08-19 14:51:27 +08:00
}
selectionStarted = true;
}
protected override void OnTextDeselected()
{
2022-08-24 21:19:32 +08:00
base.OnTextDeselected();
2022-08-19 14:51:27 +08:00
2022-08-24 21:19:32 +08:00
if (!selectionStarted) return;
2022-08-19 14:51:27 +08:00
PlayFeedbackSample(FeedbackSampleType.Deselect);
2022-08-24 21:19:32 +08:00
selectionStarted = false;
2022-08-19 14:51:27 +08:00
}
2021-12-08 16:57:53 +08:00
protected override void OnImeComposition(string newComposition, int removedTextLength, int addedTextLength, bool caretMoved)
{
base.OnImeComposition(newComposition, removedTextLength, addedTextLength, caretMoved);
if (string.IsNullOrEmpty(newComposition))
{
switch (removedTextLength)
{
case 0:
// empty composition event, composition wasn't changed, don't play anything.
return;
case 1:
// composition probably ended by pressing backspace, or was cancelled.
PlayFeedbackSample(FeedbackSampleType.TextRemove);
2021-12-08 16:57:53 +08:00
return;
default:
// longer text removed, composition ended because it was cancelled.
// could be a different sample if desired.
PlayFeedbackSample(FeedbackSampleType.TextRemove);
2021-12-08 16:57:53 +08:00
return;
}
}
if (addedTextLength > 0)
{
// some text was added, probably due to typing new text or by changing the candidate.
PlayFeedbackSample(FeedbackSampleType.TextAdd);
2021-12-08 16:57:53 +08:00
return;
}
if (removedTextLength > 0)
{
// text was probably removed by backspacing.
// it's also possible that a candidate that only removed text was changed to.
PlayFeedbackSample(FeedbackSampleType.TextRemove);
2021-12-08 16:57:53 +08:00
return;
}
if (caretMoved)
{
// only the caret/selection was moved.
PlayFeedbackSample(FeedbackSampleType.CaretMove);
2021-12-08 16:57:53 +08:00
}
}
protected override void OnImeResult(string result, bool successful)
{
base.OnImeResult(result, successful);
if (successful)
{
// composition was successfully completed, usually by pressing the enter key.
PlayFeedbackSample(FeedbackSampleType.TextConfirm);
2021-12-08 16:57:53 +08:00
}
else
{
// composition was prematurely ended, eg. by clicking inside the textbox.
// could be a different sample if desired.
PlayFeedbackSample(FeedbackSampleType.TextConfirm);
2021-12-08 16:57:53 +08:00
}
}
2018-10-02 11:02:47 +08:00
protected override void OnFocus(FocusEvent e)
2018-04-13 17:19:50 +08:00
{
BorderThickness = 3;
2018-10-02 11:02:47 +08:00
base.OnFocus(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnFocusLost(FocusLostEvent e)
2018-04-13 17:19:50 +08:00
{
BorderThickness = 0;
2018-10-02 11:02:47 +08:00
base.OnFocusLost(e);
2018-04-13 17:19:50 +08:00
}
protected override Drawable GetDrawableCharacter(char c) => new FallingDownContainer
{
AutoSizeAxes = Axes.Both,
Child = new OsuSpriteText { Text = c.ToString(), Font = OsuFont.GetFont(size: CalculatedTextSize) },
};
2020-02-08 04:42:47 +08:00
protected override Caret CreateCaret() => caret = new OsuCaret
2020-02-08 04:42:47 +08:00
{
CaretWidth = CaretWidth,
SelectionColour = SelectionColour,
};
private SampleChannel? getSampleChannel(FeedbackSampleType feedbackSampleType)
2022-08-26 22:29:03 +08:00
{
var samples = sampleMap[feedbackSampleType];
2022-08-26 22:29:03 +08:00
if (samples == null || samples.Length == 0)
return null;
2022-08-26 22:29:03 +08:00
return samples[RNG.Next(0, samples.Length)]?.GetChannel();
}
2022-08-26 22:29:03 +08:00
protected void PlayFeedbackSample(FeedbackSampleType feedbackSample) => Schedule(() =>
{
if (Time.Current < sampleLastPlaybackTime + 15) return;
2022-08-26 22:29:03 +08:00
SampleChannel? channel = getSampleChannel(feedbackSample);
2022-08-26 22:29:03 +08:00
if (channel == null) return;
double pitch = 0.98 + RNG.NextDouble(0.04);
if (feedbackSample == FeedbackSampleType.SelectCharacter)
pitch += ((double)SelectedText.Length / Math.Max(1, Text.Length)) * 0.15f;
2022-08-26 22:29:03 +08:00
channel.Frequency.Value = pitch;
channel.Play();
sampleLastPlaybackTime = Time.Current;
});
2022-08-26 22:29:03 +08:00
2020-02-09 14:36:44 +08:00
private class OsuCaret : Caret
2020-02-08 04:42:47 +08:00
{
2020-02-09 03:25:16 +08:00
private const float caret_move_time = 60;
private readonly CaretBeatSyncedContainer beatSync;
2020-02-08 04:42:47 +08:00
2020-02-09 14:36:44 +08:00
public OsuCaret()
2020-02-08 04:42:47 +08:00
{
2020-02-09 03:25:16 +08:00
RelativeSizeAxes = Axes.Y;
Size = new Vector2(1, 0.9f);
Colour = Color4.Transparent;
Anchor = Anchor.CentreLeft;
Origin = Anchor.CentreLeft;
Masking = true;
CornerRadius = 1;
InternalChild = beatSync = new CaretBeatSyncedContainer
{
RelativeSizeAxes = Axes.Both,
};
2020-02-08 04:42:47 +08:00
}
2020-02-09 03:25:16 +08:00
public override void Hide() => this.FadeOut(200);
public float CaretWidth { get; set; }
public Color4 SelectionColour { get; set; }
2020-02-08 04:42:47 +08:00
public override void DisplayAt(Vector2 position, float? selectionWidth)
{
2020-02-09 03:25:16 +08:00
beatSync.HasSelection = selectionWidth != null;
2020-02-08 04:42:47 +08:00
2020-02-09 03:25:16 +08:00
if (selectionWidth != null)
{
this.MoveTo(new Vector2(position.X, position.Y), 60, Easing.Out);
this.ResizeWidthTo(selectionWidth.Value + CaretWidth / 2, caret_move_time, Easing.Out);
this.FadeColour(SelectionColour, 200, Easing.Out);
}
else
{
this.MoveTo(new Vector2(position.X - CaretWidth / 2, position.Y), 60, Easing.Out);
this.ResizeWidthTo(CaretWidth, caret_move_time, Easing.Out);
this.FadeColour(Color4.White, 200, Easing.Out);
}
2020-02-08 04:42:47 +08:00
}
private class CaretBeatSyncedContainer : BeatSyncedContainer
{
2020-02-09 03:25:16 +08:00
private bool hasSelection;
public bool HasSelection
{
set
{
hasSelection = value;
if (value)
this.FadeTo(0.5f, 200, Easing.Out);
}
}
2020-02-08 04:42:47 +08:00
2020-02-09 03:25:16 +08:00
public CaretBeatSyncedContainer()
2020-02-08 04:42:47 +08:00
{
MinimumBeatLength = 300;
2020-02-09 03:25:16 +08:00
InternalChild = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White,
};
2020-02-08 04:42:47 +08:00
}
2020-06-23 12:49:18 +08:00
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
2020-02-08 04:42:47 +08:00
{
2020-02-09 03:25:16 +08:00
if (!hasSelection)
this.FadeTo(0.7f).FadeTo(0.4f, timingPoint.BeatLength, Easing.InOutSine);
2020-02-08 04:42:47 +08:00
}
}
}
2018-04-13 17:19:50 +08:00
}
}