Skip to content

Commit

Permalink
Line View now understands line breaks.
Browse files Browse the repository at this point in the history
This is done via the markup system, using a self-closing [br /] marker.
  • Loading branch information
McJones committed Jan 25, 2024
1 parent b49080c commit 52d2a6a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- enabling/disabling C# linking will force an entire C# reimport
- enabling/disabling asset linking will force a reimport of all `yarnprojects`
- `Yarn.Unity.ActionAnalyser.Action` now has a `MethodIdentifierName` property, which is the short form of the method name.
- `LineView` now will add in line breaks when it encounters a self closing `[br /]` marker.

### Changed

Expand Down
23 changes: 21 additions & 2 deletions Runtime/Views/LineView.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
#if USE_TMP
Expand Down Expand Up @@ -386,7 +387,7 @@ IEnumerator PresentLine()
}
else
{
lineText.text = text.Text;
lineText.text = LineView.AddLineBreaks(text);
}

if (useTypewriterEffect)
Expand Down Expand Up @@ -554,8 +555,9 @@ public override void DialogueComplete()
/// </remarks>
/// <param name="line">The parsed marked up line with it's attributes.</param>
/// <param name="palette">The palette mapping attributes to colours.</param>
/// <param name="applyLineBreaks">If the [br /] marker is found in the line should this be replaced with a line break?</param>
/// <returns>A TMP formatted string with the palette markup values injected within.</returns>
public static string PaletteMarkedUpText(Markup.MarkupParseResult line, MarkupPalette palette)
public static string PaletteMarkedUpText(Markup.MarkupParseResult line, MarkupPalette palette, bool applyLineBreaks = true)
{
string lineOfText = line.Text;
line.Attributes.Sort((a, b) => (b.Position.CompareTo(a.Position)));
Expand All @@ -570,6 +572,23 @@ public static string PaletteMarkedUpText(Markup.MarkupParseResult line, MarkupPa
lineOfText = lineOfText.Insert(attribute.Position + attribute.Length, "</color>");
lineOfText = lineOfText.Insert(attribute.Position, $"<color=#{ColorUtility.ToHtmlStringRGB(markerColour)}>");
}

if (applyLineBreaks && attribute.Name == "br")
{
lineOfText = lineOfText.Insert(attribute.Position, "<br>");
}
}
return lineOfText;
}

public static string AddLineBreaks(Markup.MarkupParseResult line)
{
string lineOfText = line.Text;
line.Attributes.Sort((a, b) => (b.Position.CompareTo(a.Position)));
foreach (var attribute in line.Attributes.Where(a => a.Name == "br"))
{
// we then replace the marker with the tmp <br>
lineOfText = lineOfText.Insert(attribute.Position, "<br>");
}
return lineOfText;
}
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Views/OptionView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public DialogueOption Option

if (palette != null)
{
text.text = LineView.PaletteMarkedUpText(line, palette);
text.text = LineView.PaletteMarkedUpText(line, palette, false);
}
else
{
Expand Down

0 comments on commit 52d2a6a

Please sign in to comment.