osu/osu.Game/Graphics/UserInterface/OsuTextBox.cs

292 lines
9.8 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 09:19:50 +00:00
#nullable enable
using System.Linq;
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2020-02-07 20:42:47 +00:00
using osu.Framework.Audio.Track;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.Sprites;
2018-11-20 07:51:59 +00:00
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Extensions.Color4Extensions;
2020-02-08 19:25:16 +00:00
using osu.Framework.Graphics.Shapes;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
using osu.Framework.Utils;
2020-02-07 20:42:47 +00:00
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Graphics.Containers;
using osu.Game.Overlays;
2020-02-07 20:42:47 +00:00
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Graphics.UserInterface
{
2019-12-24 05:21:16 +00:00
public class OsuTextBox : BasicTextBox
2018-04-13 09:19:50 +00: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 09:19:50 +00:00
protected override float LeftRightPadding => 10;
2019-12-24 09:23:09 +00:00
protected override float CaretWidth => 3;
2018-04-13 09:19:50 +00:00
protected override SpriteText CreatePlaceholder() => new OsuSpriteText
{
2019-02-20 07:52:36 +00:00
Font = OsuFont.GetFont(italics: true),
2018-04-13 09:19:50 +00:00
Margin = new MarginPadding { Left = 2 },
};
private readonly Sample?[] textAddedSamples = new Sample[4];
private Sample? capsTextAddedSample;
private Sample? textRemovedSample;
private Sample? textCommittedSample;
private Sample? caretMovedSample;
private OsuCaret? caret;
2018-04-13 09:19:50 +00:00
public OsuTextBox()
{
Height = 40;
TextContainer.Height = 0.5f;
CornerRadius = 5;
LengthLimit = 1000;
2018-04-13 09:19:50 +00:00
2018-06-27 07:06:26 +00:00
Current.DisabledChanged += disabled => { Alpha = disabled ? 0.3f : 1; };
2018-04-13 09:19:50 +00:00
}
[BackgroundDependencyLoader(true)]
private void load(OverlayColourProvider? colourProvider, OsuColour colour, AudioManager audio)
2018-04-13 09:19:50 +00: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);
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");
2018-04-13 09:19:50 +00:00
}
private Color4 selectionColour;
protected override Color4 SelectionColour => selectionColour;
2019-12-24 05:21:16 +00:00
2020-08-19 13:10:58 +00:00
protected override void OnUserTextAdded(string added)
{
2020-08-19 13:10:58 +00:00
base.OnUserTextAdded(added);
if (added.Any(char.IsUpper) && AllowUniqueCharacterSamples)
capsTextAddedSample?.Play();
else
playTextAddedSample();
}
2020-08-19 13:10:58 +00:00
protected override void OnUserTextRemoved(string removed)
{
2020-08-19 13:10:58 +00:00
base.OnUserTextRemoved(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();
}
2021-12-08 08:57:53 +00: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.
textRemovedSample?.Play();
return;
default:
// longer text removed, composition ended because it was cancelled.
// could be a different sample if desired.
textRemovedSample?.Play();
return;
}
}
if (addedTextLength > 0)
{
// some text was added, probably due to typing new text or by changing the candidate.
playTextAddedSample();
return;
}
if (removedTextLength > 0)
{
// text was probably removed by backspacing.
// it's also possible that a candidate that only removed text was changed to.
textRemovedSample?.Play();
return;
}
if (caretMoved)
{
// only the caret/selection was moved.
caretMovedSample?.Play();
}
}
protected override void OnImeResult(string result, bool successful)
{
base.OnImeResult(result, successful);
if (successful)
{
// composition was successfully completed, usually by pressing the enter key.
textCommittedSample?.Play();
}
else
{
// composition was prematurely ended, eg. by clicking inside the textbox.
// could be a different sample if desired.
textCommittedSample?.Play();
}
}
2018-10-02 03:02:47 +00:00
protected override void OnFocus(FocusEvent e)
2018-04-13 09:19:50 +00:00
{
BorderThickness = 3;
2018-10-02 03:02:47 +00:00
base.OnFocus(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override void OnFocusLost(FocusLostEvent e)
2018-04-13 09:19:50 +00:00
{
BorderThickness = 0;
2018-10-02 03:02:47 +00:00
base.OnFocusLost(e);
2018-04-13 09:19:50 +00: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-07 20:42:47 +00:00
protected override Caret CreateCaret() => caret = new OsuCaret
2020-02-07 20:42:47 +00:00
{
CaretWidth = CaretWidth,
SelectionColour = SelectionColour,
};
2021-12-08 09:30:08 +00:00
private void playTextAddedSample() => textAddedSamples[RNG.Next(0, textAddedSamples.Length)]?.Play();
2020-02-09 06:36:44 +00:00
private class OsuCaret : Caret
2020-02-07 20:42:47 +00:00
{
2020-02-08 19:25:16 +00:00
private const float caret_move_time = 60;
private readonly CaretBeatSyncedContainer beatSync;
2020-02-07 20:42:47 +00:00
2020-02-09 06:36:44 +00:00
public OsuCaret()
2020-02-07 20:42:47 +00:00
{
2020-02-08 19:25:16 +00: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-07 20:42:47 +00:00
}
2020-02-08 19:25:16 +00:00
public override void Hide() => this.FadeOut(200);
public float CaretWidth { get; set; }
public Color4 SelectionColour { get; set; }
2020-02-07 20:42:47 +00:00
public override void DisplayAt(Vector2 position, float? selectionWidth)
{
2020-02-08 19:25:16 +00:00
beatSync.HasSelection = selectionWidth != null;
2020-02-07 20:42:47 +00:00
2020-02-08 19:25:16 +00: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-07 20:42:47 +00:00
}
private class CaretBeatSyncedContainer : BeatSyncedContainer
{
2020-02-08 19:25:16 +00:00
private bool hasSelection;
public bool HasSelection
{
set
{
hasSelection = value;
if (value)
this.FadeTo(0.5f, 200, Easing.Out);
}
}
2020-02-07 20:42:47 +00:00
2020-02-08 19:25:16 +00:00
public CaretBeatSyncedContainer()
2020-02-07 20:42:47 +00:00
{
MinimumBeatLength = 300;
2020-02-08 19:25:16 +00:00
InternalChild = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White,
};
2020-02-07 20:42:47 +00:00
}
2020-06-23 04:49:18 +00:00
protected override void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, ChannelAmplitudes amplitudes)
2020-02-07 20:42:47 +00:00
{
2020-02-08 19:25:16 +00:00
if (!hasSelection)
this.FadeTo(0.7f).FadeTo(0.4f, timingPoint.BeatLength, Easing.InOutSine);
2020-02-07 20:42:47 +00:00
}
}
}
2018-04-13 09:19:50 +00:00
}
}