OptionControlFactory.cs 3.6 KB

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