main.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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, long_story_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. import { long_story_list } from './edit/long_story_list';
  15. import { select_long_story_level } from './edit/select_long_story_level';
  16. const { ccclass, property } = _decorator;
  17. @ccclass('main')
  18. export class main extends Component {
  19. public static g_canvas:Canvas = null;
  20. public static loading_view:Node = null;
  21. public static g_audioSource:AudioSource = null;
  22. public static cur_play_audio:string = "";
  23. public static Singleton:main = null;
  24. @property(Node) select_long_story_level_node:Node = null;
  25. @property(Node) long_story_list:Node = null;
  26. @property(Node) level_list:Node = null;
  27. @property(Node) btn_back_level_list:Node = null;
  28. @property(control) control_view:control = null;
  29. @property(edit_scene) edit_scene_view:edit_scene = null;
  30. @property(Node) action_node:Node = null;
  31. @property(Node) res_action_node:Node = null;
  32. @property(Node) attributes_node:Node = null;
  33. protected start(): void {
  34. this.initView()
  35. main.Singleton = this;
  36. main.g_canvas = this.node.getComponent(Canvas);
  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 initView() {
  53. this.select_long_story_level_node.active = true
  54. this.select_long_story_level_node.getComponent(select_long_story_level).initView(()=>{
  55. // 显示-关卡
  56. this.select_long_story_level_node.active = false
  57. this.level_list.active = true
  58. this.level_list.getComponent(level_list).init(null,()=>{
  59. this.select_long_story_level_node.active = true
  60. this.level_list.active = false
  61. },this.onSelectLevel.bind(this))
  62. },()=>{
  63. this.select_long_story_level_node.active = false
  64. this.long_story_list.active = true
  65. this.long_story_list.getComponent(long_story_list).init(()=>{
  66. // 显示-长篇列表
  67. this.select_long_story_level_node.active = true
  68. this.long_story_list.active = false
  69. },(d:long_story_list_item_data)=>{
  70. // 显示-关卡
  71. this.long_story_list.active = false
  72. this.level_list.active = true
  73. this.level_list.getComponent(level_list).init(d,()=>{
  74. this.long_story_list.active = true
  75. this.level_list.active = false
  76. },this.onSelectLevel.bind(this))
  77. })
  78. })
  79. this.btn_back_level_list.on(Node.EventType.TOUCH_END,()=>{
  80. this.onBackLevelList()
  81. })
  82. }
  83. onBackLevelList(){
  84. this.level_list.active = true;
  85. ClientEvent.dispatchEvent(config.Event.BackLevelList)
  86. }
  87. public static getAudioSource(){
  88. return main.g_audioSource;
  89. }
  90. public getEditSceneView():edit_scene {
  91. return this.edit_scene_view
  92. }
  93. onSelectLevel(data:level_list_item_data){
  94. this.startEdit(data.id)
  95. }
  96. add_res(data){
  97. let self = this;
  98. tools.show_loading(()=>{
  99. self.control_view.loading_all_resources(data,()=>{
  100. self.level_list.active = false;
  101. tools.hide_loading()
  102. self.control_view.init(self)
  103. self.edit_scene_view.init(self)
  104. })
  105. })
  106. }
  107. startEdit(id:number){
  108. http.post("/tool/mysnote/level_info", {"id":`${id}`},(err,data)=>{
  109. // console.log('level_info data=',data)
  110. if(!err){
  111. let _data = JSON.parse(data);
  112. this.add_res(_data)
  113. }
  114. })
  115. }
  116. onDragWidget(node:Node){
  117. if(this.action_node.children.length<=0&&!this.action_node.active){
  118. this.action_node.active = true;
  119. let clone_node = instantiate(node)
  120. clone_node.parent = this.action_node;
  121. clone_node.getComponent(widget_item).setData(node.getComponent(widget_item).getData())
  122. let n_p = node.parent.getComponent(UITransform).convertToWorldSpaceAR(node.position)
  123. clone_node.position = n_p;
  124. }
  125. }
  126. onDragRes(node:Node){
  127. if(this.res_action_node.children.length<=0&&!this.res_action_node.active){
  128. this.res_action_node.active = true;
  129. let clone_node = instantiate(node)
  130. clone_node.getComponent(base_res).setType(node.getComponent(base_res).getType())
  131. clone_node.getComponent(base_res).setData(node.getComponent(base_res).getData())
  132. clone_node.parent = this.res_action_node;
  133. let n_p = node.parent.getComponent(UITransform).convertToWorldSpaceAR(node.position)
  134. clone_node.position = n_p;
  135. }
  136. }
  137. onMoveWidget(Event:EventTouch){
  138. console.log("onMoveWidget")
  139. if(this.action_node.children.length>0){
  140. this.action_node.children[0].position = new Vec3(Event.getUILocation().x,Event.getUILocation().y)
  141. }
  142. }
  143. onMoveRes(Event:EventTouch){
  144. console.log("onMoveRes")
  145. if(this.res_action_node.children.length>0){
  146. this.res_action_node.children[0].position = new Vec3(Event.getUILocation().x,Event.getUILocation().y)
  147. }
  148. }
  149. onEndRes(Event:EventTouch){
  150. if(this.res_action_node.children.length<=0){
  151. return;
  152. }
  153. let rect = this.attributes_node.getComponent(Attributes).getUrlCom().getRect()
  154. if(rect.contains(Event.getUILocation())){
  155. console.log("onEndRes")
  156. let type = this.res_action_node.children[0].getComponent(base_res).getType()
  157. if(type===config.select_res_btn_type.SOUND_LIST){
  158. tools.showToast("类型不一致!")
  159. }else{
  160. let data = this.res_action_node.children[0].getComponent(img_item).getData()
  161. this.attributes_node.getComponent(Attributes).getUrlCom().update_att(data.name)
  162. let att = this.attributes_node.getComponent(Attributes).get_cur_att_data();
  163. att.src = data.url
  164. att.src_name = data.name
  165. ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,att,config.attributes_list_type.url)
  166. }
  167. }
  168. ClientEvent.dispatchEvent(config.Event.DragResEndOnCheck,Event.getUILocation(),this.res_action_node.children[0])
  169. this.res_action_node.removeAllChildren()
  170. this.res_action_node.active = false;
  171. }
  172. onEndWidget(Event:EventTouch){
  173. let size = new Size(this.edit_scene_view.getComponent(UITransform).contentSize.width*0.5,this.edit_scene_view.getComponent(UITransform).contentSize.height*0.5);
  174. let pos = this.node.getComponent(UITransform).convertToWorldSpaceAR(this.edit_scene_view.node.position);
  175. let rect = new Rect(pos.x-size.width/2,pos.y-size.height/2,size.width,size.height)
  176. if(rect.contains(Event.getUILocation())){
  177. console.log("onEndWidget")
  178. ClientEvent.dispatchEvent(config.Event.DragWidgetEnd)
  179. }
  180. this.action_node.removeAllChildren()
  181. this.action_node.active = false;
  182. }
  183. IsHaveDragActionRes(){
  184. return this.res_action_node.children.length>0
  185. }
  186. public onPushEndWidget(node:Node,container_id:number){
  187. this.onDragWidget(node)
  188. ClientEvent.dispatchEvent(config.Event.DragWidgetEnd,container_id)
  189. this.action_node.removeAllChildren()
  190. this.action_node.active = false;
  191. }
  192. }