main.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { _decorator, AudioSource, Canvas, Component, EventTouch, instantiate, Node, Rect, Size, UITransform, Vec3 } from 'cc';
  2. import { level_list } from './edit/level_list';
  3. import { http } from './http';
  4. import { level_list_item_data } from '../data/data';
  5. import { tools } from './tools';
  6. import { control } from './edit/control';
  7. import { config } from './config';
  8. import { ClientEvent } from './clientEvent';
  9. import { edit_scene } from './edit/edit_scene';
  10. import { Attributes } from './edit/Attributes';
  11. import { base_res } from './edit/base_res';
  12. import { widget_item } from './edit/widget_item';
  13. import { img_item } from './edit/img_item';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('main')
  16. export class main extends Component {
  17. public static g_canvas:Canvas = null;
  18. public static loading_view:Node = null;
  19. public static g_audioSource:AudioSource = null;
  20. public static cur_play_audio:string = "";
  21. public static Singleton:main = null;
  22. @property(Node) level_list:Node = null;
  23. @property(Node) btn_back_level_list:Node = null;
  24. @property(control) control_view:control = null;
  25. @property(edit_scene) edit_scene_view:edit_scene = null;
  26. @property(Node) action_node:Node = null;
  27. @property(Node) res_action_node:Node = null;
  28. @property(Node) attributes_node:Node = null;
  29. protected start(): void {
  30. this.level_list.active = true;
  31. main.Singleton = this;
  32. main.g_canvas = this.node.getComponent(Canvas);
  33. this.level_list.getComponent(level_list).init(this.onSelectLevel.bind(this))
  34. this.btn_back_level_list.on(Node.EventType.TOUCH_END,()=>{
  35. this.onBackLevelList()
  36. })
  37. main.g_audioSource = this.node.addComponent(AudioSource)
  38. ClientEvent.on(config.Event.DragWidget,this.onDragWidget,this)
  39. ClientEvent.on(config.Event.DragRes,this.onDragRes,this)
  40. this.action_node.on(Node.EventType.MOUSE_MOVE,this.onMoveWidget,this)
  41. this.action_node.on(Node.EventType.TOUCH_END,this.onEndWidget,this)
  42. this.action_node.on(Node.EventType.MOUSE_UP,this.onEndWidget,this)
  43. this.action_node.on(Node.EventType.MOUSE_LEAVE,this.onEndWidget,this)
  44. this.action_node.on(Node.EventType.TOUCH_CANCEL,this.onEndWidget,this)
  45. this.res_action_node.on(Node.EventType.MOUSE_MOVE,this.onMoveRes,this)
  46. // this.res_action_node.on(Node.EventType.TOUCH_END,this.onEndRes,this)
  47. this.res_action_node.on(Node.EventType.MOUSE_UP,this.onEndRes,this)
  48. this.res_action_node.on(Node.EventType.MOUSE_LEAVE,this.onEndRes,this)
  49. this.res_action_node.on(Node.EventType.TOUCH_CANCEL,this.onEndRes,this)
  50. this.attributes_node.getComponent(Attributes).initView(this)
  51. }
  52. public static getAudioSource(){
  53. return main.g_audioSource;
  54. }
  55. onSelectLevel(data:level_list_item_data){
  56. this.startEdit(data.id)
  57. }
  58. add_res(data){
  59. let self = this;
  60. tools.show_loading(()=>{
  61. self.control_view.loading_all_resources(data,()=>{
  62. self.level_list.active = false;
  63. tools.hide_loading()
  64. self.control_view.init(self)
  65. self.edit_scene_view.init(self)
  66. })
  67. })
  68. }
  69. startEdit(id:number){
  70. http.post("/tool/mysnote/level_info", {"id":`${id}`},(err,data)=>{
  71. if(!err){
  72. let _data = JSON.parse(data);
  73. this.add_res(_data)
  74. }
  75. })
  76. }
  77. onBackLevelList(){
  78. this.level_list.active = true;
  79. }
  80. onDragWidget(node:Node){
  81. if(this.action_node.children.length<=0&&!this.action_node.active){
  82. this.action_node.active = true;
  83. let clone_node = instantiate(node)
  84. clone_node.parent = this.action_node;
  85. clone_node.getComponent(widget_item).setData(node.getComponent(widget_item).getData())
  86. let n_p = node.parent.getComponent(UITransform).convertToWorldSpaceAR(node.position)
  87. clone_node.position = n_p;
  88. }
  89. }
  90. onDragRes(node:Node){
  91. if(this.res_action_node.children.length<=0&&!this.res_action_node.active){
  92. this.res_action_node.active = true;
  93. let clone_node = instantiate(node)
  94. clone_node.getComponent(base_res).setType(node.getComponent(base_res).getType())
  95. clone_node.getComponent(base_res).setData(node.getComponent(base_res).getData())
  96. clone_node.parent = this.res_action_node;
  97. let n_p = node.parent.getComponent(UITransform).convertToWorldSpaceAR(node.position)
  98. clone_node.position = n_p;
  99. }
  100. }
  101. onMoveWidget(Event:EventTouch){
  102. console.log("onMoveWidget")
  103. if(this.action_node.children.length>0){
  104. this.action_node.children[0].position = new Vec3(Event.getUILocation().x,Event.getUILocation().y)
  105. }
  106. }
  107. onMoveRes(Event:EventTouch){
  108. console.log("onMoveRes")
  109. if(this.res_action_node.children.length>0){
  110. this.res_action_node.children[0].position = new Vec3(Event.getUILocation().x,Event.getUILocation().y)
  111. }
  112. }
  113. onEndRes(Event:EventTouch){
  114. if(this.res_action_node.children.length<=0){
  115. return;
  116. }
  117. let rect = this.attributes_node.getComponent(Attributes).getUrlCom().getRect()
  118. if(rect.contains(Event.getUILocation())){
  119. console.log("onEndRes")
  120. let type = this.res_action_node.children[0].getComponent(base_res).getType()
  121. if(type===config.select_res_btn_type.SOUND_LIST){
  122. tools.showToast("类型不一致!")
  123. }else{
  124. let data = this.res_action_node.children[0].getComponent(img_item).getData()
  125. this.attributes_node.getComponent(Attributes).getUrlCom().update_att(data.name)
  126. let att = this.attributes_node.getComponent(Attributes).get_cur_att_data();
  127. att.src = data.url
  128. att.src_name = data.name
  129. ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,att,config.attributes_list_type.url)
  130. }
  131. }
  132. ClientEvent.dispatchEvent(config.Event.DragResEndOnCheck,Event.getUILocation(),this.res_action_node.children[0])
  133. this.res_action_node.removeAllChildren()
  134. this.res_action_node.active = false;
  135. }
  136. onEndWidget(Event:EventTouch){
  137. let size = new Size(this.edit_scene_view.getComponent(UITransform).contentSize.width*0.5,this.edit_scene_view.getComponent(UITransform).contentSize.height*0.5);
  138. let pos = this.node.getComponent(UITransform).convertToWorldSpaceAR(this.edit_scene_view.node.position);
  139. let rect = new Rect(pos.x-size.width/2,pos.y-size.height/2,size.width,size.height)
  140. if(rect.contains(Event.getUILocation())){
  141. console.log("onEndWidget")
  142. ClientEvent.dispatchEvent(config.Event.DragWidgetEnd)
  143. }
  144. this.action_node.removeAllChildren()
  145. this.action_node.active = false;
  146. }
  147. IsHaveDragActionRes(){
  148. return this.res_action_node.children.length>0
  149. }
  150. }