OptionControlFactory.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using SRDebugger.UI.Controls;
  5. using SRDebugger.UI.Controls.Data;
  6. using SRF;
  7. using UnityEngine;
  8. namespace SRDebugger.Internal
  9. {
  10. public static class OptionControlFactory
  11. {
  12. public static OptionsControlBase CreateControl(OptionDefinition from, string categoryPrefix = null)
  13. {
  14. if (OptionControlFactory._dataControlPrefabs == null)
  15. {
  16. OptionControlFactory._dataControlPrefabs = Resources.LoadAll<DataBoundControl>("SRDebugger/UI/Prefabs/Options");
  17. }
  18. if (OptionControlFactory._actionControlPrefab == null)
  19. {
  20. OptionControlFactory._actionControlPrefab = Resources.LoadAll<ActionControl>("SRDebugger/UI/Prefabs/Options").FirstOrDefault<ActionControl>();
  21. }
  22. if (OptionControlFactory._actionControlPrefab == null)
  23. {
  24. UnityEngine.Debug.LogError("[SRDebugger.Options] Cannot find ActionControl prefab.");
  25. }
  26. if (from.Property != null)
  27. {
  28. return OptionControlFactory.CreateDataControl(from, categoryPrefix);
  29. }
  30. if (from.Method != null)
  31. {
  32. return OptionControlFactory.CreateActionControl(from, categoryPrefix);
  33. }
  34. throw new Exception("OptionDefinition did not contain property or method.");
  35. }
  36. private static ActionControl CreateActionControl(OptionDefinition from, string categoryPrefix = null)
  37. {
  38. ActionControl actionControl = SRInstantiate.Instantiate<ActionControl>(OptionControlFactory._actionControlPrefab);
  39. if (actionControl == null)
  40. {
  41. UnityEngine.Debug.LogWarning("[SRDebugger.OptionsTab] Error creating action control from prefab");
  42. return null;
  43. }
  44. actionControl.SetMethod(from.Name, from.Method);
  45. actionControl.Option = from;
  46. return actionControl;
  47. }
  48. private static DataBoundControl CreateDataControl(OptionDefinition from, string categoryPrefix = null)
  49. {
  50. DataBoundControl dataBoundControl = OptionControlFactory._dataControlPrefabs.FirstOrDefault((DataBoundControl p) => p.CanBind(from.Property.PropertyType, !from.Property.CanWrite));
  51. if (dataBoundControl == null)
  52. {
  53. UnityEngine.Debug.LogWarning("[SRDebugger.OptionsTab] Can't find data control for type {0}".Fmt(new object[]
  54. {
  55. from.Property.PropertyType
  56. }));
  57. return null;
  58. }
  59. DataBoundControl dataBoundControl2 = SRInstantiate.Instantiate<DataBoundControl>(dataBoundControl);
  60. try
  61. {
  62. string text = from.Name;
  63. if (!string.IsNullOrEmpty(categoryPrefix) && text.StartsWith(categoryPrefix))
  64. {
  65. text = text.Substring(categoryPrefix.Length);
  66. }
  67. dataBoundControl2.Bind(text, from.Property);
  68. dataBoundControl2.Option = from;
  69. }
  70. catch (Exception exception)
  71. {
  72. UnityEngine.Debug.LogError("[SRDebugger.Options] Error binding to property {0}".Fmt(new object[]
  73. {
  74. from.Name
  75. }));
  76. UnityEngine.Debug.LogException(exception);
  77. UnityEngine.Object.Destroy(dataBoundControl2);
  78. dataBoundControl2 = null;
  79. }
  80. return dataBoundControl2;
  81. }
  82. private static IList<DataBoundControl> _dataControlPrefabs;
  83. private static ActionControl _actionControlPrefab;
  84. }
  85. }