示例#1
0
SWFDisplayItem
add_button(SWFMovie mo)
{
	SWFDisplayItem it;
	SWFMovieClip mc;
	SWFButtonRecord br;
	SWFShape sh1, sh2, sh3, sh4, sh1a, sh2a, sh3a, sh4a;
	SWFButton bu = newSWFButton();
	static SWFMovieClip ermc; /* Events-reporting mc */
	mc = newSWFMovieClip();

	if ( ! ermc )
	{
		ermc = newSWFMovieClip();
		SWFMovieClip_add(ermc, newSWFAction(
			"_global.dumpObj = function(o,indent) {"
			"	var s = '';"
			"	if ( typeof(o) == 'object' ) {"
			"		s += '{';"
			"		var first=1;"
			"		for (var i in o) {"
			"			if (!first) s+=',';"
			"			s+= i+':'+dumpObj(o[i]);"
			"			first=0;"
			"		}"
			"		s += '}';"
			"	} else {"
			"		s += o;"
			"	}"
			"	return s;"
			"};"
			"if ( _root.buttonChild == undefined ) _root.buttonChild = [];"
			"var myDepth = getDepth()+16383;"
			"var myName = ''+this;"
			"if ( _root.buttonChild[myDepth] == undefined ) _root.buttonChild[myDepth] = {nam:myName,exe:1,uld:0};"
			"else _root.buttonChild[myDepth]['exe']++;"
			 //"_root.note('Actions in frame0 of '+this+' at depth '+myDepth+' executed.');" 
			"this.onUnload = function() {"
			"	var myDepth = -(getDepth()+32769-16383);"
			//"	_root.note(''+this+' at depth '+myDepth+' unloaded.');"
			"	_root.buttonChild[myDepth]['uld']++;"
			"};"
            "for (i in _level0.square1.button) { trace (i); };"
			//"_root.note('buttonChilds:'+dumpObj(_root.buttonChild));"
		));
		SWFMovieClip_nextFrame(ermc);
	}

	sh1 = make_fill_square(0, 0, 40, 40, 0, 0, 0, 0, 0, 0);
	sh1a = make_fill_square(30, 30, 5, 5, 128, 128, 128, 128, 128, 128);
	sh2 = make_fill_square(0, 0, 40, 40, 255, 0, 0, 255, 0, 0);
	sh2a = make_fill_square(30, 30, 5, 5, 128, 0, 0, 128, 0, 0);
	sh3 = make_fill_square(0, 0, 40, 40, 0, 255, 0, 0, 255, 0);
	sh3a = make_fill_square(30, 30, 5, 5, 0, 128, 0, 0, 128, 0);
	sh4 = make_fill_square(0, 0, 40, 40, 255, 255, 0, 255, 255, 0);
	sh4a = make_fill_square(30, 30, 5, 5, 128, 128, 0, 128, 128, 0);

	/* Higher depth DisplayObject is intentionally added before lower depth one */
	br = SWFButton_addCharacter(bu, (SWFCharacter)sh1a, SWFBUTTON_HIT);
	SWFButtonRecord_setDepth(br, 2);
	br = SWFButton_addCharacter(bu, (SWFCharacter)sh1, SWFBUTTON_HIT);
	SWFButtonRecord_setDepth(br, 1);

	/* Higher depth DisplayObject is intentionally added before lower depth one */
	br = SWFButton_addCharacter(bu, (SWFCharacter)sh2a, SWFBUTTON_UP );
	SWFButtonRecord_setDepth(br, 2);
	br = SWFButton_addCharacter(bu, (SWFCharacter)sh2, SWFBUTTON_UP );
	SWFButtonRecord_setDepth(br, 1);

	/* Higher depth DisplayObject is intentionally added before lower depth one */
	br = SWFButton_addCharacter(bu, (SWFCharacter)sh3a, SWFBUTTON_DOWN );
	SWFButtonRecord_setDepth(br, 2);
	br = SWFButton_addCharacter(bu, (SWFCharacter)sh3, SWFBUTTON_DOWN );
	SWFButtonRecord_setDepth(br, 1);

	/* Higher depth DisplayObject is intentionally added before lower depth one */
	br = SWFButton_addCharacter(bu, (SWFCharacter)sh4a, SWFBUTTON_OVER );
	SWFButtonRecord_setDepth(br, 2);
	br = SWFButton_addCharacter(bu, (SWFCharacter)sh4, SWFBUTTON_OVER );
	SWFButtonRecord_setDepth(br, 1);

	/* Add events reported DisplayObject in all states at depth 10 */
	br = SWFButton_addCharacter(bu, (SWFCharacter)ermc, SWFBUTTON_HIT|SWFBUTTON_DOWN|SWFBUTTON_OVER|SWFBUTTON_UP);
	SWFButtonRecord_setDepth(br, 10);

	/* Add events reported DisplayObject just HIT state at depth 11 */
	br = SWFButton_addCharacter(bu, (SWFCharacter)ermc, SWFBUTTON_HIT);
	SWFButtonRecord_setDepth(br, 11);

	/* Add events reported DisplayObject just UP state at depth 12 */
	br = SWFButton_addCharacter(bu, (SWFCharacter)ermc, SWFBUTTON_UP);
	SWFButtonRecord_setDepth(br, 12);

	/* Add events reported DisplayObject just OVER state at depth 13 */
	br = SWFButton_addCharacter(bu, (SWFCharacter)ermc, SWFBUTTON_OVER);
	SWFButtonRecord_setDepth(br, 13);

	/* Add events reported DisplayObject just DOWN state at depth 14 */
	br = SWFButton_addCharacter(bu, (SWFCharacter)ermc, SWFBUTTON_DOWN);
	SWFButtonRecord_setDepth(br, 14);


	SWFButton_addAction(bu, compileSWFActionCode(
		"_root.msg='MouseOut';"
		"if ( _root.testno == 4 || _root.testno == 9 || _root.testno == 14 ) {"
		"	_root.check_equals(_root.printBounds(getBounds()), '-0.05,-0.05 40.05,40.05');"
		/* Target of button action is the button's parent sprite */
		"	_root.check_equals(_target, '/square1');"
		"	setTarget('/');"
		"	_root.check_equals(_target, '/');"
		"	_root.testno++;"
		"	_root.note(_root.testno+'. Press mouse button inside the square, and release it outside.');"
		"} else {"
		//"	_root.note('SWFBUTTON_MOUSEOUT');"
		"	_root.xfail('Unexpectedly got SWFBUTTON_MOUSEOUT event (testno:'+_root.testno+')');"
		"}"
		), SWFBUTTON_MOUSEOUT);

	SWFButton_addAction(bu, compileSWFActionCode(
		"_root.msg='MouseOver';"

		"if ( _root.testno == 1 ) {" /* ONLY CHECK buttonChild on first frame */

		/* "_root.note('buttonChild is '+dumpObj(_root.buttonChild));" */

		/* added OVER state char */
		"	_root.check_equals(_root.buttonChild.realLength(), 3);"

		/* OVER state char loaded */
		"	_root.check_equals(typeof(_root.buttonChild[13]), 'object');"
		"	_root.check_equals(_root.buttonChild[13].nam, '_level0.square1.button.instance7');"
		"	_root.check_equals(_root.buttonChild[13].exe, 1);" /* OVER state char */
		"	_root.check_equals(_root.buttonChild[13].uld, 0);" /* OVER state char */

		/* UP state char unloaded */
		"	_root.check_equals(_root.buttonChild[12].exe, 1);"
		"	_root.check_equals(_root.buttonChild[12].uld, 1);"
		"	_root.check_equals(typeof(_level0.square1.button.instance6), 'movieclip');"
		"	_root.check_equals(_level0.square1.button.instance6._name, 'instance6');"
		"	_root.check_equals(_level0.square1.button.instance6.getDepth(), -16398);"

		/* ALL state char still there, not reloaded, not unloaded */
		"	_root.check_equals(_root.buttonChild[10].exe, 1);"
		"	_root.check_equals(_root.buttonChild[10].uld, 0);"

		"}"

		"if ( _root.testno == 1 || _root.testno == 6 || _root.testno == 11 ) {"

		//"	_root.note('buttonChild is '+dumpObj(_root.buttonChild));"
		"	_root.check_equals(_root.printBounds(getBounds()), '-0.05,-0.05 40.05,40.05');"
		/* Target of button action is the button's parent sprite */
		"	_root.check_equals(_target, '/square1');"
		"	setTarget('/');"
		"	_root.check_equals(_target, '/');"
		"	_root.testno++;"
		"	_root.note(_root.testno+'. Press (and keep pressed) the mouse button inside the square.');"
		"} else {"
		//"	_root.note('SWFBUTTON_MOUSEOVER');"
		// need MOUSEOVER for MOUSEUPOUTSIDE
		"	if ( _root.testno != 5 && _root.testno != 10 && _root.testno != 15 ) {"
		"		_root.fail('Unexpectedly got SWFBUTTON_MOUSEOVER event (testno:'+_root.testno+')');"
		"	}"
		"}"
		), SWFBUTTON_MOUSEOVER);

	SWFButton_addAction(bu, compileSWFActionCode(
		"_root.msg='MouseDown';"

		"if ( _root.testno == 2 ) {" /* ONLY CHECK buttonChild on first frame */

		/* Added DOWN state char */
		"	_root.check_equals(_root.buttonChild.realLength(), 4);"

		/* DOWN state char loaded */
		"	_root.check_equals(typeof(_root.buttonChild[14]), 'object');"
		"	_root.check_equals(_root.buttonChild[14].nam, '_level0.square1.button.instance8');"
		"	_root.check_equals(_root.buttonChild[14].exe, 1);" 
		"	_root.check_equals(_root.buttonChild[14].uld, 0);" 

		/* OVER state char unloaded */
		"	_root.check_equals(_root.buttonChild[13].exe, 1);" 
		"	_root.check_equals(_root.buttonChild[13].uld, 1);"

		/* ALL state char still there, not reloaded, not unloaded */
		"	_root.check_equals(_root.buttonChild[10].exe, 1);"
		"	_root.check_equals(_root.buttonChild[10].uld, 0);"

		"}"

		"if ( _root.testno == 2 || _root.testno == 7 || _root.testno == 12 ) {"
		"	_root.check_equals(_root.printBounds(getBounds()), '-0.05,-0.05 40.05,40.05');"
		/* Target (and name) of button action is the button's parent sprite */
		"	_root.check_equals(_target, '/square1');"
		"	_root.check_equals(_name, 'square1');"
		"	setTarget('/');"
		"	_root.check_equals(_target, '/');"
		"	_root.check_equals(typeof(_name), 'string');"
		"	_root.check_equals(_name, '');"
		"	_root.testno++;"
		"	_root.note(_root.testno+'. Depress the mouse button inside the square.');"
		"} else {"
		//"	_root.note('SWFBUTTON_MOUSEDOWN');"
		// need MOUSEDOWN for MOUSEUPOUTSIDE
		"	if ( _root.testno != 5 && _root.testno != 10 && _root.testno != 15 ) {"
		"		_root.fail('Unexpectedly got SWFBUTTON_MOUSEDOWN event (testno:'+_root.testno+')');"
		"	}"
		"}"
		), SWFBUTTON_MOUSEDOWN);

	SWFButton_addAction(bu, compileSWFActionCode(
		"_root.msg='MouseUp';"
		"if ( _root.testno == 3 || _root.testno == 8 || _root.testno == 13 ) {"
		"	_root.check_equals(_root.printBounds(getBounds()), '-0.05,-0.05 40.05,40.05');"
		/* Target of button action is the button's parent sprite */
		"	_root.check_equals(_target, '/square1');"
		"	setTarget('/');"
		"	_root.check_equals(_target, '/');"
		"	_root.testno++;"
		"	_root.note(_root.testno+'. Move the mouse pointer off the square.');"
		"} else {"
		//"	_root.note('SWFBUTTON_MOUSEUP');"
		"	_root.fail('Unexpectedly got SWFBUTTON_MOUSEUP event (testno:'+_root.testno+')');"
		"}"
		), SWFBUTTON_MOUSEUP);

	/* SWFBUTTON_MOUSEUPOUTSIDE *should* be invoked !! */
	SWFButton_addAction(bu, compileSWFActionCode(
		"_root.msg='MouseUpOutside';"
		"if ( _root.testno == 5 || _root.testno == 10 || _root.testno == 15 ) {"
		"	_root.check_equals(_root.printBounds(getBounds()), '-0.05,-0.05 40.05,40.05');"
		/* Target of button action is the button's parent sprite */
		"	_root.check_equals(_target, '/square1');"
		"	_root.check_equals(_name, 'square1');"
		"	setTarget('/');"
		"	_root.check_equals(_target, '/');"
		"	_root.nextFrame();"
		"} else {"
		//"	_root.note('SWFBUTTON_MOUSEUPOUTSIDE');"
		"	_root.fail('Unexpectedly got SWFBUTTON_MOUSEUPOUTSIDE event (testno:'+_root.testno+')');"
		"}"
		), SWFBUTTON_MOUSEUPOUTSIDE);

	/* Keypress */
	SWFButton_addAction(bu, compileSWFActionCode(
		"_root.note('KeyPress: a');"
		//"_root.check(Key.isDown('a'));"
	), SWFBUTTON_KEYPRESS('a'));
	SWFButton_addAction(bu, compileSWFActionCode(
		"_root.note('KeyPress: b');"
		//"_root.check(Key.isDown('b'));"
	), SWFBUTTON_KEYPRESS('b'));

	it = SWFMovieClip_add(mc, (SWFBlock)bu);
	SWFDisplayItem_setName(it, "button");
	SWFMovieClip_nextFrame(mc); /* showFrame */

	it = SWFMovie_add(mo, (SWFBlock)mc);
	return it;
}
示例#2
0
/// This test checks the event order of key events.
//
/// Known listeners are:
/// 1. MovieClips with a defined key event
/// 2. Button with a defined key event
/// 3. Anything added to Key listeners in ActionScript.
//
/// The test adds objects in this order:
//
/// Frame 1:
/// 1. mc1
/// 3. button1 (responds to 'a')
/// 3. o1 (actionscript key listener object)
/// 4. mc2
///
/// Frame 2:
/// 5. button2 (responds to 'a')
/// 6. button3 (responds to 'b')
/// 3. o2 (actionscript key listener object)
//
/// The test shows that, irrespective of construction order:
/// 1. MovieClips are notified first
/// 2. ActionScript listeners are notified second.
/// 3. Buttons are notified last.
//
/// Additionally:
//
/// 1. Only one button action can respond to any key.
int
main(int argc, char** argv)
{
  SWFMovie mo;
  SWFMovieClip mc, dejagnuclip;
  SWFButtonRecord br;
  SWFButton bu;
  SWFDisplayItem it1, it, it2;
  SWFShape sh1, sh2;

  const char *srcdir=".";
  if ( argc>1 ) 
    srcdir=argv[1];
  else
  {
      fprintf(stderr, "Usage: %s <mediadir>\n", argv[0]);
      return 1;
  }

  Ming_init();
  mo = newSWFMovieWithVersion(OUTPUT_VERSION);
  SWFMovie_setDimension(mo, 800, 600);

  SWFMovie_setRate (mo, 12.0);

  dejagnuclip = get_dejagnu_clip((SWFBlock)get_default_font(srcdir),
          10, 0, 0, 800, 600);
  SWFMovie_add(mo, (SWFBlock)dejagnuclip);

  SWFMovie_nextFrame(mo);

  add_actions(mo,
          "_root.order = '';"
          "_root.note('Press \"a\" then \"b\"');"
          "_root.note('Do not press any other keys!');"
          );

  mc = newSWFMovieClip();
  it = SWFMovie_add(mo, (SWFBlock)mc);
  SWFDisplayItem_setName(it, "mc1");
  SWFDisplayItem_addAction(it,
          newSWFAction("trace('mc1'); _root.order += 'mc1,';"),
          SWFACTION_KEYDOWN);

  bu = newSWFButton();
  
  sh1 = make_fill_square(200, 0, 40, 40, 0, 0, 0, 0, 0, 0);
  sh2 = make_fill_square(200, 0, 40, 40, 0, 0, 0, 0, 255, 0);

  br = SWFButton_addCharacter(bu, (SWFCharacter)sh1, SWFBUTTON_HIT);
  SWFButtonRecord_setDepth(br, 3);
  
  br = SWFButton_addCharacter(bu, (SWFCharacter)sh1, SWFBUTTON_UP);
  SWFButtonRecord_setDepth(br, 4);

  br = SWFButton_addCharacter(bu, (SWFCharacter)sh2, SWFBUTTON_OVER);
  SWFButtonRecord_setDepth(br, 5);

  SWFButton_addAction(bu,
          newSWFAction("trace('button1'); _root.order += 'button1,';"),
		  SWFBUTTON_KEYPRESS('a'));

  it1 = SWFMovie_add(mo, (SWFBlock)bu);
  SWFDisplayItem_setName(it, "button1");

  SWFMovie_add(mo, newSWFAction(
              "o1 = {};"
              "o1.onKeyDown = function() {"
              "    trace('o1'); "
              "    _root.order += 'o1,';"
              "};"
              "Key.addListener(o1);"
              ));

  mc = newSWFMovieClip();
  it = SWFMovie_add(mo, (SWFBlock)mc);
  SWFDisplayItem_setName(it, "mc2");
  SWFDisplayItem_addAction(it,
          newSWFAction("trace('mc2'); _root.order += 'mc2,';"),
          SWFACTION_KEYDOWN);

  SWFMovie_nextFrame(mo); // Frame 2

  bu = newSWFButton();
  
  sh1 = make_fill_square(240, 0, 40, 40, 0, 0, 0, 0, 0, 0);
  sh2 = make_fill_square(240, 0, 40, 40, 0, 0, 0, 0, 255, 0);

  br = SWFButton_addCharacter(bu, (SWFCharacter)sh1, SWFBUTTON_HIT);
  SWFButtonRecord_setDepth(br, 3);
  
  br = SWFButton_addCharacter(bu, (SWFCharacter)sh1, SWFBUTTON_UP);
  SWFButtonRecord_setDepth(br, 4);

  br = SWFButton_addCharacter(bu, (SWFCharacter)sh2, SWFBUTTON_OVER);
  SWFButtonRecord_setDepth(br, 5);

  SWFButton_addAction(bu,
          newSWFAction("trace('button2'); _root.order += 'button2,';"),
		  SWFBUTTON_KEYPRESS('a'));

  it2 = SWFMovie_add(mo, (SWFBlock)bu);
  SWFDisplayItem_setName(it, "button2");
  
  bu = newSWFButton();
  
  sh1 = make_fill_square(280, 0, 40, 40, 0, 0, 0, 0, 0, 0);
  sh2 = make_fill_square(280, 0, 40, 40, 0, 0, 0, 0, 255, 0);

  br = SWFButton_addCharacter(bu, (SWFCharacter)sh1, SWFBUTTON_HIT);
  SWFButtonRecord_setDepth(br, 3);
  
  br = SWFButton_addCharacter(bu, (SWFCharacter)sh1, SWFBUTTON_UP);
  SWFButtonRecord_setDepth(br, 4);

  br = SWFButton_addCharacter(bu, (SWFCharacter)sh2, SWFBUTTON_OVER);
  SWFButtonRecord_setDepth(br, 5);

  SWFButton_addAction(bu,
          newSWFAction(
              "trace('button3'); "
              "_root.order += 'button3,';"
              "play();"
              ),
		  SWFBUTTON_KEYPRESS('b'));

  it = SWFMovie_add(mo, (SWFBlock)bu);
  SWFDisplayItem_setName(it, "button3");
  
  SWFMovie_add(mo, newSWFAction(
              "o2 = {};"
              "o2.onKeyDown = function() {"
              "    trace('o2'); "
              "    _root.order += 'o2,';"
              "};"
              "Key.addListener(o2);"
              "stop();"
              ));

  SWFMovie_nextFrame(mo);

  xcheck_equals(mo, "_root.order",
          "'mc2,mc1,o1,o2,button1,mc2,mc1,o1,o2,button3,'");
  
  SWFMovie_nextFrame(mo);

  SWFDisplayItem_remove(it2);
  
  add_actions(mo,
          "_root.order = '';"
          "_root.note('Press \"a\" then \"b\" again');"
          "_root.note('Do not press any other keys!');"
          "stop();"
          );
  SWFMovie_nextFrame(mo);

  // Check that removing the second button associated with 'a' does not
  // remove the key trigger for button2. There's no reason to think it should,
  // but it could happen if it's implemented badly!
  check_equals(mo, "_root.order",
          "'mc2,mc1,o1,o2,button1,mc2,mc1,o1,o2,button3,'");

  SWFMovie_add(mo, newSWFAction("stop();"));

  //Output movie
  puts("Saving " OUTPUT_FILENAME );
  SWFMovie_save(mo, OUTPUT_FILENAME);

  return 0;
}