[Script] Line Scroller
Posted: October 10th, 2011, 8:03 pm
This script allows for scrolling through multiple lines of text that are separated with line breaks (such as the Lyrics provied by the undocumented Lyrics option of the NowPlaying plugin).
Options:
Example: MeasureName=FirstMeasure|SecondMeasure|ThirdMeasure
Variables
These variables are created by the script.
Options:
- MeasureName
The name of the measure returning the text to be read by the script. - FileName
The complete path to a file to be read by the script. - Meter
The name of the meter used to display the text. - Lines
The number of lines to be displayed at a time. Defaults to 5. - Mode
When set to 1, the lines of text are returned to a single meter. When set to 2, the lines of text are sent to a number of meters equal to Lines. The meter names should be in the format Meter1,Meter2,Meter3...etc. Defaults to 1. - Characters
The width of each line in characters. Defaults to 30.
Example: MeasureName=FirstMeasure|SecondMeasure|ThirdMeasure
Variables
These variables are created by the script.
- Title
Either the name of the Measure or the name of the FileName without path or extension. - Lines
The number of lines captured by the script. - LinePos
A number between 0 and 1 in relation to the current scroll position. This variable can be used to create a scroll bar.
- Scroll up one line
!CommandMeasure "LuaMeasureName" "Scroll(-1)" - Scroll down one line
!CommandMeasure "LuaMeasureName" "Scroll(1)" - Scroll up one page
!CommandMeasure "LuaMeasureName" "Scroll(-PROPERTIES.Lines)" - Scroll down one page
!CommandMeasure "LuaMeasureName" "Scroll(PROPERTIES.Lines)" - Return to the first line
!CommandMeasure "LuaMeasureName" "LineNum=0 Draw()" - Scroll to the last line
!CommandMeasure "LuaMeasureName" "LineNum=#Lines-PROPERTIES.Lines Draw()" - Force the script to reload the text
!CommandMeasure "LuaMeasureName" "LoadLines()" - Switch to the Next Input
!CommandMeasure "LuaMeasureName" "ChangeInput(1)" - Switch to the Previous Input
!CommandMeasure "LuaMeasureName" "ChangeInput(-1)"
Code: Select all
--LineScroller v2.3 by Smurfier (smurfier20@gmail.com)
--This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.
PROPERTIES={
MeasureName='';
FileName='';
Lines=5;
Meter='';
Method=1;
Characters=30;
}
function Initialize()
--Initialize Variables
Version='LineScroller 2.3-'
Chars,PROPERTIES.Lines=tonumber(PROPERTIES.Characters),tonumber(PROPERTIES.Lines)
Text,OldText,Lines,LineNum,InputNum,Input='',' ',{},0,1,{}
--Create Input Database
for i=1,3 do Input[i]={} end
for word in string.gmatch(PROPERTIES.MeasureName,'[^|]+') do
table.insert(Input[1],'Measure')
table.insert(Input[2],word)
table.insert(Input[3],SKIN:GetMeasure(word))
end
for word in string.gmatch(PROPERTIES.FileName,'[^|]+') do
table.insert(Input[1],'File')
table.insert(Input[2],string.match(word,'.-([^\\]-)%.[^%.]+$'))
table.insert(Input[3],word)
end
end
function Update()
LoadLines()
return #Lines>0 and 'Success!' or 'Error!'
end
function Scroll(a)
if a<0 then
LineNum=(LineNum+a)<0 and 0 or LineNum+a
elseif a>0 then
LineNum=(LineNum+a)>(#Lines-PROPERTIES.Lines) and #Lines-PROPERTIES.Lines or LineNum+a
else
print(Version..'Error: Invalid Scroll Value!')
end
Draw()
end
function ChangeInput(a)
if a<0 then
InputNum=(InputNum+a)<1 and 1 or InputNum+a
elseif a>0 then
InputNum=(InputNum+a)>#Input[1] and #Input[1] or InputNum+a
else
print(Version..'Error: Invalid Input Value!')
end
LoadLines()
end
function Draw()
SKIN:Bang('!SetVariable LinePos '..(LineNum/#Lines))
if PROPERTIES.Method==1 then
SKIN:Bang('!SetOption '..PROPERTIES.Meter..' Text "'..(#Lines>0 and table.concat(Lines,'\n',LineNum+1,LineNum+PROPERTIES.Lines) or '')..'"')
else
for i=1,PROPERTIES.Lines do SKIN:Bang('!SetOption '..PROPERTIES.Meter..i..' Text "'..(Lines[i+LineNum] and Lines[i+LineNum] or '')..'"') end
end
end
function LoadLines()
--Load the text
if Input[1][InputNum]=='Measure' then
Text=Input[3][InputNum]:GetStringValue()
elseif Input[1][InputNum]=='File' then
local File=io.input(Input[3][InputNum],'r')
if File then Text=io.read('*all') else print(Version..'Error: File Read Error!') end
io.close(File)
else
print(Version..'Error: Input Error!')
end
--If text changed since last update, parse
if Text~=OldText then
Lines,LineNum={},0
--Add space between line breaks and parse
for word in string.gmatch(string.gsub(string.gsub(Text,'%"',''),'\n\n','\n \n'),'[^\n]+') do ParseLines(word) end
if string.len(Text)==0 then print(Version..'Error: No Text to Display!') end
OldText=Text
--Set Title and Lines
SKIN:Bang('!Execute [!SetVariable Title "'..(Input[2][InputNum] and Input[2][InputNum] or 'Input Error!')..'"][!SetVariable Lines '..#Lines..']')
Draw()
end
end
function ParseLines(input)
--No need to parse if less then width
if string.len(input)<=Chars then
table.insert(Lines,input)
--If spaces exist, split into words and concatenate within width
elseif string.match(input,'%s') then
local Words,i={},1
--Split words
for word in string.gmatch(input,'[^%s]+') do
--If word is longer than Chars, split.
for i=1,math.ceil(string.len(word)/Chars) do table.insert(Words,string.sub(word,1+(i-1)*Chars,i*Chars)) end
end
--Put the words back together into lines
repeat
local ThisLine=""
repeat
ThisLine=ThisLine..Words[i]..' '
i=i+1
until string.len(ThisLine..(Words[i] and Words[i] or ""))>=Chars or i>#Words
table.insert(Lines,ThisLine)
until i>#Words
--If greater than width and no spaces exist, break into lines
--This line only exists for performance.
else
for i=1,math.ceil(string.len(input)/Chars) do table.insert(Lines,string.sub(input,1+(i-1)*Chars,i*Chars)) end
end
end