PageRenderTime 61ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/flashbackend/src/x3dom/FlashBackend.as

https://gitlab.com/oytunistrator/x3dom
ActionScript | 506 lines | 274 code | 88 blank | 144 comment | 26 complexity | e2e7d5c52ef6588f73a2cf6c40b6750b MD5 | raw file
  1. package x3dom
  2. {
  3. import flash.display.LoaderInfo;
  4. import flash.display.MovieClip;
  5. import flash.display.Sprite;
  6. import flash.display.Stage;
  7. import flash.display.Stage3D;
  8. import flash.display.StageAlign;
  9. import flash.display.StageScaleMode;
  10. import flash.display3D.Context3D;
  11. import flash.display3D.Context3DBlendFactor;
  12. import flash.display3D.Context3DProfile;
  13. import flash.display3D.Context3DRenderMode;
  14. import flash.events.Event;
  15. import flash.events.KeyboardEvent;
  16. import flash.events.MouseEvent;
  17. import flash.external.ExternalInterface;
  18. import flash.system.LoaderContext;
  19. import flash.system.Security;
  20. [SWF(backgroundColor="#000000", width="550", height="400", frameRate="120")]
  21. public class FlashBackend extends MovieClip
  22. {
  23. Security.allowDomain('*');
  24. [Embed(source="../../res/Library.swf", symbol="LoadingText")]
  25. private var LoadingScreen:Class;
  26. [Embed(source="../../res/Library.swf", symbol="InfoField")]
  27. private static var InfoField:Class;
  28. private static var _stage:Stage;
  29. /**
  30. * Handle 3D Scene managing and rendering
  31. */
  32. private static var _context3D:Context3D = null;
  33. /**
  34. * Handle 3D Scene managing
  35. */
  36. private var _scene:X3DScene = null;
  37. /**
  38. * Contains all AS3 calback functions for JS
  39. */
  40. private var _bridge:Bridge;
  41. /**
  42. * Handle 3D Scene rendering
  43. */
  44. private var _renderer:Renderer;
  45. //private var _renderer:LPPRenderer;
  46. /**
  47. * Index of our SWFObject in the JavaScript x3dom.canvases array
  48. */
  49. private var _canvasIdx:Number;
  50. /**
  51. * The current stage width in pixels
  52. */
  53. private static var _stageWidth:Number = 0;
  54. /**
  55. * The current stage height in pixels
  56. */
  57. private static var _stageHeight:Number = 0;
  58. /**
  59. * The current stage height in pixels
  60. */
  61. private var _mouseDragging:Boolean = false;
  62. /**
  63. * The current mouse x-position
  64. */
  65. private static var _mousePosX:Number = 0;
  66. /**
  67. * The current mouse y-position
  68. */
  69. private static var _mousePosY:Number = 0;
  70. /**
  71. * LoaderContext for loading cross-domain images
  72. */
  73. private static var _loaderContext:LoaderContext;
  74. /**
  75. * The current pressed mouse button 0=None, 1=Left, 2=Middle, 4=Right
  76. */
  77. private var _mouseButton:Number = 0;
  78. /**
  79. *
  80. */
  81. private static var _loadingScreen:Sprite;
  82. /**
  83. *
  84. */
  85. private static var _infoField:Sprite;
  86. /**
  87. *
  88. */
  89. private static var _texLoadWheel:Sprite;
  90. /**
  91. *
  92. */
  93. private var _renderType:String;
  94. /**
  95. * Main entry point of the x3dom flash renderer
  96. */
  97. public function FlashBackend()
  98. {
  99. _stage = stage;
  100. //Enable doubleClick feature for the stage
  101. _stage.doubleClickEnabled = true;
  102. //Set stage align to TopLeft
  103. _stage.align = StageAlign.TOP_LEFT;
  104. _stage.scaleMode = StageScaleMode.NO_SCALE;
  105. //Get FlashVars
  106. this.getFlashVars();
  107. //Init LoadingScreen
  108. this.initLoadingScreen();
  109. //Init EventListener for Mouse interaction
  110. this.initInfoField();
  111. //Init EventListener for Mouse interaction
  112. this.initEventListener();
  113. //Create Context3D
  114. this.createContext3D();
  115. //Create LoaderContext for crossdomain loading
  116. _loaderContext = new LoaderContext();
  117. _loaderContext.checkPolicyFile = true;
  118. //_loaderContext.securityDomain = SecurityDomain.currentDomain;
  119. //_loaderContext.applicationDomain = ApplicationDomain.currentDomain;
  120. }
  121. private function initLoadingScreen() : void
  122. {
  123. _loadingScreen = new LoadingScreen();
  124. _loadingScreen.x = _stageWidth/2;
  125. _loadingScreen.y = _stageHeight/2;
  126. addChild(_loadingScreen);
  127. }
  128. private function initInfoField() : void
  129. {
  130. _infoField = new InfoField();
  131. _infoField.x = _stageWidth - _infoField.width - 10;
  132. _infoField.y = 10;
  133. _infoField.visible = false;
  134. addChild(_infoField);
  135. }
  136. public static function getContext() : Context3D
  137. {
  138. return _context3D
  139. }
  140. public static function getHeight() : Number
  141. {
  142. return _stageHeight;
  143. }
  144. public static function getWidth() : Number
  145. {
  146. return _stageWidth;
  147. }
  148. public static function getMouseX() : Number
  149. {
  150. return _mousePosX;
  151. }
  152. public static function getMouseY() : Number
  153. {
  154. return _mousePosY;
  155. }
  156. public static function setFPS(fps:Number) : void
  157. {
  158. fps = Math.round(fps);
  159. InfoField(_infoField).txt_FPS.text = fps.toString();
  160. }
  161. public static function setObjs(objs:Number) : void
  162. {
  163. InfoField(_infoField).txt_Objs.text = objs.toString();
  164. }
  165. public static function setTris(tris:Number) : void
  166. {
  167. InfoField(_infoField).txt_Tris.text = tris.toString();
  168. }
  169. public static function stage() : Stage
  170. {
  171. return _stage;
  172. }
  173. public static function getLoaderContext() : LoaderContext
  174. {
  175. return _loaderContext;
  176. }
  177. /**
  178. * Get FlashVars from <object>-Tag
  179. */
  180. private function getFlashVars() : void
  181. {
  182. //Get flashVars
  183. var param:Object = LoaderInfo(this.root.loaderInfo).parameters;
  184. //Set canvasIdx from flashVars
  185. this._canvasIdx = Number(param.canvasIdx);
  186. //Set stageWidth and stageHeight from flashVars
  187. _stageWidth = _stage.stageWidth;//Number(param.width);
  188. _stageHeight = _stage.stageHeight;//Number(param.height);
  189. _renderType = String(param.renderType);
  190. }
  191. /**
  192. * Init EventListener for mouse interaction
  193. */
  194. private function initEventListener() : void
  195. {
  196. _stage.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
  197. _stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
  198. _stage.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN, handleRightMouseDown);
  199. _stage.addEventListener(MouseEvent.RIGHT_MOUSE_UP, handleMouseUp);
  200. _stage.addEventListener(MouseEvent.MIDDLE_MOUSE_DOWN, handleMiddleMouseDown);
  201. _stage.addEventListener(MouseEvent.MIDDLE_MOUSE_UP, handleMouseUp);
  202. _stage.addEventListener(MouseEvent.ROLL_OVER, handleMouseOver);
  203. _stage.addEventListener(Event.MOUSE_LEAVE, handleMouseOut);
  204. _stage.addEventListener(MouseEvent.DOUBLE_CLICK, handleDoubleClick);
  205. _stage.addEventListener(MouseEvent.MOUSE_WHEEL, handleMouseWheel);
  206. _stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
  207. _stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
  208. _stage.addEventListener(Event.RESIZE, handleResize);
  209. }
  210. /**
  211. * Request Context3D and set viewport size and location
  212. */
  213. private function createContext3D() : void
  214. {
  215. //Add EventListener for Context3D creation
  216. if(_stage.stage3Ds.length > 0)
  217. {
  218. var stage3D:Stage3D = _stage.stage3Ds[0];
  219. stage3D.addEventListener(Event.CONTEXT3D_CREATE, handleContext3DCreate);
  220. //Request Context3D
  221. stage3D.requestContext3D(Context3DRenderMode.AUTO, Context3DProfile.BASELINE);
  222. }
  223. }
  224. /**
  225. * Handle Context3D creation, create X3DScene and JS2ASBridge
  226. */
  227. private function handleContext3DCreate(e:Event): void
  228. {
  229. var stage3D:Stage3D = e.target as Stage3D;
  230. _context3D = stage3D.context3D;
  231. //Check if 3D Context is avaible
  232. if ( _context3D == null )
  233. {
  234. x3dom.Debug.logError("Flash can't create 3D Context");
  235. return;
  236. }
  237. //Enable error checking for debugging
  238. _context3D.enableErrorChecking = true;
  239. _context3D.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA, Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA );
  240. // The back buffer size is in actual pixels
  241. _context3D.configureBackBuffer( _stageWidth, _stageHeight, 8, true );
  242. //Create X3DScene for scene managing
  243. this._scene = new X3DScene();
  244. if (this._renderType == "forward")
  245. {
  246. this._renderer = new ForwardRenderer(_scene);
  247. } else {
  248. this._renderer = new LPPRenderer(_scene);
  249. }
  250. //Create JSToASBridge for communication
  251. this._bridge = new Bridge(_scene, _renderer);
  252. //Say JS that Flash is ready for rendering
  253. ExternalInterface.call("x3dom.bridge.setFlashReady", _context3D.driverInfo, _canvasIdx);
  254. }
  255. public static function getLoadingScreen() : Sprite
  256. {
  257. return _loadingScreen;
  258. }
  259. /**
  260. * Handle LeftMouseDown-Event and call the corresponding JS-Function
  261. */
  262. private function handleMouseDown(e:MouseEvent) : void
  263. {
  264. //Update mouse properties
  265. _mousePosX = e.stageX;
  266. _mousePosY = e.stageY;
  267. _mouseButton = 1;
  268. _mouseDragging = true;
  269. //Check for special keys
  270. if (e.shiftKey) { _mouseButton = 1; }
  271. if (e.ctrlKey) { _mouseButton = 4; }
  272. if (e.altKey) { _mouseButton = 2; }
  273. //Call JS MouseDown function
  274. ExternalInterface.call("x3dom.bridge.onMouseDown", _mousePosX, _mousePosY, _mouseButton, _canvasIdx);
  275. }
  276. /**
  277. * Handle RightMouseDown-Event and call the corresponding JS-Function
  278. */
  279. private function handleRightMouseDown(e:MouseEvent) : void
  280. {
  281. //Update mouse properties
  282. _mousePosX = e.stageX;
  283. _mousePosY = e.stageY;
  284. _mouseButton = 2;
  285. _mouseDragging = true;
  286. //Check for special keys
  287. if (e.shiftKey) { _mouseButton = 1; }
  288. if (e.ctrlKey) { _mouseButton = 4; }
  289. if (e.altKey) { _mouseButton = 2; }
  290. //Call JS MouseDown function
  291. ExternalInterface.call("x3dom.bridge.onMouseDown", _mousePosX, _mousePosY, _mouseButton, _canvasIdx);
  292. }
  293. /**
  294. * Handle MiddleMouseDown-Event and call the corresponding JS-Function
  295. */
  296. private function handleMiddleMouseDown(e:MouseEvent) : void
  297. {
  298. //Update mouse properties
  299. _mousePosX = e.stageX;
  300. _mousePosY = e.stageY;
  301. _mouseButton = 4;
  302. _mouseDragging = true;
  303. //Check for special keys
  304. if (e.shiftKey) { _mouseButton = 1; }
  305. if (e.ctrlKey) { _mouseButton = 4; }
  306. if (e.altKey) { _mouseButton = 2; }
  307. //Call JS MouseDown function
  308. ExternalInterface.call("x3dom.bridge.onMouseDown", _mousePosX, _mousePosY, _mouseButton, _canvasIdx);
  309. }
  310. /**
  311. * Handle MouseUp-Event and call the corresponding JS-Function
  312. */
  313. private function handleMouseUp(e:MouseEvent) : void
  314. {
  315. //Update mouse properties
  316. _mouseButton = 0;
  317. _mouseDragging = false;
  318. //Call JS MouseDown function
  319. ExternalInterface.call("x3dom.bridge.onMouseUp", _mousePosX, _mousePosY, _mouseButton, _canvasIdx);
  320. }
  321. /**
  322. * Handle MouseOver-Event and call the corresponding JS-Function
  323. */
  324. private function handleMouseOver(e:MouseEvent) : void
  325. {
  326. //Update mouse properties
  327. _mouseButton = 0;
  328. _mouseDragging = false;
  329. //Call JS MouseDown function
  330. ExternalInterface.call("x3dom.bridge.onMouseOver", _mousePosX, _mousePosY, _mouseButton, _canvasIdx);
  331. }
  332. /**
  333. * Handle MouseOut-Event and call the corresponding JS-Function
  334. */
  335. private function handleMouseOut(e:Event) : void
  336. {
  337. //Update mouse properties
  338. _mouseButton = 0;
  339. _mouseDragging = false;
  340. //Call JS MouseDown function
  341. ExternalInterface.call("x3dom.bridge.onMouseOut", _mousePosX, _mousePosY, _mouseButton, _canvasIdx);
  342. }
  343. /**
  344. * Handle DoubleClick-Event and call the corresponding JS-Function
  345. */
  346. private function handleDoubleClick(e:MouseEvent) : void
  347. {
  348. //Update mouse properties
  349. _mousePosX = e.stageX;
  350. _mousePosY = e.stageY;
  351. _mouseButton = 0;
  352. _mouseDragging = false;
  353. //Call JS MouseDown function
  354. ExternalInterface.call("x3dom.bridge.onDoubleClick", _mousePosX, _mousePosY, _mouseButton, _canvasIdx);
  355. }
  356. /**
  357. * Handle MouseMove-Event and call the corresponding JS-Function
  358. */
  359. private function handleMouseMove(e:MouseEvent) : void
  360. {
  361. //Update mouse properties
  362. _mousePosX = e.stageX;
  363. _mousePosY = e.stageY;
  364. //Check for special keys
  365. if (e.shiftKey) { _mouseButton = 1; }
  366. if (e.ctrlKey) { _mouseButton = 4; }
  367. if (e.altKey) { _mouseButton = 2; }
  368. //Call JS MouseMove function
  369. if(_mouseDragging) {
  370. ExternalInterface.call("x3dom.bridge.onMouseDrag", _mousePosX, _mousePosY, _mouseButton, _canvasIdx);
  371. } else {
  372. ExternalInterface.call("x3dom.bridge.onMouseMove", _mousePosX, _mousePosY, _mouseButton, _canvasIdx);
  373. }
  374. }
  375. /**
  376. * Handle MouseWheel-Event and call the corresponding JS-Function
  377. */
  378. private function handleMouseWheel(e:MouseEvent) : void
  379. {
  380. //Update mouse properties
  381. _mousePosY -= 1.5 * e.delta;
  382. _mouseButton = 2;
  383. //Call JS MouseWheel function
  384. ExternalInterface.call("x3dom.bridge.onMouseWheel", _mousePosX, _mousePosY, _mouseButton, _canvasIdx);
  385. }
  386. /**
  387. * Handle KeyDown-Event and call the corresponding JS-Function
  388. */
  389. private function handleKeyDown(e:KeyboardEvent) : void
  390. {
  391. //Call JS KeyDown function
  392. ExternalInterface.call("x3dom.bridge.onKeyDown", e.charCode, _canvasIdx);
  393. //Show/Hide InfoFied
  394. if(e.charCode == 32) {
  395. if(FlashBackend._infoField.visible) {
  396. FlashBackend._infoField.visible = false;
  397. } else {
  398. FlashBackend._infoField.visible = true;
  399. }
  400. }
  401. }
  402. /**
  403. * Handle scene resize
  404. */
  405. public function handleResize(event:Event):void
  406. {
  407. //Save new size
  408. _stageWidth = _stage.stageWidth;
  409. _stageHeight = _stage.stageHeight;
  410. //rearrange infofield
  411. _infoField.x = _stageWidth - _infoField.width - 10;
  412. if (_context3D != null)
  413. {
  414. _context3D.configureBackBuffer( _stageWidth, _stageHeight, 8, true );
  415. }
  416. }
  417. }
  418. }