-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3072 from jorolf/abstract-text-box
Make TextBox abstract and add custom carets
- Loading branch information
Showing
5 changed files
with
312 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,187 @@ | ||
// 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 osu.Framework.Extensions.Color4Extensions; | ||
using osu.Framework.Graphics.Containers; | ||
using osu.Framework.Graphics.Shapes; | ||
using osu.Framework.Graphics.Sprites; | ||
using osu.Framework.Input.Events; | ||
using osuTK; | ||
using osuTK.Graphics; | ||
|
||
namespace osu.Framework.Graphics.UserInterface | ||
{ | ||
public class BasicTextBox : TextBox | ||
{ | ||
protected override float CaretWidth => 2; | ||
protected virtual float CaretWidth => 2; | ||
|
||
protected override Color4 SelectionColour => FrameworkColour.YellowGreen; | ||
private const float caret_move_time = 60; | ||
|
||
protected virtual Color4 SelectionColour => FrameworkColour.YellowGreen; | ||
|
||
protected Color4 BackgroundCommit { get; set; } = FrameworkColour.Green; | ||
|
||
private Color4 backgroundFocused = new Color4(100, 100, 100, 255); | ||
private Color4 backgroundUnfocused = new Color4(100, 100, 100, 120); | ||
|
||
private readonly Box background; | ||
|
||
protected Color4 BackgroundFocused | ||
{ | ||
get => backgroundFocused; | ||
set | ||
{ | ||
backgroundFocused = value; | ||
if (HasFocus) | ||
background.Colour = value; | ||
} | ||
} | ||
|
||
protected Color4 BackgroundUnfocused | ||
{ | ||
get => backgroundUnfocused; | ||
set | ||
{ | ||
backgroundUnfocused = value; | ||
if (!HasFocus) | ||
background.Colour = value; | ||
} | ||
} | ||
|
||
protected virtual Color4 InputErrorColour => Color4.Red; | ||
|
||
public BasicTextBox() | ||
{ | ||
CornerRadius = 0; | ||
Add(background = new Box | ||
{ | ||
RelativeSizeAxes = Axes.Both, | ||
Depth = 1, | ||
Colour = BackgroundUnfocused, | ||
}); | ||
|
||
BackgroundFocused = FrameworkColour.BlueGreen; | ||
BackgroundUnfocused = FrameworkColour.BlueGreenDark; | ||
BackgroundCommit = FrameworkColour.Green; | ||
TextFlow.Height = 0.75f; | ||
TextContainer.Height = 0.75f; | ||
} | ||
|
||
protected override void NotifyInputError() => background.FlashColour(InputErrorColour, 200); | ||
|
||
protected override void Commit() | ||
{ | ||
base.Commit(); | ||
|
||
background.Colour = ReleaseFocusOnCommit ? BackgroundUnfocused : BackgroundFocused; | ||
background.ClearTransforms(); | ||
background.FlashColour(BackgroundCommit, 400); | ||
} | ||
|
||
protected override void OnFocusLost(FocusLostEvent e) | ||
{ | ||
base.OnFocusLost(e); | ||
|
||
background.ClearTransforms(); | ||
background.Colour = BackgroundFocused; | ||
background.FadeColour(BackgroundUnfocused, 200, Easing.OutExpo); | ||
} | ||
|
||
protected override void OnFocus(FocusEvent e) | ||
{ | ||
base.OnFocus(e); | ||
|
||
background.ClearTransforms(); | ||
background.Colour = BackgroundUnfocused; | ||
background.FadeColour(BackgroundFocused, 200, Easing.Out); | ||
} | ||
|
||
protected override Drawable GetDrawableCharacter(char c) => new SpriteText { Text = c.ToString(), Font = FrameworkFont.Condensed.With(size: CalculatedTextSize) }; | ||
protected override Drawable GetDrawableCharacter(char c) => new FallingDownContainer | ||
{ | ||
AutoSizeAxes = Axes.Both, | ||
Child = new SpriteText { Text = c.ToString(), Font = FrameworkFont.Condensed.With(size: CalculatedTextSize) } | ||
}; | ||
|
||
protected override SpriteText CreatePlaceholder() => new SpriteText | ||
protected override SpriteText CreatePlaceholder() => new FadingPlaceholderText | ||
{ | ||
Colour = FrameworkColour.YellowGreen, | ||
Font = FrameworkFont.Condensed, | ||
Anchor = Anchor.CentreLeft, | ||
Origin = Anchor.CentreLeft, | ||
X = CaretWidth, | ||
}; | ||
|
||
public class FallingDownContainer : Container | ||
{ | ||
public override void Show() | ||
{ | ||
var col = (Color4)Colour; | ||
this.FadeColour(col.Opacity(0)).FadeColour(col, caret_move_time * 2, Easing.Out); | ||
} | ||
|
||
public override void Hide() | ||
{ | ||
this.FadeOut(200); | ||
this.MoveToY(DrawSize.Y, 200, Easing.InExpo); | ||
} | ||
} | ||
|
||
public class FadingPlaceholderText : SpriteText | ||
{ | ||
public override void Show() => this.FadeIn(200); | ||
|
||
public override void Hide() => this.FadeOut(200); | ||
} | ||
|
||
protected override Caret CreateCaret() => new BasicCaret | ||
{ | ||
CaretWidth = CaretWidth, | ||
SelectionColour = SelectionColour, | ||
}; | ||
|
||
public class BasicCaret : Caret | ||
{ | ||
public BasicCaret() | ||
{ | ||
RelativeSizeAxes = Axes.Y; | ||
Size = new Vector2(1, 0.9f); | ||
|
||
Colour = Color4.Transparent; | ||
Anchor = Anchor.CentreLeft; | ||
Origin = Anchor.CentreLeft; | ||
|
||
Masking = true; | ||
CornerRadius = 1; | ||
|
||
InternalChild = new Box | ||
{ | ||
RelativeSizeAxes = Axes.Both, | ||
Colour = Color4.White, | ||
}; | ||
} | ||
|
||
public override void Hide() => this.FadeOut(200); | ||
|
||
public float CaretWidth { get; set; } | ||
|
||
public Color4 SelectionColour { get; set; } | ||
|
||
public override void DisplayAt(Vector2 position, float? selectionWidth) | ||
{ | ||
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 | ||
.FadeTo(0.5f, 200, Easing.Out) | ||
.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) | ||
.Loop(c => c.FadeTo(0.7f).FadeTo(0.4f, 500, Easing.InOutSine)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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 osu.Framework.Graphics.Containers; | ||
using osuTK; | ||
|
||
namespace osu.Framework.Graphics.UserInterface | ||
{ | ||
/// <summary> | ||
/// A UI component generally used to show the current cursor location in a text edit field. | ||
/// </summary> | ||
public abstract class Caret : CompositeDrawable | ||
{ | ||
/// <summary> | ||
/// Request the caret be displayed at a particular location, with an optional selection length. | ||
/// </summary> | ||
/// <param name="position">The position (in parent space) where the caret should be displayed.</param> | ||
/// <param name="selectionWidth">If a selection is active, the length (in parent space) of the selection. The caret should extend to display this selection to the user.</param> | ||
public abstract void DisplayAt(Vector2 position, float? selectionWidth); | ||
} | ||
} |
Oops, something went wrong.