laya.effect.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. (function(window,document,Laya){
  2. var __un=Laya.un,__uns=Laya.uns,__static=Laya.static,__class=Laya.class,__getset=Laya.getset,__newvec=Laya.__newvec;
  3. var BlurFilter=laya.filters.BlurFilter,ColorFilter=laya.filters.ColorFilter,ColorUtils=laya.utils.ColorUtils;
  4. var Component=laya.components.Component,Ease=laya.utils.Ease,Event=laya.events.Event,GlowFilter=laya.filters.GlowFilter;
  5. var Handler=laya.utils.Handler,Node=laya.display.Node,Sprite=laya.display.Sprite,Tween=laya.utils.Tween,Utils=laya.utils.Utils;
  6. /**
  7. *...
  8. *@author ww
  9. */
  10. //class laya.effect.FilterSetterBase
  11. var FilterSetterBase=(function(){
  12. function FilterSetterBase(){
  13. this._filter=null;
  14. this._target=null;
  15. }
  16. __class(FilterSetterBase,'laya.effect.FilterSetterBase');
  17. var __proto=FilterSetterBase.prototype;
  18. __proto.paramChanged=function(){
  19. Laya.systemTimer.callLater(this,this.buildFilter);
  20. }
  21. __proto.buildFilter=function(){
  22. if (this._target){
  23. this.addFilter(this._target);
  24. }
  25. }
  26. __proto.addFilter=function(sprite){
  27. if (!sprite)return;
  28. if (!sprite.filters){
  29. sprite.filters=[this._filter];
  30. }else{
  31. var preFilters;
  32. preFilters=sprite.filters;
  33. if (preFilters.indexOf(this._filter)< 0){
  34. preFilters.push(this._filter);
  35. sprite.filters=Utils.copyArray([],preFilters);
  36. }
  37. }
  38. }
  39. __proto.removeFilter=function(sprite){
  40. if (!sprite)return;
  41. sprite.filters=null;
  42. }
  43. __getset(0,__proto,'target',null,function(value){
  44. if (this._target !=value){
  45. this._target=value;
  46. this.paramChanged();
  47. }
  48. });
  49. return FilterSetterBase;
  50. })()
  51. /**
  52. *@Script {name:ButtonEffect}
  53. *@author ww
  54. */
  55. //class laya.effect.ButtonEffect
  56. var ButtonEffect=(function(){
  57. function ButtonEffect(){
  58. this._tar=null;
  59. this._curState=0;
  60. this._curTween=null;
  61. /**
  62. *effectScale
  63. *@prop {name:effectScale,type:number,tips:"缩放值",default:"1.5"}
  64. */
  65. this.effectScale=1.5;
  66. /**
  67. *tweenTime
  68. *@prop {name:tweenTime,type:number,tips:"缓动时长",default:"300"}
  69. */
  70. this.tweenTime=300;
  71. /**
  72. *effectEase
  73. *@prop {name:effectEase,type:ease,tips:"效果缓动类型"}
  74. */
  75. this.effectEase=null;
  76. /**
  77. *backEase
  78. *@prop {name:backEase,type:ease,tips:"恢复缓动类型"}
  79. */
  80. this.backEase=null;
  81. }
  82. __class(ButtonEffect,'laya.effect.ButtonEffect');
  83. var __proto=ButtonEffect.prototype;
  84. __proto.toChangedState=function(){
  85. this._curState=1;
  86. if (this._curTween)Tween.clear(this._curTween);
  87. this._curTween=Tween.to(this._tar,{scaleX:this.effectScale,scaleY:this.effectScale },this.tweenTime,Ease[this.effectEase],Handler.create(this,this.tweenComplete));
  88. }
  89. __proto.toInitState=function(){
  90. if (this._curState==2)return;
  91. if (this._curTween)Tween.clear(this._curTween);
  92. this._curState=2;
  93. this._curTween=Tween.to(this._tar,{scaleX:1,scaleY:1 },this.tweenTime,Ease[this.backEase],Handler.create(this,this.tweenComplete));
  94. }
  95. __proto.tweenComplete=function(){
  96. this._curState=0;
  97. this._curTween=null;
  98. }
  99. /**
  100. *设置控制对象
  101. *@param tar
  102. */
  103. __getset(0,__proto,'target',null,function(tar){
  104. this._tar=tar;
  105. tar.on(/*laya.events.Event.MOUSE_DOWN*/"mousedown",this,this.toChangedState);
  106. tar.on(/*laya.events.Event.MOUSE_UP*/"mouseup",this,this.toInitState);
  107. tar.on(/*laya.events.Event.MOUSE_OUT*/"mouseout",this,this.toInitState);
  108. });
  109. return ButtonEffect;
  110. })()
  111. /**
  112. *效果插件基类,基于对象池管理
  113. */
  114. //class laya.effect.EffectBase extends laya.components.Component
  115. var EffectBase=(function(_super){
  116. function EffectBase(){
  117. /**动画持续时间,单位为毫秒*/
  118. this.duration=1000;
  119. /**动画延迟时间,单位为毫秒*/
  120. this.delay=0;
  121. /**重复次数,默认为播放一次*/
  122. this.repeat=0;
  123. /**缓动类型,如果为空,则默认为匀速播放*/
  124. this.ease=null;
  125. /**触发事件,如果为空,则创建时触发*/
  126. this.eventName=null;
  127. /**效用作用的目标对象,如果为空,则是脚本所在的节点本身*/
  128. this.target=null;
  129. /**效果结束后,是否自动移除节点*/
  130. this.autoDestroyAtComplete=true;
  131. this._comlete=null;
  132. this._tween=null;
  133. EffectBase.__super.call(this);
  134. }
  135. __class(EffectBase,'laya.effect.EffectBase',_super);
  136. var __proto=EffectBase.prototype;
  137. __proto._onAwake=function(){this.target=this.target|| this.owner;
  138. if (this.autoDestroyAtComplete)this._comlete=Handler.create(this.target,this.target.destroy,null,false);
  139. if (this.eventName)this.owner.on(this.eventName,this,this._exeTween);
  140. else this._exeTween();
  141. }
  142. __proto._exeTween=function(){
  143. this._tween=this._doTween();
  144. this._tween.repeat=this.repeat;
  145. }
  146. __proto._doTween=function(){
  147. return null;
  148. }
  149. __proto.onReset=function(){
  150. this.duration=1000;
  151. this.delay=0;
  152. this.repeat=0;
  153. this.ease=null;
  154. this.target=null;
  155. if (this.eventName){
  156. this.owner.off(this.eventName,this,this._exeTween);
  157. this.eventName=null;
  158. }
  159. if (this._comlete){
  160. this._comlete.recover();
  161. this._comlete=null;
  162. }
  163. if (this._tween){
  164. this._tween.clear();
  165. this._tween=null;
  166. }
  167. }
  168. return EffectBase;
  169. })(Component)
  170. /**
  171. *...
  172. *@author ww
  173. */
  174. //class laya.effect.ColorFilterSetter extends laya.effect.FilterSetterBase
  175. var ColorFilterSetter=(function(_super){
  176. function ColorFilterSetter(){
  177. /**
  178. *brightness 亮度,范围:-100~100
  179. */
  180. this._brightness=0;
  181. /**
  182. *contrast 对比度,范围:-100~100
  183. */
  184. this._contrast=0;
  185. /**
  186. *saturation 饱和度,范围:-100~100
  187. */
  188. this._saturation=0;
  189. /**
  190. *hue 色调,范围:-180~180
  191. */
  192. this._hue=0;
  193. /**
  194. *red red增量,范围:0~255
  195. */
  196. this._red=0;
  197. /**
  198. *green green增量,范围:0~255
  199. */
  200. this._green=0;
  201. /**
  202. *blue blue增量,范围:0~255
  203. */
  204. this._blue=0;
  205. /**
  206. *alpha alpha增量,范围:0~255
  207. */
  208. this._alpha=0;
  209. this._color=null;
  210. ColorFilterSetter.__super.call(this);
  211. this._filter=new ColorFilter();
  212. }
  213. __class(ColorFilterSetter,'laya.effect.ColorFilterSetter',_super);
  214. var __proto=ColorFilterSetter.prototype;
  215. __proto.buildFilter=function(){
  216. this._filter.reset();
  217. this._filter.color(this.red,this.green,this.blue,this.alpha);
  218. this._filter.adjustHue(this.hue);
  219. this._filter.adjustContrast(this.contrast);
  220. this._filter.adjustBrightness(this.brightness);
  221. this._filter.adjustSaturation(this.saturation);
  222. _super.prototype.buildFilter.call(this);
  223. }
  224. __getset(0,__proto,'brightness',function(){
  225. return this._brightness;
  226. },function(value){
  227. this._brightness=value;
  228. this.paramChanged();
  229. });
  230. __getset(0,__proto,'alpha',function(){
  231. return this._alpha;
  232. },function(value){
  233. this._alpha=value;
  234. this.paramChanged();
  235. });
  236. __getset(0,__proto,'contrast',function(){
  237. return this._contrast;
  238. },function(value){
  239. this._contrast=value;
  240. this.paramChanged();
  241. });
  242. __getset(0,__proto,'hue',function(){
  243. return this._hue;
  244. },function(value){
  245. this._hue=value;
  246. this.paramChanged();
  247. });
  248. __getset(0,__proto,'saturation',function(){
  249. return this._saturation;
  250. },function(value){
  251. this._saturation=value;
  252. this.paramChanged();
  253. });
  254. __getset(0,__proto,'green',function(){
  255. return this._green;
  256. },function(value){
  257. this._green=value;
  258. this.paramChanged();
  259. });
  260. __getset(0,__proto,'red',function(){
  261. return this._red;
  262. },function(value){
  263. this._red=value;
  264. this.paramChanged();
  265. });
  266. __getset(0,__proto,'blue',function(){
  267. return this._blue;
  268. },function(value){
  269. this._blue=value;
  270. this.paramChanged();
  271. });
  272. __getset(0,__proto,'color',function(){
  273. return this._color;
  274. },function(value){
  275. this._color=value;
  276. var colorO;
  277. colorO=ColorUtils.create(value);
  278. this._red=colorO.arrColor[0] *255;
  279. this._green=colorO.arrColor[1] *255;
  280. this._blue=colorO.arrColor[2] *255;
  281. this.paramChanged();
  282. });
  283. return ColorFilterSetter;
  284. })(FilterSetterBase)
  285. /**
  286. *...
  287. *@author ww
  288. */
  289. //class laya.effect.GlowFilterSetter extends laya.effect.FilterSetterBase
  290. var GlowFilterSetter=(function(_super){
  291. function GlowFilterSetter(){
  292. /**
  293. *滤镜的颜色
  294. */
  295. this._color="#ff0000";
  296. /**
  297. *边缘模糊的大小 0~20
  298. */
  299. this._blur=4;
  300. /**
  301. *X轴方向的偏移
  302. */
  303. this._offX=6;
  304. /**
  305. *Y轴方向的偏移
  306. */
  307. this._offY=6;
  308. GlowFilterSetter.__super.call(this);
  309. this._filter=new GlowFilter(this._color);
  310. }
  311. __class(GlowFilterSetter,'laya.effect.GlowFilterSetter',_super);
  312. var __proto=GlowFilterSetter.prototype;
  313. __proto.buildFilter=function(){
  314. this._filter=new GlowFilter(this.color,this.blur,this.offX,this.offY);
  315. _super.prototype.buildFilter.call(this);
  316. }
  317. __getset(0,__proto,'blur',function(){
  318. return this._blur;
  319. },function(value){
  320. this._blur=value;
  321. this.paramChanged();
  322. });
  323. __getset(0,__proto,'color',function(){
  324. return this._color;
  325. },function(value){
  326. this._color=value;
  327. this.paramChanged();
  328. });
  329. __getset(0,__proto,'offX',function(){
  330. return this._offX;
  331. },function(value){
  332. this._offX=value;
  333. this.paramChanged();
  334. });
  335. __getset(0,__proto,'offY',function(){
  336. return this._offY;
  337. },function(value){
  338. this._offY=value;
  339. this.paramChanged();
  340. });
  341. return GlowFilterSetter;
  342. })(FilterSetterBase)
  343. /**
  344. *...
  345. *@author ww
  346. */
  347. //class laya.effect.BlurFilterSetter extends laya.effect.FilterSetterBase
  348. var BlurFilterSetter=(function(_super){
  349. function BlurFilterSetter(){
  350. this._strength=4;
  351. BlurFilterSetter.__super.call(this);
  352. this._filter=new BlurFilter(this.strength);
  353. }
  354. __class(BlurFilterSetter,'laya.effect.BlurFilterSetter',_super);
  355. var __proto=BlurFilterSetter.prototype;
  356. __proto.buildFilter=function(){
  357. this._filter=new BlurFilter(this.strength);
  358. _super.prototype.buildFilter.call(this);
  359. }
  360. __getset(0,__proto,'strength',function(){
  361. return this._strength;
  362. },function(value){
  363. this._strength=value;
  364. });
  365. return BlurFilterSetter;
  366. })(FilterSetterBase)
  367. /**
  368. *淡出效果
  369. */
  370. //class laya.effect.FadeOut extends laya.effect.EffectBase
  371. var FadeOut=(function(_super){
  372. function FadeOut(){
  373. FadeOut.__super.call(this);;
  374. }
  375. __class(FadeOut,'laya.effect.FadeOut',_super);
  376. var __proto=FadeOut.prototype;
  377. __proto._doTween=function(){
  378. this.target.alpha=1;
  379. return Tween.to(this.target,{alpha:0},this.duration,Ease[this.ease],this._comlete,this.delay);
  380. }
  381. return FadeOut;
  382. })(EffectBase)
  383. /**
  384. *淡入效果
  385. */
  386. //class laya.effect.FadeIn extends laya.effect.EffectBase
  387. var FadeIn=(function(_super){
  388. function FadeIn(){
  389. FadeIn.__super.call(this);;
  390. }
  391. __class(FadeIn,'laya.effect.FadeIn',_super);
  392. var __proto=FadeIn.prototype;
  393. __proto._doTween=function(){
  394. this.target.alpha=0;
  395. return Tween.to(this.target,{alpha:1},this.duration,Ease[this.ease],this._comlete,this.delay);
  396. }
  397. return FadeIn;
  398. })(EffectBase)
  399. })(window,document,Laya);