PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/php/mk/classes/composers/mk_composers.php

http://mikrokosmos.googlecode.com/
PHP | 549 lines | 353 code | 147 blank | 49 comment | 59 complexity | 4e3ef3006300b89df4a05cacbd292be8 MD5 | raw file
  1. <?php
  2. /*
  3. * Created on 11/ago/2008
  4. *
  5. * @package mikrokosmos
  6. * @author jaco_at_pixeldump
  7. * @description "hi level" classes to ease repetitive tasks
  8. */
  9. if(!class_exists("MKTagHeader")){ class MKTagHeader {}}
  10. class MKSwfComposer extends MKSwf {
  11. var $fc = array(); // array of frameComposers
  12. // the constructor
  13. function MKSwfComposer() {
  14. }
  15. function expand_timeline($numberOfFrames = 1){
  16. for($i = 0; $i < $numberOfFrames; $i++) $this->fc[] = new MKFrameComposer();
  17. }
  18. function reduce_timeline($numberOfFrames = 1){
  19. for($i = 0; i < $numberOfFrames; $i++) array_pop($this->fc);
  20. }
  21. function insert_frameAt($index = 0){
  22. /* TODO */
  23. }
  24. function remove_frameAt($index = 0){
  25. /* TODO */
  26. }
  27. function get_frameByIndex($index = 0){
  28. /* TODO */
  29. }
  30. function add_frameComposer(){
  31. /* TODO */
  32. }
  33. function save() {
  34. /* TODO */
  35. }
  36. }
  37. class MKFrameComposer extends SwfFrame {
  38. function MKFrameComposer(){
  39. }
  40. }
  41. class MKBitmapAssetComposer {
  42. var $swfVersion = 8;
  43. var $symbolName;
  44. var $swfFN;
  45. var $imageFN;
  46. var $alphaFN;
  47. var $imageDir = SERVER_MKIMAGES;
  48. var $alphaDir = SERVER_MKIMAGES;
  49. var $swf;
  50. var $bmp;
  51. var $bmpWidth;
  52. var $bmpHeight;
  53. // < swf9
  54. var $sc; // shape
  55. var $mc; // movieclip
  56. // >= swf9
  57. var $sn; // sceneNames
  58. var $dab; // dummmyAS3BitmapClass
  59. var $cn; // classNames
  60. var $po3;
  61. // the constructor
  62. function MKBitmapAssetComposer($swfVersion, $symbolName, $swfFN, $imageFN, $alphaFN = "",
  63. $imageDir = SERVER_MKIMAGES,
  64. $alphaDir = SERVER_MKIMAGES){
  65. $this->swfVersion = $swfVersion;
  66. $this->symbolName = $symbolName;
  67. $this->swfFN = $swfFN;
  68. $this->imageFN = $imageFN;
  69. $this->alphaFN = $alphaFN;
  70. $this->imageDir = $imageDir;
  71. $this->alphaFN = $alphaFN;
  72. $this->alphaDir = $alphaDir;
  73. $bs = getimagesize($imageDir .$imageFN);
  74. $this->bmpWidth = $bs[0];
  75. $this->bmpHeight = $bs[1];
  76. $this->swf = new MKSwf($swfFN, $swfVersion, $bs[0], $bs[1]);
  77. $this->swf->set_isCompressed("yes");
  78. $this->bmp = new BitmapItem();
  79. }
  80. //
  81. function build_bitmapTag($quality = 100){
  82. if(strlen($this->alphaFN)){
  83. $this->bmp->set_image_source($this->imageFN, $this->imageDir);
  84. $this->bmp->load_alpha_from_file($this->alphaFN, $this->alphaDir);
  85. $bmpTD = $this->bmp->update_lossy_tagData($quality);
  86. }
  87. else $bmpTD = $this->bmp->create_from_file($this->imageFN, $this->imageDir, $quality);
  88. return $bmpTD;
  89. }
  90. //
  91. function compose_AVM2Asset($bmpTD, $saveAsSprite = false){
  92. $this->swf->set_AS3_flag(true);
  93. $this->dab = new DummyAs3BitmapClass($this->symbolName);
  94. $this->sn = new MKSceneNames("", array("MKScene01"));
  95. $this->cn = new MKClassNames("");
  96. $this->cn->add_class_definition($this->symbolName);
  97. $snTD = $this->sn->update_tagData();
  98. $dabTD = $this->dab->update_tagData();
  99. $cnTD = $this->cn->update_tagData();
  100. $this->swf->add_tagData($snTD);
  101. $this->swf->add_tagData($dabTD);
  102. $this->swf->add_tagData($bmpTD);
  103. $this->swf->add_tagData($cnTD);
  104. if($saveAsSprite){
  105. $this->po3 = new MKPlaceObject3("", 1, 1, 1, true);
  106. $this->po3->set_pfHasCacheAsBitmap(true);
  107. $po3TD = $this->po3->update_tagData();
  108. $this->swf->add_tagData($po3TD);
  109. }
  110. }
  111. //
  112. function compose_AVM1Asset($bmpTD){
  113. $this->sc = new MKShapeContainer("", 2);
  114. $mad = array();
  115. $mad["x"] = 0;
  116. $mad["y"] = 0;
  117. $mad["width"] = $this->bmpWidth;
  118. $mad["height"] = $this->bmpHeight;
  119. $sc->add_bitmap_fill(1, CLIPPED_BITMAP_FILL, $mad);
  120. $sc->add_rect($mad["x"], $mad["y"], $mad["width"], $mad["height"]); // image bounding box
  121. $this->mc = new MKMovieClip("", 3);
  122. $this->sc->add_tagData($this->sc->update_place_tagData());
  123. $mcEoTD = $this->mc->set_linkage_name($this->symbolName);
  124. $mcTD = $this->mc->update_tagData();
  125. $this->swf->add_tagData($bmpTD);
  126. $this->swf->add_tagData($mcTD);
  127. $this->swf->add_tagData($mcEoTD);
  128. }
  129. //
  130. function save($quality = 100, $saveAsSprite = false){
  131. $bmpTD = $this->build_bitmapTag($quality);
  132. if($this->swfVersion <= 8) $this->compose_AVM1Asset($bmpTD);
  133. else $this->compose_AVM2Asset($bmpTD, $saveAsSprite);
  134. $this->swf->save();
  135. }
  136. }
  137. /**
  138. * create an swf with a symbolName
  139. * Sprite, MovieClip for AVM1/2, depending on $swfVersion value
  140. *
  141. */
  142. class MKDisplayObjectAssetComposer {
  143. var $swfVersion = 8;
  144. var $symbolName;
  145. var $swfFN = SWF_DEFAULT_NAME;
  146. var $svgFN = "";
  147. var $svgDir = SERVER_MKSVG;
  148. var $svgAr = array(); // list of svg files to build mc
  149. // each item is: svg -> an MKSimpleSvgReader instance
  150. // svgFN -> its source filename,
  151. // used for indexing
  152. var $frameNumber = 0; // mc timeline working frame
  153. var $depthAr = array(); // array of involved depth
  154. var $flFN = ""; // movieclip frame list source file
  155. var $swf;
  156. var $svg;
  157. var $po2; // used to place mc on stage
  158. // < swf9
  159. var $mc; // movieclip
  160. // >= swf9
  161. var $sn; // sceneNames
  162. var $dab; // dummmyAS3DisplayObjectClass
  163. var $cn; // classNames
  164. var $sc; // shape
  165. function MKDisplayObjectAssetComposer($swfVersion, $symbolName, $svgFN = "", $svgDir = SERVER_MKSVG){
  166. $this->swfVersion = $swfVersion;
  167. $this->symbolName = $symbolName;
  168. if(strlen($svgFN)) {
  169. $this->svgFN = $svgFN;
  170. $this->swfFN = basename($svgFN, ".svg") .".swf";
  171. }
  172. $this->svgDir = $svgDir;
  173. $this->swf = new MKSwf($this->swfFN, $swfVersion);
  174. $this->swf->set_isCompressed("yes");
  175. }
  176. //
  177. function set_mc($mc) {
  178. $this->mc = $mc;
  179. $this->classState = 1; // means "no mc build needed" /* TODO */
  180. }
  181. function get_mc() { return $this->mc; }
  182. //
  183. function set_fps($fps = 31) { $this->swf->set_fps($fps); }
  184. function timeline_from_directory ($svgArDir = SERVER_MKSVG){
  185. $placeDepth = 1;
  186. $this->svgDir = $svgArDir;
  187. if(!file_exists($svgArDir)) {
  188. log_write_entry(get_class($this) .": cannot build mc timeline from invalid svg directory");
  189. return;
  190. }
  191. $svgFiles = get_fileList($svgArDir, ".svg", false);
  192. $sCount = count($svgFiles);
  193. $idx = $this->add_svgItem($svgFiles[0]);
  194. $svg = $this->svgAr[$idx]["svg"];
  195. $this->mc = new MKMovieClip("", count($svgFiles) + 1);
  196. $this->mc->add_tagData($svg->update_place_tagData());
  197. $this->swf->set_stageSize($svg->get_svgWidth(), $svg->get_svgHeight());
  198. for($i = 1; $i < $sCount; $i++){
  199. $idx = $this->add_svgItem($svgFiles[$i]);
  200. $svg = $this->svgAr[$idx]["svg"];
  201. $this->mc->add_tagData($svg->update_place_tagData($placeDepth), $i + 1);
  202. }
  203. }
  204. //
  205. function create_mc_from_file($flFN = "", $flDir = SERVER_MKCODE){
  206. $flPath = $flDir .$flFN;
  207. $flXml = new XmlLib_xmlParser($flDir .$flFN);
  208. $doc = & $flXml->getDocument();
  209. $this->mc = new MKMovieClip();
  210. if($doc->nodeName == "framelist"){
  211. $this->parse_frameList($doc, $flDir);
  212. $this->mc->set_itemID(count($this->svgAr) + 1);
  213. }
  214. }
  215. //
  216. function parse_frameList($flNode, $flDir){
  217. $allowedTags = array("frameblock", "frame");
  218. $w = (int) $flNode->getAttribute("width");
  219. $h = (int) $flNode->getAttribute("height");
  220. if($w && $h) $this->swf->set_stageSize($w, $h);
  221. $useAS3 = $flNode->getAttribute("useAS3");
  222. if($useAS3) $this->swf->set_AS3_flag((bool) $useAS3);
  223. $fps = (int) $flNode->getAttribute("fps");
  224. if($fps) $this->swf->set_fps($fps);
  225. $bgColor = $flNode->getAttribute("backgroundColor");
  226. if($bgColor) $this->swf->set_backgroundColor(($bgColor));
  227. $source = $flNode->getAttribute("source");
  228. //if($source == ".") $this->svgDir = $flDir; // TODO: fix this in case of parent (../ and so on) directories
  229. if($source == ".") $this->svgDir = $flDir;
  230. $fl = $flNode->childNodes();
  231. foreach($fl as $f){
  232. $fName = $f->nodeName;
  233. if(in_array($fName, $allowedTags, true)){
  234. $evStr = "\$this-" .">parse_" .$fName ."(\$f);";
  235. eval($evStr);
  236. }
  237. }
  238. }
  239. //
  240. function parse_frameblock($fbNode) {
  241. $fNodes = array();
  242. $frames = array();
  243. $repeatType = $fbNode->getAttribute("repeat");
  244. $duration = $fbNode->getAttribute("duration");
  245. $repeatType = ($repeatType) ? $repeatType: "count";
  246. $duration = ($duration) ? $duration : 1;
  247. $fBlock = $fbNode->childNodes();
  248. // collect frame
  249. foreach($fBlock as $f){
  250. if($f->nodeName == "frame") $fNodes[] = $f;
  251. }
  252. $fCount = count($fNodes);
  253. if(strpos($duration, ":") !== false){ // duration is time based
  254. $fbNodeps = $this->swf->get_fps();
  255. $timeAr = split(":", $duration);
  256. $msec = (int) $timeAr[0] * 60000 + (int) $timeAr[1] * 1000 + (int) $timeAr[2];
  257. $mpf = 1000 / $fbNodeps;
  258. $totalFrameCount = floor($msec / $mpf);
  259. }
  260. else $totalFrameCount = $duration * $fCount; // duration is frame count based
  261. for($i = 0, $k = 0; $i < $totalFrameCount; $i++){
  262. /* TODO: manage repeatType:random */
  263. $frames[] = $fNodes[$k++];
  264. if($k >= $fCount) $k = 0;
  265. }
  266. if($repeatType == "randomGlobal") shuffle($frames);
  267. for($i = 0; $i < $totalFrameCount; $i++){
  268. $fNode = $frames[$i];
  269. $fNode->setAttribute("number", ($i + 1));
  270. $this->parse_frame($fNode);
  271. }
  272. }
  273. //
  274. function parse_frame($fNode) {
  275. $cNodes = $fNode->childNodes();
  276. $this->frameNumber++;
  277. if(count($cNodes)){
  278. foreach($cNodes as $c){
  279. if($c->nodeName == "svg") {
  280. $svg = $this->parse_svgNode($c);
  281. if($this->frameNumber > 1) {
  282. $placeDepth = max($c->getAttribute("depth"), 1);
  283. $this->mc->add_tagData($svg->update_place_tagData($placeDepth), $this->frameNumber);
  284. continue;
  285. }
  286. $this->mc->add_tagData($svg->update_place_tagData());
  287. }
  288. }
  289. }
  290. else $this->mc->add_frameData();
  291. }
  292. //
  293. function parse_svgNode($sNode){
  294. $svgFN = $sNode->getAttribute("source");
  295. $idx = $this->add_svgItem($svgFN);
  296. $svg = $this->svgAr[$idx]["svg"];
  297. $x = (float) $sNode->getAttribute("x");
  298. $y = (float) $sNode->getAttribute("y");
  299. $scaleX = (float) $sNode->getAttribute("scaleX") * 100;
  300. $scaleY = (float) $sNode->getAttribute("scaleY") * 100;
  301. $rotate = (float) $sNode->getAttribute("rotate");
  302. $x = ($x) ? $x : 0;
  303. $y = ($y) ? $y : 0;
  304. $scaleX = ($scaleX) ? $scaleX : 1;
  305. $scaleY = ($scaleY) ? $scaleY : 1;
  306. $rotate = ($rotate) ? $rotate : 0;
  307. if($x || $y) $svg->move($x, $y);
  308. if($scaleX != 1 || $scaleY != 1) $svg->scale($scaleX, $scaleY);
  309. if($rotate) $svg->rotate($rotate);
  310. $this->svgAr[$idx]["svg"] = $svg;
  311. return $svg;
  312. }
  313. // add an svg to svgAr, if there are not duplicates
  314. // return the index of svgItem added
  315. function add_svgItem($svgFN){
  316. $sCount = count($this->svgAr);
  317. $svgFileNames = array();
  318. for($i = 0; $i < $sCount; $i++)
  319. $svgFileNames[] = $this->svgAr[$i]["svgFN"];
  320. if(!$sCount || !in_array($svgFN, $svgFileNames, true)){ // item will be added
  321. $svgItem = array();
  322. $svgItem["svg"] = new MKSimpleSvgReader($svgFN, $this->svgDir, $sCount + 1);
  323. $svgItem["svgFN"] = $svgFN;
  324. $this->svgAr[] = $svgItem;
  325. return $sCount;
  326. }
  327. for($i = 0; $i < $sCount; $i++){
  328. $svgFileNames[] = $this->svgAr[$i]["svgFN"];
  329. if($svgFileNames[$i] == $svgFN) return $i; // the requested item is already present, return its index
  330. }
  331. log_write_entry(get_class($this) .": ERROR in add_svgItem");
  332. return -1; // ERROR
  333. }
  334. //
  335. function build_mc(){
  336. $this->mc = new MKMovieClip("", 2);
  337. $this->svg = new MKSimpleSvgReader($this->svgFN, $this->svgDir);
  338. $svgPoTD = $this->svg->update_place_tagData();
  339. $this->swf->set_stageSize($this->svg->get_svgWidth(), $this->svg->get_svgHeight());
  340. $this->mc->add_tagData($svgPoTD);
  341. }
  342. //
  343. function compose_AVM1Asset($placeOnStage = false){
  344. if(count($this->svgAr)){
  345. foreach($this->svgAr as $svg)
  346. $svgTDs[] = $svg->update_tagData();
  347. }
  348. else $svgTDs = array($this->svg->update_tagData());
  349. $mcTD = $this->mc->update_tagData();
  350. $mcEoTD = $this->mc->set_linkage_name($this->symbolName);
  351. foreach($svgTDs as $svgTD)
  352. $this->swf->add_tagData($svgTD);
  353. $this->swf->add_tagData($mcTD);
  354. $this->swf->add_tagData($mcEoTD);
  355. if($placeOnStage) $this->swf->add_tagData($this->mc->update_place_tagData());
  356. }
  357. //
  358. function compose_AVM2Asset($placeOnStage = false){
  359. $mcID = $this->mc->get_itemID();
  360. $this->swf->set_AS3_flag(true);
  361. $this->sn = new MKSceneNames("", array("MKScene01"));
  362. $this->cn = new MKClassNames("");
  363. $this->cn->add_class_definition($this->symbolName, $mcID);
  364. $this->dab = new DummyAs3DisplayObjectClass($this->symbolName);
  365. $snTD = $this->sn->update_tagData();
  366. $dabTD = $this->dab->update_tagData();
  367. $svgTDs = array();
  368. if(count($this->svgAr)){
  369. foreach($this->svgAr as $svgItem)
  370. $svgTDs[] = $svgItem["svg"]->update_tagData();
  371. }
  372. else $svgTDs = array($this->svg->update_tagData());
  373. $mcTD = $this->mc->update_tagData();
  374. $cnTD = $this->cn->update_tagData();
  375. $this->swf->add_tagData($snTD);
  376. $this->swf->add_tagData($dabTD);
  377. foreach($svgTDs as $svgTD)
  378. $this->swf->add_tagData($svgTD);
  379. $this->swf->add_tagData($mcTD);
  380. //$mc = new MKMovieClip("", 6);
  381. //$mcTD = $mc->update_tagData();
  382. //$this->swf->add_tagData($mcTD);
  383. $this->swf->add_tagData($cnTD);
  384. if($placeOnStage){
  385. $this->po2 = new MKPlaceObject2("", 1, $mcID);
  386. $this->po2->set_name("my" .$this->symbolName);
  387. $po2TD = $this->po2->update_tagData();
  388. $this->swf->add_tagData($po2TD);
  389. }
  390. }
  391. //
  392. function save($placeOnStage = false, $swfFN = "", $swfDir = ""){
  393. if(strlen($swfFN))
  394. $this->swf->set_swfFileName(basename($swfFN, ".swf") .".swf");
  395. if(strlen($swfDir)) $this->swf->set_swfDir($swfDir);
  396. if(strlen($this->svgFN)) $this->build_mc();
  397. if($this->swfVersion <= 8) $this->compose_AVM1Asset($placeOnStage);
  398. else $this->compose_AVM2Asset($placeOnStage);
  399. $this->swf->save();
  400. }
  401. }
  402. ?>