tools.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import { _decorator, assetManager, Color, Component, ImageAsset, instantiate, Node, Prefab, resources, Size, Sprite, SpriteFrame, sys, Texture2D, Vec2 } from 'cc';
  2. import { main } from './main';
  3. import { toast } from './toast';
  4. import { add_scene } from './edit/add_scene';
  5. import { dialog_view } from './dialog_view';
  6. import { scene_select_list } from './edit/scene_select_list';
  7. import { sceme_type_select_item } from './sceme_type_select_item';
  8. import { slide_dir_select } from './edit/slide_dir_select';
  9. import { click_type_select } from './click_type_select';
  10. import { add_event } from './edit/event/add_event';
  11. import { add_task } from './edit/task/add_task';
  12. import { add_animation } from './edit/animation/add_animation';
  13. import { att_ani_data, event_item, widget_item_data } from '../data/data';
  14. import { select_widget_list } from './edit/event/select_widget_list';
  15. import { select_animation } from './edit/animation/select_animation';
  16. import { show_widget_list } from './edit/show_widget_list';
  17. import { select_ui_top_type } from './edit/uiWidget/select_ui_top_type';
  18. import { show_select_child_scene } from './edit/uiWidget/show_select_child_scene';
  19. import { show_select_evele_list } from './edit/show_select_evele_list';
  20. import { select_collect_event } from './edit/event/select_collect_event';
  21. import { select_res_list } from './select_res_list';
  22. import { select_task_zhao_xi_jie } from './edit/task/select_task_zhao_xi_jie';
  23. import { show_select_z_index } from './edit/attributes/show_select_z_index';
  24. import { game_run } from './run/game_run';
  25. import { config } from './config';
  26. import { edit_event } from './edit/event/edit_event';
  27. import { select_sound_list } from './edit/select_sound_list';
  28. import { select_task_da_guai } from './edit/task/select_task_da_guai';
  29. import { edit_event_view } from './edit/event/edit_event_view';
  30. import { tip_type_select } from './edit/tip_type_select';
  31. import { show_copy_scene } from './edit/show_copy_scene';
  32. import { show_select_more_scene_page } from './edit/show_select_more_scene_page';
  33. import { select_task_dai_dao_ju } from './edit/task/select_task_dai_dao_ju';
  34. import { set_color } from './edit/set_color';
  35. import { show_setup_res_anchor } from './edit/show_setup_res_anchor';
  36. import { dialog_input } from './dialog_input';
  37. import { control } from './edit/control';
  38. import { http } from './http';
  39. export class tools {
  40. public static show_dialog(title="是否确定?",sure_callback,cancel_callback=null){
  41. resources.load("prefab/dialog_view",Prefab,(err, prefab)=>{
  42. let node = instantiate(prefab);
  43. node.parent = main.g_canvas.node;
  44. node.getComponent(dialog_view).show(title,sure_callback,cancel_callback)
  45. })
  46. }
  47. public static show_dialog_input(content:string,sure_callback,cancel_callback=null){
  48. resources.load("prefab/dialog_input",Prefab,(err, prefab)=>{
  49. let node = instantiate(prefab);
  50. node.parent = main.g_canvas.node;
  51. node.getComponent(dialog_input).show(content,sure_callback,cancel_callback)
  52. })
  53. }
  54. public static show_loading(call=null){
  55. resources.load("prefab/loading",Prefab,(err, prefab)=>{
  56. if(!main.loading_view){
  57. let node = instantiate(prefab);
  58. node.name = "loading"
  59. node.parent = main.g_canvas.node;
  60. main.loading_view = node;
  61. if(call!=null){
  62. call()
  63. }
  64. }
  65. })
  66. }
  67. public static hide_loading(){
  68. if(main.loading_view!=null){
  69. main.loading_view.removeFromParent()
  70. main.loading_view =null;
  71. }
  72. }
  73. public static showToast(text:string){
  74. resources.load("prefab/toast",Prefab,(err, prefab)=>{
  75. let node = instantiate(prefab);
  76. node.name = "toast"
  77. node.parent = main.g_canvas.node;
  78. node.getComponent(toast).show(text)
  79. })
  80. }
  81. public static show_add_scene(call){
  82. resources.load("prefab/add_scene",Prefab,(err, prefab)=>{
  83. let node = instantiate(prefab);
  84. node.parent = main.g_canvas.node;
  85. node.getComponent(add_scene).initView(call)
  86. })
  87. }
  88. public static add_scene_page(call){
  89. resources.load("prefab/sceme_type_select_item",Prefab,(err, prefab)=>{
  90. let node = instantiate(prefab);
  91. node.parent = main.g_canvas.node;
  92. node.getComponent(sceme_type_select_item).initView(call)
  93. })
  94. }
  95. public static show_copy_scene(sceneName:string,call) {
  96. resources.load("prefab/show_copy_scene", Prefab, (err, prefab)=> {
  97. let node = instantiate(prefab);
  98. node.parent = main.g_canvas.node;
  99. node.getComponent(show_copy_scene).initView(sceneName,call)
  100. })
  101. }
  102. public static show_slide_dir_select(call){
  103. resources.load("prefab/slide_dir_select",Prefab,(err, prefab)=>{
  104. let node = instantiate(prefab);
  105. node.parent = main.g_canvas.node;
  106. node.getComponent(slide_dir_select).show(call)
  107. })
  108. }
  109. public static show_tip_type_select(call){
  110. resources.load("prefab/tip_type_select",Prefab,(err, prefab)=>{
  111. let node = instantiate(prefab);
  112. node.parent = main.g_canvas.node;
  113. node.getComponent(tip_type_select).show(call)
  114. })
  115. }
  116. public static show_click_type_select(call){
  117. resources.load("prefab/click_type_select",Prefab,(err, prefab)=>{
  118. let node = instantiate(prefab);
  119. node.parent = main.g_canvas.node;
  120. node.getComponent(click_type_select).show(call)
  121. })
  122. }
  123. public static show_add_event(call){
  124. resources.load("prefab/add_event",Prefab,(err, prefab)=>{
  125. let node = instantiate(prefab);
  126. node.parent = main.g_canvas.node;
  127. node.getComponent(add_event).show(call)
  128. })
  129. }
  130. public static show_add_task(call){
  131. resources.load("prefab/add_task",Prefab,(err, prefab)=>{
  132. let node = instantiate(prefab);
  133. node.parent = main.g_canvas.node;
  134. node.getComponent(add_task).show(call)
  135. })
  136. }
  137. public static show_add_animation(list,call){
  138. resources.load("prefab/add_animation",Prefab,(err, prefab)=>{
  139. let node = instantiate(prefab);
  140. node.parent = main.g_canvas.node;
  141. node.getComponent(add_animation).show(list,call)
  142. })
  143. }
  144. public static show_select_widget_list(list:widget_item_data[],call,id:number=-1){
  145. resources.load("prefab/select_widget_list",Prefab,(err, prefab)=>{
  146. let node = instantiate(prefab);
  147. node.parent = main.g_canvas.node;
  148. node.getComponent(select_widget_list).show(list,call,id)
  149. })
  150. }
  151. public static show_widget_list(list){
  152. resources.load("prefab/show_widget_list",Prefab,(err, prefab)=>{
  153. let node = instantiate(prefab);
  154. node.parent = main.g_canvas.node;
  155. node.getComponent(show_widget_list).show(list)
  156. })
  157. }
  158. public static show_select_animation_list(list:att_ani_data[],call,id:number=-1){
  159. resources.load("prefab/select_animation",Prefab,(err, prefab)=>{
  160. let node = instantiate(prefab);
  161. node.parent = main.g_canvas.node;
  162. node.getComponent(select_animation).show(list,call,id)
  163. })
  164. }
  165. public static show_select_animation_delete_list(list:att_ani_data[],call,id:number=-1) {
  166. resources.load("prefab/select_animation",Prefab,(err, prefab)=>{
  167. let node = instantiate(prefab);
  168. node.parent = main.g_canvas.node;
  169. node.getComponent(select_animation).showWithDelete(list,call,id)
  170. })
  171. }
  172. public static show_select_ui_top_type(call){
  173. resources.load("prefab/ui/select_ui_top_type",Prefab,(err, prefab)=>{
  174. let node:Node = instantiate(prefab);
  175. node.parent = main.g_canvas.node;
  176. node.getComponent(select_ui_top_type).show(call)
  177. })
  178. }
  179. public static show_select_child_scene(list,call){
  180. resources.load("prefab/ui/show_select_child_scene",Prefab,(err, prefab)=>{
  181. let node:Node = instantiate(prefab);
  182. node.parent = main.g_canvas.node;
  183. node.getComponent(show_select_child_scene).show(list,call)
  184. })
  185. }
  186. public static show_select_evele_list(list,call){
  187. if(list.length == 0) {
  188. return tools.showToast('任务事件列表是空的')
  189. }
  190. resources.load("prefab/show_select_evele_list",Prefab,(err, prefab)=>{
  191. let node:Node = instantiate(prefab);
  192. node.parent = main.g_canvas.node;
  193. node.getComponent(show_select_evele_list).show(list,call)
  194. })
  195. }
  196. public static show_setup_res_anchor(img_sf:SpriteFrame,img_size:Size,img_anchor:Vec2,callback:Function) {
  197. resources.load("prefab/show_setup_res_anchor",Prefab,(err, prefab)=>{
  198. let node:Node = instantiate(prefab);
  199. node.parent = main.g_canvas.node;
  200. node.getComponent(show_setup_res_anchor).show(img_sf,img_size,img_anchor,callback)
  201. })
  202. }
  203. public static select_collect_event(list,call,event_id,event_list_id){
  204. resources.load("prefab/ui/select_collect_event",Prefab,(err, prefab)=>{
  205. let node:Node = instantiate(prefab);
  206. node.parent = main.g_canvas.node;
  207. node.getComponent(select_collect_event).show(list,call,event_id,event_list_id)
  208. })
  209. }
  210. public static select_res_list(call){
  211. resources.load("prefab/ui/select_res_list",Prefab,(err, prefab)=>{
  212. let node:Node = instantiate(prefab);
  213. node.parent = main.g_canvas.node;
  214. node.getComponent(select_res_list).initView(call)
  215. })
  216. }
  217. public static select_sound_list(call){
  218. resources.load("prefab/ui/select_sound_list",Prefab,(err, prefab)=>{
  219. let node:Node = instantiate(prefab);
  220. node.parent = main.g_canvas.node;
  221. node.getComponent(select_sound_list).initView(call)
  222. })
  223. }
  224. public static select_task_zhao_xi_jie(data){
  225. resources.load("prefab/task/select_task_zhao_xi_jie",Prefab,(err, prefab)=>{
  226. let node:Node = instantiate(prefab);
  227. node.parent = main.g_canvas.node;
  228. node.getComponent(select_task_zhao_xi_jie).show(data)
  229. })
  230. }
  231. public static select_task_da_guai(data){
  232. resources.load("prefab/task/select_task_da_guai",Prefab,(err, prefab)=>{
  233. let node:Node = instantiate(prefab);
  234. node.parent = main.g_canvas.node;
  235. node.getComponent(select_task_da_guai).show(data)
  236. })
  237. }
  238. public static select_task_dai_dao_ju(data){
  239. resources.load("prefab/task/select_task_dai_dao_ju",Prefab,(err, prefab)=>{
  240. let node:Node = instantiate(prefab);
  241. node.parent = main.g_canvas.node;
  242. node.getComponent(select_task_dai_dao_ju).show(data)
  243. })
  244. }
  245. public static show_select_z_index(call){
  246. resources.load("prefab/show_select_z_index",Prefab,(err, prefab)=>{
  247. let node:Node = instantiate(prefab);
  248. node.parent = main.g_canvas.node;
  249. node.getComponent(show_select_z_index).show(call)
  250. })
  251. }
  252. public static show_edit_event_view(){
  253. resources.load("prefab/ui/edit_event_view",Prefab,(err, prefab)=>{
  254. let node:Node = instantiate(prefab);
  255. node.parent = main.g_canvas.node;
  256. node.getComponent(edit_event_view).show()
  257. })
  258. }
  259. public static show_select_dir(call){
  260. resources.load("prefab/show_select_z_index",Prefab,(err, prefab)=>{
  261. let node:Node = instantiate(prefab);
  262. node.parent = main.g_canvas.node;
  263. node.getComponent(show_select_z_index).showDir(call)
  264. })
  265. }
  266. public static show_select_more_scene_page(data,cur_index,call) {
  267. resources.load("prefab/show_select_more_scene_page",Prefab,(err, prefab)=>{
  268. let node:Node = instantiate(prefab);
  269. node.parent = main.g_canvas.node;
  270. node.getComponent(show_select_more_scene_page).show(data,cur_index,call)
  271. })
  272. }
  273. public static loadUrl(url:string,spr:Sprite,call=null){
  274. if(url.length<=0){
  275. return null;
  276. }
  277. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  278. if (!err && imageAsset2) {
  279. const texture = new Texture2D();
  280. texture.image = imageAsset2;
  281. let spFrame2 = new SpriteFrame();
  282. spFrame2.texture = texture;
  283. if(spr!=null) {
  284. spr.spriteFrame = spFrame2;
  285. }
  286. call && call(spFrame2)
  287. } else {
  288. call && call(null)
  289. }
  290. });
  291. }
  292. public static loadSceneImg(url:string,call){
  293. if(url.length<=0){
  294. return null;
  295. }
  296. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  297. if (!err && imageAsset2) {
  298. const texture = new Texture2D();
  299. texture.image = imageAsset2;
  300. let spFrame2 = new SpriteFrame();
  301. spFrame2.texture = texture;
  302. spFrame2.addRef()
  303. call({"url":url,"sf":spFrame2})
  304. }
  305. });
  306. }
  307. public static loadSceneMp3(url:string,call){
  308. if(url.length<=0){
  309. return null;
  310. }
  311. assetManager.loadRemote(url, (err: any, clip: any)=> {
  312. clip.addRef()
  313. if (!err && clip) {
  314. call({"url":url,"clip":clip})
  315. }
  316. });
  317. }
  318. public static game_run(callback=null){
  319. resources.load("prefab/run/game_run",Prefab,(err, prefab)=>{
  320. let node:Node = instantiate(prefab);
  321. node.parent = main.g_canvas.node;
  322. node.getComponent(game_run).run(callback)
  323. })
  324. }
  325. public static game_run_all(){
  326. resources.load("prefab/run/game_run",Prefab,(err, prefab)=>{
  327. let node:Node = instantiate(prefab);
  328. node.parent = main.g_canvas.node;
  329. node.getComponent(game_run).runAll()
  330. })
  331. }
  332. public static isWidgetType(type:number){
  333. return type!=config.Widget_Type_List.TEXT_SOUND
  334. }
  335. public static show_edit_event(type:number,data:event_item,delete_call){
  336. resources.load("prefab/edit_event",Prefab,(err, prefab)=>{
  337. let node:Node = instantiate(prefab);
  338. node.parent = main.g_canvas.node;
  339. node.getComponent(edit_event).show(type,data,delete_call)
  340. })
  341. }
  342. // 去除左边和右边空格
  343. public static trimLeftAndRightblank(str: string):string {
  344. const reg = /^\s+|\s+$/g;
  345. return str.replace(reg, '')
  346. }
  347. public static show_set_color(color:Color,call){
  348. resources.load("prefab/set_color",Prefab,(err, prefab)=>{
  349. let node:Node = instantiate(prefab);
  350. node.parent = main.g_canvas.node;
  351. node.getComponent(set_color).show(color,call)
  352. })
  353. }
  354. //请求保存编辑场景
  355. public static requestSaveEditScene() {
  356. tools.show_loading(()=>{
  357. // let data = new FormData()
  358. let content = control.Singleton.get_bag_data().content
  359. let zujian_id = config.last_id;
  360. let id = control.Singleton.get_bag_data().id;
  361. // data.append("content",JSON.stringify(content))
  362. // data.append("zujian_id",zujian_id+"")
  363. // data.append("id",id+"")
  364. console.log("btn_save",content)
  365. let request_data = {"content":JSON.stringify(content),"zujian_id":zujian_id,"id":id}
  366. http.post("/tool/mysnote/save", request_data, (err,data)=>{
  367. if(!err){
  368. tools.showToast("保存成功!")
  369. }
  370. tools.hide_loading()
  371. })
  372. })
  373. }
  374. //请求获取服务器动画列表
  375. public static requestGetAnimationList(call) {
  376. http.get('/tool/mysnote/get_animation',(err,data)=>{
  377. // console.log('err=',err,'data=',data)
  378. if(!err) {
  379. let _data = JSON.parse(data);
  380. if(_data.code==config.MSG_CODE.SUCCESS) {
  381. let content_list = JSON.parse(_data.content.content)
  382. let list:att_ani_data[] = []
  383. for (let index = 0; index < content_list.length; index++) {
  384. const element = content_list[index];
  385. list.push(element)
  386. }
  387. call(list)
  388. } else {
  389. tools.showToast('获取动画列表失败')
  390. }
  391. }
  392. })
  393. }
  394. //添加动画到动画列表
  395. public static requestAddAnimationToAnimationList(ani_data:att_ani_data) {
  396. let string = ani_data.ani_name
  397. tools.show_dialog_input(string, (content)=>{
  398. tools.requestGetAnimationList((ani_list)=>{
  399. let copy_ani_data = JSON.parse(JSON.stringify(ani_data)) //深拷贝
  400. copy_ani_data.ani_name = content
  401. ani_list.push(copy_ani_data)
  402. tools.requestSyncAnimationList(ani_list)
  403. })
  404. })
  405. }
  406. //同步服务器动画列表
  407. public static requestSyncAnimationList(list:att_ani_data[]) {
  408. let request_data = {"content": JSON.stringify(list)}
  409. // console.log('request_data=',request_data)
  410. http.post('/tool/mysnote/snyc_animation',request_data, (err,data)=>{
  411. // console.log('同步 err=',err,'data',data)
  412. if(!err){
  413. tools.showToast("动画列表同步成功!")
  414. }
  415. })
  416. }
  417. }