PageRenderTime 50ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/flash/src/tuio/flash/events/TUIO.as

https://github.com/proflexor/MikesFlickrWall
ActionScript | 664 lines | 473 code | 38 blank | 153 comment | 70 complexity | f484916be063cbb3afc89f352b5cc379 MD5 | raw file
  1. /*
  2. * TODO: Make into a proper singleton
  3. * FIXME: Initilization Bug
  4. * TODO: Implement SWIPES and other events see: TouchEvent
  5. */
  6. package tuio.flash.events {
  7. import flash.display.DisplayObject;
  8. import flash.display.DisplayObjectContainer;
  9. import flash.display.Sprite;
  10. import flash.display.Stage;
  11. import flash.events.DataEvent;
  12. import flash.events.Event;
  13. import flash.events.IOErrorEvent;
  14. import flash.events.MouseEvent;
  15. import flash.events.ProgressEvent;
  16. import flash.events.SecurityErrorEvent;
  17. import flash.geom.Point;
  18. import flash.net.NetConnection;
  19. import flash.net.Responder;
  20. import flash.net.URLLoader;
  21. import flash.net.URLRequest;
  22. import flash.net.XMLSocket;
  23. import flash.text.TextField;
  24. import flash.text.TextFieldAutoSize;
  25. import flash.text.TextFormat;
  26. import flash.utils.getTimer;
  27. //import caurina.transitions.Tweener;
  28. public class TUIO
  29. {
  30. //private static var INSTANCE:TUIO;
  31. private static var STAGE:Stage;
  32. private static var SOCKET:XMLSocket;
  33. private static var SOCKET_STATUS:Boolean;
  34. private static var HOST:String;
  35. private static var PORT:Number;
  36. private static var FRAME_RATE:Number;
  37. private static var FRAME_COUNT:Number;
  38. //-------------------------------------- DEBUG VARS
  39. internal static var DEBUG:Boolean;
  40. private static var INITIALIZED:Boolean = false;
  41. private static var PLAYBACK:Boolean;
  42. private static var RECORDING:Boolean;
  43. private static var DEBUG_TEXT:TextField;
  44. private static var DEBUG_BUTTON:Sprite;
  45. private static var RECORD_BUTTON:Sprite;
  46. private static var PLAYBACK_BUTTON:Sprite;
  47. private static var PLAYBACK_URL:String;
  48. private static var PLAYBACK_LOADER:URLLoader;
  49. private static var PLAYBACK_XML:XML;
  50. private static var RECORDED_XML:XML;
  51. private static var SERVICE:NetConnection;
  52. private static var RESPONDER:Responder;
  53. //--------------------------------------
  54. private static var ID_ARRAY:Array;
  55. private static var EVENT_ARRAY:Array;
  56. private static var OBJECT_ARRAY:Array;
  57. //--------------------------------------
  58. private static var SWIPE_THRESHOLD:Number = 1000;
  59. private static var HOLD_THRESHOLD:Number = 4000;
  60. //---------------------------------------------------------------------------------------------------------------------------------------------
  61. // CONSTRUCTOR - SINGLETON
  62. //---------------------------------------------------------------------------------------------------------------------------------------------
  63. public static function init ($STAGE:DisplayObjectContainer, $HOST:String, $PORT:Number, $PLAYBACK_URL:String, $DEBUG:Boolean = true):void
  64. {
  65. if (INITIALIZED) { trace("TUIO Already Initialized!"); return; }
  66. // FIXME: VERIFY STAGE IS AVAILABLE
  67. STAGE = $STAGE.stage;
  68. STAGE.align = "TL";
  69. STAGE.scaleMode = "noScale";
  70. HOST=$HOST;
  71. PORT=$PORT;
  72. SERVICE = new NetConnection();
  73. SERVICE.connect("http://touchlib.com/amfphp/gateway.php");
  74. PLAYBACK_URL = $PLAYBACK_URL;
  75. DEBUG = $DEBUG;
  76. INITIALIZED = true;
  77. RECORDING = false;
  78. PLAYBACK = false;
  79. OBJECT_ARRAY = new Array();
  80. ID_ARRAY = new Array();
  81. EVENT_ARRAY = new Array();
  82. if(DEBUG)
  83. {
  84. //activateDebugMode();
  85. }
  86. else
  87. {
  88. RECORDED_XML = new XML();
  89. RECORDED_XML = <OSCPackets></OSCPackets>;
  90. RECORDING = false;
  91. }
  92. SOCKET = new XMLSocket();
  93. SOCKET.addEventListener(Event.CLOSE, closeHandler);
  94. SOCKET.addEventListener(Event.CONNECT, connectHandler);
  95. SOCKET.addEventListener(DataEvent.DATA, dataHandler);
  96. SOCKET.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  97. //SOCKET.addEventListener(ProgressEvent.PROGRESS, progressHandler);
  98. SOCKET.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  99. startSocket(null);
  100. //socketStatus(null); http://nuigroup.com/forums/viewthread/3516/
  101. }
  102. //---------------------------------------------------------------------------------------------------------------------------------------------
  103. // PUBLIC METHODS
  104. //---------------------------------------------------------------------------------------------------------------------------------------------
  105. /*
  106. public static function addEventListener(e:EventDispatcher):void
  107. {
  108. EVENT_ARRAY.push(e);
  109. }
  110. */
  111. public static function returnBlobs():Array{
  112. return OBJECT_ARRAY;
  113. }
  114. //---------------------------------------------------------------------------------------------------------------------------------------------
  115. public static function addObjectListener(id:Number, reciever:Object):void
  116. {
  117. var tmpObj:TUIOObject = getObjectById(id);
  118. if(tmpObj)
  119. {
  120. tmpObj.addListener(reciever);
  121. }
  122. }
  123. //---------------------------------------------------------------------------------------------------------------------------------------------
  124. public static function removeObjectListener(id:Number, reciever:Object):void
  125. {
  126. var tmpObj:TUIOObject = getObjectById(id);
  127. if(tmpObj)
  128. {
  129. tmpObj.removeListener(reciever);
  130. }
  131. }
  132. //---------------------------------------------------------------------------------------------------------------------------------------------
  133. public static function getObjectById(id:Number):TUIOObject
  134. {
  135. if(id == 0)
  136. {
  137. return new TUIOObject("mouse", 0, STAGE.mouseX, STAGE.mouseY, 0, 0, 0, 0, 10, 10, null);
  138. }
  139. for(var i:int=0; i<OBJECT_ARRAY.length; i++)
  140. {
  141. if(OBJECT_ARRAY[i].ID == id)
  142. {
  143. return OBJECT_ARRAY[i];
  144. }
  145. }
  146. return null;
  147. }
  148. //---------------------------------------------------------------------------------------------------------------------------------------------
  149. public static function startSocket(e:Event):void
  150. {
  151. try
  152. {
  153. SOCKET.connect(HOST, PORT);
  154. }
  155. catch (e:Error) { throw(e) }
  156. }
  157. public static function stopSocket(e:Event):void
  158. {
  159. try
  160. {
  161. SOCKET.close();
  162. }
  163. catch (e:Error) { }
  164. }
  165. //---------------------------------------------------------------------------------------------------------------------------------------------
  166. // PRIVATE METHODS
  167. //---------------------------------------------------------------------------------------------------------------------------------------------
  168. private static function processMessage(msg:XML):void
  169. {
  170. // SPEEDTEST: For speed testing the function
  171. if (DEBUG)
  172. var time:Number = getTimer();
  173. var fseq:String;
  174. var node:XML;
  175. for each(node in msg.MESSAGE)
  176. {
  177. if(node.ARGUMENT[0] && node.ARGUMENT[0].@VALUE == "fseq")
  178. fseq = node.ARGUMENT[1].@VALUE;
  179. }
  180. for each(node in msg.MESSAGE)
  181. {
  182. if(node.ARGUMENT[0] && node.ARGUMENT[0].@VALUE == "alive")
  183. {
  184. for each (var obj1:TUIOObject in OBJECT_ARRAY)
  185. {
  186. obj1.TUIO_ALIVE = false;
  187. }
  188. var newIdArray:Array = new Array();
  189. for each(var aliveItem:XML in node.ARGUMENT.(@VALUE != "alive"))
  190. {
  191. if(getObjectById(aliveItem.@VALUE))
  192. getObjectById(aliveItem.@VALUE).TUIO_ALIVE = true;
  193. }
  194. ID_ARRAY = newIdArray;
  195. }
  196. }
  197. for each(node in msg.MESSAGE)
  198. {
  199. if(node.ARGUMENT[0])
  200. {
  201. var type:String;
  202. if(node.@NAME == "/tuio/2Dobj")
  203. {
  204. /*
  205. // fixme: ensure everything is working properly here.
  206. type = node.ARGUMENT[0].@VALUE;
  207. if(type == "set")
  208. {
  209. var sID = node.ARGUMENT[1].@VALUE;
  210. var id = node.ARGUMENT[2].@VALUE;
  211. var x = Number(node.ARGUMENT[3].@VALUE) * STAGE.stageWidth;
  212. var y = Number(node.ARGUMENT[4].@VALUE) * STAGE.stageHeight;
  213. var a = Number(node.ARGUMENT[5].@VALUE);
  214. var X = Number(node.ARGUMENT[6].@VALUE);
  215. var Y = Number(node.ARGUMENT[7].@VALUE);
  216. var A = Number(node.ARGUMENT[8].@VALUE);
  217. var m = node.ARGUMENT[9].@VALUE;
  218. var r = node.ARGUMENT[10].@VALUE;
  219. // send object update event..
  220. var objArray:Array = STAGE.getObjectsUnderPoint(new Point(x, y));
  221. var stagePoint:Point = new Point(x,y);
  222. var displayObjArray:Array = STAGE.getObjectsUnderPoint(stagePoint);
  223. var dobj = null;
  224. // if(displayObjArray.length > 0)
  225. // dobj = displayObjArray[displayObjArray.length-1];
  226. var tuioobj = getObjectById(id);
  227. if(tuioobj == null)
  228. {
  229. tuioobj = new TUIOObject("2Dobj", id, x, y, X, Y, sID, a, 0, 0, dobj);
  230. STAGE.addChild(tuioobj.TUIO_CURSOR);
  231. OBJECT_ARRAY.push(tuioobj);
  232. tuioobj.notifyCreated();
  233. } else {
  234. tuioobj.TUIO_CURSOR.x = x;
  235. tuioobj.TUIO_CURSOR.y = y;
  236. tuioobj.x = x;
  237. tuioobj.y = y;
  238. tuioobj.oldX = tuioobj.x;
  239. tuioobj.oldY = tuioobj.y;
  240. tuioobj.dX = X;
  241. tuioobj.dY = Y;
  242. tuioobj.setObjOver(dobj);
  243. tuioobj.notifyMoved();
  244. }
  245. try
  246. {
  247. if(tuioobj.TUIO_OBJECT && tuioobj.TUIO_OBJECT.parent)
  248. {
  249. var localPoint:Point = tuioobj.TUIO_OBJECT.parent.globalToLocal(stagePoint);
  250. tuioobj.TUIO_OBJECT.dispatchEvent(new TouchEvent(TouchEvent.MOUSE_MOVE, true, false, x, y, localPoint.x, localPoint.y, tuioobj.oldX, tuioobj.oldY, tuioobj.TUIO_OBJECT, false,false,false, true, m, "2Dobj", id, sID, a));
  251. }
  252. } catch (e)
  253. {
  254. }
  255. }
  256. */
  257. }
  258. else if(node.@NAME == "/tuio/2Dcur")
  259. {
  260. type = node.ARGUMENT[0].@VALUE;
  261. if(type == "set")
  262. {
  263. var id:int;
  264. var x:Number,
  265. y:Number,
  266. X:Number,
  267. Y:Number,
  268. m:Number,
  269. wd:Number = 0,
  270. ht:Number = 0;
  271. try
  272. {
  273. id = node.ARGUMENT[1].@VALUE;
  274. x = Number(node.ARGUMENT[2].@VALUE) * STAGE.stageWidth;
  275. y = Number(node.ARGUMENT[3].@VALUE) * STAGE.stageHeight;
  276. X = Number(node.ARGUMENT[4].@VALUE);
  277. Y = Number(node.ARGUMENT[5].@VALUE);
  278. m = Number(node.ARGUMENT[6].@VALUE);
  279. if(node.ARGUMENT[7])
  280. wd = Number(node.ARGUMENT[7].@VALUE) * STAGE.stageWidth;
  281. if(node.ARGUMENT[8])
  282. ht = Number(node.ARGUMENT[8].@VALUE) * STAGE.stageHeight;
  283. } catch (e:Error)
  284. {
  285. trace("Error Parsing TUIO XML");
  286. }
  287. //trace("Blob : ("+id + ")" + x + " " + y + " " + wd + " " + ht);
  288. var stagePoint:Point = new Point(x,y);
  289. var displayObjArray:Array = STAGE.getObjectsUnderPoint(stagePoint);
  290. //trace(displayObjArray);
  291. var dobj:DisplayObject = null;
  292. if(displayObjArray.length > 0)
  293. dobj = displayObjArray[displayObjArray.length-1];
  294. trace("TOUCHED: " + dobj.name);
  295. var tuioobj:TUIOObject = getObjectById(id);
  296. if(tuioobj == null)
  297. {
  298. tuioobj = new TUIOObject("2Dcur", id, x, y, X, Y, -1, 0, wd, ht, dobj);
  299. STAGE.addChild(tuioobj.TUIO_CURSOR);
  300. OBJECT_ARRAY.push(tuioobj);
  301. tuioobj.notifyCreated();
  302. } else {
  303. tuioobj.TUIO_CURSOR.x = x;
  304. tuioobj.TUIO_CURSOR.y = y;
  305. tuioobj.oldX = tuioobj.x;
  306. tuioobj.oldY = tuioobj.y;
  307. tuioobj.x = x;
  308. tuioobj.y = y;
  309. tuioobj.width = wd;
  310. tuioobj.height = ht;
  311. tuioobj.area = wd * ht;
  312. tuioobj.dX = X * STAGE.stageWidth;
  313. tuioobj.dY = Y * STAGE.stageHeight;
  314. tuioobj.setObjOver(dobj);
  315. var d:Date = new Date();
  316. // bugbee fix
  317. if(!( int(X*1000) == 0 && int(Y*1000) == 0) )
  318. {
  319. tuioobj.notifyMoved();
  320. }
  321. if( int(Y*250) == 0 && int(Y*250) == 0) {
  322. if(Math.abs(d.time - tuioobj.lastModifiedTime) > HOLD_THRESHOLD)
  323. {
  324. for(var ndx:int=0; ndx<EVENT_ARRAY.length; ndx++)
  325. {
  326. EVENT_ARRAY[ndx].dispatchEvent(tuioobj.getTouchEvent(tuio.flash.events.TUIOTouchEvent.HOLD));
  327. }
  328. tuioobj.lastModifiedTime = d.time;
  329. }
  330. }
  331. /*
  332. * // SWIP RIGHT OR LEFT (with thresholds)
  333. if(blobs.length == 1)
  334. {
  335. if((blobs[0].history.length) >= 10 && (blobs[0].history.length) <= 50 ){
  336. if((my-blobs[0].history[0].y) <= 25 && (my-blobs[0].history[0].y) >= -25){
  337. if((mx-blobs[0].history[0].x) >= 75 || (mx-blobs[0].history[0].x) <= -75){
  338. if(mx > blobs[0].history[0].x){
  339. EVENT_ARRAY[ndx].dispatchEvent(tuioobj.getTouchEvent(TouchEvent.SWIPE_RIGHT));
  340. }
  341. else{
  342. EVENT_ARRAY[ndx].dispatchEvent(tuioobj.getTouchEvent(TouchEvent.SWIPE_LEFT));
  343. }
  344. }
  345. }//else()
  346. }
  347. }
  348. */
  349. }
  350. try
  351. {
  352. if(tuioobj.TUIO_OBJECT && tuioobj.TUIO_OBJECT.parent)
  353. {
  354. var localPoint:Point = tuioobj.TUIO_OBJECT.parent.globalToLocal(stagePoint);
  355. tuioobj.TUIO_OBJECT.dispatchEvent(new TUIOTouchEvent(TUIOTouchEvent.MOUSE_MOVE, true, false, x, y, localPoint.x, localPoint.y, tuioobj.dX, tuioobj.dY, tuioobj.oldX, tuioobj.oldY, tuioobj.TUIO_OBJECT, false,false,false, true, m, "2Dcur", id, 0, 0));
  356. }
  357. } catch (e:Error)
  358. {
  359. trace("(" + e + ") Dispatch event failed " + tuioobj.ID);
  360. }
  361. }
  362. }
  363. }
  364. }
  365. if(DEBUG)
  366. {
  367. DEBUG_TEXT = new TextField();
  368. DEBUG_TEXT.text = "";
  369. DEBUG_TEXT.visible = false;
  370. }
  371. for (var i:int=0; i<OBJECT_ARRAY.length; i++ )
  372. {
  373. if(OBJECT_ARRAY[i].TUIO_ALIVE == false)
  374. {
  375. OBJECT_ARRAY[i].notifyRemoved();
  376. STAGE.removeChild(OBJECT_ARRAY[i].TUIO_CURSOR);
  377. OBJECT_ARRAY.splice(i, 1);
  378. i--;
  379. } else {
  380. if(DEBUG)
  381. {
  382. var tmp:int = (int(OBJECT_ARRAY[i].area)/-100000);
  383. DEBUG_TEXT.appendText(" " + (i + 1) +" - " +OBJECT_ARRAY[i].ID + " X:" + int(OBJECT_ARRAY[i].x) + " Y:" + int(OBJECT_ARRAY[i].y) +
  384. " A:" + int(tmp) + " \n");
  385. DEBUG_TEXT.visible = true;
  386. }
  387. }
  388. }
  389. // SPEEDTEST END
  390. //trace("SPEEDTEST: " + time + " - " + getTimer() + " = " + (getTimer() - time));
  391. if(DEBUG)
  392. DEBUG_TEXT.appendText(" T - " + (getTimer() - time)+" \n");
  393. }
  394. //---------------------------------------------------------------------------------------------------------------------------------------------
  395. private static function activateDebugMode():void
  396. {
  397. var format:TextFormat = new TextFormat("Verdana", 10, 0xFFFFFF);
  398. DEBUG_TEXT = new TextField();
  399. DEBUG_TEXT.defaultTextFormat = format;
  400. DEBUG_TEXT.autoSize = TextFieldAutoSize.LEFT;
  401. DEBUG_TEXT.background = true;
  402. DEBUG_TEXT.backgroundColor = 0x000000;
  403. DEBUG_TEXT.border = true;
  404. DEBUG_TEXT.borderColor = 0x333333;
  405. DEBUG_TEXT.x = STAGE.stageWidth-200;
  406. DEBUG_TEXT.y = 25;
  407. STAGE.addChild( DEBUG_TEXT );
  408. STAGE.setChildIndex(DEBUG_TEXT, STAGE.numChildren-1);
  409. RECORDED_XML = <OSCPackets></OSCPackets>;
  410. //
  411. DEBUG_BUTTON = new Sprite();
  412. DEBUG_BUTTON.graphics.beginFill(0xFFFFFF);
  413. DEBUG_BUTTON.graphics.drawRect(10, 25, 50, 50);
  414. DEBUG_BUTTON.addEventListener(MouseEvent.CLICK, toggleDebug);
  415. DEBUG_BUTTON.addEventListener(TUIOTouchEvent.CLICK, toggleDebug);
  416. DEBUG_BUTTON.alpha = 0.85;
  417. STAGE.addChildAt(DEBUG_BUTTON, STAGE.numChildren-1);
  418. RECORD_BUTTON = new Sprite();
  419. RECORD_BUTTON.graphics.beginFill(0xFF0000);
  420. RECORD_BUTTON.graphics.drawRect(10, 75, 50, 50);
  421. RECORD_BUTTON.addEventListener(MouseEvent.CLICK, toggleRecord);
  422. RECORD_BUTTON.addEventListener(TUIOTouchEvent.CLICK, toggleRecord);
  423. RECORD_BUTTON.alpha = 0.25;
  424. STAGE.addChildAt(RECORD_BUTTON, STAGE.numChildren-1);
  425. PLAYBACK_BUTTON = new Sprite();
  426. PLAYBACK_BUTTON.graphics.beginFill(0x00FF00);
  427. PLAYBACK_BUTTON.graphics.drawRect(10, 125, 50, 50);
  428. PLAYBACK_BUTTON.addEventListener(MouseEvent.CLICK, togglePlayback);
  429. PLAYBACK_BUTTON.addEventListener(TUIOTouchEvent.CLICK, togglePlayback);
  430. PLAYBACK_BUTTON.alpha = 0.25;
  431. STAGE.addChildAt(PLAYBACK_BUTTON, STAGE.numChildren - 1);
  432. //STAGE.addEventListener(MouseEvent.MOUSE_MOVE, socketStatus);
  433. //STAGE.addEventListener(TouchEvent.MOUSE_MOVE, socketStatus);
  434. }
  435. //---------------------------------------------------------------------------------------------------------------------------------------------
  436. private static function socketStatus(e:Event):void
  437. {
  438. SOCKET_STATUS = SOCKET.connected;
  439. if (!SOCKET_STATUS)
  440. {
  441. DEBUG_TEXT.text = " TUIO Socket Not Found... \n Host - "+HOST+"\n Port - "+PORT+" ";
  442. STAGE.addEventListener(Event.ENTER_FRAME, startSocket);
  443. //STAGE.removeEventListener(MouseEvent.MOUSE_MOVE, socketStatus);
  444. //STAGE.removeEventListener(TouchEvent.MOUSE_MOVE, socketStatus);
  445. DEBUG_BUTTON.graphics.clear();
  446. DEBUG_BUTTON.graphics.beginFill(0xFF0000,0.5);
  447. DEBUG_BUTTON.graphics.drawRect(10, 25, 50, 50);
  448. DEBUG_TEXT.x = 65;
  449. } else
  450. {
  451. DEBUG_TEXT.x = STAGE.stageWidth-200;
  452. DEBUG_TEXT.text = "";
  453. STAGE.removeEventListener(Event.ENTER_FRAME, startSocket);
  454. DEBUG_BUTTON.graphics.clear();
  455. DEBUG_BUTTON.graphics.beginFill(0xFFFFFF,1);
  456. DEBUG_BUTTON.graphics.drawRect(10, 25, 50, 50);
  457. }
  458. }
  459. //---------------------------------------------------------------------------------------------------------------------------------------------
  460. private static function togglePlayback(e:Event):void
  461. {
  462. if (PLAYBACK) return;
  463. PLAYBACK_URL = "http://touchlib.com/amfphp/services/test.xml";
  464. if(PLAYBACK_URL != "")
  465. {
  466. PLAYBACK_BUTTON.alpha = 0.9;
  467. PLAYBACK_LOADER = new URLLoader();
  468. PLAYBACK_LOADER.addEventListener("complete", xmlPlaybackLoaded);
  469. PLAYBACK_LOADER.addEventListener( ProgressEvent.PROGRESS, onProgressHandler);
  470. PLAYBACK_LOADER.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  471. PLAYBACK_LOADER.load(new URLRequest(PLAYBACK_URL));
  472. }
  473. }
  474. //---------------------------------------------------------------------------------------------------------------------------------------------
  475. private static function xmlPlaybackLoaded(e:Event):void
  476. {
  477. trace("Playing from XML file...");
  478. PLAYBACK_XML = new XML(PLAYBACK_LOADER.data);
  479. PLAYBACK_LOADER.close();
  480. FRAME_COUNT = PLAYBACK_XML.OSCPACKET.length();
  481. STAGE.addEventListener(Event.ENTER_FRAME, frameUpdate);
  482. FRAME_RATE = STAGE.frameRate;
  483. STAGE.frameRate = 30;
  484. }
  485. //---------------------------------------------------------------------------------------------------------------------------------------------
  486. private static function frameUpdate(e:Event):void
  487. {
  488. if(PLAYBACK_XML && PLAYBACK_XML.OSCPACKET && PLAYBACK_XML.OSCPACKET[0])
  489. {
  490. RECORD_BUTTON.removeEventListener(MouseEvent.CLICK, toggleRecord);
  491. RECORD_BUTTON.removeEventListener(TUIOTouchEvent.CLICK, toggleRecord);
  492. //
  493. PLAYBACK = true;
  494. processMessage(PLAYBACK_XML.OSCPACKET[0]);
  495. delete PLAYBACK_XML.OSCPACKET[0];
  496. DEBUG_TEXT.visible = true;
  497. DEBUG_TEXT.appendText (" " + "F - " + PLAYBACK_XML.OSCPACKET.length() + " / " + FRAME_COUNT + " ");
  498. if (PLAYBACK_XML.OSCPACKET.length() < 1)
  499. {
  500. RECORD_BUTTON.addEventListener(MouseEvent.CLICK, toggleRecord);
  501. RECORD_BUTTON.addEventListener(TUIOTouchEvent.CLICK, toggleRecord);
  502. //
  503. PLAYBACK = false;
  504. DEBUG_TEXT.text = '';
  505. PLAYBACK_BUTTON.alpha = 0.25;
  506. STAGE.removeEventListener(Event.ENTER_FRAME, frameUpdate);
  507. trace('START FPS - ' + FRAME_RATE);
  508. trace('PLAYBACK FPS - ' + STAGE.frameRate);
  509. STAGE.frameRate = FRAME_RATE;
  510. trace('END FPS - '+STAGE.frameRate);
  511. }
  512. }
  513. }
  514. //---------------------------------------------------------------------------------------------------------------------------------------------
  515. private static function toggleDebug(e:Event):void
  516. {
  517. stopSocket(null);
  518. startSocket(null);
  519. if(!DEBUG){
  520. DEBUG=true;
  521. e.target.alpha = 0.85;
  522. //e.target.scaleX = e.target.scaleY = 1.0;
  523. RECORD_BUTTON.visible = true;
  524. PLAYBACK_BUTTON.visible = true;
  525. DEBUG_TEXT.visible = true;
  526. //DEBUG_TEXT.text = '';
  527. }
  528. else{
  529. DEBUG=false;
  530. e.target.alpha = 0.25;
  531. //e.target.scaleX = e.target.scaleY = 0.5;
  532. RECORD_BUTTON.visible = false;
  533. PLAYBACK_BUTTON.visible = false;
  534. DEBUG_TEXT.visible = false;
  535. //DEBUG_TEXT.text = '';
  536. }
  537. }
  538. //---------------------------------------------------------------------------------------------------------------------------------------------
  539. private static function toggleRecord(e:Event):void
  540. {
  541. //
  542. var RESPONDER:Responder = new Responder(saveSessionResult, onFault);
  543. if(!RECORDING){
  544. PLAYBACK_BUTTON.removeEventListener(MouseEvent.CLICK, togglePlayback);
  545. PLAYBACK_BUTTON.removeEventListener(TUIOTouchEvent.CLICK, togglePlayback);
  546. //
  547. RECORDING = true;
  548. e.target.alpha = 0.9;
  549. trace('-----------------------------------------------------------------------------------------------------');
  550. trace('-------------------------------------- Record ON ----------------------------------------------------');
  551. trace('-----------------------------------------------------------------------------------------------------');
  552. SERVICE.call("touchlib.clearSession", RESPONDER);
  553. RECORDED_XML = <OSCPackets></OSCPackets>;
  554. //
  555. }
  556. else {
  557. PLAYBACK_BUTTON.addEventListener(MouseEvent.CLICK, togglePlayback);
  558. PLAYBACK_BUTTON.addEventListener(TUIOTouchEvent.CLICK, togglePlayback);
  559. //
  560. RECORDING = false;
  561. e.target.alpha = 0.25;
  562. trace('-----------------------------------------------------------------------------------------------------');
  563. trace('-------------------------------------- Record OFF ---------------------------------------------------');
  564. trace('-----------------------------------------------------------------------------------------------------');
  565. SERVICE.call("touchlib.saveSession", RESPONDER, RECORDED_XML.toXMLString());
  566. //trace(RECORDED_XML.toString());
  567. RECORDED_XML = <OSCPackets></OSCPackets>;
  568. trace('-------------------------------------- Recording END ------------------------------------------------');
  569. }
  570. }
  571. //---------------------------------------------------------------------------------------------------------------------------------------------
  572. private static function saveSessionResult(result:String):void
  573. {
  574. DEBUG_TEXT.text = result;
  575. trace(result);
  576. }
  577. //---------------------------------------------------------------------------------------------------------------------------------------------
  578. private static function dataHandler(e:DataEvent):void
  579. {
  580. if(RECORDING)
  581. RECORDED_XML.appendChild( XML(e.data) );
  582. processMessage(XML(e.data));
  583. }
  584. //---------------------------------------------------------------------------------------------------------------------------------------------
  585. private static function closeHandler(event:Event):void
  586. {
  587. //trace("closeHandler: " + event);
  588. //socketStatus(null);
  589. }
  590. private static function connectHandler(event:Event):void
  591. {
  592. //trace("connectHandler: " + event);
  593. //socketStatus(null);
  594. }
  595. private static function onFault(e:Event ):void
  596. {
  597. //trace("There was a problem: " + e.description);
  598. }
  599. private static function ioErrorHandler(event:IOErrorEvent):void
  600. {
  601. trace("ioErrorHandler: " + event);
  602. }
  603. private static function onProgressHandler(e:ProgressEvent):void
  604. {
  605. var percent:Number = 100*(e.target.bytesLoaded/e.target.bytesTotal);
  606. DEBUG_TEXT.visible = true;
  607. DEBUG_TEXT.text = " Loading... "+int(percent)+"% ";
  608. }
  609. private static function securityErrorHandler(event:SecurityErrorEvent):void
  610. {
  611. trace("securityErrorHandler: " + event);
  612. }
  613. //---------------------------------------------------------------------------------------------------------------------------------------------
  614. }
  615. }