From bc56371bab3599b2a2e0bf58d2b992d1b56ae61f Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 17 Feb 2024 13:17:45 -0700 Subject: [PATCH] Dialog box now supports styling and automatic pages Added example dialog box to project. --- .../Controls/Games/DialogBox.cs | 35 +++++++++++++++---- .../FormsSampleProject.gluj | 1 + .../FormsSampleProject/Screens/MainMenu.cs | 24 ++++++++----- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/Engines/Forms/FlatRedBall.Forms/FlatRedBall.Forms.Shared/Controls/Games/DialogBox.cs b/Engines/Forms/FlatRedBall.Forms/FlatRedBall.Forms.Shared/Controls/Games/DialogBox.cs index d1c00a9a4..3afcecab4 100644 --- a/Engines/Forms/FlatRedBall.Forms/FlatRedBall.Forms.Shared/Controls/Games/DialogBox.cs +++ b/Engines/Forms/FlatRedBall.Forms/FlatRedBall.Forms.Shared/Controls/Games/DialogBox.cs @@ -329,19 +329,24 @@ private void ShowInternal(string text, bool forceImmediatePrint) textComponent.SetProperty("Text", text); + var tags = BbCodeParser.Parse(text, CustomSetPropertyOnRenderable.Tags); + var strippedLength = BbCodeParser.RemoveTags(text, tags).Length; + var shouldPrintCharacterByCharacter = LettersPerSecond > 0 && !forceImmediatePrint; if(shouldPrintCharacterByCharacter) { coreTextObject.MaxLettersToShow = 0; var allTextShownState = new global::Gum.DataTypes.Variables.StateSave(); + + allTextShownState.Variables.Add(new global::Gum.DataTypes.Variables.VariableSave { Name = "TextInstance.MaxLettersToShow", - Value = text.Length, + Value = strippedLength, SetsValue = true }); - var duration = text.Length / (float)LettersPerSecond; + var duration = strippedLength / (float)LettersPerSecond; showLetterTweener = this.Visual.InterpolateTo(NoTextShownState, allTextShownState, duration, InterpolationType.Linear, Easing.Out); @@ -360,7 +365,7 @@ private void ShowInternal(string text, bool forceImmediatePrint) } else { - coreTextObject.MaxLettersToShow = text.Length; + coreTextObject.MaxLettersToShow = strippedLength; if (TakingInput && continueIndicatorInstance != null) { @@ -378,6 +383,7 @@ private void ShowInternal(string text, bool forceImmediatePrint) private string[] ConvertToPages(string text) { + var limitsLines = this.coreTextObject.MaxNumberOfLines != null || this.textComponent.HeightUnits != global::Gum.DataTypes.DimensionUnitType.RelativeToChildren; @@ -390,14 +396,18 @@ private string[] ConvertToPages(string text) } else { + var foundTags = BbCodeParser.Parse(text, CustomSetPropertyOnRenderable.Tags); + + var withRemovedTags = BbCodeParser.RemoveTags(text, foundTags); + var unlimitedLines = new List(); var oldVerticalMode = this.coreTextObject.TextOverflowVerticalMode; this.coreTextObject.TextOverflowVerticalMode = RenderingLibrary.Graphics.TextOverflowVerticalMode.SpillOver; - coreTextObject.RawText = text; + coreTextObject.RawText = withRemovedTags; coreTextObject.UpdateLines(unlimitedLines); this.coreTextObject.TextOverflowVerticalMode = oldVerticalMode; - this.textComponent.SetProperty("Text", text); + this.textComponent.SetProperty("Text", withRemovedTags); var limitedLines = coreTextObject.WrappedText; @@ -416,13 +426,21 @@ private string[] ConvertToPages(string text) currentPage.Clear(); StringBuilder stringBuilder = new StringBuilder(); + int strippedTextCount = 0; while(absoluteLineNumber < unlimitedLines.Count) { stringBuilder.Clear(); for(int i = 0; i < limitedLines.Count && absoluteLineNumber < unlimitedLines.Count; i++) { - stringBuilder.Append(unlimitedLines[absoluteLineNumber]); + var toAppend = unlimitedLines[absoluteLineNumber]; + var sizeBeforeTags = toAppend.Length; + if(foundTags.Count > 0) + { + toAppend = BbCodeParser.AddTags(toAppend, foundTags, strippedTextCount); + } + strippedTextCount += sizeBeforeTags; + stringBuilder.Append(toAppend); absoluteLineNumber++; } pages.Add(stringBuilder.ToString()); @@ -472,7 +490,10 @@ private void ReactToInputForAdvancing(bool forceImmediatePrint) return; } //////////////////End Early Out/////////////////// - var hasMoreToType = coreTextObject.MaxLettersToShow < currentPageText?.Length; + //var hasMoreToType = coreTextObject.MaxLettersToShow < currentPageText?.Length; + + // Use the raw text since that has stripped out the tags + var hasMoreToType = coreTextObject.MaxLettersToShow < coreTextObject.RawText.Length; if (hasMoreToType) { showLetterTweener?.Stop(); diff --git a/Samples/FormsSampleProject/FormsSampleProject/FormsSampleProject.gluj b/Samples/FormsSampleProject/FormsSampleProject/FormsSampleProject.gluj index db7ce3eca..e942b97d3 100644 --- a/Samples/FormsSampleProject/FormsSampleProject/FormsSampleProject.gluj +++ b/Samples/FormsSampleProject/FormsSampleProject/FormsSampleProject.gluj @@ -99,6 +99,7 @@ "AspectRatioWidth": 16.0, "AspectRatioHeight": 9.0, "SupportLandscape": true, + "AllowWindowResizing": true, "Scale": 100, "ScaleGum": 100, "DominantInternalCoordinates": 1, diff --git a/Samples/FormsSampleProject/FormsSampleProject/Screens/MainMenu.cs b/Samples/FormsSampleProject/FormsSampleProject/Screens/MainMenu.cs index 0711ffb76..86fb3337e 100644 --- a/Samples/FormsSampleProject/FormsSampleProject/Screens/MainMenu.cs +++ b/Samples/FormsSampleProject/FormsSampleProject/Screens/MainMenu.cs @@ -54,20 +54,28 @@ void CustomInitialize() private async void HandleShowDialogButtonClicked(object sender, EventArgs e) { var dialog = new DialogBox(); - //dialog.LettersPerSecond = 24; - dialog.LettersPerSecond = null; + dialog.LettersPerSecond = 47; dialog.IsFocused = true; var dialogVisual = dialog.Visual; dialogVisual.XUnits = Gum.Converters.GeneralUnitType.PixelsFromMiddle; dialogVisual.XOrigin = RenderingLibrary.Graphics.HorizontalAlignment.Center; - dialogVisual.Y = 20; + dialogVisual.Y = -20; + dialogVisual.YUnits = Gum.Converters.GeneralUnitType.PixelsFromLarge; + dialogVisual.YOrigin = RenderingLibrary.Graphics.VerticalAlignment.Bottom; + await dialog.ShowAsync("This is a DialogBox in [Color=Red]FlatRedBall.Forms[/Color]. It supports " + + "lots of features including typing out the text [Color=Yellow]letter-by-letter[/Color], multiple pages, " + + "and even [Color=Green]styling[/Color] using BBCode. Wow, how handy! You can create mulitple " + + "pages by explicitly giving it a string array, or you can give it a long string and let the " + + "dialog box [Color=Pink]automatically[/Color] handle the multiple pages."); - await dialog.ShowAsync("This is some really long text. We want to show long text so that it " + - "line wraps and so that it has enough text to fill an entire page. The DialogBox control " + - "should automatically detect if the text is too long for a single page and it should break " + - "it up into multiple pages. You can advance this dialog by clicking on it with the mouse or " + - "by pressing the space bar on the keyboard"); + + + //await dialog.ShowAsync("This is [Color=Orange]some really[/Color] long [Color=Pink]text[/Color]. [Color=Purple]We[/Color] want to show long text so that it " + + // "line wraps [Color=Cyan]and[/Color] so that it has [Color=Green]enough[/Color] text to fill an [Color=Yellow]entire page[/Color]. The DialogBox control " + + // "should automatically detect if the text is too long for a single page and it should break " + + // "it up into multiple pages. You can advance this dialog by clicking on it with the [Color=Blue]mouse[/Color] or " + + // "by pressing the [Color=Gold]space bar[/Color] on the keyboard."); dialog.Visual.RemoveFromManagers(); }