v1.2.0.1-beta1
Marquee Text Example

Scrolls long text horizontally with only a portion visible at one time.

See the published documentation for a properly formatted version of this README.

This script's main function, marquee(), takes a line of text as input and updates a Touch Portal State with a portion of that text visible (up to a specified maximum length), pauses for some time, then sends the next set of characters that should be visible. This creates a scrolling animation like a marquee.

marquee() takes some optional parameters to specify how quickly the text should scroll, how long to delay before starting, or re-starting, the scroll, and whether to restart the scroll after showing the full text once. See comments in the script itself for more details.

Note
This code can also be found in the project's repository at
https://github.com/mpaperno/DSEP4TP/tree/main/resources/examples/Marquee/
1/*
2This script is designed to run in a Private engine instance
3and will update the State Name of whichever action invoked the `marquee()` function.
4
5`marquee()` function parameters:
6 `text` - The full text to show.
7 `maxLength` - The maximum number of characters to show at one time.
8 If `text` length is <= `maxLength` then the whole text will be shown at once, with no scrolling.
9 `scrollDelay` - This is the scroll delay in milliseconds; it dictates how quickly the text scrolls.
10 `startDelay` - How many milliseconds to wait before (re)starting the scroll. This can create an extra pause at the start and at the end of the text.
11 `restartAtEnd` - Whether to restart the scroll animation after all the text has been shown once.
12*/
13var marquee = function(text, maxLength = 32, scrollDelay = 200, startDelay = 1000, restartAtEnd = true)
14{
15 // Make sure any currently running marquee animation is stopped.
16 marqueeStop();
17
18 // If text fits into maximum length then just return the text as-is
19 // (this automatically sends the correct State update to Touch Portal).
20 if (text.length <= maxLength)
21 return text;
22
23 // track the first character of the text to show in the scrolling marquee
24 var start = 0;
25
26 // This function sends the State update to TP with a portion of the full text.
27 function update()
28 {
29 // Bail out if animation stop has been requested.
30 if (!_data.runMarquee)
31 return;
32
33 // Get just the portion of the text to send this time.
34 const result = text.slice(start, start + maxLength);
35 // And send it.
36 TP.stateUpdate(result);
37
38 // Check if end of text is visible
39 if (start + maxLength >= text.length)
40 start = 0; // Restart the counter at first character.
41 else
42 ++start; // Increment the starting character position.
43
44 // if we're at the end of the text and restart wasn't requested then exit now.
45 if (!start && !restartAtEnd)
46 return;
47
48 // Schedule a timer for next scroll animation "frame."
49 // If we're at the start or end of the text then use the startDelay, otherwise the scrollDelay.
50 _data.timerId = setTimeout(update, start > 1 ? scrollDelay : startDelay);
51 }
52
53 // Set the "run" flag (the animation can be cancelled with `marqueeStop()`)
54 _data.runMarquee = true;
55 // Send the first part of the text now.
56 update();
57}
58
59// This function can be used to stop/cancel the scrolling animation at any point.
60// It is also called by `marquee()` to ensure any current animation is stopped before
61// starting a new one.
62var marqueeStop = function() {
63 // Set flag for `update()` function
64 _data.runMarquee = false;
65 // Cancel any running timer
66 if (_data.timerId > -1)
67 clearTimeout(_data.timerId);
68 // reset timer ID
69 _data.timerId = -1;
70}
71
72// These variables are used internally to stop/cancel the marquee scroll.
73var _data = _data || {
74 timerId: -1,
75 runMarquee: false
76}
void start(String program, Array< String > arguments=[])
Starts the given program in a new process, passing the command line arguments in arguments.
< Promise|string > text()
The global TP namespace is used as a container for all Touch Portal API methods, which are invoked wi...
Definition: touchportal.dox:7
void stateUpdate(String value)
Send a State update to Touch Portal with given value for the current script instance.

Example usage on a button

In an actual button/flow you would most likely put the action inside an event handler which reacts to a change in a State/Value you would like to display, and put that State/Value's macro inside the quotes of the marquee() call, instead of my static example. Eg.

marquee("${value:com.github.ChristopheCVB.TouchPortal.Spotify.TouchPortalSpotifyPlugin.BaseCategory.state.currentTrackName}")

Cancel marquee scroll

The script also has a function to cancel/stop the scroll animation on demand. Here is an example of a button for that.
The key point is that it uses the same State Name as the action which started the marquee to begin with (and also runs in a Private instance).