control.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { _decorator, assetManager, AudioClip, Button, Color, Component, ImageAsset, Node, Prefab, Sprite, SpriteFrame, Texture2D } from 'cc';
  2. import { config } from '../config';
  3. import { bag_data, scene_item_data } from '../../data/data';
  4. import { res_list } from './res_list';
  5. import { edit_scene } from './edit_scene';
  6. import { main } from '../main';
  7. import { gameManager } from '../run/gameManager';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('control')
  10. export class control extends Component {
  11. @property(Node) btn_res:Node = null;
  12. @property(Node) btn_control:Node = null;
  13. @property(Node) btn_select_scene:Node = null;
  14. @property(Node) btn_task:Node = null;
  15. private res_and_control_select_index:number = 0;
  16. private res_and_control_btn_list:Node[] = null;
  17. @property(Node) btn_item_list:Node = null;
  18. @property(Node) btn_bg_list:Node = null;
  19. @property(Node) btn_component_list:Node = null;
  20. @property(Node) btn_sound_list:Node = null;
  21. private res_select_index:number = 0;
  22. private res_btn_list:Node[] = null;
  23. @property(Node) item_list:Node = null;
  24. @property(Node) bg_list:Node = null;
  25. @property(Node) component_list:Node = null;
  26. @property(Node) sound_list:Node = null;
  27. private res_view_list:Node[] = null;
  28. public static Singleton:control = null;
  29. private m_bag_data:bag_data = null;
  30. public static res_map:Map<string,SpriteFrame> = new Map()
  31. public static mp3_cache:Map<string,AudioClip> = new Map()
  32. @property(Prefab) prefab_img_item:Prefab = null;
  33. @property(Prefab) prefab_sound_item:Prefab = null;
  34. private res_data_list:any[] = []
  35. @property(Node) res_list:Node = null;
  36. @property(Node) widget_list:Node = null;
  37. @property(Node) task_list:Node = null;
  38. @property(Node) scene_list:Node = null;
  39. private m_main:main = null;
  40. protected start(): void {
  41. control.Singleton = this;
  42. }
  43. public get_bag_data():bag_data{
  44. return this.m_bag_data;
  45. }
  46. public push_scene_data(data:scene_item_data){
  47. this.m_bag_data.content.push(data)
  48. }
  49. public remove_scene_data(data:scene_item_data){
  50. let i = this.m_bag_data.content.indexOf(data)
  51. if(i!=-1){
  52. let temp = []
  53. for (let index = 0; index < this.m_bag_data.content.length; index++) {
  54. const element = this.m_bag_data.content[index];
  55. if(i===index){
  56. }else{
  57. temp.push(element)
  58. }
  59. }
  60. this.m_bag_data.content = temp;
  61. }
  62. }
  63. public init(_main:main){
  64. this.m_main = _main
  65. if(this.res_and_control_btn_list==null){
  66. this.res_and_control_btn_list = []
  67. this.btn_res.name = `${config.select_res_and_control_type.RES_TYPE}`
  68. this.res_and_control_btn_list.push(this.btn_res)
  69. this.btn_control.name = `${config.select_res_and_control_type.CONTROL_TYPE}`
  70. this.res_and_control_btn_list.push(this.btn_control)
  71. this.btn_select_scene.name = `${config.select_res_and_control_type.SCENE_SELECT}`
  72. this.res_and_control_btn_list.push(this.btn_select_scene)
  73. this.btn_task.name = `${config.select_res_and_control_type.TASK}`
  74. this.res_and_control_btn_list.push(this.btn_task)
  75. }
  76. this.res_and_control_select_index = config.select_res_and_control_type.RES_TYPE;
  77. if(this.res_btn_list==null){
  78. this.res_btn_list = [];
  79. this.btn_item_list.name = `${config.select_res_btn_type.ITEM_LIST}`
  80. this.res_btn_list.push(this.btn_item_list)
  81. this.btn_component_list.name = `${config.select_res_btn_type.COMPONENT_LIST}`
  82. this.res_btn_list.push(this.btn_component_list)
  83. this.btn_bg_list.name = `${config.select_res_btn_type.BG_LIST}`
  84. this.res_btn_list.push(this.btn_bg_list)
  85. this.btn_sound_list.name = `${config.select_res_btn_type.SOUND_LIST}`
  86. this.res_btn_list.push(this.btn_sound_list)
  87. this.res_view_list = []
  88. this.res_view_list.push(this.item_list)
  89. this.res_view_list.push(this.component_list)
  90. this.res_view_list.push(this.bg_list)
  91. this.res_view_list.push(this.sound_list)
  92. }
  93. this.res_select_index = config.select_res_btn_type.ITEM_LIST;
  94. this.initBtnEvent()
  95. this.initViews();
  96. }
  97. initViews(){
  98. for (let index = 0; index < this.res_view_list.length; index++) {
  99. const element = this.res_view_list[index];
  100. if(index===config.select_res_btn_type.SOUND_LIST){
  101. element.getComponent(res_list).initView(this.prefab_sound_item,index,this.res_data_list[index])
  102. }else{
  103. element.getComponent(res_list).initView(this.prefab_img_item,index,this.res_data_list[index])
  104. }
  105. element.active = this.res_select_index==index;
  106. }
  107. }
  108. public getResDataByType(type:number){
  109. for (let index = 0; index < this.res_view_list.length; index++) {
  110. const element = this.res_view_list[index];
  111. if(element.getComponent(res_list).getType()==type){
  112. return element.getComponent(res_list).getData()
  113. }
  114. }
  115. return null;
  116. }
  117. update_res_select_views(){
  118. for (let index = 0; index < this.res_view_list.length; index++) {
  119. const element = this.res_view_list[index];
  120. element.active = this.res_select_index==index;
  121. }
  122. }
  123. public loading_all_resources(data,call_back){
  124. this.m_bag_data = bag_data.initBagData(data.content);
  125. if(this.m_bag_data.content==undefined||this.m_bag_data.content==null||this.m_bag_data.content.length<=0){
  126. this.m_bag_data.content = []
  127. }
  128. this.res_data_list = []
  129. let total = this.m_bag_data.item_list.length+this.m_bag_data.component_list.length+this.m_bag_data.bg_list.length+this.m_bag_data.sound_list.length;
  130. this.res_data_list.push(this.m_bag_data.item_list)
  131. this.res_data_list.push(this.m_bag_data.component_list)
  132. this.res_data_list.push(this.m_bag_data.bg_list)
  133. this.res_data_list.push(this.m_bag_data.sound_list)
  134. let count = 0;
  135. let call = ()=>{
  136. count++;
  137. if(count>=total){
  138. call_back()
  139. }
  140. }
  141. for (let index = 0; index < this.m_bag_data.item_list.length; index++) {
  142. let url = this.m_bag_data.item_list[index].url;
  143. let name = this.m_bag_data.item_list[index].name;
  144. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  145. if (!err && imageAsset2) {
  146. const texture = new Texture2D();
  147. texture.image = imageAsset2;
  148. let spFrame2 = new SpriteFrame();
  149. spFrame2.texture = texture;
  150. spFrame2.addRef()
  151. control.res_map.set(name,spFrame2)
  152. gameManager.res_map.set(url,spFrame2)
  153. call()
  154. }
  155. });
  156. }
  157. for (let index = 0; index < this.m_bag_data.bg_list.length; index++) {
  158. let url = this.m_bag_data.bg_list[index].url;
  159. let name = this.m_bag_data.bg_list[index].name;
  160. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  161. if (!err && imageAsset2) {
  162. const texture = new Texture2D();
  163. texture.image = imageAsset2;
  164. let spFrame2 = new SpriteFrame();
  165. spFrame2.texture = texture;
  166. spFrame2.addRef()
  167. control.res_map.set(name,spFrame2)
  168. gameManager.res_map.set(url,spFrame2)
  169. call()
  170. }
  171. });
  172. }
  173. for (let index = 0; index < this.m_bag_data.component_list.length; index++) {
  174. let url = this.m_bag_data.component_list[index].url;
  175. let name = this.m_bag_data.component_list[index].name;
  176. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  177. if (!err && imageAsset2) {
  178. const texture = new Texture2D();
  179. texture.image = imageAsset2;
  180. let spFrame2 = new SpriteFrame();
  181. spFrame2.texture = texture;
  182. spFrame2.addRef()
  183. control.res_map.set(name,spFrame2)
  184. gameManager.res_map.set(url,spFrame2)
  185. call()
  186. }
  187. });
  188. }
  189. for (let index = 0; index < this.m_bag_data.sound_list.length; index++) {
  190. let url = this.m_bag_data.sound_list[index].url;
  191. let name = this.m_bag_data.sound_list[index].name;
  192. assetManager.loadRemote(url, (err: any, clip: any)=> {
  193. clip.addRef()
  194. if (!err && clip) {
  195. control.mp3_cache.set(name,clip)
  196. gameManager.mp3_cache.set(url,clip)
  197. call()
  198. }
  199. });
  200. }
  201. }
  202. initBtnEvent(){
  203. for (let index = 0; index < this.res_and_control_btn_list.length; index++) {
  204. const element = this.res_and_control_btn_list[index];
  205. this.res_view_list[index].active = false;
  206. element.off(Node.EventType.TOUCH_END)
  207. element.on(Node.EventType.TOUCH_END,()=>{
  208. this.select_res_btn_type_on_click(parseInt(element.name))
  209. },element)
  210. }
  211. this.select_res_btn_type_on_click(this.res_and_control_select_index)
  212. for (let index = 0; index < this.res_btn_list.length; index++) {
  213. const element = this.res_btn_list[index];
  214. element.off(Node.EventType.TOUCH_END)
  215. element.on(Node.EventType.TOUCH_END,()=>{
  216. this.select_res_and_control_type_on_click(parseInt(element.name))
  217. },element)
  218. }
  219. this.select_res_and_control_type_on_click(this.res_select_index)
  220. }
  221. hide_all(){
  222. this.task_list.active = false;
  223. this.widget_list.active = false;
  224. this.res_list.active = false;
  225. this.scene_list.active = false;
  226. this.btn_res.getComponent(Sprite).color = Color.GRAY;
  227. this.btn_control.getComponent(Sprite).color = Color.GRAY;
  228. this.btn_select_scene.getComponent(Sprite).color = Color.GRAY;
  229. this.btn_task.getComponent(Sprite).color = Color.GRAY;
  230. }
  231. show_res(){
  232. this.hide_all();
  233. this.res_list.active = true;
  234. this.btn_res.getComponent(Sprite).color = Color.YELLOW;
  235. }
  236. show_widget(){
  237. this.hide_all();
  238. this.widget_list.active = true;
  239. this.btn_control.getComponent(Sprite).color = Color.YELLOW;
  240. }
  241. show_select_scene(){
  242. this.hide_all();
  243. this.scene_list.active = true;
  244. this.btn_select_scene.getComponent(Sprite).color = Color.YELLOW;
  245. }
  246. show_task(){
  247. this.hide_all();
  248. this.task_list.active = true;
  249. this.btn_task.getComponent(Sprite).color = Color.YELLOW;
  250. }
  251. select_res_btn_type_on_click(index){
  252. switch (index) {
  253. case config.select_res_and_control_type.RES_TYPE:
  254. this.show_res()
  255. break;
  256. case config.select_res_and_control_type.CONTROL_TYPE:
  257. this.show_widget()
  258. break;
  259. case config.select_res_and_control_type.SCENE_SELECT:
  260. this.show_select_scene()
  261. break;
  262. case config.select_res_and_control_type.TASK:
  263. this.show_task()
  264. break;
  265. }
  266. this.res_and_control_select_index = index
  267. }
  268. select_res_and_control_type_on_click(index){
  269. let select_node = this.res_btn_list[index]
  270. for (let i = 0; i < this.res_btn_list.length; i++) {
  271. const element = this.res_btn_list[i];
  272. if(i==index){
  273. }else{
  274. element.getComponent(Sprite).color = Color.GRAY;
  275. }
  276. }
  277. select_node.getComponent(Sprite).color = Color.YELLOW;
  278. this.res_select_index = index
  279. this.update_res_select_views()
  280. }
  281. }