/src/com/pblabs/box2D/Box2DDebugComponent.as

https://github.com/joelhooks/PushButtonEngine
ActionScript | 204 lines | 158 code | 32 blank | 14 comment | 17 complexity | ea7c2eb63b56372f020197e3a8e5e153 MD5 | raw file
  1. /*******************************************************************************
  2. * PushButton Engine
  3. * Copyright (C) 2009 PushButton Labs, LLC
  4. * For more information see http://www.pushbuttonengine.com
  5. *
  6. * This file is licensed under the terms of the MIT license, which is included
  7. * in the License.html file at the root directory of this SDK.
  8. ******************************************************************************/
  9. package com.pblabs.box2D
  10. {
  11. import Box2D.Dynamics.b2DebugDraw;
  12. import com.pblabs.engine.debug.Logger;
  13. import com.pblabs.rendering2D.DisplayObjectRenderer;
  14. import flash.display.Sprite;
  15. import flash.geom.Point;
  16. /**
  17. * Helper component to visualize Box2D debug state. Properties let you
  18. * toggle what is drawn.
  19. */
  20. public class Box2DDebugComponent extends DisplayObjectRenderer
  21. {
  22. public function get spatialManager():Box2DManagerComponent
  23. {
  24. return _manager;
  25. }
  26. public function set spatialManager(value:Box2DManagerComponent):void
  27. {
  28. _manager = value;
  29. }
  30. public function get manager():Box2DManagerComponent
  31. {
  32. Logger.warn(this, "get manager", "manager is deprecated; switch to spatialManager.");
  33. return spatialManager;
  34. }
  35. public function set manager(value:Box2DManagerComponent):void
  36. {
  37. spatialManager = value;
  38. Logger.warn(this, "set manager", "manager is deprecated; switch to spatialManager.");
  39. }
  40. override public function get layerIndex():int
  41. {
  42. // Always draw last.
  43. if(scene && scene.layerCount)
  44. return scene.layerCount - 1;
  45. else
  46. return 0;
  47. }
  48. override protected function onAdd():void
  49. {
  50. super.onAdd();
  51. displayObject = new Sprite();
  52. _zIndex = 30000;
  53. _drawer.m_sprite = displayObject as Sprite;
  54. _drawer.m_fillAlpha = 0.3;
  55. _drawer.m_lineThickness = 1.0;
  56. applyDebugFlags();
  57. }
  58. override protected function onRemove():void
  59. {
  60. // Suppress auto-registration behavior.
  61. super.onRemove();
  62. }
  63. override protected function onReset():void
  64. {
  65. if (spatialManager)
  66. spatialManager.setDebugDrawer(_drawer);
  67. }
  68. public function get drawShapes():Boolean
  69. {
  70. return _drawShapes;
  71. }
  72. public function set drawShapes(value:Boolean):void
  73. {
  74. _drawShapes = value;
  75. applyDebugFlags();
  76. }
  77. public function get drawJoints():Boolean
  78. {
  79. return _drawJoints;
  80. }
  81. public function set drawJoints(value:Boolean):void
  82. {
  83. _drawJoints = value;
  84. applyDebugFlags();
  85. }
  86. public function get drawCoreShapes():Boolean
  87. {
  88. return _drawCoreShapes;
  89. }
  90. public function set drawCoreShapes(value:Boolean):void
  91. {
  92. _drawCoreShapes = value;
  93. applyDebugFlags();
  94. }
  95. public function get drawAABB():Boolean
  96. {
  97. return _drawAABB;
  98. }
  99. public function set drawAABB(value:Boolean):void
  100. {
  101. _drawAABB = value;
  102. applyDebugFlags();
  103. }
  104. public function get drawOBB():Boolean
  105. {
  106. return _drawOBB;
  107. }
  108. public function set drawOBB(value:Boolean):void
  109. {
  110. _drawOBB = value;
  111. applyDebugFlags();
  112. }
  113. public function get drawPairs():Boolean
  114. {
  115. return _drawPairs;
  116. }
  117. public function set drawPairs(value:Boolean):void
  118. {
  119. _drawPairs = value;
  120. applyDebugFlags();
  121. }
  122. public function get drawCenterOfMass():Boolean
  123. {
  124. return _drawCenterOfMass;
  125. }
  126. public function set drawCenterOfMass(value:Boolean):void
  127. {
  128. _drawCenterOfMass = value;
  129. applyDebugFlags();
  130. }
  131. public function applyDebugFlags():void
  132. {
  133. if (_drawShapes)
  134. _drawer.AppendFlags(b2DebugDraw.e_shapeBit);
  135. else
  136. _drawer.ClearFlags(b2DebugDraw.e_shapeBit);
  137. if (_drawJoints)
  138. _drawer.AppendFlags(b2DebugDraw.e_jointBit);
  139. else
  140. _drawer.ClearFlags(b2DebugDraw.e_jointBit);
  141. if (_drawCoreShapes)
  142. _drawer.AppendFlags(b2DebugDraw.e_coreShapeBit);
  143. else
  144. _drawer.ClearFlags(b2DebugDraw.e_coreShapeBit);
  145. if (_drawAABB)
  146. _drawer.AppendFlags(b2DebugDraw.e_aabbBit);
  147. else
  148. _drawer.ClearFlags(b2DebugDraw.e_aabbBit);
  149. if (_drawOBB)
  150. _drawer.AppendFlags(b2DebugDraw.e_obbBit);
  151. else
  152. _drawer.ClearFlags(b2DebugDraw.e_obbBit);
  153. if (_drawPairs)
  154. _drawer.AppendFlags(b2DebugDraw.e_pairBit);
  155. else
  156. _drawer.ClearFlags(b2DebugDraw.e_pairBit);
  157. if (_drawCenterOfMass)
  158. _drawer.AppendFlags(b2DebugDraw.e_centerOfMassBit);
  159. else
  160. _drawer.ClearFlags(b2DebugDraw.e_centerOfMassBit);
  161. }
  162. private var _manager:Box2DManagerComponent = null;
  163. protected var _drawer:b2DebugDraw = new b2DebugDraw();
  164. protected var _drawShapes:Boolean = true;
  165. protected var _drawJoints:Boolean = true;
  166. protected var _drawCoreShapes:Boolean = false;
  167. protected var _drawAABB:Boolean = false;
  168. protected var _drawOBB:Boolean = false;
  169. protected var _drawPairs:Boolean = false;
  170. protected var _drawCenterOfMass:Boolean = false;
  171. }
  172. }