Posted By

alejandromb on 12/07/09


Tagged

basic base as3 actionscript3 component


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tspitzr


AS3 | KCComponent | Basic Component


 / Published in: ActionScript 3
 

URL: http://www.kirikacode.com.ar/

  1. package kc.core {
  2.  
  3. import flash.display.DisplayObject;
  4. import flash.display.DisplayObjectContainer;
  5. import flash.display.MovieClip;
  6. import flash.events.Event;
  7.  
  8. import kc.events.KCComponentEvent;
  9. import kc.utils.ClassUtil;
  10. import kc.utils.UID;
  11.  
  12. [Event( name="disabled", type="kc.events.KCComponentEvent" )]
  13. [Event( name="enabled", type="kc.events.KCComponentEvent" )]
  14. [Event( name="dataChange", type="kc.events.KCComponentEvent" )]
  15.  
  16. public class KCComponent extends MovieClip {
  17.  
  18. // @protected
  19.  
  20. protected var _data:XML;
  21. protected var _autorelease:Boolean;
  22.  
  23. // @private
  24.  
  25. private var _uid:String;
  26. private var _owner:DisplayObjectContainer;
  27.  
  28. // @constructor
  29.  
  30. public function KCComponent( data:XML = null, autorelease:Boolean = true ) {
  31. super();
  32. // FlashCore
  33. this.stop();
  34. this.focusRect = false;
  35. this.tabEnabled = false;
  36. this.tabChildren = false;
  37. // KCCompoenent
  38. this.data = data;
  39. this.autorelease = autorelease;
  40. this.addEventListener(
  41. Event.ADDED_TO_STAGE,
  42. $config
  43. );
  44. }
  45.  
  46. // @override
  47.  
  48. override public function get enabled():Boolean {
  49. return super.enabled;
  50. }
  51.  
  52. override public function set enabled( value:Boolean ):void {
  53. super.enabled = value;
  54. this.dispatchEvent(
  55. new KCComponentEvent(
  56. ( ! value )
  57. ? KCComponentEvent.DISABLED
  58. : KCComponentEvent.ENABLED
  59. )
  60. );
  61. }
  62.  
  63. // @methods
  64.  
  65. public function get data():XML {
  66. return this._data;
  67. }
  68.  
  69. public function set data( value:XML ):void {
  70. if( this._data === value ) return;
  71. this._data = value;
  72. this.dispatchEvent(
  73. new KCComponentEvent (
  74. KCComponentEvent.DATA_CHANGE
  75. )
  76. );
  77. }
  78.  
  79. public function get autorelease():Boolean {
  80. return this._autorelease;
  81. }
  82.  
  83. public function set autorelease( value:Boolean ):void {
  84.  
  85. if( this._autorelease == value ) {
  86. return;
  87. }
  88.  
  89. this._autorelease = value;
  90.  
  91. if( ! this._autorelease ){
  92. this.removeEventListener(
  93. Event.REMOVED_FROM_STAGE,
  94. purge
  95. );
  96. }else{
  97. this.addEventListener(
  98. Event.REMOVED_FROM_STAGE,
  99. purge
  100. );
  101. }
  102.  
  103. }
  104.  
  105. public function get owner():DisplayObjectContainer {
  106. return this._owner || this.parent;
  107. }
  108.  
  109. public function set owner( value:DisplayObjectContainer ):void {
  110. this._owner = value;
  111. }
  112.  
  113. public function get uid():String {
  114. if( this._uid == null ){
  115. this._uid = UID.create();
  116. } return this._uid;
  117. }
  118.  
  119. public function owns( value:DisplayObject ):Boolean {
  120. if ( this.contains( value ) ) return true;
  121. try{
  122. while( value && value != this ){
  123. if( value is IKCComponent ){
  124. value = IKCComponent( value ).owner;
  125. }else{
  126. value = value.parent;
  127. }
  128. }
  129. }catch( e:SecurityError ){
  130. return false;
  131. } return value == this;
  132. }
  133.  
  134. public function applyToAllChildren( action:String, ...rest ):void {
  135. var child:IKCComponent;
  136. for ( var a:int = 0; a < this.numChildren; a++ ) {
  137. if ( this.getChildAt(a) is IKCComponent ) {
  138. child = this.getChildAt(a) as IKCComponent;
  139. if( ClassUtil.isMethod( child, action ) ) {
  140. child[action].apply( child, rest );
  141. }else if ( ClassUtil.isPropertyWritable( child, action ) ) {
  142. child[action] = rest[0];
  143. }
  144. }
  145. }
  146. }
  147.  
  148. public function position( x:Number, y:Number = NaN ):void {
  149. this.x = x;
  150. this.y = y || x;
  151. }
  152.  
  153. public function scale( x:Number, y:Number = NaN ):void {
  154. this.scaleX = x;
  155. this.scaleY = y || x;
  156. }
  157.  
  158. public function size( w:Number, h:Number = NaN ):void {
  159. this.width = w;
  160. this.height = h || w;
  161. }
  162.  
  163. // @purge
  164.  
  165. public function purge(...rest):void {
  166.  
  167. this._data = null;
  168. this._uid = null;
  169. this._owner = null;
  170. this._autorelease = undefined;
  171.  
  172. if ( this.hasEventListener( Event.REMOVED_FROM_STAGE ) ) {
  173. this.removeEventListener(
  174. Event.REMOVED_FROM_STAGE,
  175. purge
  176. );
  177. }
  178.  
  179. if ( this.hasEventListener( Event.ADDED_TO_STAGE ) ) {
  180. this.removeEventListener(
  181. Event.ADDED_TO_STAGE,
  182. $config
  183. );
  184. }
  185.  
  186. }
  187.  
  188. // @handlers
  189.  
  190. protected function $config(e:Event):void {
  191.  
  192. this.removeEventListener(
  193. Event.ADDED_TO_STAGE,
  194. $config
  195. );
  196.  
  197. }
  198.  
  199. }
  200.  
  201. }
  202.  
  203. package kc.events {
  204.  
  205. import flash.events.Event;
  206.  
  207. public class KCComponentEvent extends Event {
  208.  
  209. // @const
  210.  
  211. public static const ENABLED:String = "enabled";
  212. public static const DISABLED:String = "disabled";
  213. public static const DATA_CHANGE:String = "dataChange";
  214.  
  215. // @constructor
  216.  
  217. public function KCComponentEvent( type:String, bubbles:Boolean=false, cancelable:Boolean=false ) {
  218. super( type, bubbles, cancelable );
  219. }
  220.  
  221. // @override
  222.  
  223. override public function clone():Event {
  224. return new KCComponentEvent( this.type, this.bubbles, this.cancelable );
  225. }
  226.  
  227. override public function toString():String {
  228. return this.formatToString( "KCComponentEvent", "type", "bubbles", "cancelable", "eventPhase" );
  229. }
  230.  
  231. }
  232.  
  233. }
  234.  
  235. package kc.utils {
  236.  
  237. import kc.core.KCStatic;
  238.  
  239. public class UID extends KCStatic {
  240.  
  241. // @const
  242.  
  243. private static const CHARS:Array = new Array( 48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70 );
  244. private static const SEPARATOR:uint = 45;
  245.  
  246. // @constructor
  247.  
  248. public function UID() {
  249. super();
  250. }
  251.  
  252. // @methods
  253.  
  254. public static function create( value:Array = null ):String {
  255.  
  256. var uid:Array = new Array();
  257. var template:Array = value || new Array( 8,4,4,4,12 );
  258. var last:int = template.length - 1;
  259.  
  260. for ( var a:uint = 0; a < template.length; a++ ) {
  261. for ( var b:uint = 0; b < template[a]; b++ ) {
  262. uid.push( CHARS[ Math.floor( Math.random() * CHARS.length ) ] );
  263. } if ( a < last ) {
  264. uid.push( SEPARATOR );
  265. }
  266. }
  267.  
  268. var time:String = String(
  269. "0000000" + new Date().getTime().toString(16).toUpperCase()
  270. ).substr(
  271. Math.random() * template[last]
  272. );
  273.  
  274. return String(
  275. String.fromCharCode.apply(
  276. null,
  277. uid
  278. )
  279. ).substr(
  280. 0,
  281. -time.length
  282. ) + time;
  283.  
  284. }
  285.  
  286. }
  287.  
  288. }

Report this snippet  

You need to login to post a comment.