DesktopX: Scripting Scrolling Text

From WinCustomize Wiki

Jump to: navigation, search


Scrolling Text
Original Author:sViz
Date Created:October 27, 2007
Application:DesktopX
Programs Used:DesktopX


Contents

[edit] Scrolling Text

This is a step-by-step tutorial on how to scroll an object.


[edit] STEP 1 - Making an object scroll

Create a text object. Create a new script. Type up the script below, and apply. You don’t have to type the comments.

(NOTE: The object will keep scrolling off the screen, don't panic.)

Image:Dx_ScrollTextScript1.PNG


[edit] STEP 2- Giving your object boundaries

Now we want to stop the object when it reaches the edge of the screen. It only requires a minor adjustment. Replace the OnTimer1 procedure with this one:

Image:Dx_ScrollTextScript2.PNG


[edit] STEP 3- Scrolling in a loop

We want to make the object go back to the starting position and keep scrolling in a loop. This time we'll declare the starting positions as variables and use them to set the object's position.

Dim startposX
Dim startposY

Sub Object_OnScriptEnter
	startposX = system.screenwidth/2
	startposY= system.screenheight/2
	'--Set the starting position in the middle of your screen.
	object.top = startposY
	object.left = startposX
	object.settimer 1, 10
End Sub

Sub Object_OnTimer1
	'--If object has reached screen edge,
	'--go back to starting position
	If object.left <= 0 Then
		Object.left = startposX
	Else
	'--Move object back 2 pixels everytime the timer fires
	object.left= object.left - 2
	End If
End Sub

[edit] STEP 4- Small scale scrolling

Now that we're getting the hang of the art of scrolling, let's downsize it to scroll in a smaller area. This is useful for RSS feeds or media player information.

Create another object. Call it "maskobject". Use a plain image. Make it 200 W x 30 H. Now position the text object inside the mask. Next, r-click on the textobject and set parent to maskobject.

You should have this:

Image:Dx_ScrollTextImg1.png

Okay, now for the script. We want to make it wait before starting to scroll. We need to make sure the text is completely out of view before starting over. We need to set a new 'start over' position.

Dim startposX
Dim startposY
Dim startoverpos

Sub Object_OnScriptEnter
	startposX = (desktopx.object("maskobject").width/2) - (object.width/2)
	startposY= (desktopx.object("maskobject").height/2) - (object.height/2)
	startoverpos= desktopx.object("maskobject").width
	'--Set the starting position at center of mask
	object.top = startposY
	object.left = startposX
	object.settimer 1, 2000
End Sub

'---2 second pause before scrolling
Sub Object_OnTimer1
	object.settimer 2, 10
	object.killtimer 1
End Sub

Sub Object_OnTimer2
	'--If object is completely out of view,
	'--move to start over position
	If object.right <= 0 Then
		Object.left = startoverpos
	Else
		'--Move object back 2 pixels everytime the timer fires
		object.left= object.left - 2
	End If
End Sub



[edit] STEP 5- Getting Creative

[edit] Pause Scrolling Each Lap

Here is a script that will pause the text just before it goes out of view. The start position is set to the left-most side of the mask. The text moves 1 pixel at time.

Dim startposX
Dim startposY
Dim startoverpos

Sub Object_OnScriptEnter
	startposX = 2
	startposY= 10
	startoverpos= desktopx.object("maskobject").width
	'--Set the starting position center of mask
	object.top = startposY
	object.left = startposX
	object.settimer 1, 10
End Sub

Sub Object_OnTimer1
	'--Stop scrolling when object reaches left-most side
	If object.left = 1 Then
		object.killtimer 1
		object.settimer 2, 2000
	End If
	'--If object is completely out of view,
	'--move to start over position
	If object.right <= 1 Then
		Object.left = startoverpos
	End If
	'--Move object back 1 pixel everytime the timer fires
	object.left= object.left - 1
End Sub

'---2 second pause before resuming scrolling
Sub Object_OnTimer2
	object.killtimer 2
	object.settimer 1, 10
End Sub

[edit] Scrolling a Group

You may have more than one text object you want to scroll along the same space. This was a challenge I came across while trying to make an RSS feed.

Create a maskobject, 200 W x 30 H. Create a text object.

Name your text object with the name ending in the number 1. My text object is called "info1" so when we clone it, the clones will be numbered accordingly (e.g. info2, info3 etc.). This is very important.

Make your text object’s parent the maskobject. Next, clone your text object as many times as you want. Group all of your text objects. Call the group "scrollinginfo".

You should have this:

Image:Dx_ScrollTextImg2.png

Now, we need to declare how many objects are in the group. We need to align our objects with equal spacing in between each text object. These scripts go into the “maskobject”

Dim startposX
Dim startposY
Dim spacing
Dim objcount

Sub Object_OnScriptEnter
	startposX = 2
	startposY= 8
	spacing= 50
	objcount= Desktopx.GroupObjects("scrollinginfo").count
	'--Set the starting position and align all objects in group
	For Each elem In Desktopx.GroupObjects("scrollinginfo")
		elem.top = startposY
	Next
	For x = 1 To objcount
		If x = 1 Then
			Desktopx.Object("info1").Left = 2
		Else
			Desktopx.Object("info" & x).left = Desktopx.Object("info" & x-1).right + spacing
		End If
	Next
	Object.SetTimer 2, 2000
End Sub

OnTimer2 will pause the scrolling. OnTimer1 will scroll the text and call the function to loop the text once it is out of view.

'---2 second pause before resuming scrolling
Sub Object_OnTimer2
	object.killtimer 2
	object.settimer 1, 10
End Sub

Sub Object_OnTimer1
	'--Move each object in the group
	For Each elem In Desktopx.GroupObjects("scrollinginfo")
		elem.left = elem.left - 1
		'--If an object has scrolled out of view call function to loop the object
		If elem.right < 0 Then
			Call LoopText
		'--Pause when an object reaches the left-most side
		'-This can be commented out if you don't want to pause
		ElseIf elem.left = startposX Then
			object.KillTimer 1
			object.SetTimer 2, 2000
		End If
	Next
End Sub

The LoopText function will check the position of each text object in a loop, identifying which one has scrolled out of view and repositioning it to the end of the line.

'---Function to identify object that has scrolled out of view and move it to end of the line
Function LoopText
	For x= 1 To objcount
		If x = 1 Then
		'--If first object in group has scrolled out of view, reposition it after the last object in group
			If desktopx.object("info" & x).right < 0 Then
				desktopx.Object("info" & x).left = desktopx.Object("info" & objcount).right + spacing
			End If
		Else
			'--If any other object in group has scrolled out of view, reposition it after the object that comes before it
			If desktopx.Object("info" & x).right < 0 Then
				desktopx.Object("info" & x).left = desktopx.Object("info" & x-1).right + spacing
			End If
		End If
	Next
End Function

[edit] Tips

None right now. :)

[edit] Further Reading