v1.2.1.0
All Classes Namespaces Functions Variables Enumerations Enumerator Properties Modules Pages
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 = 0)
14{
15 marqueeStop();
16
17 if (text.length <= maxLength)
18 return text;
19
20 // track the first character of the text to show in the scrolling marquee
21 var start = 0;
22 var firstCycle = true;
23 text += " - " + "\u2000".repeat(maxLength);
24
25 function update()
26 {
27 if (!_data.runMarquee)
28 return;
29
30 const result = text.slice(start, start + maxLength);
31
32 TP.stateUpdate(result);
33
34 if (start + maxLength >= text.length) {
35 start = 0; // Restart the counter at first character.
36 if (firstCycle) {
37 text = "\u2000".repeat(maxLength) + text; // <<< EDIT
38 firstCycle = false;
39 }
40 }
41 else {
42 ++start; // Increment the starting character position.
43 }
44
45 _data.timerId = setTimeout(update, start > 1 ? scrollDelay : startDelay);
46 }
47
48 _data.runMarquee = true;
49
50 update();
51}
52
53// This function can be used to stop/cancel the scrolling animation at any point.
54// It is also called by `marquee()` to ensure any current animation is stopped before
55// starting a new one.
56var marqueeStop = function() {
57 // Set flag for `update()` function
58 _data.runMarquee = false;
59 // Cancel any running timer
60 if (_data.timerId > -1)
61 clearTimeout(_data.timerId);
62 // reset timer ID
63 _data.timerId = -1;
64}
65
66// These variables are used internally to stop/cancel the marquee scroll.
67var _data = _data || {
68 timerId: -1,
69 runMarquee: false
70}
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:6
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).