/src/com/cf/view/component/list/content/items/ContentItemBase.as

https://github.com/isaacueca/copacino.com · ActionScript · 335 lines · 204 code · 59 blank · 72 comment · 26 complexity · 5f4cd656a6491423785b54cd93037873 MD5 · raw file

  1. package com.cf.view.component.list.content.items
  2. {
  3. import com.cf.model.vo.AssetInfo;
  4. import com.cf.util.Component;
  5. import com.cf.util.Settings;
  6. import com.cf.util.TextFactory;
  7. import com.cf.view.component.tile.Tile;
  8. import flash.display.Bitmap;
  9. import flash.display.BitmapData;
  10. import flash.display.Shape;
  11. import flash.display.Sprite;
  12. import flash.events.Event;
  13. import flash.events.MouseEvent;
  14. import flash.text.TextField;
  15. import gs.TweenLite;
  16. public class ContentItemBase extends Component
  17. {
  18. // EVENTs
  19. public static const STATE_ACTIVE :String = "contentItem/state/active";
  20. public static const STATE_INACTIVE :String = "contentItem/state/inactive";
  21. public static const CURSOR_ENABLED:String = "contentItem/cursor/enabled";
  22. public static const CURSOR_DISABLED:String = "contentItem/cursor/disabled";
  23. public static const CURSOR_CLICK:String = "contentItem/cursor/click";
  24. public static const MODE_LARGE :String = "contentItem/mode/large";
  25. public static const MODE_NORMAL :String = "contentItem/mode/normal";
  26. public static const MODE_LARGE_IS_READY:String = "contentItem/status/largeIsReady";
  27. public static const MODE_LARGE_LOADING:String = "contentItem/status/largeLoading";
  28. protected var _mode:String = MODE_NORMAL;
  29. // DATA
  30. protected var _assetInfo:AssetInfo;
  31. protected var _content:Sprite = new Sprite();
  32. protected var _aspectRatio:Number = 1.33; // DEFAULT
  33. protected var _isFirstItem:Boolean = false;
  34. protected var _isLastItem:Boolean = false;
  35. // VISUAL
  36. protected var _bg:Sprite = new Sprite();
  37. protected var _slate:Bitmap;
  38. protected var _captionContainer:Sprite = new Sprite();
  39. protected var _tint:Sprite = new Sprite();
  40. protected var _caption:Sprite = new Sprite();
  41. protected var _stroke:Shape = new Shape();
  42. // BUTTONS
  43. private var _viewLargeButton:Tile;
  44. private var _closeLargeButton:Tile;
  45. // CONTAINER
  46. public function ContentItemBase( assetInfo:AssetInfo )
  47. {
  48. super();
  49. this.name = assetInfo.name;
  50. _state = STATE_INACTIVE;
  51. _assetInfo = assetInfo;
  52. _slate = _assetInfo.slate;
  53. addChild( _bg );
  54. addChild( _content );
  55. addChild( _captionContainer );
  56. addChild( _stroke );
  57. addChild( _tint );
  58. _bg.mouseChildren = _bg.mouseEnabled;
  59. _tint.mouseChildren = _tint.mouseEnabled = false;
  60. _tint.alpha = 1 - Settings.CONTENT_ALPHA_INACTIVE;
  61. // TODO: tie tinting to scroll (for now just hide the tint)
  62. _tint.visible = false;
  63. }
  64. //
  65. // OVERRIDES
  66. //
  67. protected override function init() : void
  68. {
  69. // BUILD CAPTION
  70. if ( _assetInfo.slateMediaVO.post_excerpt != "" )
  71. {
  72. _captionContainer.addChild( _caption );
  73. var capText:TextField = TextFactory.TagText( _assetInfo.slateMediaVO.post_excerpt.toLowerCase() );
  74. var capBg:Sprite = new Sprite();
  75. capBg.addChild( new Bitmap( new BitmapData( capText.width + (Settings.TILE_TEXT_HORIZONTAL_MARGIN << 1), Settings.TILE_HEIGHT, false, Settings.CONTENT_NAV_BG ) ) );
  76. capBg.alpha = Settings.CONTENT_NAV_BG_ALPHA;
  77. capText.x = Settings.TILE_TEXT_HORIZONTAL_MARGIN;
  78. // capBg.mouseChildren = capBg.mouseEnabled = false;
  79. // capText.mouseEnabled = false;
  80. _caption.addChild( capBg );
  81. _caption.addChild( capText );
  82. }
  83. // Y
  84. //_captionContainer.y = Settings.CONTENT_AREA_HEIGHT - Settings.CONTENT_NAV_VERT_MARGIN + Settings.TILE_HEIGHT + 1;
  85. // VIEW LARGE button for work pieces
  86. if ( _assetInfo.isViewLargeEnabled )
  87. {
  88. _viewLargeButton = new Tile("view large", 0x555555, true );
  89. _viewLargeButton.alpha = .7;
  90. _viewLargeButton.x = (_caption.width == 0) ? 0 : _caption.width + Settings.TILE_MARGIN;
  91. _captionContainer.addChild( _viewLargeButton );
  92. _viewLargeButton.addEventListener( MouseEvent.MOUSE_DOWN, viewLarge_mouse_down );
  93. }
  94. position();
  95. }
  96. override protected function position() : void
  97. {
  98. super.position();
  99. /*if ( _mode == MODE_NORMAL )
  100. {
  101. _captionContainer.y = Settings.CONTENT_AREA_HEIGHT - Settings.CONTENT_NAV_VERT_MARGIN + Settings.TILE_HEIGHT + 1;
  102. }
  103. else if ( _mode == MODE_LARGE )
  104. {
  105. // CAPTION
  106. _captionContainer.y = stage.stageHeight - Settings.CONTENT_NAV_VERT_MARGIN + Settings.TILE_HEIGHT + 1;
  107. }*/
  108. _captionContainer.y = _bottomEdge - Settings.CONTENT_NAV_VERT_MARGIN + Settings.TILE_HEIGHT + 1;
  109. // DISABLE VIEW LARGE
  110. if ( _bottomEdge < Settings.CONTENT_AREA_HEIGHT )
  111. {
  112. if ( _viewLargeButton != null ) _viewLargeButton.visible = false;
  113. }
  114. else
  115. {
  116. if ( _viewLargeButton != null ) _viewLargeButton.visible = true;
  117. }
  118. // CAPTION X
  119. /*if ( desiredWidth > stage.stageWidth && _mode == MODE_LARGE )
  120. {
  121. //TweenLite.to( _captionContainer, 1, { x: (desiredWidth - stage.stageWidth) >> 1 });
  122. _captionContainer.x = (desiredWidth - stage.stageWidth) >> 1;
  123. }
  124. else if ( desiredWidth > (stage.stageWidth * Settings.LIST_WIDTH_PERCENT) && _mode == MODE_NORMAL )
  125. {
  126. //TweenLite.to( _captionContainer, 1, { x: (desiredWidth - (stage.stageWidth * Settings.LIST_WIDTH_PERCENT)) >> 1 });
  127. _captionContainer.x = (desiredWidth - (stage.stageWidth * Settings.LIST_WIDTH_PERCENT)) >> 1;
  128. }
  129. else
  130. {
  131. _captionContainer.x = 0;
  132. }*/
  133. // SIZE tint to content
  134. _tint.width = desiredWidth; // _content.width;
  135. _tint.height = desiredHeight; // _content.height;
  136. // STROKE
  137. _stroke.x = desiredWidth;
  138. _stroke.height = desiredWidth;
  139. }
  140. override public function get desiredHeight():Number
  141. {
  142. if ( _mode == MODE_LARGE ) return stage.stageHeight;
  143. else
  144. {
  145. if ( stage.stageHeight < (Settings.CONTENT_AREA_HEIGHT + (2 * Settings.CONTENT_SCROLL_HORZ_CONTAINER_HEIGHT)) )
  146. {
  147. return ( stage.stageHeight - (2 * Settings.CONTENT_SCROLL_HORZ_CONTAINER_HEIGHT));
  148. }
  149. else
  150. return Settings.CONTENT_AREA_HEIGHT;
  151. }
  152. }
  153. //
  154. // PROTECTED
  155. //
  156. protected function addStroke():void
  157. {
  158. if ( !_isLastItem )
  159. {
  160. // STROKE
  161. _stroke.graphics.lineStyle( 1, Settings.CONTENT_STROKE_COLOR );
  162. _stroke.graphics.lineTo( 0, Settings.CONTENT_AREA_HEIGHT );
  163. _stroke.x = this.width;
  164. }
  165. }
  166. //
  167. // EVENT HANLDERS
  168. //
  169. protected function viewLarge_mouse_down(e:MouseEvent):void
  170. {
  171. dispatchEvent( new Event( MODE_LARGE, true, true ) );
  172. }
  173. protected function closeLarge_mouse_down(e:MouseEvent):void
  174. {
  175. dispatchEvent( new Event( MODE_NORMAL, true, true ) );
  176. }
  177. //
  178. // PRIVATE
  179. //
  180. private function createCloseButton():void
  181. {
  182. _closeLargeButton = new Tile("exit large", 0x555555 );
  183. _closeLargeButton.alpha = .7;
  184. _closeLargeButton.x = (_caption.width == 0) ? 0 : _caption.width + Settings.TILE_MARGIN;
  185. _captionContainer.addChild( _closeLargeButton );
  186. _closeLargeButton.addEventListener( MouseEvent.MOUSE_DOWN, closeLarge_mouse_down );
  187. }
  188. private function get _bottomEdge():Number
  189. {
  190. if ( _mode == MODE_NORMAL)
  191. {
  192. if ( stage.stageHeight < ( Settings.CONTENT_AREA_HEIGHT + (2 * Settings.CONTENT_SCROLL_HORZ_CONTAINER_HEIGHT) ) )
  193. {
  194. return stage.stageHeight - (2 * Settings.CONTENT_SCROLL_HORZ_CONTAINER_HEIGHT);
  195. }
  196. else
  197. return Settings.CONTENT_AREA_HEIGHT;
  198. }
  199. else
  200. {
  201. return stage.stageHeight;
  202. }
  203. }
  204. //
  205. // PROTECTED
  206. //
  207. protected function initTint(width:Number, height:Number):void
  208. {
  209. _tint.addChild( new Bitmap( new BitmapData( width, height, false, Settings.MODAL_TINT_COLOR ) ) );
  210. _tint.alpha = Settings.MODAL_TINT_ALPHA;
  211. if ( _isFirstItem ) _tint.alpha = 0;
  212. }
  213. //
  214. // PUBLIC API
  215. //
  216. public function get tint():Sprite
  217. {
  218. return _tint;
  219. }
  220. public function toActive():void
  221. {
  222. TweenLite.to( _tint, .6, { alpha: 1 - Settings.CONTENT_ALPHA_ACTIVE } );
  223. this.state = STATE_ACTIVE;
  224. }
  225. public function toInactive( useTint:Boolean = true ):void
  226. {
  227. if ( useTint ) TweenLite.to( _tint, .6, { alpha: 1 - Settings.CONTENT_ALPHA_INACTIVE } );
  228. this.state = STATE_INACTIVE;
  229. }
  230. public function toModeLarge():void
  231. {
  232. // UPDATE mode 1st so mode-dependent functions will be accurate
  233. _mode = MODE_LARGE;
  234. if ( _closeLargeButton == null ) createCloseButton();
  235. // SWAP buttons (if they exist)
  236. if ( _closeLargeButton ) _closeLargeButton.visible = true;
  237. if ( _viewLargeButton ) _viewLargeButton.visible = false;
  238. // CAPTION
  239. TweenLite.to( _captionContainer, 1, { y: stage.stageHeight - Settings.CONTENT_NAV_VERT_MARGIN + Settings.TILE_HEIGHT + 1 });
  240. // RESIZE TINT
  241. TweenLite.to( _tint, .8, { delay:1, width: desiredWidth, height: desiredHeight } );
  242. // STROKE
  243. TweenLite.to( _stroke, 1, { delay:1, x: desiredWidth, height: desiredHeight } );
  244. }
  245. public function toModeNormal():void
  246. {
  247. // UPDATE mode 1st so mode-dependent functions will be accurate
  248. _mode = MODE_NORMAL;
  249. // SWAP buttons
  250. if ( _closeLargeButton ) _closeLargeButton.visible = false;
  251. if ( _viewLargeButton ) _viewLargeButton.visible = true;
  252. // RECALC desired size
  253. //var scale:Number = desiredHeight / Settings.CONTENT_AREA_HEIGHT;
  254. //this.desiredWidth = desiredWidth / scale;
  255. //this.desiredHeight = desiredHeight / scale;
  256. // CAPTION y
  257. TweenLite.to( _captionContainer, 1, { y: Settings.CONTENT_AREA_HEIGHT - Settings.CONTENT_NAV_VERT_MARGIN + Settings.TILE_HEIGHT + 1 });
  258. // RESIZE TINT
  259. TweenLite.to( _tint, 1, { width: desiredWidth, height: desiredHeight } );
  260. // STROKE
  261. TweenLite.to( _stroke, .8, { x: desiredWidth, height: desiredHeight } );
  262. }
  263. public function get aspectRatio():Number
  264. {
  265. return _aspectRatio;
  266. }
  267. public function set isFirstItem( b:Boolean ):void
  268. {
  269. _isFirstItem = b;
  270. }
  271. public function set isLastItem( b:Boolean ):void
  272. {
  273. _isLastItem = b;
  274. }
  275. }
  276. }