big merge from master, fix rpm creation, drop fetching swfdec
[gnash.git] / testsuite / misc-ming.all / SpriteButtonEventsTest.c
blob72b88d497d3917d9c2058aad0996f1b148adc5a9
1 /***********************************************************************
3 * Copyright (C) 2005, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ***********************************************************************
22 * Test case for mouse events.
24 * In a movie of 120x120 pixels, it places a movieclip containing a squared
25 * sprite-button in the middle of the stage, and a text area on top.
27 * The movie has 3 frames, with the second adding a shape at a lower depth
28 * and the third one at an higher depth respect to the button.
30 * The following events print the event name in the text area
31 * (called _root.textfield) and change the color of the button:
33 * RollOut : red button (initial state)
34 * RollOver : yellow button
35 * Press : green button
36 * Release : yellow button (same as MouseOver, but the label on top changes)
38 ***********************************************************************/
40 #include "ming_utils.h"
42 #include <ming.h>
43 #include <stdio.h>
44 #include <stdlib.h>
46 #define OUTPUT_VERSION 6
47 #define OUTPUT_FILENAME "SpriteButtonEventsTest.swf"
49 SWFFont font;
51 void add_event(SWFMovie mo, const char* name, const char* event, const char* action);
52 void add_code(SWFMovie mo, const char* code);
53 void add_text_field(SWFMovie mo, const char* name, const char* varname, const char* initial_label, int depth, int x, int y);
54 void set_text(SWFMovie mo, const char* text);
55 SWFDisplayItem add_square(SWFMovie mo, byte r, byte g, byte b, int depth);
57 void
58 add_event(SWFMovie mo, const char* name, const char* event, const char* action)
60 SWFAction ac;
61 char buf[1024];
63 sprintf(buf,
64 "event=undefined;"
65 "%s.on%s=function() { %s; };"
66 , name, event, action
68 ac = compileSWFActionCode(buf);
70 SWFMovie_add(mo, (SWFBlock)ac);
73 void
74 add_code(SWFMovie mo, const char* code)
76 SWFAction ac;
78 ac = compileSWFActionCode(code);
80 SWFMovie_add(mo, (SWFBlock)ac);
83 SWFDisplayItem add_button(SWFMovie mo);
84 SWFDisplayItem
85 add_button(SWFMovie mo)
87 SWFDisplayItem it;
88 SWFMovieClip mc;
89 SWFShape sh1, sh2, sh3, sh4;
90 SWFMovieClip bu = newSWFMovieClip();
92 mc = newSWFMovieClip();
94 sh1 = make_fill_square(0, 0, 40, 40, 0, 0, 0, 0, 0, 0); // black
95 sh2 = make_fill_square(0, 0, 40, 40, 255, 0, 0, 255, 0, 0); // red
96 sh3 = make_fill_square(0, 0, 40, 40, 0, 255, 0, 0, 255, 0); // green
97 sh4 = make_fill_square(0, 0, 40, 40, 255, 255, 0, 255, 255, 0); // yellow
99 SWFMovieClip_add(bu, (SWFBlock)sh2); // red when idle
100 SWFMovieClip_add(bu, (SWFBlock)compileSWFActionCode(
101 "onRollOut = function() {"
102 " _root.note('onRollOut');"
103 " updateAfterEvent();"
104 " _root.msg='RollOut';"
105 " gotoAndStop(1);"
106 "};"
107 "onRollOver = function() {"
108 " _root.note('onRollOver');"
109 " updateAfterEvent();"
110 " _root.msg='RollOver';"
111 " gotoAndStop(3);"
112 "};"
113 "onMouseDown = function() {"
114 " _root.note('onMouseDown');"
115 " updateAfterEvent();"
116 " _root.msg2='MouseDown';"
117 "};"
118 "onMouseUp = function() {"
119 " _root.note('onMouseUp');"
120 " updateAfterEvent();"
121 " _root.msg2='MouseUp';"
122 "};"
123 "onPress = function() {"
124 " _root.note('onPress');"
125 " updateAfterEvent();"
126 " _root.msg='Press';"
127 " gotoAndStop(2);"
128 "};"
129 "onRelease = function() {"
130 " _root.note('onRelease');"
131 " updateAfterEvent();"
132 " _root.msg='Release';"
133 " gotoAndStop(3);"
134 "};"
135 "onReleaseOutside = function() {"
136 " _root.note('onReleaseOutside');"
137 " updateAfterEvent();"
138 " _root.msg='ReleaseOutside';"
139 " gotoAndStop(1);"
140 "};"
141 "_root.onMouseWheel = function(delta, t) {"
142 " _root.note('onMouseWheel: ' + delta + ', ' + t + ', ' + arguments.length);"
143 " _root.note('onMouseWheel: ' + delta + ', ' + t);"
144 " _root.msg='onMouseWheel: ' + delta + ', ' + t + ', ' + arguments.length;"
145 "};"
146 "Mouse.addListener(_root);"
147 "stop();"
150 SWFMovieClip_nextFrame(bu);
152 SWFMovieClip_add(bu, (SWFBlock)sh3); // green on button press
153 SWFMovieClip_add(bu, (SWFBlock)newSWFAction("stop();"));
154 SWFMovieClip_nextFrame(bu);
156 SWFMovieClip_add(bu, (SWFBlock)sh4); // yellow on mouse over
157 SWFMovieClip_add(bu, (SWFBlock)newSWFAction("stop();"));
158 SWFMovieClip_nextFrame(bu);
160 it = SWFMovieClip_add(mc, (SWFBlock)bu);
161 SWFDisplayItem_setName(it, "button");
162 SWFMovieClip_nextFrame(mc); /* showFrame */
164 it = SWFMovie_add(mo, (SWFBlock)mc);
165 return it;
168 void
169 add_text_field(SWFMovie mo, const char* name, const char* varname, const char* initial_label, int depth, int x, int y)
171 SWFDisplayItem it;
172 SWFTextField tf = newSWFTextField();
173 SWFTextField_setFont(tf, (void*)font);
174 SWFTextField_addChars(tf, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345689:.,/\\#@?!");
175 SWFTextField_setVariableName(tf, varname);
176 SWFTextField_addString(tf, "Idle");
177 SWFTextField_setBounds(tf, 120, 12);
178 SWFTextField_setFlags(tf, SWFTEXTFIELD_DRAWBOX|SWFTEXTFIELD_NOEDIT);
180 it = SWFMovie_add(mo, (SWFBlock)tf);
181 SWFDisplayItem_moveTo(it, x, y+2);
182 SWFDisplayItem_setDepth(it, depth);
183 SWFDisplayItem_setName(it, name); // "textfield");
185 // Label
186 tf = newSWFTextField();
187 SWFTextField_setFont(tf, (void*)font);
188 SWFTextField_addString(tf, initial_label);
189 SWFTextField_setFlags(tf, SWFTEXTFIELD_DRAWBOX|SWFTEXTFIELD_NOEDIT);
190 it = SWFMovie_add(mo, (SWFBlock)tf);
191 SWFDisplayItem_scale(it, 0.3, 0.3);
192 SWFDisplayItem_setDepth(it, depth*10);
193 SWFDisplayItem_moveTo(it, x, y);
196 void
197 set_text(SWFMovie mo, const char* text)
199 char buf[1024];
200 sprintf(buf, "_root.msg=\"%s\";", text);
201 add_code(mo, buf);
205 main(int argc, char **argv)
207 SWFMovie mo;
208 SWFDisplayItem it;
209 const char *srcdir=".";
210 char fdbfont[256];
211 SWFMovieClip dejagnuclip;
213 /*********************************************
215 * Initialization
217 *********************************************/
219 puts("Setting things up");
221 Ming_init();
222 Ming_useSWFVersion (OUTPUT_VERSION);
223 Ming_setScale(20.0);
225 mo = newSWFMovie();
226 SWFMovie_setDimension(mo, 800, 600);
227 SWFMovie_setRate(mo, 1);
229 if ( argc>1 ) srcdir=argv[1];
230 else
232 fprintf(stderr, "Usage: %s <mediadir>\n", argv[0]);
233 return 1;
236 sprintf(fdbfont, "%s/Bitstream-Vera-Sans.fdb", srcdir);
237 FILE *font_file = fopen(fdbfont, "r");
238 if ( font_file == NULL )
240 perror(fdbfont);
241 exit(1);
243 /*SWFBrowserFont bfont = newSWFBrowserFont("_sans");*/
244 font = loadSWFFontFromFile(font_file);
246 /* Dejagnu equipment */
247 dejagnuclip = get_dejagnu_clip((SWFBlock)font, 10, 0, 0, 800, 600);
248 it = SWFMovie_add(mo, (SWFBlock)dejagnuclip);
249 SWFDisplayItem_setDepth(it, 200);
250 SWFDisplayItem_move(it, 200, 0);
252 add_text_field(mo, "textfield", "_root.msg", "Button events", 10, 0, 5);
253 add_text_field(mo, "textfield2", "_root.msg2", "Mouse events", 11, 0, 100);
254 add_text_field(mo, "textfield3", "_root.msg3", "Key events", 12, 0, 80);
256 SWFMovie_nextFrame(mo);
258 /*****************************************************
260 * Add button
262 *****************************************************/
264 it = add_button(mo);
265 SWFDisplayItem_moveTo(it, 40, 30);
266 SWFDisplayItem_setName(it, "square1");
267 SWFDisplayItem_setDepth(it, 2);
269 // Mouse pointer events
270 //add_actions(mo, "square1.button.onRollOver = function() { _root.msg2 = 'RollOver'; };");
271 //add_actions(mo, "square1.button.onRollOut = function() { _root.msg2 = 'RollOut'; };");
273 // Mouse buttons events
274 //add_actions(mo, "square1.button.onPress = function() { _root.msg2 = 'Press'; };");
275 //add_actions(mo, "square1.button.onRelease = function() { _root.msg2 = 'Release'; gotoAndStop(1); };");
276 //add_actions(mo, "square1.button.onReleaseOutside = function() { _root.msg2 = 'ReleaseOutside'; };");
278 // Focus events
279 add_actions(mo, "square1.button.onSetFocus = function() { _root.msg3 = 'SetFocus'; };");
281 // Key events - button needs focus for these to work
282 add_actions(mo, "square1.button.onKeyDown = function() { _root.msg3 = 'KeyDown'; };");
283 add_actions(mo, "square1.button.onKeyUp = function() { _root.msg3 = 'KeyUp'; };");
286 SWFMovie_nextFrame(mo); /* showFrame */
288 /*****************************************************
290 * On second frame, add a shape at lower depth
292 *****************************************************/
295 SWFShape sh = make_fill_square(0, 0, 120, 120, 0, 0, 0, 0, 255, 0);
296 SWFDisplayItem itsh = SWFMovie_add(mo, (SWFBlock)sh);
297 SWFDisplayItem_setDepth(itsh, 1);
299 SWFMovie_nextFrame(mo); /* showFrame */
302 /*****************************************************
304 * On third frame, add a shape at higher depth
306 *****************************************************/
309 SWFShape sh = make_fill_square(0, 0, 120, 120, 0, 0, 0, 0, 255, 0);
310 SWFDisplayItem itsh = SWFMovie_add(mo, (SWFBlock)sh);
311 SWFDisplayItem_setDepth(itsh, 3);
312 SWFDisplayItem_setColorAdd(itsh, 0, 0, 0, -128);
314 SWFMovie_nextFrame(mo); /* showFrame */
317 /*****************************************************
319 * On third frame, add a shape at higher depth
321 *****************************************************/
325 add_actions(mo,
326 "square1.button.enabled = false;"
327 //"_root.msg = _root.msg2 = _root.msg3 = 'Idle';"
328 "stop();"
329 "totals();"
331 SWFMovie_nextFrame(mo); /* showFrame */
334 /*****************************************************
336 * Save it...
338 *****************************************************/
340 puts("Saving " OUTPUT_FILENAME );
342 SWFMovie_save(mo, OUTPUT_FILENAME);
344 return 0;