scene_page.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. protected onDestroy(): void {
  64. this.mWidgetList.clear()
  65. ClientEvent.off(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS,this.on_zhao_bu_tong_finish.bind(this),this)
  66. ClientEvent.off(config.EventRun.NOTICE_EVENT,this.widgetBeActive.bind(this),this)
  67. }
  68. widgetBeActive(widgetId:number,event:event_item){
  69. let item = this.mWidgetList.get(widgetId)
  70. if(item){
  71. item.getComponent(widget_base).beActive(widgetId,event)
  72. }
  73. console.log("event.type",event.type)
  74. if(event.type===config.event_type.scene_ani){
  75. let ani_id = event.event_item_scene_ani.ani_id;
  76. this.mCurAnimation = this.getAniById(ani_id)
  77. if(this.mCurAnimation){
  78. this.mCurRunAnimation = this.mCurAnimation.ani_frame_list;
  79. if(this.mCurRunAnimation.length<2){
  80. }else{
  81. this.runAnimation()
  82. }
  83. }else{
  84. return tools.showToast(`错误的动画配置!id:${this.mData.att.id}-请检查`)
  85. }
  86. }else if(event.type===config.event_type.delete_drag_other_data){
  87. let wd = event.event_item_delete_drag_other_data.widget_id
  88. let index = event.event_item_delete_drag_other_data.delete_drag_other_index
  89. item = this.mWidgetList.get(wd)
  90. if(item){
  91. item.getComponent(widget_drag).deleteOtherDrag(index)
  92. }else{
  93. tools.showToast("删除拖拽目标配置错误!")
  94. }
  95. }
  96. }
  97. on_zhao_bu_tong_finish(widget:zhao_xi_jie_item_data){
  98. let item = this.mWidgetList.get(widget.widget_id)
  99. if(item===null){
  100. return tools.showToast("配置找茬错误!")
  101. }
  102. item.getComponent(widget_base).showZhaoButongFinishStatus()
  103. }
  104. public initViewAtt(){
  105. if(this.mData.is_full_screen){
  106. }else{
  107. this.initPos()
  108. this.initSize()
  109. }
  110. this.initBG()
  111. this.initWidgetList()
  112. }
  113. public initWidgetList(){
  114. let widget_list = gameManager.getWidgetList(this.mData).sort((a,b)=>{
  115. return a.att.z - b.att.z;
  116. })
  117. for (let index = 0; index < widget_list.length; index++) {
  118. const widget_data = widget_list[index];
  119. let item:Node = null;
  120. switch (widget_data.type) {
  121. case config.Widget_Type_List.CLICK_TYPE:
  122. item = instantiate(this.widget_click_prefab)
  123. break;
  124. case config.Widget_Type_List.SLIDE_TYPE:
  125. item = instantiate(this.widget_slide_prefab)
  126. break;
  127. case config.Widget_Type_List.DRAG_TYPE:
  128. item = instantiate(this.widget_drag_prefab)
  129. break;
  130. case config.Widget_Type_List.IMG_TYPE:
  131. item = instantiate(this.widget_img_prefab)
  132. break;
  133. case config.Widget_Type_List.ACTION_TYPE:
  134. item = instantiate(this.widget_animation_prefab)
  135. break;
  136. }
  137. let com = item.getComponent(widget_base);
  138. if(com===null){
  139. console.log("error--widget_data.type:",widget_data.type)
  140. }else{
  141. item.name =`type${widget_data.type}-id${ widget_data.att.id}`
  142. com.initView(widget_data)
  143. item.parent = this.content;
  144. this.mWidgetList.set(widget_data.att.id,item)
  145. }
  146. }
  147. }
  148. public initBG(){
  149. if(this.mSceneAtt.src.length>0){
  150. tools.loadUrl(this.mSceneAtt.src,this.content.getComponent(Sprite))
  151. }
  152. }
  153. public initSize(){
  154. this.content.getComponent(UITransform).contentSize = new Size(this.mSceneAtt.width,this.mSceneAtt.height)
  155. }
  156. public initPos(){
  157. this.content.position = new Vec3(this.mSceneAtt.x,this.mSceneAtt.y)
  158. }
  159. runAnimation(){
  160. if(this.bindTarget!=null){
  161. Tween.stopAllByTarget(this.bindTarget)
  162. this.bindTarget = null;
  163. }
  164. // Tween.stopAll()
  165. let getFrameData = (index)=>{
  166. let cur_frame:ani_frame =this.mCurRunAnimation[index];
  167. let up_frame:ani_frame = index>0?this.mCurRunAnimation[index-1]:this.mCurRunAnimation[0];
  168. return {"cur_frame":cur_frame,"up_frame":up_frame}
  169. }
  170. let cur_index = 0;
  171. let data = getFrameData(cur_index);
  172. let call_back = ()=>{
  173. cur_index++;
  174. if(cur_index>=this.mCurRunAnimation.length){
  175. if(this.mCurAnimation.isLoop){
  176. cur_index = 0;
  177. }else{
  178. this.mPlayStatus = false;
  179. return;
  180. }
  181. }
  182. let data = getFrameData(cur_index);
  183. this.runFrame(data.cur_frame,data.up_frame,call_back)
  184. }
  185. this.runFrame(data.cur_frame,data.up_frame,call_back)
  186. }
  187. runFrame(frame:ani_frame,up_frame:ani_frame,call){
  188. let self = this;
  189. let tweenDuration: number = frame.next_time;
  190. // this.content.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(frame.url)
  191. let n_pos:Vec3 = new Vec3(frame.pos_x,frame.pos_y)
  192. let _color = new Color()
  193. _color = _color.fromHEX(frame.color)
  194. this.bindTarget = new BindTarget();
  195. let rotation = up_frame.rotation == undefined?0:up_frame.rotation
  196. this.bindTarget.rotation = rotation
  197. this.bindTarget.color = new Color().fromHEX(up_frame.color)
  198. this.bindTarget.opacity = up_frame.transparent;
  199. this.bindTarget.size = new Size(up_frame.size_width,up_frame.size_height)
  200. this.bindTarget.pos = new Vec3(up_frame.pos_x,up_frame.pos_y)
  201. let color_opactiy_tw_size = tween(this.bindTarget)
  202. .to( tweenDuration, { pos:n_pos,color: _color,opacity:frame.transparent,size:new Size(frame.size_width,frame.size_height),rotation:frame.rotation }, {
  203. onUpdate(tar:BindTarget){
  204. self.content.getComponent(Sprite).color = tar.color;
  205. self.content.getComponent(UIOpacity).opacity = tar.opacity;
  206. // self.content.getComponent(UITransform).setContentSize(tar.size)
  207. self.content.position = tar.pos
  208. self.content.angle = tar.rotation;
  209. }
  210. }).call(()=>{
  211. call()
  212. });
  213. color_opactiy_tw_size.start();
  214. }
  215. private getAniById(id:number){
  216. if(this.mAnimationList.length>0){
  217. for (let index = 0; index < this.mAnimationList.length; index++) {
  218. const element = this.mAnimationList[index];
  219. if(id==element.ani_id){
  220. return element;
  221. }
  222. }
  223. }
  224. return null;
  225. }
  226. }