UIKeys.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Collections.Generic;
  2. using XGame.Framework.UI;
  3. namespace FL.FGUI
  4. {
  5. public static partial class UIKeys
  6. {
  7. private static Dictionary<string, UIKey> _nameToKeys;
  8. private static Dictionary<UIKey, UILayer> _uikeyToLayerMap;
  9. public static UIKey NameToKey(string uiName)
  10. {
  11. if (string.IsNullOrEmpty(uiName))
  12. return null;
  13. if (_nameToKeys == null)
  14. {
  15. _nameToKeys = new Dictionary<string, UIKey>();
  16. }
  17. if (_nameToKeys.TryGetValue(uiName, out UIKey key))
  18. {
  19. return key;
  20. }
  21. var property = typeof(UIKeys).GetProperty(uiName, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.IgnoreCase);
  22. if (property != null)
  23. {
  24. key = property.GetValue(null) as UIKey;
  25. _nameToKeys.Add(uiName, key);
  26. return key;
  27. }
  28. XGame.Log.Error($"找不到UIKey. uiName:{uiName}");
  29. return null;
  30. }
  31. public static UILayer GetLayer(UIKey uikey)
  32. {
  33. if (_uikeyToLayerMap == null)
  34. {
  35. _uikeyToLayerMap = new Dictionary<UIKey, UILayer>();
  36. }
  37. if (_uikeyToLayerMap.TryGetValue(uikey, out var layer))
  38. {
  39. return layer;
  40. }
  41. layer = UILayer.Normal;
  42. var property = uikey.UIViewType.GetProperty("Layer", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.IgnoreCase);
  43. if (property != null)
  44. {
  45. layer = (UILayer)property.GetValue(null);
  46. }
  47. _uikeyToLayerMap.Add(uikey, layer);
  48. return layer;
  49. }
  50. public static void Dispose()
  51. {
  52. _nameToKeys?.Clear();
  53. _nameToKeys = null;
  54. _uikeyToLayerMap?.Clear();
  55. _uikeyToLayerMap = null;
  56. }
  57. }
  58. }