Skip to content

Commit

Permalink
Improved string handling and centered patched intro texts
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrdacor committed Sep 8, 2023
1 parent 1f14882 commit b79b9ef
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
28 changes: 26 additions & 2 deletions Ambermoon.Data.Legacy/Serialization/IntroData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Ambermoon.Data.Legacy.ExecutableData;
using Ambermoon.Data.Serialization;
using static Ambermoon.Data.Legacy.Serialization.AmigaExecutable;

Expand All @@ -16,7 +17,7 @@ internal class IntroTwinlakeImagePart : IIntroTwinlakeImagePart
internal readonly struct TextCommand : IIntroTextCommand
{
public IntroTextCommandType Type { get; private init; }
public int[] Args { get; private init; }
public int[] Args { get; internal init; }

internal static bool TryParse(IDataReader dataReader, List<string> texts, out TextCommand? textCommand)
{
Expand Down Expand Up @@ -564,6 +565,7 @@ Graphic ReadFrame()
var textCommandGroups = new List<int[]>(commandCount);
var currentTextCommandGroup = new List<int>(3);
int index = 0;
int firstWhiteTextCommandIndex = -1;

foreach (var textCommand in textCommands)
{
Expand All @@ -579,6 +581,15 @@ Graphic ReadFrame()
currentTextCommandGroup.Clear();
}
}
else if (firstWhiteTextCommandIndex == -1 &&
textCommand.Type == IntroTextCommandType.SetTextColor &&
textCommand.Args[0] == 0x0ccc)
{
firstWhiteTextCommandIndex = index;

while (firstWhiteTextCommandIndex > 0 && textCommands[firstWhiteTextCommandIndex - 1].Type != IntroTextCommandType.Clear)
--firstWhiteTextCommandIndex;
}

++index;
}
Expand All @@ -595,7 +606,20 @@ Graphic ReadFrame()
for (int t = 0; t < commandTexts.Length; ++t)
{
if (t < textCommandGroups[i].Length)
textCommandTexts[textCommands[textCommandGroups[i][t]].Args[2]] = commandTexts[t];
{
int commandIndex = textCommandGroups[i][t];
var command = (TextCommand)textCommands[commandIndex];
string commandText = new string(commandTexts[t].Normalize().Where(ch => ch == ' ' || glyphs.ContainsKey(ch)).ToArray());
textCommandTexts[command.Args[2]] = commandText;

if (commandText.Length != 0 && firstWhiteTextCommandIndex != -1 && commandIndex >= firstWhiteTextCommandIndex)
{
var args = command.Args;
int width = commandText.Sum(ch => ch == ' ' ? 6 : glyphs[ch].Advance);
args[0] = 160 - width / 2;
textCommands[commandIndex] = command with { Args = args };
}
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions Ambermoon.Data.Legacy/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ IEnumerable<byte> CharToGlyph(char ch, bool rune, char? fallbackChar = null)
foreach (var glyph in CharToGlyph(fallbackChar.Value, rune))
yield return glyph;
}
else if (ch.ToString().Normalize().Any(c => c > 32 && c < 128))
{
var glyph = CharToGlyph(ch.ToString().Normalize().First(c => c > 32 && c < 128), rune, ' ').First();

if (glyph == (byte)SpecialGlyph.SoftSpace)
throw new AmbermoonException(ExceptionScope.Data, $"Unsupported text character '{ch}'.");

yield return glyph;
}
else
throw new AmbermoonException(ExceptionScope.Data, $"Unsupported text character '{ch}'.");
}
Expand Down
1 change: 1 addition & 0 deletions Ambermoon.net/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ Graphic LoadGraphic(int width, int height)
public Text CreateText(IRenderView renderView, Layer layer, Rect area, string text,
byte displayLayer, TextAlign textAlign = TextAlign.Center, byte alpha = 255, Rect clipArea = null)
{
text = new string(text.Normalize().Where(ch => ch == ' ' || glyphs.ContainsKey(upperOnly ? char.ToUpper(ch) : ch)).ToArray());
var renderText = new Text(renderView, layer, text, glyphs, characters, displayLayer, spaceWidth, upperOnly,
textureAtlasIndexOffset, alpha, clipArea);
renderText.Place(area, textAlign);
Expand Down
4 changes: 2 additions & 2 deletions Ambermoon.net/Intro.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ public IntroActionLogoFlyin(IntroGraphic introGraphic, IntroActionType actionTyp

if (actionType == IntroActionType.ThalionLogoFlyIn)
{
textClipArea = new Rect(100, 160, 220, 0);
presentsText = largeFont.CreateText(renderView, Layer.IntroText, new Rect(100, 160, 220, 22), introData.Texts[IntroText.Presents], 20, TextAlign.Left, 255, textClipArea);
textClipArea = new Rect(0, 160, 320, 0);
presentsText = largeFont.CreateText(renderView, Layer.IntroText, new Rect(0, 160, 320, 22), introData.Texts[IntroText.Presents], 20, TextAlign.Center, 255, textClipArea);
presentsText.Visible = false;
}
}
Expand Down

0 comments on commit b79b9ef

Please sign in to comment.