i want to properly render strings with bbcode and some animations. i.e. this string should be rendered as a red Hello World! There’s no pressing need for this, just wanted to try it this way. if i really cant figure it out I’ll go back to the function that takes alternating strings and colors, but imo bbcode would be a lot easier to read and use since im already used to it, plus it would be a lot more flexible than taking and checking the type of multiple function arguments
examplestring=“[color=#ff0032]Hello World![/color]”
Actual code below. Current way to displaying different colors is to set the colors table, similar to setting mesh.colors
-- texttest
-- Use this function to perform your initial setup
function setup()
test0 = coloredtext(teststring,100,HEIGHT/2,10,10,WIDTH/2)
test2=coloredtext('a short string', 300, 200, 10,16,200)
test2.nextshake=0.0 --causes it to bug out
for i in ipairs(test2.colors) do
test2.colors[i]=color(10*i,255-10*i,0)
end
end
coloredtext = class()
function coloredtext:init(s,x,y,w,h,wrapwidth)
self.colors = {}
self.positions = {}
self.strings = {}
self.timer = 0
self.nextshake = 1
self.shakedirection = 1
self.shakeindex = 1 --only used in coloredtext:update2()
local i = 1
local count = 0
local wrapx = x + wrapwidth
local newx, newy = x, y
for c in s:gmatch"." do
self.strings[i] = c
self.colors[i] = color(255)
newx = x + w * count
newy = newx > wrapx and newy - h or newy
count = newx > wrapx and 0 or count + 1
self.positions[i] = vec2(newx, newy)
i = i+1
end
end
function coloredtext:draw()
for i,v in ipairs(self.positions) do
fill(self.colors[i])
text(self.strings[i], v.x, v.y)
end
end
function coloredtext:update()
self.timer = self.timer + DeltaTime
if self.timer > self.nextshake then
local char = self.positions[math.random(#self.positions)]
local t1 = tween(0.025, char, {char.x-2, char.y+2})
local t2 = tween(0.025, char, {char.x, char.y})
tween.sequence(t1, t2)
self.timer = 0
end
end
--warning: setting self.nextshake very low results in repeated tweens being applied to the same character
--causing it to not properly reset in time for the next tween
function coloredtext:update2()
self.timer=self.timer+DeltaTime
if self.timer > self.nextshake then
local char = self.positions[self.shakeindex]
local t1 = tween(0.025, char, {char.x-2, char.y+2})
local t2 = tween(0.025, char, {char.x, char.y})
tween.sequence(t1, t2)
self.timer = 0
self.shakeindex=self.shakeindex+self.shakedirection
if self.shakeindex >= #self.positions then
self.shakedirection=-1
elseif self.shakeindex <= 1 then
self.shakedirection=1
end
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
test0:update() --this shakes random characters
test0:draw()
test2:update2() --shakes in order by index
test2:draw()
end
teststring =
"this is a very long string this is a very long stringthis is a very long string this is a very long stringthis is a very long string this is a very long stringthis is a very long string this is a very long string"