control.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. if(total==0) {
  135. call_back()
  136. return
  137. }
  138. let count = 0;
  139. let call = ()=>{
  140. count++;
  141. if(count>=total){
  142. call_back()
  143. }
  144. }
  145. for (let index = 0; index < this.m_bag_data.item_list.length; index++) {
  146. let url = this.m_bag_data.item_list[index].url;
  147. let name = this.m_bag_data.item_list[index].name;
  148. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  149. if (!err && imageAsset2) {
  150. const texture = new Texture2D();
  151. texture.image = imageAsset2;
  152. let spFrame2 = new SpriteFrame();
  153. spFrame2.texture = texture;
  154. spFrame2.addRef()
  155. control.res_map.set(name,spFrame2)
  156. gameManager.res_map.set(url,spFrame2)
  157. call()
  158. }
  159. });
  160. }
  161. for (let index = 0; index < this.m_bag_data.bg_list.length; index++) {
  162. let url = this.m_bag_data.bg_list[index].url;
  163. let name = this.m_bag_data.bg_list[index].name;
  164. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  165. if (!err && imageAsset2) {
  166. const texture = new Texture2D();
  167. texture.image = imageAsset2;
  168. let spFrame2 = new SpriteFrame();
  169. spFrame2.texture = texture;
  170. spFrame2.addRef()
  171. control.res_map.set(name,spFrame2)
  172. gameManager.res_map.set(url,spFrame2)
  173. call()
  174. }
  175. });
  176. }
  177. for (let index = 0; index < this.m_bag_data.component_list.length; index++) {
  178. let url = this.m_bag_data.component_list[index].url;
  179. let name = this.m_bag_data.component_list[index].name;
  180. assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
  181. if (!err && imageAsset2) {
  182. const texture = new Texture2D();
  183. texture.image = imageAsset2;
  184. let spFrame2 = new SpriteFrame();
  185. spFrame2.texture = texture;
  186. spFrame2.addRef()
  187. control.res_map.set(name,spFrame2)
  188. gameManager.res_map.set(url,spFrame2)
  189. call()
  190. }
  191. });
  192. }
  193. for (let index = 0; index < this.m_bag_data.sound_list.length; index++) {
  194. let url = this.m_bag_data.sound_list[index].url;
  195. let name = this.m_bag_data.sound_list[index].name;
  196. assetManager.loadRemote(url, (err: any, clip: any)=> {
  197. clip.addRef()
  198. if (!err && clip) {
  199. control.mp3_cache.set(name,clip)
  200. gameManager.mp3_cache.set(url,clip)
  201. call()
  202. }
  203. });
  204. }
  205. }
  206. initBtnEvent(){
  207. for (let index = 0; index < this.res_and_control_btn_list.length; index++) {
  208. const element = this.res_and_control_btn_list[index];
  209. this.res_view_list[index].active = false;
  210. element.off(Node.EventType.TOUCH_END)
  211. element.on(Node.EventType.TOUCH_END,()=>{
  212. this.select_res_btn_type_on_click(parseInt(element.name))
  213. },element)
  214. }
  215. this.select_res_btn_type_on_click(this.res_and_control_select_index)
  216. for (let index = 0; index < this.res_btn_list.length; index++) {
  217. const element = this.res_btn_list[index];
  218. element.off(Node.EventType.TOUCH_END)
  219. element.on(Node.EventType.TOUCH_END,()=>{
  220. this.select_res_and_control_type_on_click(parseInt(element.name))
  221. },element)
  222. }
  223. this.select_res_and_control_type_on_click(this.res_select_index)
  224. }
  225. hide_all(){
  226. this.task_list.active = false;
  227. this.widget_list.active = false;
  228. this.res_list.active = false;
  229. this.scene_list.active = false;
  230. this.btn_res.getComponent(Sprite).color = Color.GRAY;
  231. this.btn_control.getComponent(Sprite).color = Color.GRAY;
  232. this.btn_select_scene.getComponent(Sprite).color = Color.GRAY;
  233. this.btn_task.getComponent(Sprite).color = Color.GRAY;
  234. }
  235. show_res(){
  236. this.hide_all();
  237. this.res_list.active = true;
  238. this.btn_res.getComponent(Sprite).color = Color.YELLOW;
  239. }
  240. show_widget(){
  241. this.hide_all();
  242. this.widget_list.active = true;
  243. this.btn_control.getComponent(Sprite).color = Color.YELLOW;
  244. }
  245. show_select_scene(){
  246. this.hide_all();
  247. this.scene_list.active = true;
  248. this.btn_select_scene.getComponent(Sprite).color = Color.YELLOW;
  249. }
  250. show_task(){
  251. this.hide_all();
  252. this.task_list.active = true;
  253. this.btn_task.getComponent(Sprite).color = Color.YELLOW;
  254. }
  255. select_res_btn_type_on_click(index){
  256. switch (index) {
  257. case config.select_res_and_control_type.RES_TYPE:
  258. this.show_res()
  259. break;
  260. case config.select_res_and_control_type.CONTROL_TYPE:
  261. this.show_widget()
  262. break;
  263. case config.select_res_and_control_type.SCENE_SELECT:
  264. this.show_select_scene()
  265. break;
  266. case config.select_res_and_control_type.TASK:
  267. this.show_task()
  268. break;
  269. }
  270. this.res_and_control_select_index = index
  271. }
  272. select_res_and_control_type_on_click(index){
  273. let select_node = this.res_btn_list[index]
  274. for (let i = 0; i < this.res_btn_list.length; i++) {
  275. const element = this.res_btn_list[i];
  276. if(i==index){
  277. }else{
  278. element.getComponent(Sprite).color = Color.GRAY;
  279. }
  280. }
  281. select_node.getComponent(Sprite).color = Color.YELLOW;
  282. this.res_select_index = index
  283. this.update_res_select_views()
  284. }
  285. }