main.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. public getEditSceneView():edit_scene {
  56. return this.edit_scene_view
  57. }
  58. onSelectLevel(data:level_list_item_data){
  59. this.startEdit(data.id)
  60. }
  61. add_res(data){
  62. let self = this;
  63. tools.show_loading(()=>{
  64. self.control_view.loading_all_resources(data,()=>{
  65. self.level_list.active = false;
  66. tools.hide_loading()
  67. self.control_view.init(self)
  68. self.edit_scene_view.init(self)
  69. })
  70. })
  71. }
  72. startEdit(id:number){
  73. http.post("/tool/mysnote/level_info", {"id":`${id}`},(err,data)=>{
  74. if(!err){
  75. let _data = JSON.parse(data);
  76. this.add_res(_data)
  77. }
  78. })
  79. }
  80. onBackLevelList(){
  81. this.level_list.active = true;
  82. ClientEvent.dispatchEvent(config.Event.BackLevelList)
  83. }
  84. onDragWidget(node:Node){
  85. if(this.action_node.children.length<=0&&!this.action_node.active){
  86. this.action_node.active = true;
  87. let clone_node = instantiate(node)
  88. clone_node.parent = this.action_node;
  89. clone_node.getComponent(widget_item).setData(node.getComponent(widget_item).getData())
  90. let n_p = node.parent.getComponent(UITransform).convertToWorldSpaceAR(node.position)
  91. clone_node.position = n_p;
  92. }
  93. }
  94. onDragRes(node:Node){
  95. if(this.res_action_node.children.length<=0&&!this.res_action_node.active){
  96. this.res_action_node.active = true;
  97. let clone_node = instantiate(node)
  98. clone_node.getComponent(base_res).setType(node.getComponent(base_res).getType())
  99. clone_node.getComponent(base_res).setData(node.getComponent(base_res).getData())
  100. clone_node.parent = this.res_action_node;
  101. let n_p = node.parent.getComponent(UITransform).convertToWorldSpaceAR(node.position)
  102. clone_node.position = n_p;
  103. }
  104. }
  105. onMoveWidget(Event:EventTouch){
  106. console.log("onMoveWidget")
  107. if(this.action_node.children.length>0){
  108. this.action_node.children[0].position = new Vec3(Event.getUILocation().x,Event.getUILocation().y)
  109. }
  110. }
  111. onMoveRes(Event:EventTouch){
  112. console.log("onMoveRes")
  113. if(this.res_action_node.children.length>0){
  114. this.res_action_node.children[0].position = new Vec3(Event.getUILocation().x,Event.getUILocation().y)
  115. }
  116. }
  117. onEndRes(Event:EventTouch){
  118. if(this.res_action_node.children.length<=0){
  119. return;
  120. }
  121. let rect = this.attributes_node.getComponent(Attributes).getUrlCom().getRect()
  122. if(rect.contains(Event.getUILocation())){
  123. console.log("onEndRes")
  124. let type = this.res_action_node.children[0].getComponent(base_res).getType()
  125. if(type===config.select_res_btn_type.SOUND_LIST){
  126. tools.showToast("类型不一致!")
  127. }else{
  128. let data = this.res_action_node.children[0].getComponent(img_item).getData()
  129. this.attributes_node.getComponent(Attributes).getUrlCom().update_att(data.name)
  130. let att = this.attributes_node.getComponent(Attributes).get_cur_att_data();
  131. att.src = data.url
  132. att.src_name = data.name
  133. ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,att,config.attributes_list_type.url)
  134. }
  135. }
  136. ClientEvent.dispatchEvent(config.Event.DragResEndOnCheck,Event.getUILocation(),this.res_action_node.children[0])
  137. this.res_action_node.removeAllChildren()
  138. this.res_action_node.active = false;
  139. }
  140. onEndWidget(Event:EventTouch){
  141. let size = new Size(this.edit_scene_view.getComponent(UITransform).contentSize.width*0.5,this.edit_scene_view.getComponent(UITransform).contentSize.height*0.5);
  142. let pos = this.node.getComponent(UITransform).convertToWorldSpaceAR(this.edit_scene_view.node.position);
  143. let rect = new Rect(pos.x-size.width/2,pos.y-size.height/2,size.width,size.height)
  144. if(rect.contains(Event.getUILocation())){
  145. console.log("onEndWidget")
  146. ClientEvent.dispatchEvent(config.Event.DragWidgetEnd)
  147. }
  148. this.action_node.removeAllChildren()
  149. this.action_node.active = false;
  150. }
  151. IsHaveDragActionRes(){
  152. return this.res_action_node.children.length>0
  153. }
  154. }