1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-31 20:33:38 +02:00

Allow for tokens to be partially visible.

This commit is contained in:
C272 2022-02-07 14:20:48 +00:00
parent 2dcd5c3fa7
commit 5513738905
3 changed files with 31 additions and 2 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
.idea
.vs
bin
obj
packages

View file

@ -33,6 +33,22 @@ namespace MLEM.Formatting {
/// </summary>
public string DisplayString => this.ModifiedSubstring ?? this.Substring;
/// <summary>
/// The range of characters that are visible within this token, based on <see cref="DisplayString"/> indices.
/// First value is the starting index of the visible range, second is the length.
/// </summary>
public Point DisplayVisibleRange { get; set; }
/// <summary>
/// Whether this token is entirely visible or not.
/// </summary>
public bool FullyVisible
{
get => DisplayVisibleRange.X <= 0 && DisplayVisibleRange.X + DisplayVisibleRange.Y >= DisplayString.Length;
set
{
DisplayVisibleRange = value ? new Point(0, DisplayString.Length) : Point.Zero;
}
}
/// <summary>
/// The <see cref="DisplayString"/>, but split at newline characters
/// </summary>
public string[] SplitDisplayString { get; internal set; }
@ -50,6 +66,7 @@ namespace MLEM.Formatting {
this.RawIndex = rawIndex;
this.Substring = substring;
this.RawSubstring = rawSubstring;
this.FullyVisible = true;
foreach (var code in appliedCodes)
code.Token = this;
@ -107,12 +124,18 @@ namespace MLEM.Formatting {
/// <param name="c">The character to draw</param>
/// <param name="cString">A single-character string that contains the character to draw</param>
/// <param name="indexInToken">The index within this token that the character is at</param>
/// <param name="displayIndex">The literal index within the token's display string this character is at.</param>
/// <param name="pos">The position to draw the token at</param>
/// <param name="font">The font to use to draw</param>
/// <param name="color">The color to draw with</param>
/// <param name="scale">The scale to draw at</param>
/// <param name="depth">The depth to draw at</param>
public void DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
public void DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, int displayIndex, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
//If display index not within visible span, we don't draw.
if (displayIndex < DisplayVisibleRange.X || displayIndex >= DisplayVisibleRange.X + DisplayVisibleRange.Y)
return;
foreach (var code in this.AppliedCodes) {
if (code.DrawCharacter(time, batch, c, cString, indexInToken, ref pos, font, ref color, ref scale, depth))
return;

View file

@ -117,6 +117,8 @@ namespace MLEM.Formatting {
var token = this.Tokens[t];
var drawFont = token.GetFont(font);
var drawColor = token.GetColor(color);
int literalIndex = 0;
for (var l = 0; l < token.SplitDisplayString.Length; l++) {
var line = token.SplitDisplayString[l];
for (var i = 0; i < line.Length; i++) {
@ -125,13 +127,16 @@ namespace MLEM.Formatting {
token.DrawSelf(time, batch, pos + innerOffset, drawFont, color, scale, depth);
var cString = c.ToCachedString();
token.DrawCharacter(time, batch, c, cString, i, pos + innerOffset, drawFont, drawColor, scale, depth);
token.DrawCharacter(time, batch, c, cString, i, literalIndex, pos + innerOffset, drawFont, drawColor, scale, depth);
innerOffset.X += drawFont.MeasureString(cString).X * scale;
literalIndex++;
}
// only split at a new line, not between tokens!
if (l < token.SplitDisplayString.Length - 1) {
innerOffset.X = token.InnerOffsets[l] * scale;
innerOffset.Y += font.LineHeight * scale;
literalIndex++;
}
}
}