PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/mintdigital/hemlock/display/HemlockSprite.as

http://github.com/mintdigital/hemlock
ActionScript | 384 lines | 154 code | 73 blank | 157 comment | 20 complexity | 3f8bb222e409b3e605b776a47887abd1 MD5 | raw file
  1. package com.mintdigital.hemlock.display{
  2. import com.mintdigital.hemlock.Logger;
  3. import com.mintdigital.hemlock.display.HemlockSpriteEffects;
  4. import com.mintdigital.hemlock.utils.HashUtils;
  5. import flash.display.DisplayObject;
  6. import flash.display.Sprite;
  7. import flash.geom.Matrix;
  8. import flash.utils.setTimeout;
  9. public class HemlockSprite extends Sprite{
  10. protected var _listeners:Array /* of Objects */ = [];
  11. protected var _options:Object /* of string : * */ = {};
  12. protected static var _defaultOptions:Object /* of string : * */ = {};
  13. public function HemlockSprite(options:Object = null){
  14. if(!options){ options = {}; }
  15. _options = options;
  16. if(parent){
  17. var parentSprite:HemlockSprite = parent as HemlockSprite;
  18. }
  19. if(options.x) { x = options.x; }
  20. if(options.y) { y = options.y; }
  21. if(options.width) { width = options.width; }
  22. if(options.height) { height = options.height; }
  23. if(options.visible === true || options.visible === false){
  24. options.visible ? show() : hide();
  25. }
  26. if(options.name) { name = options.name; }
  27. // TODO: Instead loop through expected options and check if defined
  28. }
  29. public function setPosition(x:Number, y:Number):void{
  30. // Changes this object's x and y. To get this object's original x
  31. // and y, use options.x and options.y.
  32. this.x = x;
  33. this.y = y;
  34. }
  35. public function setSize(width:Number, height:Number):void{
  36. // Change this object's width and height. To get this object's
  37. // original width and height, use options.width and
  38. // options.height.
  39. this.width = width;
  40. this.height = height;
  41. }
  42. public function updateSize():void{
  43. // Automatically called after addChild() and addChildren() to
  44. // avoid rendering issues.
  45. // Source: http://www.kirupa.com/forum/showthread.php?p=2397349
  46. if(!_options){ return; }
  47. if(_options.width) { this.width = _options.width; }
  48. if(_options.height){ this.height = _options.height; }
  49. }
  50. public function updateDimensions():void{
  51. // DEPRECATED. Use updateSize() instead: its name matches
  52. // setSize().
  53. updateSize();
  54. }
  55. public function show(options:Object = null):void{
  56. // Supported options:
  57. // - startListeners (default: true)
  58. options = HashUtils.merge({ startListeners: true }, options);
  59. if(options.startListeners){
  60. visible = true;
  61. // See also the overridden `visible` setter.
  62. }else{
  63. super.visible = true;
  64. }
  65. }
  66. public function hide(options:Object = null):void{
  67. // Supported options:
  68. // - stopListeners (default: true)
  69. options = HashUtils.merge({ startListeners: true }, options);
  70. if(options.stopListeners){
  71. visible = false;
  72. // See also the overridden `visible` setter.
  73. }else{
  74. super.visible = false;
  75. }
  76. }
  77. public function toggle():void{ visible = !visible; }
  78. // See also the overridden `visible` setter.
  79. override public function addChild(child:DisplayObject):DisplayObject{
  80. super.addChild(child);
  81. updateSize();
  82. return child;
  83. }
  84. public function addChildren(... displayObjects):void{
  85. for each(var displayObject:DisplayObject in displayObjects){
  86. super.addChild(displayObject);
  87. }
  88. updateSize();
  89. }
  90. public function moveChildToBack(displayObject:DisplayObject):void{
  91. // Moves `displayObject` to the lowest possible z-index.
  92. setChildIndex(displayObject, 0);
  93. }
  94. public function moveChildToFront(displayObject:DisplayObject):void{
  95. // Moves `displayObject` to the highest possible z-index.
  96. setChildIndex(displayObject, numChildren - 1);
  97. }
  98. //--------------------------------------
  99. // Effects
  100. //--------------------------------------
  101. public function fadeOut(options:Object = null):void{
  102. /*
  103. Usage:
  104. - mySprite.fade(); // Fades to alpha = 0
  105. - mySprite.fade({
  106. to: 0.5,
  107. duration: 0.75
  108. }); // Fades to alpha = 0.5 in 0.75 seconds
  109. Options:
  110. - from: Original alpha (0.0 to 1.0); defaults to current alpha
  111. - to: Target alpha (0.0 to 1.0); defaults to 0
  112. - duration: Number of seconds
  113. - onComplete: Function to run after effect
  114. */
  115. options = HashUtils.merge({
  116. from: alpha,
  117. to: 0
  118. }, options);
  119. HemlockSpriteEffects.fade(this, options);
  120. }
  121. public function fadeIn(options:Object = null):void{
  122. /*
  123. Usage:
  124. - mySprite.fade(); // Fades to alpha = 1
  125. - mySprite.fade({
  126. to: 0.5,
  127. duration: 5
  128. }); // Fades to alpha = 0.5 in 5 seconds
  129. Options:
  130. - from: Original alpha (0.0 to 1.0); defaults to 0
  131. - to: Target alpha (0.0 to 1.0); defaults to 1
  132. - duration: Number of seconds
  133. - onComplete: Function to run after effect
  134. */
  135. options = HashUtils.merge({
  136. from: 0,
  137. to: 1
  138. }, options);
  139. HemlockSpriteEffects.fade(this, options);
  140. }
  141. public function move(options:Object):void{
  142. /*
  143. Usage:
  144. - mySprite.move({
  145. xBy: -10,
  146. yBy: 20
  147. }); // Moves left 10px and down 20px
  148. - mySprite.move({
  149. xFrom: 10,
  150. xTo: 90,
  151. duration: 5
  152. }); // Moves x-coordinate from 10 to 90 in 5 seconds
  153. Options:
  154. - xBy: Number of pixels to move along the x-axis
  155. - xFrom: Starting x-coordinate
  156. - xTo: Ending x-coordinate
  157. - yBy: Number of pixels to move along the y-axis
  158. - yFrom: Starting y-coordinate
  159. - yTo: Ending y-coordinate
  160. - duration: Number of seconds
  161. - onComplete: Function to run after effect
  162. */
  163. if(!options){ options = {}; }
  164. HemlockSpriteEffects.move(this, options);
  165. }
  166. public function dropOut(options:Object = null):void{
  167. /*
  168. Usage:
  169. - mySprite.dropOut(); // Moves down and fades
  170. - mySprite.dropOut({
  171. y: 50
  172. duration: 5
  173. }); // Moves down 50px and fades in 5 seconds
  174. Options:
  175. - alphaFrom: Alpha (0.0 to 1.0) to fade from; defaults to current alpha
  176. - alphaTo: Alpha (0.0 to 1.0) to fade to; defaults to 0
  177. - x: Number of pixels to move along the x-axis; defaults to 0
  178. - y: Number of pixels to move along the y-axis; defaults to 20
  179. - duration: Number of seconds
  180. - onComplete: Function to run after effect
  181. */
  182. // TODO: Update to use BlurFilter (more blurY as effect continues)
  183. options = HashUtils.merge({
  184. y: 10
  185. }, options);
  186. // Rename options.x and options.y to options.xBy and options.yBy
  187. options = HashUtils.merge({
  188. xBy: options.x,
  189. yBy: options.y
  190. }, options);
  191. delete options.x;
  192. delete options.y;
  193. // Pull out onComplete so that it's not run after each effect
  194. var onComplete:Function = options.onComplete;
  195. if(options.onComplete){ delete options.onComplete; }
  196. fadeOut(options);
  197. move(HashUtils.merge({
  198. // Only attach onComplete to one effect
  199. onComplete: onComplete
  200. }, options));
  201. }
  202. // TODO: `dropIn` effect
  203. public function flip(newSprite:HemlockSprite, options:Object = null):void{
  204. /*
  205. Usage:
  206. - oldSprite.flip(newSprite); // 3-D flip to newSprite
  207. - oldSprite.flip(newSprite, {
  208. duration: 5
  209. }); // 3-D flip to newSprite in five seconds
  210. */
  211. if(!options){ options = {}; }
  212. HemlockSpriteEffects.flip(this, newSprite, options);
  213. }
  214. // TODO: Subclasses for buttons and other inputs?
  215. // - HemlockSprite
  216. // > HemlockForm
  217. // - Attributes: controls:Array of HemlockControls
  218. // - Events: HemlockFormEvent.SUBMIT
  219. // - Has many text inputs, password inputs, and buttons, with one button marked as default
  220. // - Exists for handling text input tab indexes, routing Return/Enter to default button, and collecting input values in an object
  221. // > HemlockLabel
  222. // - Attributes: target:HemlockControl
  223. // - Clicking gives focus to target
  224. // > HemlockControl
  225. // - Attributes: namespace:String (unique among other namespaces), name:String (unique within namespace), id:String (unique combination of namespace and name), value:Object, text:String, textField:TextField, textFormat:TextFormat, eventHandlers:Object of {eventType:Function}
  226. // - Methods: focus()
  227. // > HemlockTextField
  228. // - Attributes: defaultText:String
  229. // - Events: HemlockTextInput.FOCUS (is this built into TextField?)
  230. // > HemlockPasswordField
  231. // > HemlockButton
  232. // > HemlockModal
  233. // > HemlockNotice
  234. // > HemlockError
  235. // > HemlockProgress
  236. // - Attributes: currentIncrement:uint|null, totalIncrements:uint|null
  237. // - If no increments are given, instead show repeating animation, e.g., spinner, horizontal barber pole
  238. // TODO: Automatically start/stop HemlockControl listeners when removed from stage (see DisplayObject docs for events) or shown/hidden
  239. //--------------------------------------
  240. // Internal helpers
  241. //--------------------------------------
  242. // ...
  243. //--------------------------------------
  244. // Events
  245. //--------------------------------------
  246. public function registerListeners():void{
  247. // Registers listeners for automatic adding/removing.
  248. /* Override me if needed */
  249. }
  250. public function unregisterListeners():void {
  251. stopListeners();
  252. _listeners = [];
  253. }
  254. public function registerListener(listenee:*, eventType:String, eventHandler:Function, options:Object = null):void{
  255. // Adds listener data to _listeners, which is used in
  256. // startListeners() and stopListeners() automatically.
  257. // Supported options:
  258. // - useCapture:Boolean (default = false)
  259. // Logger.debug('HemlockSprite::registerListener() : eventType = ' + eventType);
  260. // Logger.debug('HemlockSprite::registerListener() : eventHandler = ' + eventHandler);
  261. // TODO: Handle duplicates
  262. // - Ignore if duplicate listenee, eventType, eventHandler, and options
  263. // - Overwrite if differs only on options
  264. _listeners.push(HashUtils.merge({
  265. listenee: listenee,
  266. eventType: eventType,
  267. eventHandler: eventHandler,
  268. useCapture: false
  269. }, options));
  270. }
  271. public function startListeners():void{
  272. // Adds listeners according to _listeners, which is constructed
  273. // via registerListener().
  274. for each(var listener:Object in _listeners){
  275. listener.listenee.addEventListener(listener.eventType, listener.eventHandler, listener.useCapture);
  276. }
  277. }
  278. // TODO: public function startListener(eventType:String):void
  279. // - Logger.error if eventType wasn't registered
  280. public function stopListeners():void{
  281. // Removes listeners according to _listeners, which is constructed
  282. // via registerListener().
  283. for each(var listener:Object in _listeners){
  284. listener.listenee.removeEventListener(listener.eventType, listener.eventHandler, listener.useCapture);
  285. }
  286. }
  287. // TODO: public function stopListener(eventType:String):void
  288. // - Logger.error if eventType wasn't registered
  289. public static function getVerticalMatrix():Matrix{
  290. // Returns matrix for making vertical gradients
  291. // TODO: Cache in a private member var/const
  292. var verticalMatrix:Matrix = new Matrix();
  293. verticalMatrix.createGradientBox(100, 100, 0.5 * Math.PI, 0, 0); // 90 degrees
  294. return verticalMatrix;
  295. }
  296. //--------------------------------------
  297. // Properties
  298. //--------------------------------------
  299. public function get options():Object{ return _options; }
  300. public static function get defaultOptions():Object { return _defaultOptions; }
  301. public static function set defaultOptions(value:Object):void { _defaultOptions = value; }
  302. override public function set visible(value:Boolean):void{
  303. super.visible = value;
  304. value ? startListeners() : stopListeners();
  305. }
  306. }
  307. }