~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Nevow/nevow/canvas.as

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (c) 2004 Divmod.
2
 
// See LICENSE for details.
3
 
 
4
 
 
5
 
// variables injected into the global namespace are:
6
 
//  cookie
7
 
//  port
8
 
//  prefix
9
 
 
10
 
// GLOBALS
11
 
 
12
 
var server = new XMLSocket();
13
 
 
14
 
var canvas = _root.createEmptyMovieClip('canvas', 0);
15
 
 
16
 
function sendEvent(evtTxt) {
17
 
    server.send(evtTxt); }
18
 
 
19
 
// The flash movie can control which event handlers are installed.
20
 
// The following names should be defined in the movie's namespace:
21
 
 
22
 
//  onKeyDown
23
 
//  onKeyUp
24
 
//  onMouseDown
25
 
//  onMouseUp
26
 
//  onMouseMove
27
 
 
28
 
if (onKeyDown) {
29
 
  Key.addListener(canvas);
30
 
  canvas.onKeyDown = function() {
31
 
    sendEvent('onKeyDown '+Key.getCode()); } }
32
 
 
33
 
if (onKeyUp) {
34
 
  canvas.onKeyUp = function() {
35
 
    sendEvent('onKeyUp '+Key.getCode()); } }
36
 
 
37
 
if (onMouseDown) {
38
 
  canvas.onMouseDown = function() {
39
 
    sendEvent('onMouseDown '+_root._xmouse+' '+_root._ymouse); } }
40
 
 
41
 
if (onMouseUp) {
42
 
  canvas.onMouseUp = function() {
43
 
    sendEvent('onMouseUp '+_root._xmouse+' '+_root._ymouse); } }
44
 
 
45
 
if (onMouseMove) {
46
 
  canvas.onMouseMove = function() {
47
 
    sendEvent('onMouseMove '+_root._xmouse+' '+_root._ymouse); } }
48
 
 
49
 
 
50
 
// RPC UTILITIES
51
 
 
52
 
function extractList(nodes) {
53
 
    var rv = [];
54
 
    for (i=0; i<nodes.length; i++) {
55
 
        rv.push(
56
 
            Number(nodes[i].attributes['v'])); }
57
 
    return rv; }
58
 
 
59
 
function extractDict(nodes) {
60
 
    var rv = {};
61
 
    for (i=0; i<nodes.length; i++) {
62
 
        var nod = nodes[i];
63
 
        var val = nod.attributes['v'];
64
 
        if (val == "matrixType") {
65
 
            rv[nod.attributes['k']] = val; }
66
 
        else {
67
 
            rv[nod.attributes['k']] = Number(val); } }
68
 
    return rv; }
69
 
 
70
 
function extractGroup(path) {
71
 
    var groupTarget = _root;
72
 
    var segments = path.split('.');
73
 
    for (var i = 0; i < segments.length; i++) {
74
 
        groupTarget = groupTarget[segments[i]]; }
75
 
    return groupTarget; }
76
 
 
77
 
// DRAWING APIS
78
 
 
79
 
function reposition(canvas, x, y) {
80
 
    canvas._x = Number(x);
81
 
    canvas._y = Number(y); }
82
 
 
83
 
function rotate(canvas, angle) {
84
 
    canvas._rotation = Number(angle); }
85
 
 
86
 
function alpha(canvas, percent) {
87
 
    canvas._alpha = Number(percent); }
88
 
 
89
 
function scale(canvas, x, y) {
90
 
    canvas._xscale = Number(x);
91
 
    canvas._yscale = Number(y); }
92
 
 
93
 
function swapGroup(canvas, other) {
94
 
    var groupTarget = extractGroup(other);
95
 
    canvas.swapDepths(groupTarget); }
96
 
 
97
 
function swapInt(canvas, other) {
98
 
    canvas.swapDepths(Number(other)); }
99
 
 
100
 
function gradient(canvas, type, colors, alphas, ratios, matrix) {
101
 
    var cl = extractList(colors);
102
 
    var al = extractList(alphas);
103
 
    var ra = extractList(ratios);
104
 
    var ma = extractDict(matrix);
105
 
    canvas.beginGradientFill(
106
 
        type, cl, al, ra, ma) }
107
 
 
108
 
function curve(canvas, controlX, controlY, anchorX, anchorY) {
109
 
    canvas.curveTo(
110
 
        Number(controlX),
111
 
        Number(controlY),
112
 
        Number(anchorX),
113
 
        Number(anchorY)); }
114
 
 
115
 
function fill(canvas, rgb, alpha) {
116
 
    canvas.beginFill(Number(rgb), Number(alpha)); }
117
 
 
118
 
function close(canvas) {
119
 
    canvas.endFill(); }
120
 
 
121
 
function move(canvas, x, y) {
122
 
    canvas.moveTo(Number(x), Number(y)); }
123
 
 
124
 
function line(canvas, x, y) {
125
 
        canvas.lineTo(Number(x), Number(y));    }
126
 
 
127
 
function pen(canvas, w, c, a) {
128
 
    if (a) {
129
 
        canvas.lineStyle(Number(w), Number(c), Number(a)) }
130
 
    else if (c) {
131
 
        canvas.lineStyle(Number(w), Number(c)); } 
132
 
    else if (w) {
133
 
        canvas.lineStyle(Number(w)); }
134
 
    else {
135
 
        canvas.lineStyle(); } }
