scene_page.ts 8.6 KB

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