sample #21
button
  1. <?php
  2. include("sample_utils.php"); // provides some utils and using mk
  3.  
  4. $swfFN = "button.swf"; // the file we'll generate
  5.  
  6. $btImageFN1 = "gimp_bt_off.png"; // image for button state off
  7. $btImageFN2 = "gimp_bt_over.png"; // button state over
  8. $btImageFN3 = "gimp_bt_down.png"; // state down
  9.  
  10. $swf = new MKswf($swfFN); // create a fresh swf
  11. $swf->set_swfDir(SERVER_MKTMP); // store in tmp
  12. $swf->set_stage_dimensions(170, 50); // stage dimensions
  13. $swf->set_backgroundColor("#0066FF"); // background color
  14.  
  15. $img = new Bitmap_item(); // image object, id = 1 by default
  16. $imgTD = $img->create_from_file($btImageFN1); // create from image file
  17. $swf->add_tagData($imgTD); // tag data
  18. $buttonWidth = $img->get_imageWidth(); // used for hit area
  19. $buttonHeight = $img->get_imageHeight(); //
  20.  
  21. $img = new Bitmap_item(2); //
  22. $imgTD = $img->create_from_file($btImageFN2); //
  23. $swf->add_tagData($imgTD); //
  24.  
  25. $img = new Bitmap_item(3); //
  26. $imgTD = $img->create_from_file($btImageFN3); //
  27. $swf->add_tagData($imgTD); //
  28.  
  29. $shape1 = new MKshape_container("", 4); // a shape that bound image #1
  30. $shape1->add_bitmap_fill(1); //
  31. $shape1->add_rect(0, 0, $buttonWidth, $buttonHeight); //
  32. $shapeTD1 = $shape1->update_tagData(); //
  33. $swf->add_tagData($shapeTD1); //
  34.  
  35. $shape2 = new MKshape_container("", 5); // bound image #2
  36. $shape2->add_bitmap_fill(2); //
  37. $shape2->add_rect(0, 0, $buttonWidth, $buttonHeight); //
  38. $shapeTD2 = $shape2->update_tagData(); //
  39. $swf->add_tagData($shapeTD2); //
  40.  
  41. $shape3 = new MKshape_container("", 6); // bound image #3
  42. $shape3->add_bitmap_fill(3); //
  43. $shape3->add_rect(0, 0, $buttonWidth, $buttonHeight); //
  44. $shapeTD3 = $shape3->update_tagData(); //
  45. $swf->add_tagData($shapeTD3); //
  46.  
  47. $shape4 = new MKshape_container("", 7); // hit area, only vector data
  48. $shape4->add_fillStyle_solid(); // black by default, color is pointless here
  49. $shape4->add_rect(0, 0, $buttonWidth, $buttonHeight); // use image width/height for hit area
  50. $shapeTD4 = $shape4->update_tagData(); //
  51. $swf->add_tagData($shapeTD4); //
  52.  
  53. $shapeAr = array($shape4, $shape3, $shape2, $shape1); // an array with shapes
  54. $button = new MKbutton_container("", 8); // a fresh button
  55. $button->move(10, 10); // place @10,10
  56. $buttonTD = $button->build_from_array($shapeAr); // use shape array for placing an setting states
  57. $buttonPB = $button->update_place_tagData(); // place tag data
  58. $swf->add_tagData($buttonTD); //
  59. $swf->add_tagData($buttonPB); //
  60.  
  61. $swf->swf_output(); // save
  62.  
  63. $swfHeader = $swf->fetch_assoc(); // prepare to publish
  64.  
  65. $str = render_swf_box($swfHeader, $imgDir, false);
  66. $str .= "<br/><strong>" .$swfFN ." created in :<br />" .SERVER_MKTMP ."</strong>";
  67. echo show_content($str, basename(__FILE__));
  68. ?>

buttons in swf are something complex object, they define three event "states": off, over, down and a phisical state: hit area, that is the active area of button reaction.
Despite to its complexity, IMHO buttons are almost unuseful, because their states don't fit well real world application (e.g. flip-flop state are not supported by definition). That's the reason why they are supported only after 18 months of development. For building button in mk you have to prepare the contribs and call build_from_array method.

There is not cases that a good actionscript + sprites can't be handled in better and smarter way, so professionals tend to ignore button design.
Anyway there is some case of convenient use of button, in the next sample we'll build a "trasparent hit area", that can be drawn in whatever we want shape, allowing to implement e.g. image maps or spot in virtual tour.
Another issue is related to actionscript: buttons can contain bytecode so if we want to completely deconstruct an swf we have to be able reading that.
It is suggested to avoid "button actionscript", it is obsolete and deprecated.

mikrokosmos