scene_page.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { _decorator, Color, Component, instantiate, misc, Node, Prefab, Size, Sprite, tween, Tween, UIOpacity, UITransform, Vec3, Widget } from 'cc';
  2. import { ani_frame, att_ani_data, attributes_data, event_item, scene_item_data, widget_item_data, zhao_xi_jie_item_data } from '../../data/data';
  3. import { config } from '../config';
  4. import { tools } from '../tools';
  5. import { gameManager } from './gameManager';
  6. import { widget_base } from './widget/widget_base';
  7. import { ClientEvent } from '../clientEvent';
  8. import { widget_drag } from './widget/widget_drag';
  9. const { ccclass, property } = _decorator;
  10. class BindTarget{
  11. color : Color;
  12. opacity:number;
  13. size:Size;
  14. pos:Vec3;
  15. rotation:number;
  16. }
  17. @ccclass('scene_page')
  18. export class scene_page extends Component {
  19. @property(Node) content:Node = null;
  20. @property(Node) maskView:Node = null;
  21. @property(Prefab) widget_click_prefab:Prefab = null;
  22. @property(Prefab) widget_drag_prefab:Prefab = null;
  23. @property(Prefab) widget_slide_prefab:Prefab = null;
  24. @property(Prefab) widget_img_prefab:Prefab = null;
  25. @property(Prefab) widget_animation_prefab:Prefab = null;
  26. private mData:scene_item_data = null;
  27. private mIsMask:boolean = false;
  28. private mSceneAtt:attributes_data = null;
  29. private mWidgetList:Map<number,Node> = new Map;
  30. private bindTarget:BindTarget = null;
  31. protected mAnimationList:att_ani_data[] = []; //动画组
  32. protected mCurRunAnimation:ani_frame[] = [];
  33. protected mCurAnimation:att_ani_data = null;
  34. protected mPlayStatus:boolean = false;
  35. public initView(page_data:scene_item_data){
  36. this.mData = page_data;
  37. this.mIsMask = this.mData.is_check_mask;
  38. this.mSceneAtt = this.mData.att;
  39. if(this.mData.is_full_screen){
  40. this.content.getComponent(Widget).enabled = true;
  41. }else{
  42. this.content.getComponent(Widget).enabled = false;
  43. }
  44. this.mAnimationList = this.mSceneAtt.animation_list;
  45. console.log(" this.mAnimationList", this.mAnimationList)
  46. if(this.mIsMask){
  47. this.maskView.active = true;
  48. }else{
  49. this.maskView.active = false;
  50. }
  51. this.initViewAtt()
  52. ClientEvent.on(config.EventRun.NOTICE_EVENT,this.widgetBeActive.bind(this),this)
  53. ClientEvent.on(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS,this.on_zhao_bu_tong_finish.bind(this),this)
  54. }
  55. public checkFinish(widget_id:number):boolean{
  56. let widget = this.mWidgetList.get(widget_id)
  57. if(!widget){
  58. tools.showToast("配置监听其他控件错误")
  59. return true
  60. }
  61. return widget.getComponent(widget_base).getIsFinish();
  62. }
  63. public checkShow(widget_id:number):boolean{
  64. let widget = this.mWidgetList.get(widget_id)
  65. if(!widget){
  66. return true
  67. }
  68. return widget.getComponent(widget_base).getIsShow();
  69. }
  70. protected onDestroy(): void {
  71. this.mWidgetList.clear()
  72. ClientEvent.off(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS,this.on_zhao_bu_tong_finish.bind(this),this)
  73. ClientEvent.off(config.EventRun.NOTICE_EVENT,this.widgetBeActive.bind(this),this)
  74. }
  75. widgetBeActive(widgetId:number,event:event_item){
  76. let item = this.mWidgetList.get(widgetId)
  77. if(item){
  78. item.getComponent(widget_base).beActive(widgetId,event)
  79. }
  80. console.log("event.type",event.type)
  81. if(event.type===config.event_type.scene_ani){
  82. let ani_id = event.event_item_scene_ani.ani_id;
  83. this.mCurAnimation = this.getAniById(ani_id)
  84. if(this.mCurAnimation){
  85. this.mCurRunAnimation = this.mCurAnimation.ani_frame_list;
  86. if(this.mCurRunAnimation.length<2){
  87. }else{
  88. this.runAnimation()
  89. }
  90. }else{
  91. return tools.showToast(`错误的动画配置!id:${this.mData.att.id}-请检查`)
  92. }
  93. }else if(event.type===config.event_type.delete_drag_other_data){
  94. let wd = event.event_item_delete_drag_other_data.widget_id
  95. let index = event.event_item_delete_drag_other_data.delete_drag_other_index
  96. item = this.mWidgetList.get(wd)
  97. if(item){
  98. item.getComponent(widget_drag).deleteOtherDrag(index)
  99. }else{
  100. tools.showToast("删除拖拽目标配置错误!")
  101. }
  102. }
  103. }
  104. on_zhao_bu_tong_finish(widget:zhao_xi_jie_item_data){
  105. let item = this.mWidgetList.get(widget.widget_id)
  106. if(item===null){
  107. return tools.showToast("配置找茬错误!")
  108. }
  109. item.getComponent(widget_base).showZhaoButongFinishStatus()
  110. }
  111. public initViewAtt(){
  112. if(this.mData.is_full_screen){
  113. }else{
  114. this.initPos()
  115. this.initSize()
  116. }
  117. this.initBG()
  118. this.initWidgetList()
  119. }
  120. public initWidgetList(){
  121. let widget_list = gameManager.getWidgetList(this.mData).sort((a,b)=>{
  122. return a.att.z - b.att.z;
  123. })
  124. for (let index = 0; index < widget_list.length; index++) {
  125. const widget_data = widget_list[index];
  126. let item:Node = null;
  127. switch (widget_data.type) {
  128. case config.Widget_Type_List.CLICK_TYPE:
  129. item = instantiate(this.widget_click_prefab)
  130. break;
  131. case config.Widget_Type_List.SLIDE_TYPE:
  132. item = instantiate(this.widget_slide_prefab)
  133. break;
  134. case config.Widget_Type_List.DRAG_TYPE:
  135. item = instantiate(this.widget_drag_prefab)
  136. break;
  137. case config.Widget_Type_List.IMG_TYPE:
  138. item = instantiate(this.widget_img_prefab)
  139. break;
  140. case config.Widget_Type_List.ACTION_TYPE:
  141. item = instantiate(this.widget_animation_prefab)
  142. break;
  143. }
  144. let com = item.getComponent(widget_base);
  145. if(com===null){
  146. console.log("error--widget_data.type:",widget_data.type)
  147. }else{
  148. item.name =`type${widget_data.type}-id${ widget_data.att.id}`
  149. com.initView(widget_data)
  150. item.parent = this.content;
  151. this.mWidgetList.set(widget_data.att.id,item)
  152. }
  153. }
  154. }
  155. public initBG(){
  156. if(this.mSceneAtt.src.length>0){
  157. tools.loadUrl(this.mSceneAtt.src,this.content.getComponent(Sprite))
  158. }
  159. }
  160. public initSize(){
  161. this.content.getComponent(UITransform).contentSize = new Size(this.mSceneAtt.width,this.mSceneAtt.height)
  162. }
  163. public initPos(){
  164. this.content.position = new Vec3(this.mSceneAtt.x,this.mSceneAtt.y)
  165. }
  166. runAnimation(){
  167. if(this.bindTarget!=null){
  168. Tween.stopAllByTarget(this.bindTarget)
  169. this.bindTarget = null;
  170. }
  171. // Tween.stopAll()
  172. let getFrameData = (index)=>{
  173. let cur_frame:ani_frame =this.mCurRunAnimation[index];
  174. let up_frame:ani_frame = index>0?this.mCurRunAnimation[index-1]:this.mCurRunAnimation[0];
  175. return {"cur_frame":cur_frame,"up_frame":up_frame}
  176. }
  177. let cur_index = 0;
  178. let data = getFrameData(cur_index);
  179. let call_back = ()=>{
  180. cur_index++;
  181. if(cur_index>=this.mCurRunAnimation.length){
  182. if(this.mCurAnimation.isLoop){
  183. cur_index = 0;
  184. }else{
  185. this.mPlayStatus = false;
  186. return;
  187. }
  188. }
  189. let data = getFrameData(cur_index);
  190. this.runFrame(data.cur_frame,data.up_frame,call_back)
  191. }
  192. this.runFrame(data.cur_frame,data.up_frame,call_back)
  193. }
  194. runFrame(frame:ani_frame,up_frame:ani_frame,call){
  195. let self = this;
  196. let tweenDuration: number = frame.next_time;
  197. // this.content.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(frame.url)
  198. let n_pos:Vec3 = new Vec3(frame.pos_x,frame.pos_y)
  199. let _color = new Color()
  200. _color = _color.fromHEX(frame.color)
  201. this.bindTarget = new BindTarget();
  202. let rotation = up_frame.rotation == undefined?0:up_frame.rotation
  203. this.bindTarget.rotation = rotation
  204. this.bindTarget.color = new Color().fromHEX(up_frame.color)
  205. this.bindTarget.opacity = up_frame.transparent;
  206. this.bindTarget.size = new Size(up_frame.size_width,up_frame.size_height)
  207. this.bindTarget.pos = new Vec3(up_frame.pos_x,up_frame.pos_y)
  208. let color_opactiy_tw_size = tween(this.bindTarget)
  209. .to( tweenDuration, { pos:n_pos,color: _color,opacity:frame.transparent,size:new Size(frame.size_width,frame.size_height),rotation:frame.rotation }, {
  210. onUpdate(tar:BindTarget){
  211. self.content.getComponent(Sprite).color = tar.color;
  212. self.content.getComponent(UIOpacity).opacity = tar.opacity;
  213. // self.content.getComponent(UITransform).setContentSize(tar.size)
  214. self.content.position = tar.pos
  215. self.content.angle = tar.rotation;
  216. }
  217. }).call(()=>{
  218. call()
  219. });
  220. color_opactiy_tw_size.start();
  221. }
  222. private getAniById(id:number){
  223. if(this.mAnimationList.length>0){
  224. for (let index = 0; index < this.mAnimationList.length; index++) {
  225. const element = this.mAnimationList[index];
  226. if(id==element.ani_id){
  227. return element;
  228. }
  229. }
  230. }
  231. return null;
  232. }
  233. }