From 52d2a6a8a0c5fe8aa5600580449759be759ee10c Mon Sep 17 00:00:00 2001 From: Tim Nugent Date: Thu, 25 Jan 2024 12:47:59 +1100 Subject: [PATCH] Line View now understands line breaks. This is done via the markup system, using a self-closing [br /] marker. --- CHANGELOG.md | 1 + Runtime/Views/LineView.cs | 23 +++++++++++++++++++++-- Runtime/Views/OptionView.cs | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32fa2fd5..aa12074e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Runtime/Views/LineView.cs b/Runtime/Views/LineView.cs index 3f5e3bca..6fafa471 100644 --- a/Runtime/Views/LineView.cs +++ b/Runtime/Views/LineView.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; using UnityEngine.UI; #if USE_TMP @@ -386,7 +387,7 @@ IEnumerator PresentLine() } else { - lineText.text = text.Text; + lineText.text = LineView.AddLineBreaks(text); } if (useTypewriterEffect) @@ -554,8 +555,9 @@ public override void DialogueComplete() /// /// The parsed marked up line with it's attributes. /// The palette mapping attributes to colours. + /// If the [br /] marker is found in the line should this be replaced with a line break? /// A TMP formatted string with the palette markup values injected within. - 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))); @@ -570,6 +572,23 @@ public static string PaletteMarkedUpText(Markup.MarkupParseResult line, MarkupPa lineOfText = lineOfText.Insert(attribute.Position + attribute.Length, ""); lineOfText = lineOfText.Insert(attribute.Position, $""); } + + if (applyLineBreaks && attribute.Name == "br") + { + lineOfText = lineOfText.Insert(attribute.Position, "
"); + } + } + 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
+ lineOfText = lineOfText.Insert(attribute.Position, "
"); } return lineOfText; } diff --git a/Runtime/Views/OptionView.cs b/Runtime/Views/OptionView.cs index 48bf3103..b6c7c8de 100644 --- a/Runtime/Views/OptionView.cs +++ b/Runtime/Views/OptionView.cs @@ -45,7 +45,7 @@ public DialogueOption Option if (palette != null) { - text.text = LineView.PaletteMarkedUpText(line, palette); + text.text = LineView.PaletteMarkedUpText(line, palette, false); } else {