Last active
August 12, 2020 20:36
-
-
Save GWRon/d05798bb7a000bbcac979912f83bd170 to your computer and use it in GitHub Desktop.
Parse font text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Method __drawNew:SVec2i(txt:String,x:Float,y:Float, color:SBitmapFontColor, doDraw:Int=True) | |
Const commandCharCode:Int = Asc("|") | |
Const payloadCharCode:Int = Asc("=") | |
Const escapeCharCode:Int = Asc("\") | |
Local currentColor:SColor8 | |
Local hasCurrentColor:Int = False | |
Local colorBackup:SColor8 = New SColor8(0,0,0,255) | |
' retrieve current screen color | |
'GetColor(colorBackup) | |
Local stylesB:Int = 0 | |
Local stylesI:Int = 0 | |
Local stylesColors:SColor8[] | |
Local stylesColorsIndex:Int = 0 | |
Local fontChanged:Int = False | |
'currently in use font | |
' Local font:TBitmapFont = Self | |
Local command:String | |
For Local i:Int = 0 Until txt.length | |
Local charCode:Int = txt[i] | |
Local escaped:Int = False | |
' escaping command or escape char? | |
If charCode = escapeCharCode And i < txt.length - 2 And (txt[i+1] = commandCharCode Or txt[i+1] = escapeCharCode) | |
i :+ 1 | |
charCode = txt[i] | |
escaped = True | |
EndIf | |
' reading command? | |
' for now we ignore any escape chars within an active command | |
' so no "|b \|escaped stuff|bold text|/b|" -- this would fail! | |
If charCode = commandCharCode And Not escaped | |
' finished processing txt if last char is a command char | |
If i = txt.length - 1 Then Exit | |
' read command until next commandCharCode | |
' if encountering the "=" sign, read payload | |
Local j:Int = i | |
Local isPayload:Int = False | |
'Local payload:String | |
Local payloadStart:Int = -1 | |
Local payloadEnd:Int = -1 | |
While j < txt.length - 1 | |
j :+ 1 | |
If txt[j] = payloadCharCode | |
command = txt[i + 1 .. j] | |
i = j | |
isPayload = True | |
ElseIf txt[j] = commandCharCode | |
If isPayload | |
payloadStart = i + 1 | |
payloadEnd = j | |
'payload = txt[i + 1 .. j] | |
Else | |
command = txt[i + 1 .. j] | |
EndIf | |
Exit | |
EndIf | |
Wend | |
i = j | |
' handle command | |
' If payloadStart >= 0 | |
' Print "handle command: " + command + " payload: " + txt[payloadStart .. payloadEnd] 'payload | |
' Else | |
' Print "handle command: " + command + " payload: NONE" | |
' EndIf | |
Select command | |
Case "b" | |
stylesB :+ 1 | |
If stylesB = 1 Then fontChanged = True | |
Case "/b" | |
If stylesB = 1 Then fontChanged = True | |
stylesB = Max(stylesB - 1, 0) | |
Case "i" | |
stylesI :+ 1 | |
If stylesI = 1 Then fontChanged = True | |
Case "/i" | |
If stylesI = 1 Then fontChanged = True | |
stylesI = Max(stylesI - 1, 0) | |
Case "color" | |
'read colors | |
'(avoiding "split" command which creates more strings) | |
Local cStart:Int = payloadStart | |
Local cIndex:Int = 0 | |
Local StaticArray c:Byte[4]; c[3] = 255 'default to alpha = 1.0 | |
For Local ci:Int = payloadStart To payloadEnd | |
'inbetween | |
If txt[ci] = Asc(",") | |
c[cIndex] = Byte( txt[cStart .. ci+1] ) | |
cIndex :+ 1 | |
cStart = ci + 1 | |
'last element | |
ElseIf ci = payloadEnd | |
c[cIndex] = Byte( txt[cStart ..] ) | |
EndIf | |
If cIndex >= 4 Then Exit | |
Next | |
If Not hasCurrentColor Or (currentColor.r <> c[0] Or currentColor.g <> c[1] Or currentColor.b <> c[2] Or currentColor.a <> c[3]) | |
currentColor = New SColor8(c[0], c[1], c[2], c[3]) | |
hasCurrentColor = True | |
stylesColorsIndex :+ 1 | |
If stylesColorsIndex >= stylesColors.length | |
stylesColors = stylesColors[.. stylesColorsIndex + 1] | |
EndIf | |
stylesColors[stylesColorsIndex] = currentColor | |
fontChanged = True | |
EndIf | |
Case "/color" | |
If stylesColorsIndex >= 1 | |
stylesColorsIndex = stylesColorsIndex - 1 | |
currentColor = stylesColors[stylesColorsIndex] | |
fontChanged = True | |
Else | |
' fall back to initial color | |
currentColor = colorBackup | |
hasCurrentColor = False | |
EndIf | |
End Select | |
' received char to render | |
Else | |
If fontChanged | |
' font = GetBitmapFont(font.id, stylesB > 0, styl | |
fontChanged = False | |
' Print "Fetch font: color="+currentColor.r+","+currentColor.g+","+currentColor.b+","+currentColor.a+ " bold="+(stylesB>0) + " italic="+(stylesI>0) | |
EndIf | |
' DrawChar(charCode | |
' Print "drawing: " + Chr(charCode) | |
EndIf | |
Next | |
End Method | |
'---------- | |
Struct SBitmapFontColor | |
Field ReadOnly r:Int | |
Field ReadOnly g:Int | |
Field ReadOnly b:Int | |
Field ReadOnly a:Float | |
Global WHITE:SBitmapFontColor = New SBitmapFontColor(255,255,255, 1.0) | |
Method New(r:Int, g:Int, b:Int, a:Float) | |
self.r = r | |
self.g = g | |
self.b = b | |
self.a = a | |
End Method | |
Function FromScreen:SBitmapFontColor() | |
Local r:Int, g:Int, b:Int | |
GetColor(r, g, b) | |
Return new SBitmapFontColor(r,g,b, GetAlpha()) | |
End Function | |
Function FromTColor:SBitmapFontColor(c:TColor) | |
If Not c | |
Return new SBitmapFontColor(255,255,255, 1.0) | |
Else | |
Return new SBitmapFontColor(c.r,c.g,c.b, c.a) | |
EndIf | |
End Function | |
Method Copy:SBitmapFontColor() | |
Return new SBitmapFontColor(r,g,b,a) | |
End Method | |
Method SetScreenRGBA() | |
SetColor r,g,b | |
SetAlpha a | |
End Method | |
End Struct |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment