Firstly I should mention that I didn't write this code but did rewrite it a little when trying to convert it for OryUI. Credit for this goes to Virtual Nomad.
I was going to add this to OryUI and still hope to, but it was more difficult then expected because OryUI has a wrap text function which breaks text strings up across multiple lines, and that unfortunately destroys the BBCode formatting. Needs a rethink at a later date.
That aside I think it should still be shared as you've probably wanted an easier way to colour and bold characters in the middle of a string at some point.
That is all this code does, colours and bold characters with the tags [b] Bold [ / b] (without the spaces) and [c=#ff0000]Red Text[/c] or [c=255,0,0,100]Red Semi Transparent Text[/c].
I've updated the code to work with the OryUI color convert function which lets you type colours as hex or rgb. That function is included too.
As you can see in the code you can use the ConvertBBCode function (feel free to rename it) to create text, and update text. The update version is in the main do loop and happens when you press the space bar in the example below.
SetDisplayAspect(-1)
SetScissor(0, 0, 0, 0)
SetSyncRate(30, 0)
SetWindowSize(390, 845, 0)
UseNewDefaultFonts(1)
Sync()
type typeBBData
bold as integer
changedBold as integer
changedColor as integer
color# as integer[4]
endtype
BBText as integer : BBText = ConvertBBCode(0, "Default [b][c=#FF0000]Bold Red[/c][/b] [b]Bold Default[/b] [c=#FFFF00]Yellow[/c]")
SetTextSize(BBText, 3)
SetTextAlignment(BBText, 1)
SetTextPosition(BBText, 50, 50)
do
if (GetRawKeyPressed(32))
ConvertBBCode(BBText, "AAAA [b][c=255,100,200]BBBB[/c][/b] [b]CCCC[/b] [c=#EDD46F]DDDD[/c]")
endif
sync()
loop
function ConvertBBCode(outputTextID as integer, inputString$ as string)
local bbData as typeBBData[] : bbData.length = -1 : bbData.length = len(inputString$)
local boldFlag as integer
local color# as float[4]
local colorFlag as integer
local currentChar$ as string
local currentIndex as integer : currentIndex = 1
local distanceToEndTag as integer
local endOfTagIndex as integer
local i as integer
local inputLength as integer : inputLength = len(inputString$)
local lastIndex as integer
local outputString$ as string
repeat
currentChar$ = mid(inputString$, currentIndex, 1)
if (mid(lower(inputString$), currentIndex, 3) = "[b]")
boldFlag = 1
currentIndex = currentIndex + 3
continue
endif
if (mid(lower(inputString$), currentIndex, 4) = "[/b]")
boldFlag = 0
currentIndex = currentIndex + 4
continue
endif
if (mid(lower(inputString$), currentIndex, 3) = "[c=")
endOfTagIndex = FindString(inputString$, "]", 1, currentIndex)
if (endOfTagIndex >= 0)
distanceToEndTag = endOfTagIndex - currentIndex - 3
colorFlag = 1
color# = OryUIConvertColor(mid(inputString$, currentIndex + 3, distanceToEndTag))
currentIndex = currentIndex + distanceToEndTag + 4
continue
endif
endif
if (mid(lower(inputString$), currentIndex, 4) = "[/c]")
colorFlag = 0
currentIndex = currentIndex + 4
continue
endif
outputString$ = outputString$ + currentChar$
lastIndex = len(outputString$) - 1
if (boldFlag = 1)
bbData[lastIndex].bold = 1
bbData[lastIndex].changedBold = 1
endif
if (colorFlag = 1)
bbData[lastIndex].color#[1] = color#[1]
bbData[lastIndex].color#[2] = color#[2]
bbData[lastIndex].color#[3] = color#[3]
bbData[lastIndex].color#[4] = 255
bbData[lastIndex].changedColor = 1
endif
inc currentIndex
until currentIndex > inputLength
if (outputTextID = 0)
outputTextID = CreateText(outputString$)
else
SetTextString(outputTextID, outputString$)
SetTextColor(outputTextID, GetTextColorRed(outputTextID), GetTextColorGreen(outputTextID), GetTextColorBlue(outputTextID), GetTextColorAlpha(outputTextID))
endif
for i = 0 to bbData.length
if (bbData[i].changedBold = 1)
SetTextCharBold(outputTextID,i, bbData[i].bold)
endif
if (bbData[i].changedColor = 1)
SetTextCharColor(outputTextID, i, bbData[i].color#[1], bbData[i].color#[2], bbData[i].color#[3], bbData[i].color#[4])
endif
next
endfunction outputTextID
function OryUIConvertColor(oryUIColor$ as string)
local oryUICommaCount as integer
local oryUIHexInt as integer[6]
local oryUIRGBA# as float[4] : oryUIRGBA#[1] = 255 : oryUIRGBA#[2] = 255 : oryUIRGBA#[3] = 255 : oryUIRGBA#[4] = 255
oryUICommaCount = CountStringTokens(oryUIColor$, ",")
if (oryUICommaCount = 1)
if (FindString(oryUIColor$, "#") > 0)
oryUIColor$ = ReplaceString(oryUIColor$, "#", "", -1)
if (len(oryUIColor$) = 3)
oryUIRGBA#[1] = val(mid(oryUIColor$, 1, 1) + mid(oryUIColor$, 1, 1), 16)
oryUIRGBA#[2] = val(mid(oryUIColor$, 2, 1) + mid(oryUIColor$, 2, 1), 16)
oryUIRGBA#[3] = val(mid(oryUIColor$, 3, 1) + mid(oryUIColor$, 3, 1), 16)
elseif (len(oryUIColor$) = 6)
oryUIRGBA#[1] = val(mid(oryUIColor$, 1, 2), 16)
oryUIRGBA#[2] = val(mid(oryUIColor$, 3, 2), 16)
oryUIRGBA#[3] = val(mid(oryUIColor$, 5, 2), 16)
endif
else
oryUIRGBA#[1] = GetColorRed(val(oryUIColor$))
oryUIRGBA#[2] = GetColorGreen(val(oryUIColor$))
oryUIRGBA#[3] = GetColorBlue(val(oryUIColor$))
endif
elseif (oryUICommaCount >= 3)
oryUIRGBA#[1] = valFloat(GetStringToken(oryUIColor$, ",", 1))
oryUIRGBA#[2] = valFloat(GetStringToken(oryUIColor$, ",", 2))
oryUIRGBA#[3] = valFloat(GetStringToken(oryUIColor$, ",", 3))
if (oryUICommaCount = 4) then oryUIRGBA#[4] = valFloat(GetStringToken(oryUIColor$, ",", 4))
endif
endfunction oryUIRGBA#
I've lifted this from an existing project so hopefully it's all present and works. Let me know if not.
OryUI - A WIP AGK2 UI Framework