PageRenderTime 69ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/mad-components/MadComponentsStage3D/src/com/danielfreeman/stage3Dacceleration/SlideOutNavigation.as

http://mad-components.googlecode.com/
ActionScript | 281 lines | 188 code | 25 blank | 68 comment | 22 complexity | d24da5c9b68e2c5c1a23fbacc035aa03 MD5 | raw file
  1. /**
  2. * <p>Original Author: Daniel Freeman</p>
  3. *
  4. * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:</p>
  10. *
  11. * <p>The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.</p>
  13. *
  14. * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.</p>
  21. *
  22. * <p>Licensed under The MIT License</p>
  23. * <p>Redistributions of files must retain the above copyright notice.</p>
  24. */
  25. package com.danielfreeman.stage3Dacceleration {
  26. import flash.events.MouseEvent;
  27. import flash.display.DisplayObject;
  28. import com.danielfreeman.extendedMadness.*;
  29. import com.danielfreeman.madcomponents.*;
  30. import com.adobe.utils.*;
  31. import flash.display.Sprite;
  32. import flash.display.Bitmap;
  33. import flash.display3D.Context3DProgramType;
  34. import flash.display3D.Context3DVertexBufferFormat;
  35. import flash.display3D.IndexBuffer3D;
  36. import flash.display3D.Program3D;
  37. import flash.display3D.VertexBuffer3D;
  38. import flash.geom.Matrix;
  39. import flash.display.BitmapData;
  40. import flash.geom.Point;
  41. import flash.geom.Rectangle;
  42. import flash.display3D.textures.Texture;
  43. import flash.display3D.Context3DTextureFormat;
  44. import flash.events.Event;
  45. import flash.display.DisplayObject;
  46. import flash.utils.Dictionary;
  47. import flash.display.IBitmapDrawable;
  48. public class SlideOutNavigation extends LongListScrolling {
  49. protected static const BEHIND:Number = 0.1;
  50. protected static const SLIDE_INCREMENT:Number = 0.2;
  51. protected var _slideOutNavigation:UISlideOutNavigation;
  52. protected var _listWidth:Number;
  53. protected var _listWidthNormalised:Number;
  54. protected var _open:Boolean = false;
  55. protected var _dragging:Boolean = false;
  56. /**
  57. * Stage3D acceleration for the new SlideOutNavigation component
  58. */
  59. public function SlideOutNavigation() {
  60. super();
  61. backgroundColour = 0x333333;
  62. }
  63. /**
  64. * Search the MadComponents tree, and find a UISlideOutNavigation component.
  65. * Return true if found.
  66. */
  67. override protected function allListTextures0(item:Sprite):Boolean {
  68. var found:Boolean = false;
  69. for (var i:int = 0; i<item.numChildren && !found; i++) {
  70. var child:DisplayObject = item.getChildAt(i);
  71. if (child is UISlideOutNavigation) {
  72. var slideOutNavigation:UISlideOutNavigation = UISlideOutNavigation(child);
  73. setSlideOutNavigation(slideOutNavigation);
  74. _lists.push(slideOutNavigation.list, slideOutNavigation.detail);
  75. return true;
  76. }
  77. else if (child is Sprite) {
  78. found = allListTextures0(Sprite(child));
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * Determine component size, and take over control from the component
  85. */
  86. protected function setSlideOutNavigation(value:UISlideOutNavigation):void {
  87. var point:Point = value.localToGlobal(new Point(0,0));
  88. _centre = new Rectangle(
  89. point.x,
  90. point.y,
  91. UI.scale*value.attributes.width,
  92. UI.scale*value.attributes.height
  93. );
  94. _slideOutNavigation = value;
  95. _slideOutNavigation.enabled = false;
  96. _listWidth = _slideOutNavigation.listWidth;
  97. _listWidthNormalised = 2 * UI.scale * _listWidth / _screen.stage.stageWidth;
  98. // _slideOutNavigation.addEventListener(UISlideOutNavigation.CLICKED, slideOutButton);
  99. _slideOutNavigation.navigationBar.leftButton.addEventListener(MouseEvent.MOUSE_DOWN, slideOutButton);
  100. }
  101. /**
  102. * Capture SlideOutNavigation textures. Specify the component as a parameter.
  103. */
  104. public function slideOutNavigation(value:UISlideOutNavigation):void {
  105. setSlideOutNavigation(value);
  106. listTextures(Vector.<IContainerUI>([value.list, value.detail]));
  107. }
  108. /**
  109. * Vertices of a list strip quad
  110. */
  111. override protected function pushVerticesAndUV(listRecord:ListRecord, listWidth:Number):void {
  112. var pointList:Point = Sprite(listRecord.container).localToGlobal(new Point(0, 0));
  113. var left:Number = 2 * pointList.x / _screen.stage.stageWidth - 1.0;
  114. var top:Number = - 2 * pointList.y / _screen.stage.stageHeight + 1.0;
  115. var right:Number = left + 2 * listWidth / _screen.stage.stageWidth;
  116. var bottom:Number = top - 2 * STRIP_HEIGHT / _screen.stage.stageHeight;
  117. var z:Number = (listRecord.textureIndex==0) ? BEHIND : 0;
  118. var xyzVertexBuffer:VertexBuffer3D = _context3D.createVertexBuffer(4, 3);
  119. xyzVertexBuffer.uploadFromVector(Vector.<Number>([
  120. left, bottom, z,
  121. right, bottom, z,
  122. right, top, z,
  123. left, top, z ]), 0, 4);
  124. _xyzVertexBuffersAll.push(xyzVertexBuffer);
  125. var uvVertexBuffer:VertexBuffer3D = _context3D.createVertexBuffer(4, 2);
  126. uvVertexBuffer.uploadFromVector(Vector.<Number>([
  127. 0, 1,
  128. listRecord.uvWidth, 1,
  129. listRecord.uvWidth, 0,
  130. 0, 0 ]), 0, 4);
  131. _uvVertexBuffersAll.push(uvVertexBuffer);
  132. }
  133. /**
  134. * Handler for the slide button being pressed.
  135. */
  136. protected function slideOutButton(event:Event):void {
  137. if (!_listRecordNext) {
  138. _slideOutNavigation.detail.visible = false;
  139. if (_open) {
  140. slidePage(0, 1, true);
  141. }
  142. else {
  143. slidePage(0, 1);
  144. doTakeOverFromListScroller(_slideOutNavigation.list);
  145. }
  146. }
  147. }
  148. /**
  149. * MouseDown Handler for list scroll or detail sliding
  150. */
  151. override protected function mouseDown(event:MouseEvent):void {
  152. var point:Point = _slideOutNavigation.globalToLocal(new Point(_screen.stage.mouseX, _screen.stage.mouseY));
  153. if (_open && point.x > _listWidth && point.y > UINavigationBar.HEIGHT) {
  154. startDragging();
  155. }
  156. else {
  157. super.mouseDown(event);
  158. }
  159. }
  160. /**
  161. * Start dragging the detail page
  162. */
  163. protected function startDragging():void {
  164. stopMovement(_listRecords[0]);
  165. _slideOutNavigation.detail.visible = false;
  166. _dragging = true;
  167. _stopping = false;
  168. _listRecordNext = _listRecords[1];
  169. _screen.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  170. onEnterFrame(this, dragDetail);
  171. }
  172. /**
  173. * Mouse-Up handler
  174. */
  175. override protected function mouseUp(event:MouseEvent):void {
  176. if (_dragging) {
  177. stopDragging();
  178. }
  179. else {
  180. super.mouseUp(event);
  181. }
  182. }
  183. /**
  184. * Do slide
  185. */
  186. override protected function onEnterFrameSlide(event:Event):void {
  187. _count += SLIDE_INCREMENT;
  188. drawSlideFrame();
  189. if (_stopping) {
  190. slideComplete();
  191. }
  192. else if (_count >= 1.0) {
  193. prepareToStop();
  194. }
  195. }
  196. override protected function prepareToStop():void {
  197. _stopping = true;
  198. _open = !_open;
  199. if (_open) {
  200. _slideOutNavigation.open(false);
  201. }
  202. else {
  203. _slideOutNavigation.close(false);
  204. }
  205. _slideOutNavigation.list.visible = !_open;
  206. _slideOutNavigation.detail.visible = true;
  207. }
  208. /**
  209. * Stop dragging the detail page - it will snap open or shut.
  210. */
  211. protected function stopDragging():void {
  212. var point:Point = _slideOutNavigation.globalToLocal(new Point(_screen.stage.mouseX, _screen.stage.mouseY));
  213. _count = point.x / _listWidth;
  214. _dragging = false;
  215. _back = _count < 0.5;
  216. if (_back) _count = 1 - _count;
  217. _open = _back;
  218. if (!_open) {
  219. _slideOutNavigation.open(false);
  220. }
  221. else {
  222. _slideOutNavigation.close(false);
  223. }
  224. _screen.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
  225. _enabled = false;
  226. onEnterFrame(this, onEnterFrameSlide);
  227. }
  228. /**
  229. * Slide transition complete
  230. */
  231. override protected function slideComplete():void {
  232. _listRecords[1].onScreen = !_open;
  233. setRegisters();
  234. _listRecordNext = null;
  235. _enabled = true;
  236. _count = 1.0;
  237. onEnterFrame(this, updateLists);
  238. }
  239. /**
  240. * Dragging the detail page
  241. */
  242. protected function dragDetail(event:Event):void {
  243. var point:Point = _slideOutNavigation.globalToLocal(new Point(_screen.stage.mouseX, _screen.stage.mouseY));
  244. _positionNext = Math.min(Math.max(2.0 * UI.scale * point.x / _screen.stage.stageWidth, 0), _listWidthNormalised);
  245. drawLists();
  246. }
  247. /**
  248. * Sliding the detail page
  249. */
  250. override protected function drawSlideFrame(event:Event = null):void {
  251. var shift:Number = easing(Math.min( Math.max(_count, 0), 1));
  252. _positionNext = 2.0 * UI.scale * _listWidth * (_back ? (1-shift) : shift) / _screen.stage.stageWidth;
  253. drawLists();
  254. }
  255. }
  256. }