File: text_scroller.js

package info (click to toggle)
freej 0.10git20080824-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 13,504 kB
  • ctags: 19,398
  • sloc: ansic: 135,255; cpp: 32,550; sh: 9,318; perl: 2,932; asm: 2,355; yacc: 1,178; makefile: 1,119; java: 136; lex: 94; python: 16
file content (144 lines) | stat: -rw-r--r-- 3,189 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// freej script for a simple text scroller
// it makes smart use of arrays as FIFO pipes
// and the file_to_strings(filename) function
// to read a text file into an array of strings

// (C)2005 Denis Jaromil Rojo - GNU GPL 

W = 640;
H = 480;

set_resolution(W, H);
set_fps(25);

wordspacing = 5;

// setup the keyboard quit
running = true;
kbd = new KeyboardController();
kbd.released_esc = function() { running = false; }
register_controller( kbd );

// read a text file into an array
words = file_to_strings("test.txt");

// debug
echo ( words.length + " words read" );

idx = 0;
// words.length property says how big it is
// access words with words[0] words[1] and so on


// make a black background
/*
background = new GeometryLayer();
background.color(0x00000000);
background.set_blit("alpha");
background.set_blit_value(0.2);
background.rectangle(0, 0, background.w(), background.h() );
add_layer( background );
*/

// setup the array of rendered words
// each one is a TextLayer object
scroll = new Array();

/*
  scroll[] is an Array object
  we will use it as a FIFO pipe, thru the push() and shift()
  methods of javascript arrays:

   t(n): scroll.push( txt ) pushes in text from the right (here below)
   array scroll  [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]     _/
   screen -> | <- bla - bla - bla - bla - bla - bla - | <- screen
   
   t(n+1):
       [ 0 ] | <- [ 1 ] - [ 2 ] - [ 3 ] ...
       scroll.shift() returns scroll[0] (which we delete)
       
  so let's go on...
*/

function render_word(wrd) {

    lay = new TextLayer();

    lay.size( 50 ); // set the size

    lay.print( wrd ); // print the string in the layer


    lay.set_fps(25);

    lay.start();


    add_layer( lay ); // add it to the screen

    lay.set_position( W, 400 ); // start from the right of the scree


    //    lay.slide_position( 0 - lay.w() , 200, 2); // slide to the right

    return lay;
}
  
// MAIN

// create a new TextLayer with the word
txt = render_word( words[idx] );

// append it as last element of the scroll[] array
scroll.push( txt );

// advance the index of words
idx++;

trigger = new TriggerController();
register_controller( trigger );
trigger.frame = function() {


    // slide all to the left
    for(i=0; i<scroll.length; i++) {
	echo("- " + i + " is  at " + scroll[i].x() + "x" + scroll[i].y());

	scroll[i].set_position( scroll[i].x() - 2, scroll[i].y() );
	echo("- " + i + " now at " + scroll[i].x() + "x" + scroll[i].y());
	
    }

    
    // check if the leftmost is out of screen
    if( scroll[0].x() + scroll[0].w() < 0) {
	// pull it out from the array
	txt = scroll.shift();
	// remove it from the screen
	rem_layer( txt );
	// delete it
	delete txt;
    }


    // check if the rightmost all entered the screen
    rightmost = scroll[ scroll.length -1 ];
    if( rightmost.x() + rightmost.w() <  W - wordspacing ) {
	// then we need a new one on the left
	
	// create a new TextLayer with the word
	txt = render_word( words[idx] );

	// append it as last element of the scroll[] array
	scroll.push( txt );
	
	// advance the index
	idx++;
    }

    // if the words are finished: stop
    if(idx >= words.length)
	this.frame = function() { };
    
}