control.ts 13 KB

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