scene_page.ts 11 KB

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