widget_base.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import { _decorator, Color, Component, Node, Size, Sprite, tween, Tween, UIOpacity, UITransform, Vec3 } from 'cc';
  2. import { ani_frame, att_ani_data, event_item, widget_item_data } from '../../../data/data';
  3. import { gameManager } from '../gameManager';
  4. import { ClientEvent } from '../../clientEvent';
  5. import { config } from '../../config';
  6. import { tools } from '../../tools';
  7. import { scene_page } from '../scene_page';
  8. const { ccclass, property } = _decorator;
  9. class BindTarget{
  10. color : Color;
  11. opacity:number;
  12. size:Size;
  13. pos:Vec3;
  14. rotation:number;
  15. anchorPointX:number;
  16. anchorPointY:number;
  17. scale:Vec3;
  18. }
  19. @ccclass('widget_base')
  20. export class widget_base extends Component {
  21. @property(Node) icon:Node = null;
  22. protected mData:widget_item_data = null;
  23. protected mAnimationList:att_ani_data[] = []; //动画组
  24. protected mCurRunAnimation:ani_frame[] = [];
  25. protected mCurAnimation:att_ani_data = null;
  26. protected mPlayStatus:boolean = false;
  27. protected mIsFinish:boolean = false;
  28. protected mIsActive:boolean = false;
  29. protected mIsShow:boolean = false;
  30. private bindTarget:BindTarget = null;
  31. protected isInit:boolean = true;
  32. protected initWidget(data:widget_item_data){
  33. this.mData = data;
  34. this.icon.getComponent(UITransform).contentSize = new Size(this.mData.att.width,this.mData.att.height)
  35. this.icon.position = new Vec3(this.mData.att.x,this.mData.att.y)
  36. var anchor_x = 0.5
  37. if(this.mData.att.anchor_x!=undefined&&this.mData.att.anchor_x!=null) {
  38. anchor_x = this.mData.att.anchor_x
  39. }
  40. var anchor_y = 0.5
  41. if(this.mData.att.anchor_x!=undefined&&this.mData.att.anchor_x!=null) {
  42. anchor_y = this.mData.att.anchor_y
  43. }
  44. this.icon.getComponent(UITransform).setAnchorPoint(anchor_x, anchor_y)
  45. if(this.mData.att.rotation == undefined || this.mData.att.rotation == null) {
  46. this.icon.angle = 0
  47. } else {
  48. this.icon.angle = this.mData.att.rotation
  49. }
  50. if(this.icon.getComponent(UIOpacity)===null){
  51. this.icon.addComponent(UIOpacity)
  52. }
  53. if(this.mData.att.src.length>0){
  54. this.icon.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.icon,this.mData.att.src)
  55. }
  56. this.updateDir()
  57. this.mIsShow = this.mData.att.is_show;
  58. this.mIsActive = this.mData.att.is_interaction;
  59. this.mAnimationList = this.mData.att.animation_list;
  60. if(this.mIsShow){
  61. this.node.active = true;
  62. this.onWidgetShow()
  63. }else{
  64. this.node.active = false;
  65. }
  66. if(this.isInit){
  67. this.init()
  68. }else{
  69. this.node.position = Vec3.ZERO;
  70. this.icon.position = Vec3.ZERO;
  71. this.node.active = true;
  72. this.onWidgetShow()
  73. }
  74. // ClientEvent.on(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
  75. // ClientEvent.on(config.EventRun.WIDGET_HIDE,this.hide.bind(this),this)
  76. };
  77. updateDir(){
  78. switch (this.mData.att.img_dir) {
  79. case config.widget_scale_dir.left:
  80. this.node.scale = new Vec3(-1,1,1)
  81. break;
  82. case config.widget_scale_dir.up:
  83. this.node.scale = new Vec3(1,-1,1)
  84. break;
  85. case config.widget_scale_dir.normal:
  86. this.node.scale = new Vec3(1,1,1)
  87. break;
  88. }
  89. }
  90. protected onDestroy(): void {
  91. // ClientEvent.off(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
  92. }
  93. private getAniById(id:number){
  94. if(this.mAnimationList.length>0){
  95. for (let index = 0; index < this.mAnimationList.length; index++) {
  96. const element = this.mAnimationList[index];
  97. if(id==element.ani_id){
  98. return element;
  99. }
  100. }
  101. }
  102. return null;
  103. }
  104. onWidgetShow(){
  105. this.node.active = true;
  106. this.mIsShow = true;
  107. if(this.mData.att.animation_list.length>0){
  108. if(this.mData.att.animation_list[0].ani_frame_list.length<=0){
  109. }else{
  110. this.mCurAnimation = this.getAniById(this.mData.att.animation_list[0].ani_id)
  111. if(this.mCurAnimation){
  112. this.mCurRunAnimation = this.mCurAnimation.ani_frame_list;
  113. if(this.mCurRunAnimation.length<2){
  114. }else{
  115. this.runAnimation()
  116. }
  117. }else{
  118. return tools.showToast(`错误的动画配置!id:${this.mData.att.id}-请检查`)
  119. }
  120. }
  121. }
  122. }
  123. public hide(widgetId:number,event:event_item){
  124. if(widgetId===this.mData.att.id){
  125. this.mIsShow = false;
  126. this.node.active = false;
  127. }
  128. }
  129. public beActive(widgetId:number,event:event_item){
  130. if(widgetId===this.mData.att.id){
  131. if(event.type===config.event_type.hide){
  132. this.hide(widgetId,event)
  133. return
  134. }
  135. if(!this.mIsShow){
  136. this.onWidgetShow()
  137. }
  138. if(!this.mIsActive){
  139. this.registeredEvent()
  140. }
  141. this.mIsActive = true;
  142. switch (event.type) {
  143. case config.event_type.play_ani: //播放一个动画
  144. let event_data_nai = event.event_item_play_ani_data
  145. if(event_data_nai!=null){
  146. this.mCurAnimation = this.getAniById(event_data_nai.ani_id)
  147. if(this.mCurAnimation){
  148. this.mCurRunAnimation = this.mCurAnimation.ani_frame_list;
  149. this.runAnimation()
  150. }else{
  151. return tools.showToast("错误的动画配置!请检查")
  152. }
  153. }
  154. break;
  155. case config.event_type.change_one_item_status: //改变自己的突变资源属性
  156. let event_data_change_one_item_status = event.event_item_change_one_item_status_data
  157. if(event_data_change_one_item_status!=null){
  158. // gameManager.initUiBaseAtt(this.icon,event_data_change_one_item_status.att)
  159. let att_res = event_data_change_one_item_status.att.res
  160. if(att_res.length > 0) {
  161. tools.loadUrl(att_res,this.icon.getComponent(Sprite))
  162. }
  163. }
  164. break;
  165. case config.event_type.show_new_item: //显示自己同时被激活
  166. break;
  167. case config.event_type.stop_active_event: //停止激活
  168. this.mIsActive = false;
  169. break;
  170. // case config.event_type.collect_event: //自己被触发后,被收集类给收集
  171. // break;
  172. // case config.event_type.active_event: //立刻被激活
  173. // break;
  174. case config.event_type.be_event: //被动激活,等待被玩家交互
  175. console.log("this.be_event",this.mIsShow)
  176. break;
  177. }
  178. }
  179. }
  180. runAnimation(){
  181. if(this.bindTarget!=null){
  182. Tween.stopAllByTarget(this.bindTarget)
  183. this.bindTarget = null;
  184. }
  185. if(this.mCurAnimation.isMove==undefined||this.mCurAnimation.isMove==null){
  186. this.mCurAnimation.isMove = true;
  187. }
  188. // Tween.stopAll()
  189. let getFrameData = (index)=>{
  190. let cur_frame:ani_frame =this.mCurRunAnimation[index];
  191. let up_frame:ani_frame = index>0?this.mCurRunAnimation[index-1]:this.mCurRunAnimation[0];
  192. return {"cur_frame":cur_frame,"up_frame":up_frame}
  193. }
  194. let cur_index = 0;
  195. let data = getFrameData(cur_index);
  196. let call_back = ()=>{
  197. cur_index++;
  198. if(cur_index>=this.mCurRunAnimation.length){
  199. if(this.mCurAnimation.isLoop){
  200. cur_index = 0;
  201. }else{
  202. this.mPlayStatus = false;
  203. this.onFinishAnimation()
  204. return;
  205. }
  206. }
  207. let data = getFrameData(cur_index);
  208. this.runFrame(data.cur_frame,data.up_frame,call_back)
  209. }
  210. this.runFrame(data.cur_frame,data.up_frame,call_back)
  211. }
  212. runFrame(frame:ani_frame,up_frame:ani_frame,call){
  213. if(!frame || !up_frame) { return }
  214. let self = this;
  215. let tweenDuration: number = frame.next_time;
  216. // let origin_x = self.mCurRunAnimation[0].pos_x;
  217. // let origin_y = self.mCurRunAnimation[0].pos_y
  218. this.icon.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName( this.icon,frame.url)
  219. let n_pos:Vec3 = new Vec3(frame.pos_x,frame.pos_y)
  220. let _color = new Color()
  221. _color = _color.fromHEX(frame.color)
  222. this.bindTarget = new BindTarget();
  223. let rotation = up_frame.rotation == undefined?0:up_frame.rotation
  224. this.bindTarget.rotation = rotation
  225. this.bindTarget.color = new Color().fromHEX(up_frame.color)
  226. this.bindTarget.opacity = up_frame.transparent;
  227. this.bindTarget.size = new Size(up_frame.size_width,up_frame.size_height)
  228. this.bindTarget.pos = new Vec3(up_frame.pos_x,up_frame.pos_y)
  229. up_frame.scale_x = up_frame.scale_x==undefined?1:up_frame.scale_x
  230. up_frame.scale_y = up_frame.scale_y==undefined?1:up_frame.scale_y
  231. frame.scale_x = frame.scale_x==undefined?1:frame.scale_x
  232. frame.scale_y = frame.scale_y==undefined?1:frame.scale_y
  233. this.bindTarget.scale = new Vec3(up_frame.scale_x,up_frame.scale_y)
  234. this.bindTarget.anchorPointX = up_frame.anchor_point_x==undefined?0.5:up_frame.anchor_point_x
  235. this.bindTarget.anchorPointY = up_frame.anchor_point_y==undefined?0.5:up_frame.anchor_point_y
  236. let color_opactiy_tw_size = tween(this.bindTarget)
  237. .to( tweenDuration, { scale:new Vec3(frame.scale_x,frame.scale_y),pos:n_pos,color: _color,opacity:frame.transparent,size:new Size(frame.size_width,frame.size_height),rotation:frame.rotation, anchorPointX:frame.anchor_point_x, anchorPointY:frame.anchor_point_y }, {
  238. onUpdate(tar:BindTarget){
  239. self.icon.getComponent(Sprite).color = tar.color;
  240. self.icon.getComponent(UIOpacity).opacity = tar.opacity;
  241. self.icon.getComponent(UITransform).setContentSize(tar.size)
  242. self.icon.getComponent(UITransform).setAnchorPoint(tar.anchorPointX, tar.anchorPointY)
  243. self.icon.scale = new Vec3(tar.scale.x,tar.scale.y,1);
  244. if(self.mCurAnimation.isMove){
  245. self.node.position = tar.pos
  246. }
  247. self.node.angle = tar.rotation;
  248. }
  249. }).call(()=>{
  250. call && call()
  251. });
  252. color_opactiy_tw_size.start();
  253. }
  254. public getIsFinish():boolean{
  255. return this.mIsFinish
  256. }
  257. public getIsShow():boolean{
  258. if(this.icon.getComponent(UIOpacity).opacity==0||this.mIsFinish){
  259. return false;
  260. }
  261. return this.mIsShow;
  262. }
  263. public getScenePage():scene_page {
  264. let name = this.node.parent.name
  265. let parent_name = this.node.parent.parent.name
  266. if(name=="content"){
  267. let parent = this.node.parent.parent.parent.parent
  268. let c_scene_page = parent.getComponent(scene_page)
  269. return c_scene_page
  270. }
  271. if(parent_name=="content"){
  272. let parent = this.node.parent.parent.parent.parent.parent
  273. let c_scene_page = parent.getComponent(scene_page)
  274. return c_scene_page
  275. }
  276. return null
  277. }
  278. protected registeredEvent(){
  279. }
  280. public getIcon(){
  281. return this.icon
  282. }
  283. public offEvent(){
  284. this.node.off(Node.EventType.TOUCH_START)
  285. this.node.off(Node.EventType.TOUCH_END)
  286. this.node.off(Node.EventType.TOUCH_CANCEL)
  287. this.node.off(Node.EventType.TOUCH_MOVE)
  288. this.mIsFinish = true;
  289. }
  290. protected onFinishAnimation(){
  291. }; //动画播放完成
  292. protected onFinishEvent(){
  293. this.offEvent()
  294. ClientEvent.dispatchEvent(config.EventRun.WIDGET_FINISH,this.mData.att.id)
  295. }; //完成自身
  296. protected onFialEvent(){}; // 失败完成
  297. protected init(){};
  298. public initView(data:widget_item_data,is_init:boolean = true){
  299. this.isInit = is_init;
  300. this.initWidget(data)
  301. };
  302. public getData(){
  303. return this.mData
  304. }
  305. public showZhaoButongFinishStatus(){}
  306. public hideZhaoButongFinishStatus(){}
  307. }