It is currently March 28th, 2024, 10:10 am

[Script] Line Scroller

Discuss the use of Lua in Script measures.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

[Script] Line Scroller

Post by smurfier »

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:
  • 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.
Note: Multiple inputs, including the use of both Files and Measures at the same time, can be used by using a pipe delimiter (ie |).
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.
Actions
  • 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
Last edited by smurfier on October 28th, 2011, 2:16 pm, edited 8 times in total.
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Re: [Script] Line Scroller

Post by AlC »

Hey

I tried it now and I can't bring it to working. It shows the number of lines that I input, but I can't scroll it and then I got some errors in the log:
for Down
ERROR: (00:03:47.188) Script: [string "ScrollUp"]:1: '=' expected near '<eof>'
ERROR: (00:03:47.188) Script: attempt to call a table value

for Up
ERROR: (00:04:24.266) Script: [string "ScrollUp"]:1: '=' expected near '<eof>'
ERROR: (00:04:24.266) Script: attempt to call a nil value
Here is a part of the testskin

Code: Select all

[LyricBar]
Meter=STRING
MeterStyle=Style
X=10R
Y=r
Text="Lyrics"
MouseOverAction=!Execute [!SetOption #CURRENTSECTION# FontColor #ColorMusicActive#][!UpdateMeter LyricBar][!Redraw]
MouseLeaveAction=!Execute [!SetOption #CURRENTSECTION# FontColor #ColorMusicNormal#][!UpdateMeter LyricBar][!Redraw]
LeftMouseDownAction=!Execute [!ToggleMeterGroup Lyric][!Redraw]
DynamicVariables=1
Group=Normal

.
.
.

[Lyric]
Meter=STRING
;MeasureName=mLyrics
X=210
Y=90
SolidColor=0,0,0,255
FontFace=Trebuchet MS
FontColor=255,255,255,255
Hidden=1
AntiAlias=1
ClipString=0
;Text="%1"
LeftMouseDownAction=!Execute [!ToggleMeter Lyric][!Redraw]
Group=Lyric

[LyricUP]
Meter=String
X=170
Y=90
SolidColor=0,0,0,1
Text=Up
LeftMouseDownAction=!Execute [!CommandMeasure "LineScrollerLua" "ScrollUp"]

[LyricDOWN]
Meter=String
X=170
Y=110
SolidColor=0,0,0,1
Text=Down
LeftMouseDownAction=!Execute [!CommandMeasure "LineScrollerLua" "ScrollDown"]

[LineScrollerLua]
Measure=Script
ScriptFile=#CURRENTPATH#LineScroller.lua
MeasureName=mLyrics
Lines=20
Mode=1
Meter=Lyric

....

[mLyrics]
Measure=Plugin
Plugin=NowPlaying.dll
PlayerName=[mPlayer]
PlayerType=LYRICS
Substitute="":"N\A"
Tried some combinations with !Commandmeasure ScrollUp and !Commandmeasure Draw

some help or a simple example skin would be nice

EDIT: Solved

The bangs must be :
LeftMouseDownAction=!Execute [!CommandMeasure "LineScrollerLua" "ScrollUp()"]
LeftMouseDownAction=!Execute [!CommandMeasure "LineScrollerLua" "ScrollDown()"]

Sorry, first time that I Work with !CommandMeasure

@smurfier: thanks for sharing the script
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: [Script] Line Scroller

Post by smurfier »

If you are not adding other commands, it's just:

LeftMouseDownAction=!CommandMeasure "LineScrollerLua" "ScrollUp()"
LeftMouseDownAction=!CommandMeasure "LineScrollerLua" "ScrollDown()"
User avatar
Seahorse
Posts: 1175
Joined: June 9th, 2010, 5:56 pm
Location: Locks heath, UK
Contact:

Re: [Script] Line Scroller

Post by Seahorse »

Smurfier,

any chance of combining with the marquee script or adding a word wrap, I don't want to use ClipString particularly, however, I'm not sure this is is a better option either, first song that's exceeded the 400 pixels I have allowed...
Capture.PNG
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: [Script] Line Scroller

Post by smurfier »

I am working on a solution thanks to another project. It's not perfect though. I will hopefully update later today.
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Re: [Script] Line Scroller

Post by AlC »

Hey smurfier,

in your current LineScroller script, it deletes all the blank lines. Is there a chance that you can make it so, so that the blank lines stay ?
Attachments
lyrics.png
LEFT: Lyrics with LineScroller

RIGHT: Just a meter with the lyrics measure
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: [Script] Line Scroller

Post by smurfier »

AlC wrote:Hey smurfier,

in your current LineScroller script, it deletes all the blank lines. Is there a chance that you can make it so, so that the blank lines stay ?
Not easily. The script actually removes all line breaks, then puts some of them back. I'm still working on having it properly cut long lines.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: [Script] Line Scroller

Post by smurfier »

Updated for line clipping, to preserve blank lines, and to allow parsing of files.

There is currently a bug where the number of lines parsed is not being returned. I have no idea why this is not working except for the wild theory that it has something to do with the number of functions that are being called. Any help on this front would be greatly appreciated. Fixed.
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Re: [Script] Line Scroller

Post by AlC »

Hey smurfier,

sorry I didn't have the time to test it, but at first thanks :)
Lines and Characters work well. And it display everything well.

But now I can't scroll or better said no CommandMeasure work. I don't know if the code is false, but with the old LineScroller script I can scroll up and down.
With the new I can do nothing.

Here is a part of the testskin

Code: Select all

[Lyric]
Meter=STRING
X=210
Y=90
SolidColor=0,0,0,150
FontColor=255,255,255,255
Hidden=1
LeftMouseDownAction=!Execute [!ToggleMeter Lyric][!Redraw]
Group=Lyric

[LyricUP]
Meter=String
X=170
Y=90
SolidColor=0,0,0,1
Text=Up
LeftMouseDownAction=!Execute [!CommandMeasure "LineScrollerLua" "ScrollUp()"]
FontColor=255,255,255,255

[LyricDOWN]
Meter=String
X=170
Y=150
SolidColor=0,0,0,1
Text=Down
LeftMouseDownAction=!Execute [!CommandMeasure "LineScrollerLua" "ScrollDown()"]
FontColor=255,255,255,255

[LineScrollerLua]
Measure=Script
ScriptFile=#CURRENTPATH#LineScroller.lua
MeasureName=mLyrics
Lines=40
Mode=1
Meter=Lyric
Characters=70

[mLyrics]
Measure=Plugin
Plugin=NowPlaying.dll
PlayerName=[mPlayer]
PlayerType=LYRICS
Substitute="":"N\A"
And I get no errors or warnings in the log, and the measure give as value Succes!

Any idea ??
User avatar
Seahorse
Posts: 1175
Joined: June 9th, 2010, 5:56 pm
Location: Locks heath, UK
Contact:

Re: [Script] Line Scroller

Post by Seahorse »

Scrolling is not working here either, line breaks are... :???:
Post Reply