UIControllerAnnotation.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { _decorator,Constructor, Node } from 'cc';
  2. import { UIController } from '../easy_ui_framework/UIController';
  3. // 打包之后有问题不支持名称注入
  4. export class UIControllerAnnotation {
  5. private static initMap = new Map<any, { bundleName?: any, path?: any,layer?:number }>()
  6. private static initElement = new Map<any, Map<string,{type:any,prefix?:string,path?:string}>>()
  7. static UI(param: { bundleName?: any, path: any } | string) {
  8. return function (target: Constructor) {
  9. // 首字母小写注入
  10. if(typeof param == 'string'){
  11. UIControllerAnnotation.initMap.set(target,{path:param})
  12. }else{
  13. UIControllerAnnotation.initMap.set(target,param)
  14. }
  15. }
  16. }
  17. static getElement(target:Constructor){
  18. // 这里还要找到父类的结果
  19. if(!target){
  20. return null
  21. }
  22. if(target == UIController){
  23. return UIControllerAnnotation.initElement.get(target)
  24. }
  25. let res = new Map
  26. let parent = Object.getPrototypeOf(target)
  27. let parentMap = this.getElement(parent)
  28. let myMap = UIControllerAnnotation.initElement.get(target)
  29. if(parentMap){
  30. parentMap.forEach((vlaue,key)=>res.set(key,vlaue))
  31. }
  32. if(myMap){
  33. myMap.forEach((vlaue,key)=>res.set(key,vlaue))
  34. }
  35. return res
  36. }
  37. static getUI(ui:Constructor){
  38. return UIControllerAnnotation.initMap.get(ui)
  39. }
  40. static element(type: Constructor|{type:any,prefix?:string,path?:string} = Node) {
  41. let res: PropertyDecorator = function (target:Object, prop: string) {
  42. // 首字母小写注入
  43. let map =UIControllerAnnotation.initElement.get(target.constructor)
  44. if(!map){
  45. map= new Map
  46. UIControllerAnnotation.initElement.set(target.constructor,map)
  47. }
  48. if(map.get(prop)){
  49. console.warn("存在同名注入,请检查代码,prop==>",prop)
  50. }
  51. if(typeof type == 'object'){
  52. map.set(prop,type)
  53. }else{
  54. map.set(prop,{type:type,prefix:'#'})
  55. }
  56. }
  57. return res
  58. }
  59. }