scene_page.ts 15 KB

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