local bubbleText = {}
local bubbleBorder = 25
local bubbleXoffset = 5
local screenBorder = 20
local timeStartText
function showBubble(text)
if Conditions["cond_option_speechbubbles"].ConditionValue == true then
if text.Owner:getId().tableId == eCharacters then
bubbleText[text:getId().id] = {Text = text.CurrentText, Owner = text.Owner:getName(), Time = text.TimeToWait} --missing WaitForAudio Check
timeStartText = getTime()
text.CurrentText = ""
if text.Background == false then
Interfaces["int_interactions"].Visible = false
end
end
end
end
function killBubble(text)
bubbleText[text:getId().id] = nil
if Conditions["cond_dont_show_menu"].ConditionValue == false and Conditions["cond_inventoryopen"].ConditionValue == false then
Interfaces["int_interactions"].Visible = true
end
end
registerEventHandler("textStarted","showBubble")
registerEventHandler("textStopped","killBubble")
function frameBubble()
for key, val in pairs(bubbleText) do
local char = Characters[val.Owner]
local pos = graphics.getCharacterTextPosition(char)
--graphics.font = char.Font
graphics.font = Fonts.font_Speechbubbles --use a default font for all chars
local txt = val.Text:gsub("","\n")
local dim = graphics.fontDimension(txt)
local t_Textlines = {}
for l in string.gmatch(txt,"[^\r\n]+") do
table.insert(t_Textlines, l)
end
local lines = #t_Textlines
dim.y = dim.y * lines
local txtTime = val.Time
pos.x = pos.x - game.ScrollPosition.x - (dim.x+bubbleBorder)/2
if pos.x game.WindowResolution.x - dim.x - bubbleBorder then pos.x = game.WindowResolution.x - dim.x - bubbleBorder - screenBorder end
pos.y = pos.y - game.ScrollPosition.y - (dim.y+bubbleBorder/2) - 20
if pos.y game.WindowResolution.y - dim.y - bubbleBorder then pos.y = game.WindowResolution.y - dim.y - bubbleBorder - screenBorder end
local bubbleSprite = graphics.loadFromFile("vispath:gfx/interface/simple_speechbubble.png")
local destRect = {x=pos.x-bubbleXoffset, y=pos.y, width=dim.x+bubbleBorder+bubbleXoffset*2, height=dim.y+bubbleBorder}
local nineRect = {x=20, y=20, width=30, height=30}
local pointer = nil
--draw bubble pointer
if char.Direction 300 then
pointer = graphics.loadFromFile("vispath:gfx/interface/simple_speechbubble_pointer_left.png")
pointer.position = {x = char.Position.x - game.ScrollPosition.x, y = pos.y + dim.y + 25}
elseif char.Direction > 120 and char.Direction < 240 then
pointer = graphics.loadFromFile("vispath:gfx/interface/simple_speechbubble_pointer_right.png")
pointer.position = {x = char.Position.x - game.ScrollPosition.x - 20, y = pos.y + dim.y + 25}
else
pointer = graphics.loadFromFile("vispath:gfx/interface/simple_speechbubble_pointer_mid.png")
pointer.position = {x = char.Position.x - game.ScrollPosition.x - 10, y = pos.y + dim.y + 25}
end
graphics.drawSpriteWithNineRect(bubbleSprite, destRect, nineRect, 0xffffff, 1.0)
graphics.drawSprite(pointer, 1.0, 0xffffff)
--graphics.drawFont(txt, math.floor( pos.x+bubbleBorder/2 + game.ScrollPosition.x ), math.floor( pos.y+bubbleBorder/2 + game.ScrollPosition.y ), 1.0)
--centred text
for i = 1, lines do
local dimLine = graphics.fontDimension(t_Textlines[i])
--print('line \"'..t_Textlines[i]..'\" is '..dimLine.x..' width')
graphics.drawFont(t_Textlines[i], math.floor ( pos.x + (dim.x - dimLine.x)/2 + bubbleBorder/2 ), math.floor ( ( pos.y - dimLine.y) + dimLine.y*i + bubbleBorder/2 ), 1.0)
end
--draw a line for text show time
local posLine = pos
posLine.x = pos.x + dim.x/2 + bubbleBorder/2
posLine.y = pos.y + dim.y + ( bubbleBorder/(3/2) )
local remainTime = 0
remainTime = txtTime - ( getTime() - timeStartText )
local timeOffset = remainTime / ( txtTime / ( dim.x/2 ) )
--graphics.drawFont(txt, math.floor( pos.x+bubbleBorder/2 + game.ScrollPosition.x ), math.floor( pos.y+bubbleBorder/2 + game.ScrollPosition.y ), 1.0)
graphics.drawLine(math.floor( posLine.x - timeOffset ), math.floor(posLine.y),math.floor( posLine.x + timeOffset ), math.floor(posLine.y), 0xffffff, 1.0)
end
end
graphics.addDrawFunc("frameBubble()", 1)
--script to break lines based on pixel width
function autoLineBreak(lbText, lbFont)
if not lbFont then lbFont = Fonts.default_Font end
graphics.font = lbFont
local dimOrg = graphics.fontDimension(lbText)
local dimSpace = lbFont.SpaceWidth
local width = lbFont.LineWidth
if width > game.WindowResolution.x then width = game.WindowResolution.x - 20 end
local t_textWords = {}
local dimLine = 0
lbText = lbText:gsub("<br/"..">"," <br/".." ") --necessary to easier find manual line breaks e.x. 'word<br/>word'
for w in string.gmatch(lbText,"[^ ]+") do
dimLine = dimLine + graphics.fontDimension(w).x
if w == "<br/"..">" then
dimLine = 0 - dimSpace
elseif dimLine >= width then
dimLine = graphics.fontDimension(w).x
table.insert(t_textWords,"<br/"..">")
elseif (dimLine - graphics.fontDimension(w).x) > 0 then
table.insert(t_textWords," ")
end
table.insert(t_textWords, w)
dimLine = dimLine + dimSpace
end
local newText = ""
for i=1, #t_textWords do
newText = newText..tostring( t_textWords[i] )
end
return newText
end
--speechbubble script
local bubbleText = {}
local bubbleBorder = 25
local bubbleXoffset = 5
local screenBorder = 20
local timeStartText
function showBubble(text)
if Conditions["cond_option_speechbubbles"].ConditionValue == true then
if text.Owner:getId().tableId == eCharacters then
bubbleText[text:getId().id] = {Text = text.CurrentText, Owner = text.Owner:getName(), Time = text.TimeToWait}
local countL = 0
local textLines = graphics.performLinebreaks(text.CurrentText)
for _ in pairs(textLines) do countL = countL + 1 end
timeStartText = getTime()
text.CurrentText = ""
if text.Background == false then
Interfaces["int_interactions"].Visible = false
end
end
end
end
function killBubble(text)
bubbleText[text:getId().id] = nil
if Conditions["cond_dont_show_menu"].ConditionValue == false and Conditions["cond_inventoryopen"].ConditionValue == false then
Interfaces["int_interactions"].Visible = true
end
end
registerEventHandler("textStarted","showBubble")
registerEventHandler("textStopped","killBubble")
function frameBubble()
for key, val in pairs(bubbleText) do
local char = Characters[val.Owner]
local pos = graphics.getCharacterTextPosition(char)
--graphics.font = char.Font
local bFont = Fonts.font_Speechbubbles --use a default font for al chars
graphics.font = bFont
local txt = val.Text
txt = autoLineBreak(txt, bFont) --custom auto breakline function
txt = txt:gsub("<br/"..">","\n")
local dim = graphics.fontDimension(txt)
local t_Textlines = {}
for l in string.gmatch(txt,"[^\r\n]+") do
table.insert(t_Textlines, l)
end
local lines = #t_Textlines
dim.y = dim.y * lines
--print('text width: '..dim.x)
if dim.x game.WindowResolution.x - dim.x - bubbleBorder then pos.x = game.WindowResolution.x - dim.x - bubbleBorder - screenBorder end
pos.y = pos.y - game.ScrollPosition.y - (dim.y+bubbleBorder/2) - 20
if pos.y game.WindowResolution.y - dim.y - bubbleBorder then pos.y = game.WindowResolution.y - dim.y - bubbleBorder - screenBorder end
local bubbleSprite = graphics.loadFromFile("vispath:gfx/interface/simple_speechbubble.png")
local destRect = {x=pos.x-bubbleXoffset, y=pos.y, width=dim.x+bubbleBorder+bubbleXoffset*2, height=dim.y+bubbleBorder}
local nineRect = {x=20, y=20, width=30, height=30}
local pointer = nil
--draw bubble pointer
if char.Direction 300 and dim.x > 60 then
pointer = graphics.loadFromFile("vispath:gfx/interface/simple_speechbubble_pointer_left.png")
pointer.position = {x = char.Position.x - game.ScrollPosition.x, y = pos.y + dim.y + 25}
elseif char.Direction > 120 and char.Direction 60 then
pointer = graphics.loadFromFile("vispath:gfx/interface/simple_speechbubble_pointer_right.png")
pointer.position = {x = char.Position.x - game.ScrollPosition.x - 20, y = pos.y + dim.y + 25}
else
pointer = graphics.loadFromFile("vispath:gfx/interface/simple_speechbubble_pointer_mid.png")
pointer.position = {x = char.Position.x - game.ScrollPosition.x - 10, y = pos.y + dim.y + 25}
end
graphics.drawSpriteWithNineRect(bubbleSprite, destRect, nineRect, 0xffffff, 1.0)
graphics.drawSprite(pointer, 1.0, 0xffffff)
--graphics.drawFont(txt, math.floor( pos.x+bubbleBorder/2 + game.ScrollPosition.x ), math.floor( pos.y+bubbleBorder/2 + game.ScrollPosition.y ), 1.0)
--centred text
for i = 1, lines do
local dimLine = graphics.fontDimension(t_Textlines[i])
--print('line \"'..t_Textlines[i]..'\" is '..dimLine.x..' width')
graphics.drawFont(t_Textlines[i], math.floor ( pos.x + (dim.x - dimLine.x)/2 + bubbleBorder/2 + game.ScrollPosition.x ), math.floor ( ( pos.y - dimLine.y) + dimLine.y*i + bubbleBorder/2 + game.ScrollPosition.y), 1.0)
end
--draw a line for text show time
local posLine = pos
posLine.x = pos.x + dim.x/2 + bubbleBorder/2
posLine.y = pos.y + dim.y + ( bubbleBorder/(3/2) )
local remainTime = 0
remainTime = txtTime - ( getTime() - timeStartText )
local timeOffset = remainTime / ( txtTime / ( dim.x/2 ) )
--graphics.drawFont(txt, math.floor( pos.x+bubbleBorder/2 + game.ScrollPosition.x ), math.floor( pos.y+bubbleBorder/2 + game.ScrollPosition.y ), 1.0)
graphics.drawLine(math.floor( posLine.x - timeOffset ), math.floor(posLine.y),math.floor( posLine.x + timeOffset ), math.floor(posLine.y), 0xffffff, 1.0)
end
end
graphics.addDrawFunc("frameBubble()", 1)