PageRenderTime 33ms CodeModel.GetById 16ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/asutil/style/ShadowColor.as

http://asutil.googlecode.com/
ActionScript | 265 lines | 163 code | 25 blank | 77 comment | 45 complexity | 3c5b281d53a0a5b4e99dc23e83eb74d8 MD5 | raw file
  1package asutil.style
  2{
  3	/**
  4	 * ??????
  5	 */ 
  6	public  class ShadowColor
  7	{
  8		import flash.display.DisplayObject;
  9		import flash.geom.ColorTransform;
 10		
 11		import asutil.construct.Map;
 12		import asutil.construct.impl.SimpleMap;
 13		import asutil.job.Job;
 14		import asutil.job.JobTask;
 15		import asutil.common.ASLog;
 16		
 17		/**
 18		 * ??job
 19		 */ 
 20		private var myJob:Job;
 21		/**
 22		 * ???
 23		 * KEY - ??name
 24		 * value- ??ID 
 25		 */ 
 26		private static var  threadLocal:Map = new SimpleMap;
 27		/**
 28		 * ??
 29		 */ 
 30		private  const  logger:ASLog = ASLog.getLogger(this);
 31		/**
 32	 	 * ???????255????256??????
 33	 	 */ 	    
 34	 	 private static const COMPARE_SAME:Number = 256; 
 35		/**
 36	 	 * ???????????????
 37	 	 */ 	    
 38	 	 private var isLoop:Boolean = false;
 39		/**
 40		 *???????
 41		 * ?????????????Number?????????????	 
 42	 	 */ 	    
 43	 	 private var tgColorIndex:Object= new Object;
 44		/**
 45		 * ???? 
 46	 	 */ 	    
 47	 	 private  var targetColorArray:Array = new Array; 
 48		/**
 49		 * ??????? 
 50	 	 */ 	    
 51	 	 private  var shadowEles:Array = new Array;
 52
 53		/**
 54		 * ??????
 55		 */ 
 56		 public function ShadowColor(argObject:Object):void{
 57				  //?????????
 58				  if(null == argObject.shadowEles || null == argObject.targetColorArray ){
 59				  	throw new Error("Invaild para object.");
 60				  }
 61				  this.shadowEles = argObject.shadowEles;
 62				  this.targetColorArray = argObject.targetColorArray;
 63				  
 64				  //??????
 65				  var fadeDelay:Number;
 66				  if(null != argObject.fadeDelay){
 67				    fadeDelay = new Number(argObject.fadeDelay);
 68				  }else{
 69				    fadeDelay = 50;
 70				  }
 71				  if(null != argObject.isLoop ){
 72				  	this.isLoop = argObject.isLoop ;
 73				  }
 74				  
 75				  tgColorIndex['value'] = new Number(0)
 76				  
 77				  var fadeTask:JobTask = new JobTask({
 78				       doWhat : fadePortal,
 79				       argArray : new Array( threadLocal,shadowEles,targetColorArray,tgColorIndex)
 80				  })
 81				  
 82				  initColor(this.shadowEles);
 83				  myJob = new Job({
 84				        interval:fadeDelay,
 85				        isLoop:true,
 86				        step:5
 87				  });
 88				  myJob.addTask(fadeTask);
 89		}
 90		
 91		/**
 92		 * ??
 93		 */ 
 94		public function play():void{
 95				  myJob.start();
 96		}
 97		/**
 98		 * ??????????????
 99		 */ 
100		public function stopAtColor(color:String):void{
101			myJob.sleep();
102			
103			//?????????
104			for each(var ele:DisplayObject in shadowEles){
105				var temp:ColorTransform = ele.transform.colorTransform;
106				temp.color = new  uint(color) ;
107				ele.transform.colorTransform = temp;
108			}
109		}
110		
111		/**
112		 * ????????threadLocal??????task stop
113		 */ 
114		public function stopShadow():void{
115				this.myJob.stop();
116				
117			//???threadLocal??
118			for each(var ele:DisplayObject in shadowEles){
119				threadLocal.remove(ele.name);
120			}
121				
122		}
123		/**
124		 * ???????????
125		 */ 
126		public function addColor(newColor:String):void{
127			this.targetColorArray.push(newColor);
128		}
129		
130		/**
131		 * ??????
132		 */ 
133		public function fadePortal(threadLocal:Map,shadowEles:Array,targetColorArray:Array,tgColorIndex:Object):void{
134				for each(var ele:DisplayObject in shadowEles){
135						fadeFunction(threadLocal,ele ,targetColorArray,tgColorIndex);
136				}
137		}
138		/**
139		 * ?????
140		 */ 
141		private function fadeFunction(threadLocal:Map,disO:DisplayObject,targetColorArray:Array,tgColorIndex:Object):void{
142	 	    	  			
143	 	    	  			var index:Number = tgColorIndex['value'];
144	 	    	  			//??????????
145	 	    	  			if(targetColorArray.length == index){
146	 	    	  				//????????0??????
147	 	    	  				if(true == this.isLoop){
148	 	    	  						index = 0;
149	 	    	  						tgColorIndex['value'] = index;
150	 	    	  				}else{
151	 	    	  					//??????
152	 	    	  					threadLocal.remove(disO.name);
153	 	    	  					myJob.stop();
154	 	    	  				}
155	 	    	  			}
156	 	    	  			
157	 	    	  			var targetCT:ColorTransform = reCaulateTgColor(targetColorArray,index);
158	 	    	  			if(0 == targetCT.color){
159	 	    	  				  threadLocal.remove(disO.name);
160	 	    	  	 			  myJob.stop();  
161	 	    	  			}
162	 	    	  			
163	 	    	  			if(true == isObjctInRunning(threadLocal,disO)){
164	 	    	  					 var tR:Number = targetCT.redOffset;
165									  var tG:Number = targetCT.greenOffset;
166									  var tB:Number = targetCT.blueOffset;				
167	 	 						 	  //?????????
168	 	    	  		    	  	  var isFinished:Boolean = 	fadeProcess(disO,tR,tG,tB);
169	 	    	  		    	  	  if(true == isFinished){
170	 	    	  		    	  	  	       tgColorIndex['value'] =  tgColorIndex['value'] + 1;
171	 	    	  		    	 	  }
172	 	    	  			}
173		}
174		
175		/**
176		 * ??????
177		 */ 
178		private function reCaulateTgColor(colorArray:Array,colorIndex:Number):ColorTransform{
179					var targetCT:ColorTransform = new ColorTransform;
180					  targetCT.color = colorArray[colorIndex];
181					  return targetCT;		
182		}
183		
184		/**
185		 * ??“????”
186		 * ??DisplyObject???????“??”???????????????
187		 * ??????DisplyObject??“???” 
188		 */
189		private function isObjctInRunning(threadLocal:Map,disO:Object):Boolean{
190		       if(null == threadLocal.getValue(disO.name) ){
191//		       	logger.debug("Start thread~~~~~~~~~~~~~~~~~~~~~~" + myThread);
192		       	    threadLocal.put(disO.name,myJob.getThreadId())
193				     return true;       
194		       }
195		       
196		       var runningThread:uint =  threadLocal.getValue(disO.name) as uint ;
197		       if(runningThread == myJob.getThreadId()){
198				     return true;       
199		       } 
200		       
201		     return false;       
202		}
203		/**
204		 * ????
205		 * @retrun ?????????
206		 */ 
207		private  function fadeProcess(disO:DisplayObject,tR:Number,tG:Number,tB:Number):Boolean{
208//				 	logger.info("Run~~~~~~~~~~~~~~~~~~~~~~" + myThread);
209	 	    	var curCT:ColorTransform = disO.transform.colorTransform;		
210	 	    	var curR:Number = curCT.redOffset;  
211	 	    	var curG:Number = curCT.greenOffset;  
212	 	    	var curB:Number = curCT.blueOffset;
213	 	    	
214	 	    	var nR:Number = changeColorProcess(curR,tR);
215	 	    	var nCT:ColorTransform = disO.transform.colorTransform; 
216	 	    	if(COMPARE_SAME != nR){
217	 	    	  		nCT.redOffset = nR
218	 	    	  		}
219	 	    	var nG:Number = changeColorProcess(curG,tG);
220	 	    	  if(COMPARE_SAME != nG){
221	 	    	  		nCT.greenOffset= nG
222	 	    	  }
223	 	    	var nB:Number = changeColorProcess(curB,tB);
224	 	    	  if(COMPARE_SAME != nB){
225	 	    	  		nCT.blueOffset = nB
226	 	    	  }
227	 	    	  disO.transform.colorTransform = nCT;
228	 	    	  
229	 	    	  nCT = null;//let gc go its work
230	 	    	  
231	 	    	  if(COMPARE_SAME == nR && COMPARE_SAME == nG && COMPARE_SAME ==nB){
232	 	    	  	 // logger.info("Clear: ~~~~~~~~~~~~~~~~~~" + myThread)	
233	 	    	  		return true;
234	 	    	  }
235	 	    	  		return false;
236		}
237		
238		/**
239		 * ??????????color??????null?????????????color
240		 */ 
241		private  function initColor(shadowEles:Array):void
242		{
243			for each(var disO:DisplayObject in shadowEles){
244				var colorInfo:ColorTransform = disO.transform.colorTransform;
245			 	    	if(0 == colorInfo.color){
246			 	    		colorInfo.color = 0x999999;
247							disO.transform.colorTransform = colorInfo;
248			 	    	}
249			}
250		}
251		
252	 	private  function changeColorProcess(curCol:Number,toCol:Number):Number{
253	 	    	if(curCol == toCol){
254	 	    		return COMPARE_SAME;
255	 	    	}
256	 	    	else  if(curCol > toCol){
257	 	    		return curCol - 1;
258	 	    	}
259	 	    	else  if(curCol < toCol){
260	 	    		return curCol + 1;
261	 	    	}
262	 	    	return COMPARE_SAME;
263	 	    }
264	}
265}