Skip to content

Commit

Permalink
Added dialog box to sample FRB forms project.
Browse files Browse the repository at this point in the history
Dialog box now breaks on pages.
  • Loading branch information
vchelaru committed Feb 17, 2024
1 parent 8547d00 commit 18b3677
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public override bool IsFocused
}

/// <summary>
/// The number of letters to show per second when printing out in "typewriter style". If null, 0, or negative, then the text will be shown immediately.
/// The number of letters to show per second when printing out in "typewriter style".
/// If null, 0, or negative, then the text is shown immediately.
/// </summary>
public int? LettersPerSecond { get; set; } = 20;

Expand Down Expand Up @@ -132,6 +133,7 @@ protected override void ReactToVisualChanged()

#endregion

#region Show Methods
/// <summary>
/// Shows the dialog box (adds it to managers and sets IsVisible to true) and begins showing the text.
/// </summary>
Expand Down Expand Up @@ -165,7 +167,12 @@ public void Show(IEnumerable<string> pages, FlatRedBall.Graphics.Layer frbLayer
/// </summary>
/// <param name="text">The text to print out, either immediately or letter-by-letter according to LettersPerSecond.</param>
/// <returns>A task which completes when the text has been displayed and the DialogBox has been dismissed.</returns>
public Task ShowAsync(string text) => ShowAsync(new string[] { text });
public Task ShowAsync(string text)
{
var pages = ConvertToPages(text);
return ShowAsync(pages);
}


public async Task ShowAsync(IEnumerable<string> pages, FlatRedBall.Graphics.Layer frbLayer = null)
{
Expand Down Expand Up @@ -368,6 +375,68 @@ 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;

if(!limitsLines)
{
// it can show any number of lines, it's up to the user to handle spillover
// by limiting the page length or by expanding the dialog box.
return new string[] { text };
}
else
{
var unlimitedLines = new List<string>();
var oldVerticalMode = this.coreTextObject.TextOverflowVerticalMode;
this.coreTextObject.TextOverflowVerticalMode = RenderingLibrary.Graphics.TextOverflowVerticalMode.SpillOver;
coreTextObject.RawText = text;
coreTextObject.UpdateLines(unlimitedLines);

this.coreTextObject.TextOverflowVerticalMode = oldVerticalMode;
this.textComponent.SetProperty("Text", text);

var limitedLines = coreTextObject.WrappedText;

if (unlimitedLines.Count == limitedLines.Count)
{
// no need to break it up
return new string[] { text };
}
else
{
var pages = new List<string>();

var absoluteLineNumber = 0;

var currentPage = new StringBuilder();
currentPage.Clear();

StringBuilder stringBuilder = new StringBuilder();
while(absoluteLineNumber < unlimitedLines.Count)
{
stringBuilder.Clear();

for(int i = 0; i < limitedLines.Count && absoluteLineNumber < unlimitedLines.Count; i++)
{
stringBuilder.Append(unlimitedLines[absoluteLineNumber]);
absoluteLineNumber++;
}
pages.Add(stringBuilder.ToString());
}

return pages.ToArray();
}
}


}

#endregion

#region Event Handler Methods

private void HandleClick(IWindow window)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-02-12T13:42:42.0957334Z
2024-02-17T17:18:42.7810060Z
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@
<Variable>
<Type>float</Type>
<Name>TextInstance.Height</Name>
<Value xsi:type="xsd:float">0</Value>
<Value xsi:type="xsd:float">-32</Value>
<SetsValue>true</SetsValue>
</Variable>
<Variable>
<Type>DimensionUnitType</Type>
<Name>TextInstance.Height Units</Name>
<Value xsi:type="xsd:int">4</Value>
<Value xsi:type="xsd:int">2</Value>
<SetsValue>true</SetsValue>
</Variable>
<Variable>
Expand All @@ -129,6 +129,12 @@
<Value xsi:type="xsd:string">This is a dialog box. This text will be displayed one character at a time. Typically a dialog box is added to a Screen such as the GameScreen, but it defaults to being invisible.</Value>
<SetsValue>true</SetsValue>
</Variable>
<Variable>
<Type>TextOverflowVerticalMode</Type>
<Name>TextInstance.TextOverflowVerticalMode</Name>
<Value xsi:type="xsd:int">1</Value>
<SetsValue>true</SetsValue>
</Variable>
<Variable>
<Type>float</Type>
<Name>TextInstance.Width</Name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,106 @@
"EventType": 7,
"TimestampUtc": "2024-02-12T13:42:41.0259382Z"
}
],
"Owner": [
{
"NewName": "MainMenuGum.ContainerInstance1",
"OldName": null,
"ElementType": "Screens",
"EventType": 5,
"TimestampUtc": "2024-02-17T13:16:58.849265Z"
},
{
"NewName": "MainMenuGum.SecondStack",
"OldName": "MainMenuGum.ContainerInstance1",
"ElementType": "Screens",
"EventType": 7,
"TimestampUtc": "2024-02-17T13:17:04.3539667Z"
},
{
"NewName": "MainMenuGum.ButtonStandardInstance1",
"OldName": null,
"ElementType": "Screens",
"EventType": 5,
"TimestampUtc": "2024-02-17T13:18:49.0225153Z"
},
{
"NewName": "MainMenuGum.ButtonStandardInstance1",
"OldName": "MainMenuGum.ButtonStandardInstance1",
"ElementType": "Screens",
"EventType": 7,
"TimestampUtc": "2024-02-17T13:19:26.0550836Z"
},
{
"NewName": "MainMenuGum.ShowDialogButton",
"OldName": "MainMenuGum.ButtonStandardInstance1",
"ElementType": "Screens",
"EventType": 7,
"TimestampUtc": "2024-02-17T13:19:31.3723099Z"
},
{
"NewName": "MainMenuGum.TextInstance",
"OldName": null,
"ElementType": "Screens",
"EventType": 5,
"TimestampUtc": "2024-02-17T13:30:44.7249579Z"
},
{
"NewName": null,
"OldName": "MainMenuGum.Text TextInstance in MainMenuGum",
"ElementType": "Screens",
"EventType": 6,
"TimestampUtc": "2024-02-17T13:46:43.7318549Z"
},
{
"NewName": "TestScreen",
"OldName": null,
"ElementType": "Screens",
"EventType": 0,
"TimestampUtc": "2024-02-17T14:25:22.4718202Z"
},
{
"NewName": "TestScreen.ColoredRectangleInstance",
"OldName": null,
"ElementType": "Screens",
"EventType": 5,
"TimestampUtc": "2024-02-17T14:25:25.9520485Z"
},
{
"NewName": "TestScreen.ColoredRectangleInstance1",
"OldName": null,
"ElementType": "Screens",
"EventType": 5,
"TimestampUtc": "2024-02-17T14:25:27.7887566Z"
},
{
"NewName": "TestScreen.BlueRectangle",
"OldName": "TestScreen.ColoredRectangleInstance1",
"ElementType": "Screens",
"EventType": 7,
"TimestampUtc": "2024-02-17T14:25:45.1195673Z"
},
{
"NewName": "TestScreen.WhiteRectangle",
"OldName": "TestScreen.ColoredRectangleInstance",
"ElementType": "Screens",
"EventType": 7,
"TimestampUtc": "2024-02-17T14:25:50.2923384Z"
},
{
"NewName": "MainMenuGum.TextInstance",
"OldName": null,
"ElementType": "Screens",
"EventType": 5,
"TimestampUtc": "2024-02-17T16:58:12.0981079Z"
},
{
"NewName": null,
"OldName": "MainMenuGum.Text TextInstance in MainMenuGum",
"ElementType": "Screens",
"EventType": 6,
"TimestampUtc": "2024-02-17T17:18:41.945088Z"
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Version>1</Version>
<DefaultCanvasWidth>800</DefaultCanvasWidth>
<DefaultCanvasHeight>600</DefaultCanvasHeight>
<ShowOutlines>true</ShowOutlines>
<ShowOutlines>false</ShowOutlines>
<ShowCanvasOutline>true</ShowCanvasOutline>
<ShowRuler>true</ShowRuler>
<ShowCheckerBackground>true</ShowCheckerBackground>
Expand All @@ -21,6 +21,11 @@
<ElementType>Screen</ElementType>
<LinkType>ReferenceOriginal</LinkType>
</ScreenReference>
<ScreenReference>
<Name>TestScreen</Name>
<ElementType>Screen</ElementType>
<LinkType>ReferenceOriginal</LinkType>
</ScreenReference>
<ComponentReference>
<Name>Controls/ButtonClose</Name>
<ElementType>Component</ElementType>
Expand Down
Loading

0 comments on commit 18b3677

Please sign in to comment.