tools.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import { _decorator, assetManager, Component, ImageAsset, instantiate, Node, Prefab, resources, Sprite, SpriteFrame, Texture2D } 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. export class tools {
  34. public static show_dialog(title="是否确定?",sure_callback,cancel_callback=null){
  35. resources.load("prefab/dialog_view",Prefab,(err, prefab)=>{
  36. let node = instantiate(prefab);
  37. node.parent = main.g_canvas.node;
  38. node.getComponent(dialog_view).show(title,sure_callback,cancel_callback)
  39. })
  40. }
  41. public static show_loading(call=null){
  42. resources.load("prefab/loading",Prefab,(err, prefab)=>{
  43. if(!main.loading_view){
  44. let node = instantiate(prefab);
  45. node.name = "loading"
  46. node.parent = main.g_canvas.node;
  47. main.loading_view = node;
  48. if(call!=null){
  49. call()
  50. }
  51. }
  52. })
  53. }
  54. public static hide_loading(){
  55. if(main.loading_view!=null){
  56. main.loading_view.removeFromParent()
  57. main.loading_view =null;
  58. }
  59. }
  60. public static showToast(text:string){
  61. resources.load("prefab/toast",Prefab,(err, prefab)=>{
  62. let node = instantiate(prefab);
  63. node.name = "toast"
  64. node.parent = main.g_canvas.node;
  65. node.getComponent(toast).show(text)
  66. })
  67. }
  68. public static show_add_scene(call){
  69. resources.load("prefab/add_scene",Prefab,(err, prefab)=>{
  70. let node = instantiate(prefab);
  71. node.parent = main.g_canvas.node;
  72. node.getComponent(add_scene).initView(call)
  73. })
  74. }
  75. public static add_scene_page(call){
  76. resources.load("prefab/sceme_type_select_item",Prefab,(err, prefab)=>{
  77. let node = instantiate(prefab);
  78. node.parent = main.g_canvas.node;
  79. node.getComponent(sceme_type_select_item).initView(call)
  80. })
  81. }
  82. public static show_copy_scene(sceneName:string,call) {
  83. resources.load("prefab/show_copy_scene", Prefab, (err, prefab)=> {
  84. let node = instantiate(prefab);
  85. node.parent = main.g_canvas.node;
  86. node.getComponent(show_copy_scene).initView(sceneName,call)
  87. })
  88. }
  89. public static show_slide_dir_select(call){
  90. resources.load("prefab/slide_dir_select",Prefab,(err, prefab)=>{
  91. let node = instantiate(prefab);
  92. node.parent = main.g_canvas.node;
  93. node.getComponent(slide_dir_select).show(call)
  94. })
  95. }
  96. public static show_tip_type_select(call){
  97. resources.load("prefab/tip_type_select",Prefab,(err, prefab)=>{
  98. let node = instantiate(prefab);
  99. node.parent = main.g_canvas.node;
  100. node.getComponent(tip_type_select).show(call)
  101. })
  102. }
  103. public static show_click_type_select(call){
  104. resources.load("prefab/click_type_select",Prefab,(err, prefab)=>{
  105. let node = instantiate(prefab);
  106. node.parent = main.g_canvas.node;
  107. node.getComponent(click_type_select).show(call)
  108. })
  109. }
  110. public static show_add_event(call){
  111. resources.load("prefab/add_event",Prefab,(err, prefab)=>{
  112. let node = instantiate(prefab);
  113. node.parent = main.g_canvas.node;
  114. node.getComponent(add_event).show(call)
  115. })
  116. }
  117. public static show_add_task(call){
  118. resources.load("prefab/add_task",Prefab,(err, prefab)=>{
  119. let node = instantiate(prefab);
  120. node.parent = main.g_canvas.node;
  121. node.getComponent(add_task).show(call)
  122. })
  123. }
  124. public static show_add_animation(list,call){
  125. resources.load("prefab/add_animation",Prefab,(err, prefab)=>{
  126. let node = instantiate(prefab);
  127. node.parent = main.g_canvas.node;
  128. node.getComponent(add_animation).show(list,call)
  129. })
  130. }
  131. public static show_select_widget_list(list:widget_item_data[],call,id:number=-1){
  132. resources.load("prefab/select_widget_list",Prefab,(err, prefab)=>{
  133. let node = instantiate(prefab);
  134. node.parent = main.g_canvas.node;
  135. node.getComponent(select_widget_list).show(list,call,id)
  136. })
  137. }
  138. public static show_widget_list(list){
  139. resources.load("prefab/show_widget_list",Prefab,(err, prefab)=>{
  140. let node = instantiate(prefab);
  141. node.parent = main.g_canvas.node;
  142. node.getComponent(show_widget_list).show(list)
  143. })
  144. }
  145. public static show_select_animation_list(list:att_ani_data[],call,id:number=-1){
  146. resources.load("prefab/select_animation",Prefab,(err, prefab)=>{
  147. let node = instantiate(prefab);
  148. node.parent = main.g_canvas.node;
  149. node.getComponent(select_animation).show(list,call,id)
  150. })
  151. }
  152. public static show_select_ui_top_type(call){
  153. resources.load("prefab/ui/select_ui_top_type",Prefab,(err, prefab)=>{
  154. let node:Node = instantiate(prefab);
  155. node.parent = main.g_canvas.node;
  156. node.getComponent(select_ui_top_type).show(call)
  157. })
  158. }
  159. public static show_select_child_scene(list,call){
  160. resources.load("prefab/ui/show_select_child_scene",Prefab,(err, prefab)=>{
  161. let node:Node = instantiate(prefab);
  162. node.parent = main.g_canvas.node;
  163. node.getComponent(show_select_child_scene).show(list,call)
  164. })
  165. }
  166. public static show_select_evele_list(list,call){
  167. if(list.length == 0) {
  168. return tools.showToast('任务事件列表是空的')
  169. }
  170. resources.load("prefab/show_select_evele_list",Prefab,(err, prefab)=>{
  171. let node:Node = instantiate(prefab);
  172. node.parent = main.g_canvas.node;
  173. node.getComponent(show_select_evele_list).show(list,call)
  174. })
  175. }
  176. public static select_collect_event(list,call,event_id,event_list_id){
  177. resources.load("prefab/ui/select_collect_event",Prefab,(err, prefab)=>{
  178. let node:Node = instantiate(prefab);
  179. node.parent = main.g_canvas.node;
  180. node.getComponent(select_collect_event).show(list,call,event_id,event_list_id)
  181. })
  182. }
  183. public static select_res_list(call){
  184. resources.load("prefab/ui/select_res_list",Prefab,(err, prefab)=>{
  185. let node:Node = instantiate(prefab);
  186. node.parent = main.g_canvas.node;
  187. node.getComponent(select_res_list).initView(call)
  188. })
  189. }
  190. public static select_sound_list(call){
  191. resources.load("prefab/ui/select_sound_list",Prefab,(err, prefab)=>{
  192. let node:Node = instantiate(prefab);
  193. node.parent = main.g_canvas.node;
  194. node.getComponent(select_sound_list).initView(call)
  195. })
  196. }
  197. public static select_task_zhao_xi_jie(data){
  198. resources.load("prefab/task/select_task_zhao_xi_jie",Prefab,(err, prefab)=>{
  199. let node:Node = instantiate(prefab);
  200. node.parent = main.g_canvas.node;
  201. node.getComponent(select_task_zhao_xi_jie).show(data)
  202. })
  203. }
  204. public static select_task_da_guai(data){
  205. resources.load("prefab/task/select_task_da_guai",Prefab,(err, prefab)=>{
  206. let node:Node = instantiate(prefab);
  207. node.parent = main.g_canvas.node;
  208. node.getComponent(select_task_da_guai).show(data)
  209. })
  210. }
  211. public static show_select_z_index(call){
  212. resources.load("prefab/show_select_z_index",Prefab,(err, prefab)=>{
  213. let node:Node = instantiate(prefab);
  214. node.parent = main.g_canvas.node;
  215. node.getComponent(show_select_z_index).show(call)
  216. })
  217. }
  218. public static show_edit_event_view(){
  219. resources.load("prefab/ui/edit_event_view",Prefab,(err, prefab)=>{
  220. let node:Node = instantiate(prefab);
  221. node.parent = main.g_canvas.node;
  222. node.getComponent(edit_event_view).show()
  223. })
  224. }
  225. public static show_select_dir(call){
  226. resources.load("prefab/show_select_z_index",Prefab,(err, prefab)=>{
  227. let node:Node = instantiate(prefab);
  228. node.parent = main.g_canvas.node;
  229. node.getComponent(show_select_z_index).showDir(call)
  230. })
  231. }
  232. public static show_select_more_scene_page(data,cur_index,call) {
  233. resources.load("prefab/show_select_more_scene_page",Prefab,(err, prefab)=>{
  234. let node:Node = instantiate(prefab);
  235. node.parent = main.g_canvas.node;
  236. node.getComponent(show_select_more_scene_page).show(data,cur_index,call)
  237. })
  238. }
  239. public static loadUrl(url:string,spr:Sprite){
  240. if(url.length<=0){
  241. return null;
  242. }
  243. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  244. if (!err && imageAsset2) {
  245. const texture = new Texture2D();
  246. texture.image = imageAsset2;
  247. let spFrame2 = new SpriteFrame();
  248. spFrame2.texture = texture;
  249. spr.spriteFrame = spFrame2;
  250. }
  251. });
  252. }
  253. public static loadSceneImg(url:string,call){
  254. if(url.length<=0){
  255. return null;
  256. }
  257. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  258. if (!err && imageAsset2) {
  259. const texture = new Texture2D();
  260. texture.image = imageAsset2;
  261. let spFrame2 = new SpriteFrame();
  262. spFrame2.texture = texture;
  263. spFrame2.addRef()
  264. call({"url":url,"sf":spFrame2})
  265. }
  266. });
  267. }
  268. public static loadSceneMp3(url:string,call){
  269. if(url.length<=0){
  270. return null;
  271. }
  272. assetManager.loadRemote(url, (err: any, clip: any)=> {
  273. clip.addRef()
  274. if (!err && clip) {
  275. call({"url":url,"clip":clip})
  276. }
  277. });
  278. }
  279. public static game_run(){
  280. resources.load("prefab/run/game_run",Prefab,(err, prefab)=>{
  281. let node:Node = instantiate(prefab);
  282. node.parent = main.g_canvas.node;
  283. node.getComponent(game_run).run()
  284. })
  285. }
  286. public static game_run_all(){
  287. resources.load("prefab/run/game_run",Prefab,(err, prefab)=>{
  288. let node:Node = instantiate(prefab);
  289. node.parent = main.g_canvas.node;
  290. node.getComponent(game_run).runAll()
  291. })
  292. }
  293. public static isWidgetType(type:number){
  294. return type!=config.Widget_Type_List.TEXT_SOUND
  295. }
  296. public static show_edit_event(type:number,data:event_item,delete_call){
  297. resources.load("prefab/edit_event",Prefab,(err, prefab)=>{
  298. let node:Node = instantiate(prefab);
  299. node.parent = main.g_canvas.node;
  300. node.getComponent(edit_event).show(type,data,delete_call)
  301. })
  302. }
  303. // 去除左边和右边空格
  304. public static trimLeftAndRightblank(str: string):string {
  305. const reg = /^\s+|\s+$/g;
  306. return str.replace(reg, '')
  307. }
  308. }