Add horizontal alignment tags for rich text box

This commit is contained in:
Wojciech Figat
2022-08-05 15:29:37 +02:00
parent 9caf80bdc9
commit f781fd8ab6
4 changed files with 107 additions and 12 deletions

View File

@@ -91,6 +91,8 @@ namespace FlaxEngine.GUI
{ "size", ProcessSize },
{ "img", ProcessImage },
{ "valign", ProcessVAlign },
{ "align", ProcessAlign },
{ "center", ProcessCenter },
};
private HtmlParser _parser = new HtmlParser();
@@ -242,10 +244,12 @@ namespace FlaxEngine.GUI
}
// Organize text blocks within line
var horizontalAlignments = TextBlockStyle.Alignments.Baseline;
for (int i = context.LineStartTextBlockIndex; i < _textBlocks.Count; i++)
{
ref TextBlock textBlock = ref textBlocks[i];
var vOffset = lineSize.Y - textBlock.Bounds.Height;
horizontalAlignments |= textBlock.Style.Alignment & TextBlockStyle.Alignments.HorizontalMask;
switch (textBlock.Style.Alignment & TextBlockStyle.Alignments.VerticalMask)
{
case TextBlockStyle.Alignments.Baseline:
@@ -279,6 +283,24 @@ namespace FlaxEngine.GUI
}
}
}
var hOffset = Width - lineSize.X;
if ((horizontalAlignments & TextBlockStyle.Alignments.Center) == TextBlockStyle.Alignments.Center)
{
hOffset *= 0.5f;
for (int i = context.LineStartTextBlockIndex; i < _textBlocks.Count; i++)
{
ref TextBlock textBlock = ref textBlocks[i];
textBlock.Bounds.Location.X += hOffset;
}
}
else if ((horizontalAlignments & TextBlockStyle.Alignments.Right) == TextBlockStyle.Alignments.Right)
{
for (int i = context.LineStartTextBlockIndex; i < _textBlocks.Count; i++)
{
ref TextBlock textBlock = ref textBlocks[i];
textBlock.Bounds.Location.X += hOffset;
}
}
// Move to the next line
context.LineStartCharacterIndex = lineEnd + 1;

View File

@@ -231,6 +231,49 @@ namespace FlaxEngine.GUI
}
}
private static void ProcessAlign(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 valign))
{
style.Alignment &= ~TextBlockStyle.Alignments.VerticalMask;
switch (valign)
{
case "left":
style.Alignment = TextBlockStyle.Alignments.Left;
break;
case "right":
style.Alignment = TextBlockStyle.Alignments.Right;
break;
case "center":
style.Alignment = TextBlockStyle.Alignments.Center;
break;
}
}
context.StyleStack.Push(style);
}
}
private static void ProcessCenter(ref ParsingContext context, ref HtmlTag tag)
{
if (tag.IsSlash)
{
context.StyleStack.Pop();
}
else
{
var style = context.StyleStack.Peek();
style.Alignment = TextBlockStyle.Alignments.Center;
context.StyleStack.Push(style);
}
}
private static Asset FindAsset(string name, System.Type type)
{
var ids = Content.GetAllAssetsByType(type);

View File

@@ -50,5 +50,14 @@ namespace FlaxEngine.GUI
BackgroundSelectedBrush = new SolidColorBrush(style.BackgroundSelected),
};
}
/// <inheritdoc />
protected override void OnSizeChanged()
{
base.OnSizeChanged();
// Refresh textblocks since thos emight depend on control size (eg. align right)
UpdateTextBlocks();
}
}
}

View File

@@ -32,11 +32,32 @@ namespace FlaxEngine.GUI
/// </summary>
Bottom = 4,
/// <summary>
/// Block will be aligned to the left edge of the layout (horizontally).
/// </summary>
Left = 8,
/// <summary>
/// Block will be aligned to center of the layout (horizontally).
/// </summary>
Center = 16,
/// <summary>
/// Block will be aligned to the right edge of the layout (horizontally).
/// </summary>
Right = 32,
/// <summary>
/// Mask with vertical alignment flags.
/// </summary>
[HideInEditor]
VerticalMask = Top | Middle | Bottom,
/// <summary>
/// Mask with horizontal alignment flags.
/// </summary>
[HideInEditor]
HorizontalMask = Left | Center | Right,
}
/// <summary>