mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
Allow specifying a maximum amount of characters for a TextField
This commit is contained in:
parent
fd5b83eaa0
commit
8f5d3083b5
2 changed files with 20 additions and 5 deletions
|
@ -12,6 +12,9 @@ Additions
|
||||||
- Added a strikethrough formatting code
|
- Added a strikethrough formatting code
|
||||||
|
|
||||||
### MLEM.Ui
|
### MLEM.Ui
|
||||||
|
Additions
|
||||||
|
- Allow specifying a maximum amount of characters for a TextField
|
||||||
|
|
||||||
Improvements
|
Improvements
|
||||||
- Cache TokenizedString inner offsets for non-Left text alignments to improve performance
|
- Cache TokenizedString inner offsets for non-Left text alignments to improve performance
|
||||||
|
|
||||||
|
|
|
@ -158,6 +158,11 @@ namespace MLEM.Ui.Elements {
|
||||||
this.HandleTextChange(false);
|
this.HandleTextChange(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// The maximum amount of characters that can be input into this text field.
|
||||||
|
/// If this is set, the length of <see cref="Text"/> will never exceed this value.
|
||||||
|
/// </summary>
|
||||||
|
public int? MaximumCharacters;
|
||||||
|
|
||||||
private readonly StringBuilder text = new StringBuilder();
|
private readonly StringBuilder text = new StringBuilder();
|
||||||
|
|
||||||
|
@ -305,33 +310,40 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Replaces this text field's text with the given text.
|
/// Replaces this text field's text with the given text.
|
||||||
|
/// If the resulting <see cref="Text"/> exceeds <see cref="MaximumCharacters"/>, the end will be cropped to fit.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="text">The new text</param>
|
/// <param name="text">The new text</param>
|
||||||
/// <param name="removeMismatching">If any characters that don't match the <see cref="InputRule"/> should be left out</param>
|
/// <param name="removeMismatching">If any characters that don't match the <see cref="InputRule"/> should be left out</param>
|
||||||
public void SetText(object text, bool removeMismatching = false) {
|
public void SetText(object text, bool removeMismatching = false) {
|
||||||
|
var strg = text?.ToString() ?? string.Empty;
|
||||||
if (removeMismatching) {
|
if (removeMismatching) {
|
||||||
var result = new StringBuilder();
|
var result = new StringBuilder();
|
||||||
foreach (var c in text.ToString()) {
|
foreach (var c in strg) {
|
||||||
if (this.InputRule(this, c.ToCachedString()))
|
if (this.InputRule(this, c.ToCachedString()))
|
||||||
result.Append(c);
|
result.Append(c);
|
||||||
}
|
}
|
||||||
text = result.ToString();
|
strg = result.ToString();
|
||||||
} else if (!this.InputRule(this, text.ToString()))
|
} else if (!this.InputRule(this, strg))
|
||||||
return;
|
return;
|
||||||
|
if (this.MaximumCharacters != null && strg.Length > this.MaximumCharacters)
|
||||||
|
strg = strg.Substring(0, this.MaximumCharacters.Value);
|
||||||
this.text.Clear();
|
this.text.Clear();
|
||||||
this.text.Append(text);
|
this.text.Append(strg);
|
||||||
this.CaretPos = this.text.Length;
|
this.CaretPos = this.text.Length;
|
||||||
this.HandleTextChange();
|
this.HandleTextChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Inserts the given text at the <see cref="CaretPos"/>
|
/// Inserts the given text at the <see cref="CaretPos"/>.
|
||||||
|
/// If the resulting <see cref="Text"/> exceeds <see cref="MaximumCharacters"/>, the end will be cropped to fit.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="text">The text to insert</param>
|
/// <param name="text">The text to insert</param>
|
||||||
public void InsertText(object text) {
|
public void InsertText(object text) {
|
||||||
var strg = text.ToString();
|
var strg = text.ToString();
|
||||||
if (!this.InputRule(this, strg))
|
if (!this.InputRule(this, strg))
|
||||||
return;
|
return;
|
||||||
|
if (this.MaximumCharacters != null && this.text.Length + strg.Length > this.MaximumCharacters)
|
||||||
|
strg = strg.Substring(0, this.MaximumCharacters.Value - this.text.Length);
|
||||||
this.text.Insert(this.CaretPos, strg);
|
this.text.Insert(this.CaretPos, strg);
|
||||||
this.CaretPos += strg.Length;
|
this.CaretPos += strg.Length;
|
||||||
this.HandleTextChange();
|
this.HandleTextChange();
|
||||||
|
|
Loading…
Reference in a new issue