scene_page.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. import { _decorator, Color, Component, instantiate, misc, Node, Prefab, ScrollView, Size, Sprite, tween, Tween, UIOpacity, UITransform, Vec3, Widget } from 'cc';
  2. import { ani_frame, att_ani_data, attributes_data, event_item, event_item_delete_drag_other, 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) scroll_view:Node = null;
  22. @property(Node) content:Node = null;
  23. @property(Node) maskView:Node = null;
  24. @property(Prefab) widget_click_prefab:Prefab = null;
  25. @property(Prefab) widget_drag_prefab:Prefab = null;
  26. @property(Prefab) widget_slide_prefab:Prefab = null;
  27. @property(Prefab) widget_img_prefab:Prefab = null;
  28. @property(Prefab) widget_container_prefab:Prefab = null;
  29. @property(Prefab) widget_animation_prefab:Prefab = null;
  30. private mData:scene_item_data = null;
  31. private mIsMask:boolean = false;
  32. private mSceneAtt:attributes_data = null;
  33. private mWidgetList:Map<number,Node> = new Map;
  34. private bindTarget:BindTarget = null;
  35. protected mAnimationList:att_ani_data[] = []; //动画组
  36. protected mCurRunAnimation:ani_frame[] = [];
  37. protected mCurAnimation:att_ani_data = null;
  38. protected mPlayStatus:boolean = false;
  39. public initView(page_data:scene_item_data){
  40. this.mData = page_data;
  41. this.mIsMask = this.mData.is_check_mask;
  42. this.mSceneAtt = this.mData.att;
  43. if(this.mData.is_full_screen){
  44. this.content.getComponent(Widget).enabled = true;
  45. }else{
  46. this.content.getComponent(Widget).enabled = false;
  47. }
  48. this.content.position = Vec3.ZERO;
  49. this.mAnimationList = this.mSceneAtt.animation_list;
  50. console.log("scene_page this.mAnimationList", this.mAnimationList)
  51. if(this.mIsMask){
  52. this.maskView.active = true;
  53. }else{
  54. this.maskView.active = false;
  55. }
  56. this.initViewAtt()
  57. ClientEvent.on(config.EventRun.NOTICE_EVENT,this.widgetBeActive.bind(this),this)
  58. ClientEvent.on(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS,this.on_zhao_bu_tong_finish.bind(this),this)
  59. ClientEvent.on(config.EventRun.ON_ZHAO_BU_TONG_ALL_FINISH,this.on_zhao_bu_tong_all_finish.bind(this),this)
  60. }
  61. public checkFinish(widget_id:number):boolean{
  62. let widget = this.mWidgetList.get(widget_id)
  63. if(!widget){
  64. tools.showToast("配置监听其他控件错误")
  65. return true
  66. }
  67. return widget.getComponent(widget_base).getIsFinish();
  68. }
  69. public checkShow(widget_id:number):boolean{
  70. let widget = this.mWidgetList.get(widget_id)
  71. if(!widget){
  72. return true
  73. }
  74. return widget.getComponent(widget_base).getIsShow();
  75. }
  76. protected onDestroy(): void {
  77. this.mWidgetList.clear()
  78. ClientEvent.off(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS,this.on_zhao_bu_tong_finish.bind(this),this)
  79. ClientEvent.off(config.EventRun.ON_ZHAO_BU_TONG_ALL_FINISH,this.on_zhao_bu_tong_all_finish.bind(this),this)
  80. ClientEvent.off(config.EventRun.NOTICE_EVENT,this.widgetBeActive.bind(this),this)
  81. }
  82. widgetBeActive(widgetId:number,event:event_item){
  83. let item = this.mWidgetList.get(widgetId)
  84. if(item){
  85. item.getComponent(widget_base).beActive(widgetId,event)
  86. }
  87. // console.log("scene_page=",event.type)
  88. // let log_string = `执行类型${event.type}_${config.get_event_type_name(event.type)} 控件id${widgetId}`
  89. // console.log(log_string)
  90. if(event.type===config.event_type.scene_ani){
  91. let ani_id = event.event_item_scene_ani.ani_id;
  92. this.mCurAnimation = this.getAniById(ani_id)
  93. if(this.mCurAnimation){
  94. this.mCurRunAnimation = this.mCurAnimation.ani_frame_list;
  95. if(this.mCurRunAnimation.length<2){
  96. }else{
  97. this.runAnimation()
  98. }
  99. }else{
  100. return tools.showToast(`错误的动画配置!id:${this.mData.att.id}-请检查`)
  101. }
  102. }else if(event.type===config.event_type.delete_drag_other_data){
  103. this.eventDeleteDragOtherData(event)
  104. }else if(event.type===config.event_type.active_event) {
  105. let data = event.event_item_active_event_data
  106. this.eventMoreBindingWidgetList(data, event)
  107. }else if(event.type===config.event_type.stop_active_event) {
  108. let data = event.event_item_stop_active_event_data
  109. this.eventMoreBindingWidgetList(data, event)
  110. }else if(event.type===config.event_type.hide) {
  111. let data = event.event_item_hide_data
  112. this.eventMoreBindingWidgetList(data, event)
  113. }
  114. }
  115. eventDeleteDragOtherData(event:event_item){
  116. let wd = event.event_item_delete_drag_other_data.widget_id
  117. let index = event.event_item_delete_drag_other_data.delete_drag_other_index
  118. let other_widget_list = event.event_item_delete_drag_other_data.widget_list
  119. if(other_widget_list.length==0) {
  120. let item = this.mWidgetList.get(wd)
  121. if(item){
  122. item.getComponent(widget_drag).deleteOtherDrag(index)
  123. }else{
  124. // tools.showToast("删除拖拽目标配置错误!")
  125. }
  126. } else {
  127. let data_list:event_item_delete_drag_other[] = JSON.parse(JSON.stringify(other_widget_list))
  128. if(wd!=-1) {
  129. let first_data = new event_item_delete_drag_other
  130. first_data.widget_id = wd
  131. first_data.delete_drag_other_index = index
  132. data_list.unshift(first_data)
  133. }
  134. for(let i=0;i<data_list.length;i++) {
  135. const element = data_list[i]
  136. let wd = element.widget_id
  137. let index = element.delete_drag_other_index
  138. let item = this.mWidgetList.get(wd)
  139. if(item) {
  140. item.getComponent(widget_drag).deleteOtherDrag(index)
  141. }
  142. }
  143. }
  144. }
  145. eventMoreBindingWidgetList(data, event:event_item) {
  146. if(data!=null) {
  147. if(data.binding_widget_list==null||data.binding_widget_list.length==0) {
  148. return
  149. }
  150. for(let i=0;i<data.binding_widget_list.length;i++) {
  151. const element = data.binding_widget_list[i]
  152. let e_widget_id = element.binding_widget_id
  153. let e_item = this.mWidgetList.get(e_widget_id)
  154. if(e_widget_id!=-1){
  155. if(e_item){
  156. e_item.getComponent(widget_base).beActive(e_widget_id,event)
  157. }
  158. }
  159. }
  160. }
  161. }
  162. on_zhao_bu_tong_finish(widget:zhao_xi_jie_item_data){
  163. let item = this.mWidgetList.get(widget.widget_id)
  164. if(!item){
  165. // tools.showToast("配置找茬错误!")
  166. return
  167. }
  168. item.getComponent(widget_base).showZhaoButongFinishStatus()
  169. }
  170. on_zhao_bu_tong_all_finish(data: zhao_xi_jie_data) {
  171. this.mWidgetList.forEach((n,k)=>{
  172. const widget_data = n.getComponent(widget_base).getData()
  173. if(widget_data.type == config.Widget_Type_List.CLICK_TYPE ||
  174. widget_data.type == config.Widget_Type_List.SLIDE_TYPE ||
  175. widget_data.type == config.Widget_Type_List.DRAG_TYPE) {
  176. n.getComponent(widget_base).hideZhaoButongFinishStatus()
  177. }
  178. })
  179. // let widget_list = gameManager.getWidgetList(this.mData)
  180. // for (let index = 0; index < widget_list.length; index++) {
  181. // const widget_data = widget_list[index];
  182. // if(widget_data.type == config.Widget_Type_List.CLICK_TYPE ||
  183. // widget_data.type == config.Widget_Type_List.SLIDE_TYPE ||
  184. // widget_data.type == config.Widget_Type_List.DRAG_TYPE) {
  185. // let item = this.mWidgetList.get(widget_data.att.id)
  186. // item.getComponent(widget_base).hideZhaoButongFinishStatus()
  187. // }
  188. // }
  189. }
  190. public initViewAtt(){
  191. if(this.mData.is_full_screen){
  192. }else{
  193. this.initPos()
  194. this.initSize()
  195. }
  196. this.initBG()
  197. this.initWidgetList()
  198. }
  199. public initWidgetList(){
  200. let widget_list = gameManager.getWidgetList(this.mData).sort((a,b)=>{
  201. return a.att.z - b.att.z;
  202. })
  203. let is_dai_dao_ju_drag = (id:number)=>{
  204. let task:guo_ju_qing_dai_dao_ju =gameManager.Singleton.mSceneManager.getSceneTask()._guo_ju_qing_dai_dao_ju;
  205. if(task==null){
  206. return false;
  207. }
  208. for (let index = 0; index < task.widget_list.length; index++) {
  209. const element = task.widget_list[index];
  210. if(id==element.widget_id){
  211. return true
  212. }
  213. }
  214. return false;
  215. }
  216. for (let index = 0; index < widget_list.length; index++) {
  217. const widget_data = widget_list[index];
  218. let item:Node = null;
  219. switch (widget_data.type) {
  220. case config.Widget_Type_List.CLICK_TYPE:
  221. item = instantiate(this.widget_click_prefab)
  222. break;
  223. case config.Widget_Type_List.SLIDE_TYPE:
  224. item = instantiate(this.widget_slide_prefab)
  225. break;
  226. case config.Widget_Type_List.DRAG_TYPE:
  227. if(is_dai_dao_ju_drag(widget_data.att.id)){
  228. }else{
  229. item = instantiate(this.widget_drag_prefab)
  230. }
  231. break;
  232. case config.Widget_Type_List.CONTAINER_LAYER:
  233. item = instantiate(this.widget_container_prefab)
  234. break;
  235. case config.Widget_Type_List.IMG_TYPE:
  236. item = instantiate(this.widget_img_prefab)
  237. break;
  238. case config.Widget_Type_List.ACTION_TYPE:
  239. item = instantiate(this.widget_animation_prefab)
  240. break;
  241. }
  242. if(item){
  243. let com = item.getComponent(widget_base);
  244. if(com===null){
  245. console.log("error--widget_data.type:",widget_data.type)
  246. }else{
  247. item.name =`type${widget_data.type}-id${ widget_data.att.id}`
  248. com.initView(widget_data)
  249. item.parent = this.content;
  250. this.mWidgetList.set(widget_data.att.id,item)
  251. }
  252. //新增加容器控件
  253. if(widget_data.type==config.Widget_Type_List.CONTAINER_LAYER&&com!=null){
  254. this.initContainerWidgetList(com.getIcon(),widget_data.att.container_layer.widget_list)
  255. }
  256. }
  257. }
  258. }
  259. public initContainerWidgetList(container:Node,widget_list:widget_item_data[]){
  260. widget_list.sort((a,b)=>{
  261. return a.att.z - b.att.z;
  262. })
  263. let is_dai_dao_ju_drag = (id:number)=>{
  264. let task:guo_ju_qing_dai_dao_ju =gameManager.Singleton.mSceneManager.getSceneTask()._guo_ju_qing_dai_dao_ju;
  265. if(task==null){
  266. return false;
  267. }
  268. for (let index = 0; index < task.widget_list.length; index++) {
  269. const element = task.widget_list[index];
  270. if(id==element.widget_id){
  271. return true
  272. }
  273. }
  274. return false;
  275. }
  276. for (let index = 0; index < widget_list.length; index++) {
  277. const widget_data = widget_list[index];
  278. let item:Node = null;
  279. switch (widget_data.type) {
  280. case config.Widget_Type_List.CLICK_TYPE:
  281. item = instantiate(this.widget_click_prefab)
  282. break;
  283. case config.Widget_Type_List.SLIDE_TYPE:
  284. item = instantiate(this.widget_slide_prefab)
  285. break;
  286. case config.Widget_Type_List.DRAG_TYPE:
  287. if(is_dai_dao_ju_drag(widget_data.att.id)){
  288. }else{
  289. item = instantiate(this.widget_drag_prefab)
  290. }
  291. break;
  292. // case config.Widget_Type_List.CONTAINER_LAYER:
  293. // item = instantiate(this.widget_container_prefab)
  294. // break;
  295. case config.Widget_Type_List.IMG_TYPE:
  296. item = instantiate(this.widget_img_prefab)
  297. break;
  298. case config.Widget_Type_List.ACTION_TYPE:
  299. item = instantiate(this.widget_animation_prefab)
  300. break;
  301. }
  302. if(item){
  303. let com = item.getComponent(widget_base);
  304. if(com===null){
  305. console.log("error--widget_data.type:",widget_data.type)
  306. }else{
  307. item.name =`type${widget_data.type}-id${ widget_data.att.id}`
  308. com.initView(widget_data)
  309. item.parent =container;
  310. this.mWidgetList.set(widget_data.att.id,item)
  311. }
  312. }
  313. }
  314. }
  315. public getBG(){
  316. return this.content;
  317. }
  318. public initBG(){
  319. if(this.mSceneAtt.src.length>0){
  320. tools.loadUrl(this.mSceneAtt.src,this.content.getComponent(Sprite))
  321. }
  322. }
  323. public initSize(){
  324. this.content.getComponent(UITransform).contentSize = new Size(this.mSceneAtt.width,this.mSceneAtt.height)
  325. }
  326. public initPos(){
  327. this.content.position = new Vec3(this.mSceneAtt.x,this.mSceneAtt.y)
  328. }
  329. runAnimation(){
  330. if(this.bindTarget!=null){
  331. Tween.stopAllByTarget(this.bindTarget)
  332. this.bindTarget = null;
  333. }
  334. // Tween.stopAll()
  335. let getFrameData = (index)=>{
  336. let cur_frame:ani_frame =this.mCurRunAnimation[index];
  337. let up_frame:ani_frame = index>0?this.mCurRunAnimation[index-1]:this.mCurRunAnimation[0];
  338. return {"cur_frame":cur_frame,"up_frame":up_frame}
  339. }
  340. let cur_index = 0;
  341. let data = getFrameData(cur_index);
  342. let call_back = ()=>{
  343. cur_index++;
  344. if(cur_index>=this.mCurRunAnimation.length){
  345. if(this.mCurAnimation.isLoop){
  346. cur_index = 0;
  347. }else{
  348. this.mPlayStatus = false;
  349. return;
  350. }
  351. }
  352. let data = getFrameData(cur_index);
  353. this.runFrame(data.cur_frame,data.up_frame,call_back)
  354. }
  355. this.runFrame(data.cur_frame,data.up_frame,call_back)
  356. }
  357. runFrame(frame:ani_frame,up_frame:ani_frame,call){
  358. if(!frame || !up_frame) { return }
  359. let self = this;
  360. let tweenDuration: number = frame.next_time;
  361. // this.content.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(frame.url)
  362. let n_pos:Vec3 = new Vec3(frame.pos_x,frame.pos_y)
  363. let _color = new Color()
  364. _color = _color.fromHEX(frame.color)
  365. this.bindTarget = new BindTarget();
  366. let rotation = up_frame.rotation == undefined?0:up_frame.rotation
  367. this.bindTarget.rotation = rotation
  368. this.bindTarget.color = new Color().fromHEX(up_frame.color)
  369. this.bindTarget.opacity = up_frame.transparent;
  370. this.bindTarget.size = new Size(up_frame.size_width,up_frame.size_height)
  371. this.bindTarget.pos = new Vec3(up_frame.pos_x,up_frame.pos_y)
  372. this.bindTarget.anchorPointX = up_frame.anchor_point_x==undefined?0.5:up_frame.anchor_point_x
  373. this.bindTarget.anchorPointY = up_frame.anchor_point_y==undefined?0.5:up_frame.anchor_point_y
  374. let color_opactiy_tw_size = tween(this.bindTarget)
  375. .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 }, {
  376. onUpdate(tar:BindTarget){
  377. self.content.getComponent(Sprite).color = tar.color;
  378. self.content.getComponent(UIOpacity).opacity = tar.opacity;
  379. // self.content.getComponent(UITransform).setContentSize(tar.size)
  380. self.content.position = tar.pos
  381. self.content.angle = tar.rotation;
  382. self.content.getComponent(UITransform).setAnchorPoint(tar.anchorPointX, tar.anchorPointY)
  383. }
  384. }).call(()=>{
  385. call()
  386. });
  387. color_opactiy_tw_size.start();
  388. }
  389. public startScrollTouch(){
  390. if(this.maskView!=null){
  391. this.scroll_view.getComponent(ScrollView).horizontal = true
  392. this.scroll_view.getComponent(ScrollView).vertical = true
  393. }
  394. }
  395. public stopScrollTouch(){
  396. if(this.maskView!=null){
  397. this.scroll_view.getComponent(ScrollView).horizontal = false
  398. this.scroll_view.getComponent(ScrollView).vertical = false
  399. }
  400. }
  401. private getAniById(id:number){
  402. if(this.mAnimationList.length>0){
  403. for (let index = 0; index < this.mAnimationList.length; index++) {
  404. const element = this.mAnimationList[index];
  405. if(id==element.ani_id){
  406. return element;
  407. }
  408. }
  409. }
  410. return null;
  411. }
  412. }