I'm trying to use tags to color text inside a createtext item.
Not work as expected.
Could you help me ? Perhaps another logic ? The problem occur when i remove the tags (ReplaceString)
Here my code to try :
// Project: ColorText
// Created: 2018-01-28
SetErrorMode(2)
SetWindowTitle( "ColorText" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 )
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 )
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 )
#option_explicit
#Constant TRUE 1
#Constant FALSE 0
#Constant TEXT_RED 1
#Constant TEXT_GREEN 2
#Constant TEXT_BLUE 3
#Constant TEXT_BOLD 4
Global Sentence as String
Global MyText as Integer
Function ColorText (Text_id as Integer, Action as Integer)
Local Index as Integer
Local x as Integer
Local i as Integer
Local Text as String
Local bFind as Integer
Local Start_tag as String
Local End_tag as String
Local Zone_startX as Integer[]
Local Zone_endX as Integer[]
Local Nb_tags as Integer
Local Value as Integer
Select Action
Case TEXT_RED
Start_tag = "<r>"
End_tag = "</r>"
EndCase
Case TEXT_GREEN
Start_tag = "<g>"
End_tag = "</g>"
EndCase
Case TEXT_BLUE
Start_tag = "<b>"
End_tag = "</b>"
EndCase
Case TEXT_BOLD
Start_tag = "<t>"
End_tag = "</t>"
EndCase
EndSelect
Text = GetTextString (Text_id)
bFind = TRUE
Nb_tags = 0
x = 0
Index = 1
While bFind <> FALSE
bFind = FALSE
// Find start tag position -> Zone_StartX
x = FindString (Text, Start_tag, 1, Index)
if x <> 0
Value = x - Len(Start_tag) + 1
Zone_StartX.Insert (Value)
Index = x + 1
bFind = TRUE
//message (Start_tag + " from " + str(Index))
Endif
// Find end tag position -> Zone_EndX
x = FindString (Text, End_tag, 1, Index)
if x <> 0
Value = x - Len(End_tag)
Zone_EndX.Insert (Value )
Index = x + 1
Inc Nb_tags,1
bFind = TRUE
//message (End_tag + " to " + str(index))
Endif
EndWhile
Local j as Integer
// Set Char color (or bold) from Zone_StarX to Zone_EndX
Select Action
Case TEXT_RED
For i=0 To Nb_tags-1
For j = Zone_StartX[i] to Zone_EndX[i]
SetTextCharColor (Text_Id ,j , 255, 0, 0, 255)
Next
Next
EndCase
Case TEXT_GREEN
For i=0 To Nb_tags-1
For j=Zone_StartX[i] to Zone_EndX[i]
SetTextCharColor (Text_Id, j, 0, 255, 0, 255)
Next
Next
EndCase
Case TEXT_BLUE
For i=0 To Nb_tags-1
For j=Zone_StartX[i] to Zone_EndX[i]
SetTextCharColor (Text_Id, j, 0, 0, 255, 255)
Next
Next
EndCase
Case TEXT_BOLD
For i=0 To Nb_tags-1
For j=Zone_StartX[i] to Zone_EndX[i]
SetTextCharBold (Text_Id, j, TRUE)
Next
Next
EndCase
EndSelect
// Remove the <tag> and </tag> into the initial text.
Text = ReplaceString (Text, Start_tag,"",-1)
Text = ReplaceString (Text, End_tag,"",-1)
SetTextString (Text_Id, Text)
EndFunction
Sentence = "Hello everybody <r>This is red text</r>, now <b>this is blue text</b> and <g>this is green text</g>."
Sentence = Sentence + Chr(10) + "Also <t>bold text</t>."
MyText = CreateText (Sentence)
SetTextSize (MyText, 40)
SetTextMaxWidth (MyText, 400)
SetTextPosition (MyText, (GetDeviceWidth() - GetTextTotalWidth(MyText)) /2, (GetDeviceHeight() - GetTextTotalHeight(MyText)) /2)
SetTextColor (MyText, 255,255,255,255)
ColorText (MyText, TEXT_RED)
ColorText (MyText, TEXT_GREEN)
ColorText (MyText, TEXT_BLUE)
ColorText (MyText, TEXT_BOLD)
do
Print( ScreenFPS() )
Sync()
loop
Note : i've edited my previous thread so you can now try the code on your computer.