scene_page.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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, guo_ju_qing_dai_dao_ju, 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.content.position = Vec3.ZERO;
  47. this.mAnimationList = this.mSceneAtt.animation_list;
  48. console.log("scene_page this.mAnimationList", this.mAnimationList)
  49. if(this.mIsMask){
  50. this.maskView.active = true;
  51. }else{
  52. this.maskView.active = false;
  53. }
  54. this.initViewAtt()
  55. ClientEvent.on(config.EventRun.NOTICE_EVENT,this.widgetBeActive.bind(this),this)
  56. ClientEvent.on(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS,this.on_zhao_bu_tong_finish.bind(this),this)
  57. ClientEvent.on(config.EventRun.ON_ZHAO_BU_TONG_ALL_FINISH,this.on_zhao_bu_tong_all_finish.bind(this),this)
  58. }
  59. public checkFinish(widget_id:number):boolean{
  60. let widget = this.mWidgetList.get(widget_id)
  61. if(!widget){
  62. tools.showToast("配置监听其他控件错误")
  63. return true
  64. }
  65. return widget.getComponent(widget_base).getIsFinish();
  66. }
  67. public checkShow(widget_id:number):boolean{
  68. let widget = this.mWidgetList.get(widget_id)
  69. if(!widget){
  70. return true
  71. }
  72. return widget.getComponent(widget_base).getIsShow();
  73. }
  74. protected onDestroy(): void {
  75. this.mWidgetList.clear()
  76. ClientEvent.off(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS,this.on_zhao_bu_tong_finish.bind(this),this)
  77. ClientEvent.off(config.EventRun.ON_ZHAO_BU_TONG_ALL_FINISH,this.on_zhao_bu_tong_all_finish.bind(this),this)
  78. ClientEvent.off(config.EventRun.NOTICE_EVENT,this.widgetBeActive.bind(this),this)
  79. }
  80. widgetBeActive(widgetId:number,event:event_item){
  81. let item = this.mWidgetList.get(widgetId)
  82. if(item){
  83. item.getComponent(widget_base).beActive(widgetId,event)
  84. }
  85. console.log("scene_page =",event.type)
  86. if(event.type===config.event_type.scene_ani){
  87. let ani_id = event.event_item_scene_ani.ani_id;
  88. this.mCurAnimation = this.getAniById(ani_id)
  89. if(this.mCurAnimation){
  90. this.mCurRunAnimation = this.mCurAnimation.ani_frame_list;
  91. if(this.mCurRunAnimation.length<2){
  92. }else{
  93. this.runAnimation()
  94. }
  95. }else{
  96. return tools.showToast(`错误的动画配置!id:${this.mData.att.id}-请检查`)
  97. }
  98. }else if(event.type===config.event_type.delete_drag_other_data){
  99. let wd = event.event_item_delete_drag_other_data.widget_id
  100. let index = event.event_item_delete_drag_other_data.delete_drag_other_index
  101. item = this.mWidgetList.get(wd)
  102. if(item){
  103. item.getComponent(widget_drag).deleteOtherDrag(index)
  104. }else{
  105. tools.showToast("删除拖拽目标配置错误!")
  106. }
  107. this.eventWidgetMoreDeleteDragOtheList(event.event_item_delete_drag_other_data, event)
  108. }else if(event.type===config.event_type.active_event) {
  109. let data = event.event_item_active_event_data
  110. this.eventMoreBindingWidgetList(data, event)
  111. }else if(event.type===config.event_type.stop_active_event) {
  112. let data = event.event_item_stop_active_event_data
  113. this.eventMoreBindingWidgetList(data, event)
  114. }else if(event.type===config.event_type.hide) {
  115. let data = event.event_item_hide_data
  116. this.eventMoreBindingWidgetList(data, event)
  117. }
  118. }
  119. eventMoreBindingWidgetList(data, event:event_item) {
  120. if(data!=null) {
  121. if(data.binding_widget_list==null||data.binding_widget_list.length==0) {
  122. return
  123. }
  124. for(let i=0;i<data.binding_widget_list.length;i++) {
  125. const element = data.binding_widget_list[i]
  126. let e_widget_id = element.binding_widget_id
  127. let e_item = this.mWidgetList.get(e_widget_id)
  128. if(e_widget_id!=-1){
  129. if(e_item){
  130. e_item.getComponent(widget_base).beActive(e_widget_id,event)
  131. }
  132. }
  133. }
  134. }
  135. }
  136. eventWidgetMoreDeleteDragOtheList(data, event:event_item) {
  137. if(data!=null) {
  138. if(data.widget_list==null||data.widget_list.length==0) {
  139. return
  140. }
  141. for(let i=0;i<data.widget_list.length;i++) {
  142. const element = data.widget_list[i]
  143. let wd = element.widget_id
  144. let index = element.delete_drag_other_index
  145. if(index!=-1) {
  146. let e_item = this.mWidgetList.get(wd)
  147. if(e_item){
  148. e_item.getComponent(widget_drag).deleteOtherDrag(index)
  149. }
  150. }
  151. }
  152. }
  153. }
  154. on_zhao_bu_tong_finish(widget:zhao_xi_jie_item_data){
  155. let item = this.mWidgetList.get(widget.widget_id)
  156. if(item===null){
  157. return tools.showToast("配置找茬错误!")
  158. }
  159. item.getComponent(widget_base).showZhaoButongFinishStatus()
  160. }
  161. on_zhao_bu_tong_all_finish(data: zhao_xi_jie_data) {
  162. let delay_time = zhao_xi_jie_data.finish_widget_hidden_delay_time * 1000
  163. setTimeout(()=> {
  164. let widget_list = gameManager.getWidgetList(this.mData)
  165. for (let index = 0; index < widget_list.length; index++) {
  166. const widget_data = widget_list[index];
  167. if(widget_data.type == config.Widget_Type_List.CLICK_TYPE ||
  168. widget_data.type == config.Widget_Type_List.SLIDE_TYPE ||
  169. widget_data.type == config.Widget_Type_List.DRAG_TYPE) {
  170. let item = this.mWidgetList.get(widget_data.att.id)
  171. item.getComponent(widget_base).hideZhaoButongFinishStatus()
  172. }
  173. }
  174. }, delay_time)
  175. }
  176. public initViewAtt(){
  177. if(this.mData.is_full_screen){
  178. }else{
  179. this.initPos()
  180. this.initSize()
  181. }
  182. this.initBG()
  183. this.initWidgetList()
  184. }
  185. public initWidgetList(){
  186. let widget_list = gameManager.getWidgetList(this.mData).sort((a,b)=>{
  187. return a.att.z - b.att.z;
  188. })
  189. let is_dai_dao_ju_drag = (id:number)=>{
  190. let task:guo_ju_qing_dai_dao_ju =gameManager.Singleton.mSceneManager.getSceneTask()._guo_ju_qing_dai_dao_ju;
  191. if(task==null){
  192. return false;
  193. }
  194. for (let index = 0; index < task.widget_list.length; index++) {
  195. const element = task.widget_list[index];
  196. if(id==element.widget_id){
  197. return true
  198. }
  199. }
  200. return false;
  201. }
  202. for (let index = 0; index < widget_list.length; index++) {
  203. const widget_data = widget_list[index];
  204. let item:Node = null;
  205. switch (widget_data.type) {
  206. case config.Widget_Type_List.CLICK_TYPE:
  207. item = instantiate(this.widget_click_prefab)
  208. break;
  209. case config.Widget_Type_List.SLIDE_TYPE:
  210. item = instantiate(this.widget_slide_prefab)
  211. break;
  212. case config.Widget_Type_List.DRAG_TYPE:
  213. if(is_dai_dao_ju_drag(widget_data.att.id)){
  214. }else{
  215. item = instantiate(this.widget_drag_prefab)
  216. }
  217. break;
  218. case config.Widget_Type_List.IMG_TYPE:
  219. item = instantiate(this.widget_img_prefab)
  220. break;
  221. case config.Widget_Type_List.ACTION_TYPE:
  222. item = instantiate(this.widget_animation_prefab)
  223. break;
  224. }
  225. if(item){
  226. let com = item.getComponent(widget_base);
  227. if(com===null){
  228. console.log("error--widget_data.type:",widget_data.type)
  229. }else{
  230. item.name =`type${widget_data.type}-id${ widget_data.att.id}`
  231. com.initView(widget_data)
  232. item.parent = this.content;
  233. this.mWidgetList.set(widget_data.att.id,item)
  234. }
  235. }
  236. }
  237. }
  238. public getBG(){
  239. return this.content;
  240. }
  241. public initBG(){
  242. if(this.mSceneAtt.src.length>0){
  243. tools.loadUrl(this.mSceneAtt.src,this.content.getComponent(Sprite))
  244. }
  245. }
  246. public initSize(){
  247. this.content.getComponent(UITransform).contentSize = new Size(this.mSceneAtt.width,this.mSceneAtt.height)
  248. }
  249. public initPos(){
  250. this.content.position = new Vec3(this.mSceneAtt.x,this.mSceneAtt.y)
  251. }
  252. runAnimation(){
  253. if(this.bindTarget!=null){
  254. Tween.stopAllByTarget(this.bindTarget)
  255. this.bindTarget = null;
  256. }
  257. // Tween.stopAll()
  258. let getFrameData = (index)=>{
  259. let cur_frame:ani_frame =this.mCurRunAnimation[index];
  260. let up_frame:ani_frame = index>0?this.mCurRunAnimation[index-1]:this.mCurRunAnimation[0];
  261. return {"cur_frame":cur_frame,"up_frame":up_frame}
  262. }
  263. let cur_index = 0;
  264. let data = getFrameData(cur_index);
  265. let call_back = ()=>{
  266. cur_index++;
  267. if(cur_index>=this.mCurRunAnimation.length){
  268. if(this.mCurAnimation.isLoop){
  269. cur_index = 0;
  270. }else{
  271. this.mPlayStatus = false;
  272. return;
  273. }
  274. }
  275. let data = getFrameData(cur_index);
  276. this.runFrame(data.cur_frame,data.up_frame,call_back)
  277. }
  278. this.runFrame(data.cur_frame,data.up_frame,call_back)
  279. }
  280. runFrame(frame:ani_frame,up_frame:ani_frame,call){
  281. let self = this;
  282. let tweenDuration: number = frame.next_time;
  283. // this.content.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(frame.url)
  284. let n_pos:Vec3 = new Vec3(frame.pos_x,frame.pos_y)
  285. let _color = new Color()
  286. _color = _color.fromHEX(frame.color)
  287. this.bindTarget = new BindTarget();
  288. let rotation = up_frame.rotation == undefined?0:up_frame.rotation
  289. this.bindTarget.rotation = rotation
  290. this.bindTarget.color = new Color().fromHEX(up_frame.color)
  291. this.bindTarget.opacity = up_frame.transparent;
  292. this.bindTarget.size = new Size(up_frame.size_width,up_frame.size_height)
  293. this.bindTarget.pos = new Vec3(up_frame.pos_x,up_frame.pos_y)
  294. this.bindTarget.anchorPointX = up_frame.anchor_point_x==undefined?0.5:up_frame.anchor_point_x
  295. this.bindTarget.anchorPointY = up_frame.anchor_point_y==undefined?0.5:up_frame.anchor_point_y
  296. let color_opactiy_tw_size = tween(this.bindTarget)
  297. .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 }, {
  298. onUpdate(tar:BindTarget){
  299. self.content.getComponent(Sprite).color = tar.color;
  300. self.content.getComponent(UIOpacity).opacity = tar.opacity;
  301. // self.content.getComponent(UITransform).setContentSize(tar.size)
  302. self.content.position = tar.pos
  303. self.content.angle = tar.rotation;
  304. self.content.getComponent(UITransform).setAnchorPoint(tar.anchorPointX, tar.anchorPointY)
  305. }
  306. }).call(()=>{
  307. call()
  308. });
  309. color_opactiy_tw_size.start();
  310. }
  311. private getAniById(id:number){
  312. if(this.mAnimationList.length>0){
  313. for (let index = 0; index < this.mAnimationList.length; index++) {
  314. const element = this.mAnimationList[index];
  315. if(id==element.ani_id){
  316. return element;
  317. }
  318. }
  319. }
  320. return null;
  321. }
  322. }