Add more tags to Rich Text Box

This commit is contained in:
Wojciech Figat
2022-08-03 12:57:22 +02:00
parent 64609f823c
commit ad37b8361b
9 changed files with 205 additions and 25 deletions

View File

@@ -12,6 +12,11 @@ namespace FlaxEngine.GUI
/// </summary>
public struct ParsingContext
{
/// <summary>
/// Text box control.
/// </summary>
public RichTextBox Control;
/// <summary>
/// HTML tags parser.
/// </summary>
@@ -55,6 +60,10 @@ namespace FlaxEngine.GUI
{
{ "br", ProcessBr },
{ "color", ProcessColor },
{ "alpha", ProcessAlpha },
{ "style", ProcessStyle },
{ "b", ProcessBold },
{ "i", ProcessItalic },
};
private HtmlParser _parser = new HtmlParser();
@@ -75,6 +84,7 @@ namespace FlaxEngine.GUI
_styleStack.Push(_textStyle);
var context = new ParsingContext
{
Control = this,
Parser = _parser,
StyleStack = _styleStack,
};

View File

@@ -34,5 +34,79 @@ namespace FlaxEngine.GUI
context.StyleStack.Push(style);
}
}
private static void ProcessAlpha(ref ParsingContext context, ref HtmlTag tag)
{
if (tag.IsSlash)
{
context.StyleStack.Pop();
}
else
{
var style = context.StyleStack.Peek();
if (tag.Attributes.TryGetValue(string.Empty, out var alphaText))
{
if (alphaText.Length == 3 && alphaText[0] == '#')
{
style.Color.A = ((StringUtils.HexDigit(alphaText[1]) << 4) + StringUtils.HexDigit(alphaText[2])) / 255.0f;
}
else if (alphaText.Length > 1 && alphaText[alphaText.Length - 1] == '%')
{
style.Color.A = float.Parse(alphaText.Substring(0, alphaText.Length - 1)) / 100.0f;
}
}
context.StyleStack.Push(style);
}
}
private static void ProcessStyle(ref ParsingContext context, ref HtmlTag tag)
{
if (tag.IsSlash)
{
context.StyleStack.Pop();
}
else
{
var style = context.StyleStack.Peek();
if (tag.Attributes.TryGetValue(string.Empty, out var styleName))
{
if (context.Control.Styles.TryGetValue(styleName, out var customStyle))
{
if (customStyle.Font == null)
customStyle.Font = style.Font;
style = customStyle;
}
}
context.StyleStack.Push(style);
}
}
private static void ProcessBold(ref ParsingContext context, ref HtmlTag tag)
{
if (tag.IsSlash)
{
context.StyleStack.Pop();
}
else
{
var style = context.StyleStack.Peek();
style.Font = style.Font.GetBold();
context.StyleStack.Push(style);
}
}
private static void ProcessItalic(ref ParsingContext context, ref HtmlTag tag)
{
if (tag.IsSlash)
{
context.StyleStack.Pop();
}
else
{
var style = context.StyleStack.Peek();
style.Font = style.Font.GetItalic();
context.StyleStack.Push(style);
}
}
}
}

View File

@@ -1,5 +1,7 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
using System.Collections.Generic;
namespace FlaxEngine.GUI
{
/// <summary>
@@ -23,6 +25,12 @@ namespace FlaxEngine.GUI
}
}
/// <summary>
/// The collection of custom text styles to apply (named).
/// </summary>
[EditorOrder(30)]
public Dictionary<string, TextBlockStyle> Styles = new Dictionary<string, TextBlockStyle>();
/// <summary>
/// Initializes a new instance of the <see cref="RichTextBox"/> class.
/// </summary>

View File

@@ -62,7 +62,7 @@ namespace FlaxEngine.GUI
/// <summary>
/// Updates the text blocks.
/// </summary>
protected virtual void UpdateTextBlocks()
public virtual void UpdateTextBlocks()
{
Profiler.BeginEvent("RichTextBoxBase.UpdateTextBlocks");