136
 
 
137
 
 
138
 
function clear(canvas) {
139
 
    canvas.clear(); }
140
 
 
141
 
// TEXT APIS
142
 
 
143
 
function text(canvas, cookie, val, x, y, height, width) {
144
 
    canvas.createTextField(
145
 
        "T_" + cookie, Number(cookie), 
146
 
        Number(x), Number(y), Number(height), Number(width));
147
 
    var T = canvas["T_" + cookie]
148
 
    T.text = val;
149
 
    T.selectable = false;
150
 
//    T.background = true;
151
 
//    T.backgroundColor = 0xefefef;
152
 
    }
153
 
 
154
 
function changeText(canvas, cookie, val) {
155
 
    canvas["T_" + cookie].text = val; }
156
 
 
157
 
function moveText(canvas, cookie, x, y) {
158
 
    var T = canvas["T_" + cookie];
159
 
    T._x = Number(x);
160
 
    T._y = Number(y); }
161
 
 
162
 
function rotateText(canvas, cookie, angle) {
163
 
    var A = Number(angle);
164
 
    canvas["T_" + cookie]._rotation = A; }
165
 
 
166
 
function listFonts(canvas, identifier) {
167
 
    var fonts = TextField.getFontList();
168
 
    sendEvent(
169
 
        identifier + " " + fonts); }
170
 
 
171
 
function font(canvas, cookie, fn) {
172
 
    var T = canvas["T_" + cookie];
173
 
    var F = T.getTextFormat();
174
 
    F.font = fn;
175
 
    T.setTextFormat(F); }
176
 
 
177
 
function size(canvas, cookie, s) {
178
 
    var T = canvas["T_" + cookie];
179
 
    var F = T.getTextFormat();
180
 
    F.size = Number(s);
181
 
    T.setTextFormat(F); }
182
 
 
183
 
// GROUP APIs
184
 
 
185
 
function group(canvas, cookie) {
186
 
    canvas.createEmptyMovieClip('G_'+cookie, Number(cookie));
187
 
    var G = canvas['G_'+cookie]; }
188
 
 
189
 
function setMask(canvas, otherCanvas) {
190
 
    if (!otherCanvas) {
191
 
        canvas.setMask(null); }
192
 
    else {
193
 
        var G = extractGroup(otherCanvas);
194
 
        canvas.setMask(G); } }
195
 
 
196
 
function setVisible(canvas, visible) {
197
 
    if (visible == 'True') {
198
 
        canvas._visible = true; }
199
 
    else {
200
 
        canvas._visible = false; } }
201
 
 
202
 
// IMAGE APIs
203
 
 
204
 
function image(canvas, cookie, where) {
205
 
    canvas.createEmptyMovieClip("I_"+cookie, Number(cookie));
206
 
    var I = canvas["I_"+cookie];
207
 
    I.loadMovie(where); }
208
 
 
209
 
function moveImage(canvas, cookie, x, y) {
210
 
    var I = canvas["I_" + cookie];
211
 
    I._x = Number(x);
212
 
    I._y = Number(y); }
213
 
 
214
 
function scaleImage(canvas, cookie, x, y) {
215
 
    var I = canvas["I_" + cookie];
216
 
    I._xscale = Number(x);
217
 
    I._yscale = Number(y); }
218
 
 
219
 
function alphaImage(canvas, cookie, alpha) {
220
 
    var I = canvas["I_" + cookie];
221
 
    I._alpha = Number(alpha); }
222
 
 
223
 
function rotateImage(canvas, cookie, angle) {
224
 
    var T = canvas["I_" + cookie];
225
 
    T._rotation = Number(angle); }
226
 
 
227
 
 
228
 
// SOUND APIs
229
 
 
230
 
function sound(canvas, cookie, where, streaming) {
231
 
    var S = new Sound();
232
 
    canvas["S_"+cookie] = S;
233
 
    S.loadSound(where, Boolean(streaming));
234
 
    S.stop(); }
235
 
 
236
 
function playSound(canvas, cookie, offset, timesLoop) {
237
 
    var S = canvas["S_"+cookie]
238
 
    S.start(Number(offset), Number(timesLoop)); }
239
 
 
240
 
// SERVER GLUE
241
 
 
242
 
server.onXML = function(xml) {
243
 
        xml = xml.firstChild;
244
 
        var funcName = xml.attributes['n'];
245
 
        var func = eval(funcName);
246
 
        var parms = [];
247
 
 
248
 
    parms.push(
249
 
        extractGroup(xml.attributes['t']));
250
 
 
251
 
        for(i=0; i<xml.childNodes.length; i++) {
252
 
        if (xml.childNodes[i].attributes['v']){
253
 
            parms.push(
254
 
                xml.childNodes[i].attributes['v']); }
255
 
        else {
256
 
            parms.push(
257
 
                xml.childNodes[i].childNodes[0].childNodes); } }
258
 
 
259
 
        func.apply(groupTarget, parms); }
260
 
 
261
 
server.onConnect = function(success) {
262
 
        if (success) {
263
 
                server.send(
264
 
                        "GET " + prefix + "canvas_socket/"+
265
 
                        cookie+
266
 
                        " HTTP/1.0\r\n\r\n"); } }
267
 
 
268
 
server.connect(null, port);