PageRenderTime 130ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/cf/view/component/container/ContainerBase.as

https://github.com/isaacueca/copacino.com
ActionScript | 426 lines | 270 code | 79 blank | 77 comment | 48 complexity | 138689ab28b7bc429812de269184d49b MD5 | raw file
  1. package com.cf.view.component.container
  2. {
  3. import com.cf.util.Component;
  4. import com.cf.util.Settings;
  5. import com.cf.util.Utility;
  6. import com.cf.view.component.Mask;
  7. import com.cf.view.component.OpacityLines;
  8. import com.cf.view.component.list.ListBase;
  9. import com.cf.view.component.shape.ShapeBase;
  10. import com.cf.view.component.shape.ShapePlus;
  11. import com.cf.view.component.tile.Tile;
  12. import com.cf.view.event.UIEvent;
  13. import flash.display.BlendMode;
  14. import flash.display.Sprite;
  15. import flash.errors.IllegalOperationError;
  16. import flash.events.Event;
  17. import flash.events.MouseEvent;
  18. import flash.geom.Point;
  19. import flash.geom.Rectangle;
  20. import flashpress.vo.WpPostWithTagAndCatVO;
  21. import gs.TweenLite;
  22. import gs.TweenMax;
  23. import gs.easing.Strong;
  24. public class ContainerBase extends Component
  25. {
  26. // EVENTS
  27. public static const CONTAINER_READY:String = "event/container/ready";
  28. // STATES
  29. public static const STATE_LOADING:String = "stateLoading";
  30. public static const STATE_OPEN:String = "stateOpen";
  31. public static const STATE_LIST:String = "stateList";
  32. public static const STATE_MINIMIZED:String = "stateMinimized";
  33. public static const STATE_MINIMIZED_LIST:String = "stateMinimizedList";
  34. public static const STATE_MINIMIZED_FULLSCREEN:String = "stateMinimizedFullscreen";
  35. // PROTECTED MEMBERS
  36. protected var _background:Sprite;
  37. protected var _backgroundMask:Mask;
  38. protected var _opacityLines:OpacityLines;
  39. protected var _shape:ShapeBase;
  40. protected var _list:ListBase;
  41. protected var _mask:Sprite;
  42. protected var _tileData:Array;
  43. protected var _listData:Array;
  44. // LIST scrolling
  45. protected var _shapeStartDrag:Point;
  46. // PRIVATE MEMBERS
  47. private var _linesAreInitialized:Boolean = false;
  48. private var _shapeType:Class;
  49. private var _listType:Class;
  50. // PUBLIC
  51. public var url:String;
  52. public function ContainerBase(shapeType:Class, listType:Class )
  53. {
  54. _shapeType = shapeType;
  55. _listType = listType;
  56. super();
  57. }
  58. //
  59. // OVERRIDES
  60. //
  61. override protected function init():void
  62. {
  63. super.init();
  64. // MASK
  65. _mask = new Sprite;
  66. _mask.addChild( Utility.getMaskShape() );
  67. _mask.width = stage.stageWidth;
  68. _mask.height = stage.stageHeight;
  69. addChild( _mask );
  70. this.mask = _mask;
  71. // BACKGROUND
  72. _background = new Sprite();
  73. addChild( _background );
  74. setupBackground();
  75. // BG MASK - children can choose to add a mask, but it's not required
  76. if (_backgroundMask != null)
  77. {
  78. addChild( _backgroundMask );
  79. _background.mask = _backgroundMask;
  80. }
  81. // OPACITY lines
  82. _opacityLines = new OpacityLines();
  83. addChild( _opacityLines );
  84. _opacityLines.alpha = Settings.INIT_LINE_CONTAINER_OPACITY;
  85. _opacityLines.addEventListener(OpacityLines.LINES_INIT, lines_init);
  86. _opacityLines.blendMode = BlendMode.OVERLAY;
  87. // ADD content
  88. _shape = new _shapeType();
  89. addChild( _shape );
  90. drawOpacityLines();
  91. // POSITION
  92. position();
  93. // WIRE EVENTS
  94. _shape.addEventListener( MouseEvent.CLICK, shape_click );
  95. _shape.addEventListener( MouseEvent.MOUSE_DOWN, shape_mouse_down );
  96. stage.addEventListener( MouseEvent.MOUSE_UP, stage_mouse_up );
  97. this.addEventListener( Event.ENTER_FRAME, enter_frame );
  98. }
  99. override protected function onStageResize(e:Event):void
  100. {
  101. super.onStageResize(e);
  102. }
  103. //
  104. // METHODS
  105. //
  106. protected function setupBackground():void
  107. {
  108. throw new Error("Abstract method");
  109. }
  110. override protected function position():void
  111. {
  112. // MASK
  113. _mask.width = stage.stageWidth;
  114. _mask.height = stage.stageHeight;
  115. // POSITION shape based on current state
  116. var expectedShapePos:Point = getExpectedShapePosition( _state );
  117. _shape.x = expectedShapePos.x;
  118. _shape.y = expectedShapePos.y;
  119. //if ( _state != STATE_LOADING )
  120. //{
  121. // SIZE opacity lines
  122. _opacityLines.setMinHeight( stage.stageHeight );
  123. _opacityLines.setWidth( stage.stageWidth );
  124. // SIZE BG TO STAGE
  125. _background.width = stage.stageWidth;
  126. _background.height = stage.stageHeight;
  127. //}
  128. }
  129. protected function getExpectedShapePosition( state:String ):Point
  130. {
  131. var point:Point = new Point(0,0);
  132. switch ( state )
  133. {
  134. case STATE_MINIMIZED:
  135. point.x = (stage.stageWidth >> 1) - (_shape.originPoint.x * Settings.SHAPE_MINIMIZED_SCALE);
  136. if (this._shapeType == ShapePlus) point.y = ((Settings.SHAPE_MINIMIZED_SCALE * Settings.BAR_WIDTH) >> 1) - Settings.FIRST_LINE_HEIGHT;
  137. else point.y = (Settings.TRAY_HEIGHT >> 1) - (_shape.originPoint.y * Settings.SHAPE_MINIMIZED_SCALE);
  138. break;
  139. case STATE_LOADING:
  140. case STATE_OPEN:
  141. default:
  142. point.x = (stage.stageWidth >> 1) - (_shape.originPoint.x);
  143. point.y = ((stage.stageHeight - Settings.TRAY_HEIGHT) >> 1) - (_shape.originPoint.y);
  144. // SNAP to NEXT line
  145. point.y -= (point.y % (Settings.TILE_HEIGHT + Settings.TILE_MARGIN)) - 5;
  146. point.y += (Settings.TILE_ROW_HEIGHT);
  147. break;
  148. case STATE_MINIMIZED_LIST:
  149. point.x = (stage.stageWidth >> 1) - (_shape.originPoint.x * Settings.SHAPE_MINIMIZED_SCALE);
  150. point.y = (Settings.TRAY_HEIGHT_LIST >> 1) - (_shape.originPoint.y * Settings.SHAPE_MINIMIZED_SCALE);
  151. break;
  152. /*case STATE_LIST:
  153. point.x = ( stage.stageWidth * (1 - Settings.LIST_WIDTH_PERCENT) * .5) - ( Settings.TILE_HEIGHT / 2 );
  154. point.y = Settings.LIST_PLUS_TOP;
  155. break;*/
  156. }
  157. return point;
  158. }
  159. protected function createTiles( autoReveal:Boolean=true ):void
  160. {
  161. // ADD tiles
  162. for each (var post:WpPostWithTagAndCatVO in _tileData)
  163. {
  164. _shape.addTile(new Tile(post.postTitle.toLowerCase(), 0x000000, autoReveal, post));
  165. }
  166. // ARRANGE tiles
  167. _shape.arrangeTiles();
  168. }
  169. protected function initList():void
  170. {
  171. if (_list == null)
  172. {
  173. _list = new _listType( _listData, _shape, url);
  174. addChild(_list);
  175. //_list.x = Settings.LIST_MARGIN_LEFT;
  176. //_list.y = Settings.LIST_MARGIN_TOP;
  177. }
  178. }
  179. private function lines_init(e:Event):void
  180. {
  181. _linesAreInitialized = true;
  182. }
  183. private function drawOpacityLines():void
  184. {
  185. if (!_linesAreInitialized)
  186. {
  187. _opacityLines.initLines();
  188. //TweenMax.to(opacityLinesLayer, 1.5, { width:stage.stageWidth, ease: Strong.easeOut } );
  189. }
  190. else _opacityLines.setWidth(stage.stageWidth);
  191. _linesAreInitialized = true;
  192. }
  193. //
  194. // HANDLERS
  195. //
  196. private function shape_click(e:MouseEvent):void
  197. {
  198. if (state != STATE_LIST && state != STATE_LOADING)
  199. {
  200. dispatchEvent( new UIEvent( UIEvent.SHAPE_CLICK, _shape.name, _shape.url ) );
  201. }
  202. }
  203. private function shape_mouse_down(e:MouseEvent):void
  204. {
  205. if (state == STATE_LIST)
  206. {
  207. // START DRAGGING
  208. var bounds:Rectangle = new Rectangle( _shape.x, Settings.LIST_PLUS_TOP, 0, stage.stageHeight - Settings.LIST_PLUS_TOP);
  209. //_shape.startDrag(true, bounds);
  210. //_shape.isDragging = true;
  211. //_shapeStartDrag = new Point(_shape.x, _shape.y);
  212. }
  213. }
  214. private function stage_mouse_up(e:MouseEvent):void
  215. {
  216. //_shape.stopDrag();
  217. //_shape.isDragging = false;
  218. }
  219. private function enter_frame(e:Event):void
  220. {
  221. if (state == STATE_LIST && _shape.isDragging)
  222. {
  223. // TODO move list based on distance gradient
  224. if (_shape.y > _shapeStartDrag.y)
  225. {
  226. // _list.y -= 10;
  227. }
  228. else
  229. {
  230. // _list.y = Math.min( _list.y + 10, Settings.LIST_MARGIN_TOP );
  231. }
  232. }
  233. }
  234. //
  235. // PUBLIC API
  236. //
  237. public function loadComplete():void
  238. {
  239. dispatchEvent( new Event( ContainerBase.CONTAINER_READY, true, true ) );
  240. //throw new IllegalOperationError("Abstract method, must override");
  241. }
  242. public function toListState( isCloudDisplay:Boolean = false ):void
  243. {
  244. if ( state == STATE_LIST &&
  245. (
  246. (isCloudDisplay == true && _list.state == ListBase.STATE_CLOUD_DISPLAY) ||
  247. (isCloudDisplay == false && _list.state == ListBase.STATE_REVEALED)
  248. ))
  249. return;
  250. // FADE OUT LINES
  251. TweenLite.to( _opacityLines, 2, { alpha:0 });
  252. // COLLAPSE SHAPE
  253. _shape.collapseList();
  254. // MOVE SHAPE
  255. var expectedPointPos:Point = getExpectedShapePosition( STATE_LIST );
  256. TweenMax.to(_shape, 2, { delay:1, x:expectedPointPos.x, ease: Strong.easeInOut });
  257. TweenMax.to(_shape, 2, { delay:1, y: expectedPointPos.y, ease: Strong.easeInOut });
  258. // OPEN LIST
  259. _list.reveal( .5, isCloudDisplay );
  260. // SET STATE
  261. Utility.debugColor( this, 0x245a0a, "from:", state, "to:", STATE_LIST );
  262. state = STATE_LIST;
  263. }
  264. public function toOpenState():void
  265. {
  266. if ( state == STATE_OPEN ) return;
  267. // FADE IN LINES
  268. TweenLite.to( _opacityLines, 2, { delay:1, alpha:1 });
  269. // IF it's coming from loading, don't do anything since it's a special case
  270. //if (state != STATE_LOADING)
  271. //{
  272. // MOVE SHAPE
  273. var expectedPointPos:Point = getExpectedShapePosition( STATE_OPEN );
  274. TweenMax.to(_shape, 2, { delay: 0, x: expectedPointPos.x, y: expectedPointPos.y, ease: Strong.easeInOut });
  275. // EXPAND SHAPE
  276. _shape.expand();
  277. if ( state == STATE_LIST )
  278. {
  279. // COLLAPSE LIST
  280. _list.hide( 0 );
  281. }
  282. else if ( state == STATE_MINIMIZED )
  283. {
  284. }
  285. //}
  286. Utility.debugColor( this, 0x245a0a, "from:", state, "to:", STATE_OPEN );
  287. state = STATE_OPEN;
  288. }
  289. public function toMinimizedState():void
  290. {
  291. if ( state == STATE_MINIMIZED ) return;
  292. if ( state == STATE_LIST )
  293. {
  294. // ADDITIONAL cleanup for transitioning from list to minimized
  295. _list.hide( 0 );
  296. // REVEAL
  297. _shape.reveal();
  298. }
  299. // FADE OUT LINES
  300. TweenLite.to( _opacityLines, 2, { alpha:0 });
  301. // COLLAPSE SHAPE
  302. _shape.collapse();
  303. // MOVE SHAPE
  304. var expectedPointPos:Point = getExpectedShapePosition( STATE_MINIMIZED );
  305. TweenMax.to(_shape, 2, { delay:1, x:expectedPointPos.x , y: expectedPointPos.y, ease: Strong.easeInOut });
  306. Utility.debugColor( this, 0x245a0a, "from:", state, "to:", STATE_MINIMIZED );
  307. state = STATE_MINIMIZED;
  308. }
  309. public function toMinimizedListState():void
  310. {
  311. if ( state == STATE_OPEN || state == STATE_LIST ) toMinimizedListState();
  312. // MOVE SHAPE
  313. var expectedPointPos:Point = getExpectedShapePosition( STATE_MINIMIZED_LIST );
  314. TweenMax.to(_shape, 1, { x:expectedPointPos.x , y: expectedPointPos.y, ease: Strong.easeInOut });
  315. state = STATE_MINIMIZED_LIST;
  316. }
  317. public function toMinimizedFullscreenState():void
  318. {
  319. // if ( state != STATE_MINIMIZED_FULLSCREEN )
  320. toMinimizedListState();
  321. state = STATE_MINIMIZED_FULLSCREEN;
  322. }
  323. //
  324. // GETTERS / SETTERS
  325. //
  326. public function set tileData(data:Array):void
  327. {
  328. _tileData = data;
  329. }
  330. public function set listData(data:Array):void
  331. {
  332. _listData = data;
  333. if (_list == null) initList();
  334. else _list.data = data;
  335. }
  336. public function get shape():ShapeBase
  337. {
  338. return _shape;
  339. }
  340. public function get list():ListBase
  341. {
  342. return _list;
  343. }
  344. }
  345. }