mpv chat: Add character-wrap and rework scrolling row offset

This commit is contained in:
Etoh 2017-12-27 14:59:48 +00:00
parent 92a4e4e551
commit 35891f0f0c

View File

@ -5,7 +5,7 @@
local CANVAS_WIDTH = 1920 local CANVAS_WIDTH = 1920
local CANVAS_HEIGHT = 1080 local CANVAS_HEIGHT = 1080
local ROW_HEIGHT = 100 local ROW_HEIGHT = 100
local chat_format = "{\\fs50}{\an1}{\\q2}" local chat_format = "{\\fs50}{\an1}"
local max_scrolling_rows = 100 local max_scrolling_rows = 100
local MOVEMENT_PER_SECOND = 200 local MOVEMENT_PER_SECOND = 200
local TICK_INTERVAL = 0.01 local TICK_INTERVAL = 0.01
@ -17,6 +17,7 @@ local use_alpha_rows_for_chat = true
local MOOD_NEUTRAL = 0 local MOOD_NEUTRAL = 0
local MOOD_BAD = 1 local MOOD_BAD = 1
local MOOD_GOOD = 2 local MOOD_GOOD = 2
local WORDWRAPIFY_MAGICWORD = "{\\\\fscx0} {\\\\fscx100}"
local ALPHA_WARNING_TEXT_COLOUR = "FF00FF" -- RBG local ALPHA_WARNING_TEXT_COLOUR = "FF00FF" -- RBG
local HINT_TEXT_COLOUR = "00FFFF" -- RBG local HINT_TEXT_COLOUR = "00FFFF" -- RBG
@ -34,8 +35,8 @@ function format_scrolling(xpos, ypos, text)
return string.format(chat_message) return string.format(chat_message)
end end
function format_chatroom(xpos,ypos,text) function format_chatroom(text)
local chat_message = chat_format .. "{\\pos("..xpos..","..ypos..")}" .. text.."\n" local chat_message = chat_format .. text.."\n"
return string.format(chat_message) return string.format(chat_message)
end end
@ -128,31 +129,24 @@ function process_alert_osd()
local rowsCreated = 0 local rowsCreated = 0
local stringToAdd = "" local stringToAdd = ""
if alert_osd ~= "" and mp.get_time() - last_alert_osd_time < opts['alertTimeout'] and last_alert_osd_time ~= nil then if alert_osd ~= "" and mp.get_time() - last_alert_osd_time < opts['alertTimeout'] and last_alert_osd_time ~= nil then
local xpos = opts['chatLeftMargin']
local ypos
local messageColour local messageColour
if alert_osd_mood == MOOD_NEUTRAL then if alert_osd_mood == MOOD_NEUTRAL then
messageColour = "{\\1c&H"..NEUTRAL_ALERT_TEXT_COLOUR.."}" messageColour = "{\\1c&H"..NEUTRAL_ALERT_TEXT_COLOUR.."}"
elseif alert_osd_mood == MOOD_BAD then elseif alert_osd_mood == MOOD_BAD then
messageColour = "{\\1c&H"..BAD_ALERT_TEXT_COLOUR.."}" messageColour = "{\\1c&H"..BAD_ALERT_TEXT_COLOUR.."}"
elseif alert_osd_mood == MOOD_GOOD then elseif alert_osd_mood == MOOD_GOOD then
messageColour = "{\\1c&H"..GOOD_ALERT_TEXT_COLOUR.."}" messageColour = "{\\1c&H"..GOOD_ALERT_TEXT_COLOUR.."}"
end end
local messageString local messageString = wordwrapify_string(alert_osd)
local startRow = 0 local startRow = 0
local stringLeftToProccess = alert_osd if messageString ~= '' and messageString ~= nil then
while stringLeftToProccess ~= '' and stringLeftToProccess ~= nil do
local toDisplay local toDisplay
ypos = opts['chatTopMargin'] + ((startRow+rowsCreated)*opts['chatOutputFontSize'])
toDisplay, stringLeftToProccess = trim_string(stringLeftToProccess,opts['chatSplitMessageAt'])
rowsCreated = rowsCreated + 1 rowsCreated = rowsCreated + 1
messageString = messageColour..toDisplay messageString = messageColour..messageString
if stringToAdd ~= "" then if stringToAdd ~= "" then
stringToAdd = stringToAdd .. format_chatroom(xpos,ypos,messageString) stringToAdd = stringToAdd .. format_chatroom(messageString)
else else
stringToAdd = format_chatroom(xpos,ypos,messageString) stringToAdd = format_chatroom(messageString)
end end
end end
end end
@ -161,26 +155,17 @@ end
function process_notification_osd(startRow) function process_notification_osd(startRow)
local rowsCreated = 0 local rowsCreated = 0
local startRow = startRow
local stringToAdd = "" local stringToAdd = ""
if notification_osd ~= "" and mp.get_time() - last_notification_osd_time < opts['alertTimeout'] and last_notification_osd_time ~= nil then if notification_osd ~= "" and mp.get_time() - last_notification_osd_time < opts['alertTimeout'] and last_notification_osd_time ~= nil then
local xpos = opts['chatLeftMargin']
local messageColour local messageColour
messageColour = "{\\1c&H"..NOTIFICATION_TEXT_COLOUR.."}" messageColour = "{\\1c&H"..NOTIFICATION_TEXT_COLOUR.."}"
local messageString local messageString
local startRow = startRow messageString = wordwrapify_string(notification_osd)
local stringLeftToProccess = notification_osd messageString = messageColour..messageString
while stringLeftToProccess ~= '' and stringLeftToProccess ~= nil do messageString = format_chatroom(messageString)
local toDisplay stringToAdd = messageString
local ypos = opts['chatTopMargin'] + ((startRow+rowsCreated)*opts['chatOutputFontSize']) rowsCreated = 1
toDisplay, stringLeftToProccess = trim_string(stringLeftToProccess,opts['chatSplitMessageAt'])
rowsCreated = rowsCreated + 1
messageString = messageColour..toDisplay
if stringToAdd ~= "" then
stringToAdd = stringToAdd .. format_chatroom(xpos,ypos,messageString)
else
stringToAdd =format_chatroom(xpos,ypos,messageString)
end
end
end end
return rowsCreated, stringToAdd return rowsCreated, stringToAdd
end end
@ -214,18 +199,9 @@ end
function process_chat_item_chatroom(i, startRow) function process_chat_item_chatroom(i, startRow)
local text = chat_log[i].text local text = chat_log[i].text
if text ~= '' then if text ~= '' then
local xpos = opts['chatLeftMargin'] local text = wordwrapify_string(text)
local rowNumber = i+startRow-1 local rowNumber = i+startRow-1
local ypos = opts['chatTopMargin']+(rowNumber*opts['chatOutputFontSize']) return(format_chatroom(text))
local timecreated = chat_log[i].timecreated
local timedelta = 200 * (mp.get_time() - timecreated)
-- xpos = timedelta*500-25
if timedelta < 10 then
ypos = 5+ypos-timedelta
end
return(format_chatroom(xpos,ypos,text))
end end
end end
@ -334,14 +310,13 @@ opts = {
['chatInputFontColor'] = "#000000", ['chatInputFontColor'] = "#000000",
['chatInputPosition'] = "Top", ['chatInputPosition'] = "Top",
['MaxChatMessageLength'] = 50, ['MaxChatMessageLength'] = 50,
['chatSplitMessageAt'] = 70,
['chatOutputFontFamily'] = "sans serif", ['chatOutputFontFamily'] = "sans serif",
['chatOutputFontSize'] = 50, ['chatOutputFontSize'] = 50,
['chatOutputFontWeight'] = 1, ['chatOutputFontWeight'] = 1,
['chatOutputFontUnderline'] = false, ['chatOutputFontUnderline'] = false,
['chatOutputFontColor'] = "#FFFFFF", ['chatOutputFontColor'] = "#FFFFFF",
['chatOutputMode'] = "Chatroom", ['chatOutputMode'] = "Chatroom",
['scrollingFirstRowOffset'] = 3, ['scrollingFirstRowOffset'] = 2,
-- Can be "Chatroom", "Subtitle" or "Scrolling" style -- Can be "Chatroom", "Subtitle" or "Scrolling" style
['chatMaxLines'] = 7, ['chatMaxLines'] = 7,
['chatTopMargin'] = 25, ['chatTopMargin'] = 25,
@ -562,6 +537,31 @@ function trim_string(line,maxCharacters)
return str:sub(1,pos-1), str:sub(pos) return str:sub(1,pos-1), str:sub(pos)
end end
function wordwrapify_string(line)
-- Naive helper function to find the next UTF-8 character in 'str' after 'pos'
-- by skipping continuation bytes. Assumes 'str' contains valid UTF-8.
local str = line
if str == nil or str == "" then
return str, ""
end
newstr = ""
local currentChar = 0
local nextChar = 0
local chars = 0
local maxChars = str:len()
repeat
nextChar = next_utf8(str, currentChar)
if nextChar == currentChar then
return newstr
end
newstr = newstr .. WORDWRAPIFY_MAGICWORD .. str:sub(currentChar,nextChar-1)
currentChar = nextChar
until currentChar > maxChars
return newstr
end
function trim_input() function trim_input()
-- Naive helper function to find the next UTF-8 character in 'str' after 'pos' -- Naive helper function to find the next UTF-8 character in 'str' after 'pos'
@ -886,7 +886,7 @@ function set_syncplayintf_options(input)
--mp.command('print-text "<chat>'..option.."="..tostring(value).." - "..valueType..'</chat>"') --mp.command('print-text "<chat>'..option.."="..tostring(value).." - "..valueType..'</chat>"')
end end
chat_format = get_output_style() chat_format = get_output_style()
local vertical_output_area = CANVAS_HEIGHT-(opts['chatTopMargin']+opts['chatBottomMargin']) local vertical_output_area = CANVAS_HEIGHT-(opts['chatTopMargin']+opts['chatBottomMargin']+(opts['chatOutputFontSize']+opts['scrollingFirstRowOffset']))
max_scrolling_rows = math.floor(vertical_output_area/opts['chatOutputFontSize']) max_scrolling_rows = math.floor(vertical_output_area/opts['chatOutputFontSize'])
if opts['chatDirectInput'] == true then if opts['chatDirectInput'] == true then
add_repl_alpharow_bindings(alpharowbindings) add_repl_alpharow_bindings(alpharowbindings)