PageRenderTime 4908ms CodeModel.GetById 28ms RepoModel.GetById 27ms app.codeStats 0ms

/org/tuio/debug/TuioDebug.as

http://tuio-as3.googlecode.com/
ActionScript | 640 lines | 377 code | 77 blank | 186 comment | 37 complexity | 0562529c0c344db2e89b73972ccb44ec MD5 | raw file
  1. package org.tuio.debug
  2. {
  3. import flash.display.BitmapData;
  4. import flash.display.DisplayObject;
  5. import flash.display.DisplayObjectContainer;
  6. import flash.display.Sprite;
  7. import flash.display.Stage;
  8. import flash.geom.Matrix;
  9. import flash.geom.Point;
  10. import flash.text.Font;
  11. import flash.text.TextField;
  12. import flash.text.TextFieldAutoSize;
  13. import flash.text.TextFormat;
  14. import org.tuio.*;
  15. /**
  16. *
  17. * <p>implements the interface <code>ITuioListener</code> to show debug information about all tuio cursors and objects
  18. * that are prevailing in the application.</p>
  19. *
  20. * <p>The appearance of the cursors and objects is controlled by the classes <code>TuioDebugCursor</code> and
  21. * <code>TuioDebugObject</code>. Their appearance can be tweaked with multiple settings. Additionally, custom debug cursor
  22. * and object implementations can be set via the functions <code>customCursorSprite</code> and <code>customObjectClass</code>.</p>
  23. *
  24. * @see org.tuio.ITuioListener
  25. * @see TuioDebugCursor
  26. * @see ITuioDebugCursor
  27. * @see TuioDebugObject
  28. * @see ITuioDebugObject
  29. *
  30. * @author Johannes Luderschmidt
  31. *
  32. */
  33. public class TuioDebug implements ITuioListener{
  34. [Embed(source="/org/tuio/assets/fonts.swf", fontName="Arial")]
  35. private var Arial:Class;
  36. private var arialFont:Font;
  37. private var stage:Stage;
  38. private var tuioClient:TuioClient;
  39. private var fseq:uint;
  40. private var cursors:Array;
  41. private var objects:Array;
  42. private var blobs:Array;
  43. private var _showCursors:Boolean = true;
  44. private var _showObjects:Boolean = true;
  45. private var _showBlobs:Boolean = true;
  46. private var _showDebugText:Boolean = true;
  47. private var _debugTextColor:Number = 0x0;
  48. private var _cursorRadius:Number = 13;
  49. private var _cursorColor:Number = 0x0;
  50. private var _cursorAlpha:Number = 0;
  51. private var _cursorLineThickness:Number = 3;
  52. private var _cursorLineColor:Number = 0x0;
  53. private var _cursorLineAlpha:Number = 0.5;
  54. private var _customCursorSprite:Class;
  55. private var _objectWidth:Number = 80;
  56. private var _objectHeight:Number = 80;
  57. private var _objectColor:Number = 0x0;
  58. private var _objectAlpha:Number = 0.5;
  59. private var _objectLineThickness:Number = 0;
  60. private var _objectLineColor:Number = 0x0;
  61. private var _objectLineAlpha:Number = 0;
  62. private var _customObjectClass:Class;
  63. private static var allowInst:Boolean;
  64. private static var inst:TuioDebug;
  65. public function TuioDebug(stage:Stage){
  66. if (!allowInst) {
  67. throw new Error("Error: Instantiation failed: Use TuioDebug.getInstance() instead of new.");
  68. }else{
  69. this.stage = stage;
  70. fseq = 0;
  71. cursors = [];
  72. objects = [];
  73. blobs = [];
  74. _customObjectClass = TuioDebugObject;
  75. _customCursorSprite = TuioDebugCursor;
  76. this.arialFont = new Arial();
  77. }
  78. }
  79. /**
  80. * initializes Singleton instance of TuioDebug. Must be called before <code>getInstance()</code>
  81. * can be called.
  82. *
  83. * @param stage
  84. * @return Singleton instance of TuioDebug.
  85. *
  86. */
  87. public static function init(stage:Stage):TuioDebug{
  88. if(inst == null){
  89. allowInst = true;
  90. inst = new TuioDebug(stage);
  91. allowInst = false;
  92. }
  93. return inst;
  94. }
  95. /**
  96. * Singleton instance of TuioDebug.
  97. *
  98. * @return Singleton instance of TuioDebug.
  99. *
  100. */
  101. public static function getInstance():TuioDebug{
  102. if(inst == null){
  103. throw new Error("Please initialize with method init(...) first!");
  104. }
  105. return inst;
  106. }
  107. /**
  108. * Called if a new object was tracked.
  109. * @param tuioObject the received /tuio/2Dobj.
  110. */
  111. public function addTuioObject(tuioObject:TuioObject):void{
  112. addTuioObjectWithDebugOption(tuioObject, false);
  113. }
  114. /**
  115. * creates actual debug representation of TUIO object and shows it on screen
  116. *
  117. * @param tuioObject the current TUIO object
  118. * @param debugMode sets whether the TUIO id should be shown (debugMode == false) or
  119. * whether only the hint 'Debug' should be shown (debugMode == true) as the debug TUIO session id
  120. * starts
  121. * with the highest possible unsigned integer value and decrements for each other TUIO debug
  122. * element in order to not interfere with regular TUIO session ids from a tracker or from
  123. * the TUIO debug application. Thus, the high session ids would be looking awkwardly and only
  124. * the 'Debug' string is being shown.
  125. *
  126. */
  127. public function addTuioObjectWithDebugOption(tuioObject:TuioObject, debugMode:Boolean):void{
  128. var objectSprite:Sprite;
  129. if(_customObjectClass == TuioDebugObject){
  130. objectSprite = new TuioDebugObject(tuioObject.classID, tuioObject.sessionID, tuioObject.a, _objectWidth, _objectHeight, _objectColor, _objectAlpha,_objectLineThickness, _objectLineColor, _objectLineAlpha, tuioObject.source);
  131. }else{
  132. objectSprite = new _customObjectClass(tuioObject);
  133. if(!(objectSprite is ITuioDebugObject)){
  134. throw new Error("Custom Tuio Object class must implement ITuioDebugObject.");
  135. }
  136. }
  137. var objectObject:Object = new Object();
  138. if(_showObjects){
  139. objectSprite.x = tuioObject.x*stage.stageWidth;
  140. objectSprite.y = tuioObject.y*stage.stageHeight;
  141. objectSprite.rotation = tuioObject.a/Math.PI*180;
  142. objectObject.object = objectSprite;
  143. objectObject.sessionID = tuioObject.sessionID;
  144. objectObject.source = tuioObject.source;
  145. objects.push(objectObject);
  146. stage.addChild(objectSprite);
  147. if(this.showDebugText){
  148. var label:TextField = new TextField();
  149. label.autoSize = TextFieldAutoSize.LEFT;
  150. label.selectable = false;
  151. label.background = false;
  152. label.border = false;
  153. label.text = generateObjectLabelText(objectSprite.x, objectSprite.y, tuioObject.classID, tuioObject.sessionID, debugMode);
  154. label.defaultTextFormat = debugTextFormat();
  155. label.setTextFormat(debugTextFormat());
  156. label.embedFonts = true;
  157. objectSprite.addChild(label);
  158. label.x = Math.round(_objectWidth/2);
  159. label.y = -Math.round(label.height/2);
  160. objectObject.label = label;
  161. }
  162. }
  163. }
  164. /**
  165. * updates the display of the TUIO debug object
  166. *
  167. * @param tuioObject The received /tuio/2Dobj.
  168. */
  169. public function updateTuioObject(tuioObject:TuioObject):void{
  170. updateTuioObjectWithDebugOption(tuioObject, false);
  171. }
  172. /**
  173. * updates the display of the TUIO debug object.
  174. *
  175. * @param tuioObject The received /tuio/2Dobj.
  176. * @param debugMode sets whether the TUIO id should be shown (debugMode == false) or
  177. * whether only the hint 'Debug' should be shown (debugMode == true) as the debug TUIO session id
  178. * starts
  179. * with the highest possible unsigned integer value and decrements for each other TUIO debug
  180. * element in order to not interfere with regular TUIO session ids from a tracker or from
  181. * the TUIO debug application. Thus, the high session ids would be looking awkwardly and only
  182. * the 'Debug' string is being shown.
  183. *
  184. */
  185. public function updateTuioObjectWithDebugOption(tuioObject:TuioObject, debugMode:Boolean):void{
  186. for each(var object:Object in objects){
  187. if(object.sessionID == tuioObject.sessionID){
  188. var debugObject:DisplayObjectContainer = object.object as DisplayObjectContainer;
  189. debugObject.x = tuioObject.x*stage.stageWidth;
  190. debugObject.y = tuioObject.y*stage.stageHeight;
  191. debugObject.rotation = tuioObject.a/Math.PI*180;
  192. if(_showDebugText){
  193. object.label.text = generateObjectLabelText(object.object.x, object.object.y, tuioObject.classID, tuioObject.sessionID, debugMode);
  194. object.label.setTextFormat(debugTextFormat());
  195. }
  196. break;
  197. }
  198. }
  199. }
  200. /**
  201. * Called if a tracked object was removed.
  202. *
  203. * @param tuioObject The values of the received /tuio/2Dobj.
  204. */
  205. public function removeTuioObject(tuioObject:TuioObject):void{
  206. var i:Number = 0;
  207. for each(var object:Object in objects){
  208. if(object.sessionID == tuioObject.sessionID){
  209. stage.removeChild(object.object);
  210. objects.splice(i,1);
  211. break;
  212. }
  213. i=i+1;
  214. }
  215. }
  216. /**
  217. * width of debug object rectangle.
  218. *
  219. */
  220. public function get objectWidth():Number{
  221. return _objectWidth;
  222. }
  223. public function set objectWidth(objectWidth:Number):void{
  224. _objectWidth = objectWidth;
  225. }
  226. /**
  227. * height of debug object rectangle.
  228. *
  229. */
  230. public function get objectHeight():Number{
  231. return _objectHeight;
  232. }
  233. public function set objectHeight(objectHeight:Number):void{
  234. _objectHeight = objectHeight;
  235. }
  236. /**
  237. * color of the filling of debug object rectangle.
  238. *
  239. */
  240. public function get objectColor():Number{
  241. return _objectColor;
  242. }
  243. public function set objectColor(objectColor:Number):void{
  244. _objectColor = objectColor;
  245. }
  246. /**
  247. * alpha of the filling of debug object rectangle.
  248. *
  249. */
  250. public function get objectAlpha():Number{
  251. return _objectAlpha;
  252. }
  253. public function set objectAlpha(objectAlpha:Number):void{
  254. _objectAlpha = objectAlpha;
  255. }
  256. /**
  257. * thickness of the line around a debug object rectangle.
  258. *
  259. */
  260. public function get objectLineThickness():Number{
  261. return _objectLineThickness;
  262. }
  263. public function set objectLineThickness(objectLineThickness:Number):void{
  264. _objectLineThickness = objectLineThickness;
  265. }
  266. /**
  267. * color of the line around a debug object rectangle.
  268. *
  269. */
  270. public function get objectLineColor():Number{
  271. return _objectLineColor;
  272. }
  273. public function set objectLineColor(objectLineColor:Number):void{
  274. _objectLineColor = objectLineColor;
  275. }
  276. /**
  277. * alpha of the line around a debug object rectangle.
  278. *
  279. */
  280. public function get objectLineAlpha():Number{
  281. return _objectLineAlpha;
  282. }
  283. public function set objectLineAlpha(objectLineAlpha:Number):void{
  284. _objectLineAlpha = objectLineAlpha;
  285. }
  286. /**
  287. * sets base class for the Sprite that should be drawn on screen when a new
  288. * object is added via a Tuio message.
  289. *
  290. */
  291. public function get customObjectClass():Class{
  292. return _customObjectClass;
  293. }
  294. public function set customObjectClass (customObjectClass:Class):void{
  295. _customObjectClass = customObjectClass;
  296. }
  297. /**
  298. * Called if a new cursor was tracked.
  299. * @param tuioObject The values of the received /tuio/**Dcur.
  300. */
  301. public function addTuioCursor(tuioCursor:TuioCursor):void{
  302. var cursorSprite:Sprite;
  303. if(_customCursorSprite == TuioDebugCursor){
  304. cursorSprite = new TuioDebugCursor(_cursorRadius,_cursorColor, _cursorAlpha, _cursorLineThickness, _cursorLineColor, _cursorLineAlpha);
  305. }else{
  306. try{
  307. cursorSprite = new _customCursorSprite(tuioCursor);
  308. }catch(error:Error){
  309. cursorSprite = new _customCursorSprite();
  310. }
  311. if(!(cursorSprite is ITuioDebugCursor)){
  312. throw new Error("Custom Tuio Debug Cursor class must implement ITuioDebugCursor.");
  313. }
  314. }
  315. (cursorSprite as ITuioDebugCursor).sessionId = tuioCursor.sessionID;
  316. (cursorSprite as ITuioDebugCursor).source = tuioCursor.source;
  317. var cursorObject:Object = new Object();
  318. if(_showCursors){
  319. cursorSprite.x = tuioCursor.x*stage.stageWidth;
  320. cursorSprite.y = tuioCursor.y*stage.stageHeight;
  321. cursorObject.cursor = cursorSprite;
  322. cursorObject.sessionID = tuioCursor.sessionID;
  323. cursorObject.source = tuioCursor.source;
  324. cursors.push(cursorObject);
  325. stage.addChild(cursorSprite);
  326. if(_showDebugText){
  327. var label:TextField = new TextField();
  328. label.autoSize = TextFieldAutoSize.LEFT;
  329. label.selectable = false;
  330. label.background = false;
  331. label.border = false;
  332. label.text = generateCursorLabelText(cursorSprite.x, cursorSprite.y, tuioCursor.sessionID, tuioCursor.source);
  333. label.defaultTextFormat = debugTextFormat();
  334. label.setTextFormat(debugTextFormat());
  335. label.embedFonts = true;
  336. cursorSprite.addChild(label);
  337. label.x = _cursorRadius+3;
  338. label.y = -Math.round(label.height/2);
  339. cursorObject.label = label;
  340. }
  341. }
  342. }
  343. /**
  344. * Called if a tracked cursor was updated.
  345. * @param tuioCursor The values of the received /tuio/2Dcur.
  346. */
  347. public function updateTuioCursor(tuioCursor:TuioCursor):void{
  348. for each(var cursor:Object in cursors){
  349. if(cursor.sessionID == tuioCursor.sessionID && cursor.source == tuioCursor.source){
  350. cursor.cursor.x = tuioCursor.x*stage.stageWidth;
  351. cursor.cursor.y = tuioCursor.y*stage.stageHeight;
  352. if(_showDebugText){
  353. cursor.label.text = generateCursorLabelText(cursor.cursor.x, cursor.cursor.y, tuioCursor.sessionID, tuioCursor.source);
  354. }
  355. break;
  356. }
  357. }
  358. }
  359. /**
  360. * Called if a tracked cursor was removed.
  361. * @param tuioCursor The values of the received /tuio/2Dcur.
  362. */
  363. public function removeTuioCursor(tuioCursor:TuioCursor):void{
  364. var i:Number = 0;
  365. for each(var cursor:Object in cursors){
  366. if(cursor.sessionID == tuioCursor.sessionID && cursor.source == tuioCursor.source){
  367. stage.removeChild(cursor.cursor);
  368. cursors.splice(i,1);
  369. break;
  370. }
  371. i=i+1;
  372. }
  373. }
  374. private function generateCursorLabelText(xVal:Number, yVal:Number, id:Number, source:String):String{
  375. var cursorLabel:String;
  376. cursorLabel = "x: " + xVal + "\ny: " + yVal + "\nsessionId: " + id+ "\nsource: " + source;
  377. return cursorLabel;
  378. }
  379. private function debugTextFormat():TextFormat{
  380. var format:TextFormat = new TextFormat();
  381. format.font = this.arialFont.fontName;
  382. format.color = this.debugTextColor;
  383. format.size = 11;
  384. format.underline = false;
  385. return format;
  386. }
  387. /**
  388. * Called if a new blob was tracked.
  389. * @param tuioBlob The values of the received /tuio/**Dblb.
  390. */
  391. public function addTuioBlob(tuioBlob:TuioBlob):void{
  392. if(this.showBlobs){
  393. this.showCursors = true;
  394. addTuioCursor(new TuioCursor("2dcur", tuioBlob.sessionID, tuioBlob.x, tuioBlob.y, tuioBlob.z,tuioBlob.X, tuioBlob.Y, tuioBlob.Z, tuioBlob.m, tuioBlob.frameID, "TuioDebug"));
  395. }
  396. }
  397. /**
  398. * Called if a tracked blob was updated.
  399. * @param tuioBlob The values of the received /tuio/**Dblb.
  400. */
  401. public function updateTuioBlob(tuioBlob:TuioBlob):void{
  402. if(_showBlobs){
  403. _showCursors = true;
  404. updateTuioCursor(new TuioCursor("2dcur", tuioBlob.sessionID, tuioBlob.x, tuioBlob.y, tuioBlob.z,tuioBlob.X, tuioBlob.Y, tuioBlob.Z, tuioBlob.m, tuioBlob.frameID, "TuioDebug"));
  405. }
  406. }
  407. /**
  408. * Called if a tracked blob was removed.
  409. * @param tuioBlob The values of the received /tuio/**Dblb.
  410. */
  411. public function removeTuioBlob(tuioBlob:TuioBlob):void{
  412. if(_showBlobs){
  413. _showCursors = true;
  414. removeTuioCursor(new TuioCursor("2dcur", tuioBlob.sessionID, tuioBlob.x, tuioBlob.y, tuioBlob.z,tuioBlob.X, tuioBlob.Y, tuioBlob.Z, tuioBlob.m, tuioBlob.frameID, "TuioDebug"));
  415. }
  416. }
  417. public function newFrame(id:uint):void {
  418. this.fseq = id;
  419. }
  420. private function generateObjectLabelText(xVal:Number, yVal:Number, objectId:Number, sessionId:Number, debugMode:Boolean=false):String{
  421. var objectLabel:String;
  422. if(!debugMode){
  423. objectLabel = "x: " + xVal + "\ny: " + yVal + "\nfiducialId: " + objectId+ "\nsessionId: " + sessionId;
  424. }else{
  425. objectLabel = "x: " + xVal + "\ny: " + yVal + "\nfiducialId: " + objectId+ "\nsessionId: Debug";
  426. }
  427. return objectLabel;
  428. }
  429. /**
  430. * radius of the debug cursor circle.
  431. *
  432. */
  433. public function get cursorRadius():Number{
  434. return _cursorRadius;
  435. }
  436. public function set cursorRadius(cursorRadius:Number):void{
  437. _cursorRadius = cursorRadius;
  438. }
  439. /**
  440. * color of the filling of the debug cursor circle.
  441. *
  442. */
  443. public function get cursorColor():Number{
  444. return _cursorColor;
  445. }
  446. public function set cursorColor(cursorColor:Number):void{
  447. _cursorColor = cursorColor;
  448. }
  449. /**
  450. * alpha of the filling of the debug cursor circle.
  451. *
  452. */
  453. public function get cursorAlpha():Number{
  454. return _cursorAlpha;
  455. }
  456. public function set cursorAlpha(cursorAlpha:Number):void{
  457. _cursorAlpha = cursorAlpha;
  458. }
  459. /**
  460. * thickness of the line around a debug cursor circle.
  461. *
  462. */
  463. public function get cursorLineThickness():Number{
  464. return _cursorLineThickness;
  465. }
  466. public function set cursorLineThickness(cursorLineThickness:Number):void{
  467. _cursorLineThickness = cursorLineThickness;
  468. }
  469. /**
  470. * color of the line around a debug cursor circle.
  471. *
  472. */
  473. public function get cursorLineColor():Number{
  474. return _cursorLineColor;
  475. }
  476. public function set cursorLineColor(cursorLineColor:Number):void{
  477. _cursorLineColor = cursorLineColor;
  478. }
  479. /**
  480. * alpha of the line around a debug cursor circle.
  481. *
  482. */
  483. public function get cursorLineAlpha():Number{
  484. return _cursorLineAlpha;
  485. }
  486. public function set cursorLineAlpha(cursorLineAlpha:Number):void{
  487. _cursorLineAlpha = cursorLineAlpha;
  488. }
  489. /**
  490. * controls whether debug text (session id, x position, y position and fiducial id) should be shown next to
  491. * a debug cursor or debug object.
  492. *
  493. * @param showDebugText
  494. *
  495. */
  496. public function set showDebugText(showDebugText:Boolean):void{
  497. _showDebugText = showDebugText;
  498. }
  499. public function get showDebugText():Boolean{
  500. return _showDebugText;
  501. }
  502. /**
  503. * sets base class for the Sprite that should be drawn on screen when a new
  504. * cursor is added via a Tuio message.
  505. *
  506. * @param customCursorSprite class name of class that should be used as debug cursor information.
  507. *
  508. */
  509. public function set customCursorSprite (customCursorSprite:Class):void{
  510. _customCursorSprite = customCursorSprite;
  511. }
  512. /**
  513. * returns base class of the Sprite that is being drawn on screen when a new
  514. * cursor is added via a Tuio message.
  515. *
  516. * @return class of debug cursor sprite.
  517. *
  518. */
  519. public function get customCursorSprite():Class{
  520. return _customCursorSprite;
  521. }
  522. /**
  523. * controls whether debug information for objects is shown.
  524. *
  525. * @param showCursors
  526. *
  527. */
  528. public function set showCursors(showCursors:Boolean):void{
  529. _showCursors = showCursors;
  530. }
  531. public function get showCursors():Boolean{
  532. return _showCursors;
  533. }
  534. /**
  535. * controls whether debug information for objects is shown.
  536. *
  537. * @param showCursors
  538. *
  539. */
  540. public function set showObjects(showObjects:Boolean):void{
  541. _showObjects = showObjects;
  542. }
  543. public function get showObjects():Boolean{
  544. return _showObjects;
  545. }
  546. public function get debugTextColor():Number
  547. {
  548. return _debugTextColor;
  549. }
  550. public function set debugTextColor(value:Number):void
  551. {
  552. _debugTextColor = value;
  553. }
  554. public function get showBlobs():Boolean
  555. {
  556. return _showBlobs;
  557. }
  558. public function set showBlobs(value:Boolean):void
  559. {
  560. _showBlobs = value;
  561. }
  562. }
  563. }