OptionControlFactory.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. private static readonly Dictionary<OptionType, DataBoundControl> TypeCache = new Dictionary<OptionType, DataBoundControl>();
  16. public static bool CanCreateControl(OptionDefinition from)
  17. {
  18. PopulateDataControlPrefabs();
  19. if (from.Property != null)
  20. {
  21. return TryGetDataControlPrefab(from) != null;
  22. }
  23. else
  24. {
  25. return _actionControlPrefab != null;
  26. }
  27. }
  28. /// <summary>
  29. /// Create a control from an <c>OptionDefinition</c>, optionally providing <paramref name="categoryPrefix" /> to remove
  30. /// the category name from the start of the control.
  31. /// </summary>
  32. /// <param name="from"></param>
  33. /// <param name="categoryPrefix"></param>
  34. /// <returns></returns>
  35. public static OptionsControlBase CreateControl(OptionDefinition from, string categoryPrefix = null)
  36. {
  37. PopulateDataControlPrefabs();
  38. if (from.Property != null)
  39. {
  40. return CreateDataControl(from, categoryPrefix);
  41. }
  42. if (from.Method != null)
  43. {
  44. return CreateActionControl(from, categoryPrefix);
  45. }
  46. throw new Exception("OptionDefinition did not contain property or method.");
  47. }
  48. private static void PopulateDataControlPrefabs()
  49. {
  50. if (_dataControlPrefabs == null)
  51. {
  52. _dataControlPrefabs = Resources.LoadAll<DataBoundControl>(SRDebugPaths.DataControlsResourcesPath);
  53. }
  54. if (_actionControlPrefab == null)
  55. {
  56. _actionControlPrefab =
  57. Resources.LoadAll<ActionControl>(SRDebugPaths.DataControlsResourcesPath).FirstOrDefault();
  58. }
  59. if (_actionControlPrefab == null)
  60. {
  61. Debug.LogError("[SRDebugger.Options] Cannot find ActionControl prefab.");
  62. }
  63. }
  64. private static ActionControl CreateActionControl(OptionDefinition from, string categoryPrefix = null)
  65. {
  66. var control = SRInstantiate.Instantiate(_actionControlPrefab);
  67. if (control == null)
  68. {
  69. Debug.LogWarning("[SRDebugger.OptionsTab] Error creating action control from prefab");
  70. return null;
  71. }
  72. control.SetMethod(from.Name, from.Method);
  73. control.Option = from;
  74. return control;
  75. }
  76. private static DataBoundControl CreateDataControl(OptionDefinition from, string categoryPrefix = null)
  77. {
  78. var prefab = TryGetDataControlPrefab(from);
  79. if (prefab == null)
  80. {
  81. Debug.LogWarning(
  82. "[SRDebugger.OptionsTab] Can't find data control for type {0}".Fmt(from.Property.PropertyType));
  83. return null;
  84. }
  85. var instance = SRInstantiate.Instantiate(prefab);
  86. try
  87. {
  88. var n = from.Name;
  89. // Remove category name from the start of the property name
  90. if (!string.IsNullOrEmpty(categoryPrefix) && n.StartsWith(categoryPrefix))
  91. {
  92. n = n.Substring(categoryPrefix.Length);
  93. }
  94. instance.Bind(n, from.Property);
  95. instance.Option = from;
  96. }
  97. catch (Exception e)
  98. {
  99. Debug.LogError("[SRDebugger.Options] Error binding to property {0}".Fmt(from.Name));
  100. Debug.LogException(e);
  101. Object.Destroy(instance);
  102. instance = null;
  103. }
  104. return instance;
  105. }
  106. private static DataBoundControl TryGetDataControlPrefab(OptionDefinition from)
  107. {
  108. OptionType type = new OptionType(@from.Property.PropertyType, !@from.Property.CanWrite);
  109. DataBoundControl control;
  110. if (!TypeCache.TryGetValue(type, out control))
  111. {
  112. control = _dataControlPrefabs.FirstOrDefault(p =>
  113. p.CanBind(@from.Property.PropertyType, !@from.Property.CanWrite));
  114. TypeCache.Add(type, control);
  115. }
  116. return control;
  117. }
  118. private struct OptionType
  119. {
  120. public readonly Type Type;
  121. public readonly bool IsReadyOnly;
  122. public OptionType(Type type, bool isReadyOnly)
  123. {
  124. Type = type;
  125. IsReadyOnly = isReadyOnly;
  126. }
  127. public bool Equals(OptionType other)
  128. {
  129. return Equals(Type, other.Type) && IsReadyOnly == other.IsReadyOnly;
  130. }
  131. public override bool Equals(object obj)
  132. {
  133. return obj is OptionType other && Equals(other);
  134. }
  135. public override int GetHashCode()
  136. {
  137. unchecked
  138. {
  139. return ((Type != null ? Type.GetHashCode() : 0) * 397) ^ IsReadyOnly.GetHashCode();
  140. }
  141. }
  142. }
  143. }
  144. }