OptionDefinition.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using SRF.Helpers;
  3. namespace SRDebugger
  4. {
  5. /// <summary>
  6. /// Class describing how an option should be presented within the options panel.
  7. /// </summary>
  8. public sealed class OptionDefinition
  9. {
  10. /// <summary>
  11. /// Display-name for the option.
  12. /// </summary>
  13. public string Name { get; private set; }
  14. /// <summary>
  15. /// The category that this option should be placed within.
  16. /// </summary>
  17. public string Category { get; private set; }
  18. /// <summary>
  19. /// Sort order within the category. Order is low to high, (options with lower SortPriority will appear before
  20. /// options with higher SortPriority).
  21. /// </summary>
  22. public int SortPriority { get; private set; }
  23. /// <summary>
  24. /// Whether this option is a method that should be invoked.
  25. /// </summary>
  26. public bool IsMethod
  27. {
  28. get { return Method != null; }
  29. }
  30. /// <summary>
  31. /// Whether this option is a property that has a value.
  32. /// </summary>
  33. public bool IsProperty
  34. {
  35. get { return Property != null; }
  36. }
  37. /// <summary>
  38. /// The underlying method for this OptionDefinition.
  39. /// Can be null if <see cref="IsMethod"/> is false.
  40. /// </summary>
  41. public SRF.Helpers.MethodReference Method { get; private set; }
  42. /// <summary>
  43. /// The underlying property for this OptionDefinition.
  44. /// Can be null if <see cref="IsProperty"/> is false.
  45. /// </summary>
  46. public SRF.Helpers.PropertyReference Property { get; private set; }
  47. private OptionDefinition(string name, string category, int sortPriority)
  48. {
  49. Name = name;
  50. Category = category;
  51. SortPriority = sortPriority;
  52. }
  53. public OptionDefinition(string name, string category, int sortPriority, SRF.Helpers.MethodReference method)
  54. : this(name, category, sortPriority)
  55. {
  56. Method = method;
  57. }
  58. public OptionDefinition(string name, string category, int sortPriority, SRF.Helpers.PropertyReference property)
  59. : this(name, category, sortPriority)
  60. {
  61. Property = property;
  62. }
  63. public static OptionDefinition FromMethod(string name, Action callback, string category = "Default", int sortPriority = 0)
  64. {
  65. return new OptionDefinition(name, category, sortPriority, callback);;
  66. }
  67. /// <summary>
  68. /// Create an option definition from a setter and getter lambda.
  69. /// </summary>
  70. /// <param name="name">Name to display in options menu.</param>
  71. /// <param name="getter">Method to get the current value of the property.</param>
  72. /// <param name="setter">Method to set the value of the property (can be null if read-only)</param>
  73. /// <param name="category">Category to display the option in.</param>
  74. /// <param name="sortPriority">Sort priority to arrange the option within the category.</param>
  75. /// <returns>The created option definition.</returns>
  76. public static OptionDefinition Create<T>(string name, Func<T> getter, Action<T> setter = null, string category = "Default", int sortPriority = 0)
  77. {
  78. return new OptionDefinition(name, category, sortPriority, PropertyReference.FromLambda(getter, setter));
  79. }
  80. }
  81. }