RangeParameterBase.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Runtime.Serialization;
  3. namespace WingServerCommon.Config.Parameters
  4. {
  5. /// <summary>
  6. /// Range parameter base type
  7. /// </summary>
  8. /// <typeparam name="T">Type of T</typeparam>
  9. [DataContract]
  10. public abstract class RangeParameterBase<T> : IParameter where T : struct
  11. {
  12. private T _value;
  13. private T _initialValue;
  14. /// <summary>
  15. /// The value saved in setting file.Some times the value will be set but not saved to setting file.
  16. /// </summary>
  17. public T SavedValue { get; protected set; }
  18. /// <summary>
  19. /// value changed event
  20. /// </summary>
  21. public event EventHandler ValueChanged;
  22. /// <summary>
  23. /// Min value
  24. /// </summary>
  25. [DataMember]
  26. public T MinValue { get; set; }
  27. /// <summary>
  28. /// Max value
  29. /// </summary>
  30. [DataMember]
  31. public T MaxValue { get; set; }
  32. public T InitialValue => _initialValue;
  33. /// <summary>
  34. /// parameter value
  35. /// </summary>
  36. [DataMember]
  37. public T Value
  38. {
  39. get
  40. {
  41. return _value;
  42. }
  43. set
  44. {
  45. if (!Equals(_value, value))
  46. {
  47. _value = value;
  48. OnValueChanged();
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// parameter name
  54. /// </summary>
  55. public string Name { get; set; }
  56. /// <summary>
  57. /// parameter section
  58. /// </summary>
  59. public string Section { get; set; }
  60. /// <summary>
  61. /// parameter value changed state
  62. /// </summary>
  63. public bool IsChanged => !Equals(_initialValue, Value);
  64. /// <summary>
  65. /// notify value changed
  66. /// </summary>
  67. private void OnValueChanged()
  68. {
  69. ValueChanged?.Invoke(this, EventArgs.Empty);
  70. }
  71. /// <summary>
  72. /// change initial value
  73. /// </summary>
  74. public void ChangeInitialValue()
  75. {
  76. _initialValue = Value;
  77. SavedValue = Value;
  78. }
  79. public void ChangeSavedValue()
  80. {
  81. SavedValue = Value;
  82. }
  83. /// <summary>
  84. /// Set user value.
  85. /// </summary>
  86. /// <param name="value">user value.</param>
  87. public abstract void ChangeParameterValue(object value);
  88. }
  89. /// <summary>
  90. /// Range long parameter.
  91. /// </summary>
  92. [DataContract]
  93. public class RangeLongParameter : RangeParameterBase<long>
  94. {
  95. public override void ChangeParameterValue(object value)
  96. {
  97. var longParameter = value as LongParameter;
  98. if (longParameter != null)
  99. {
  100. Value = longParameter.Value;
  101. SavedValue = Value;
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// Range int parameter.
  107. /// </summary>
  108. [DataContract]
  109. public class RangeIntParameter : RangeParameterBase<int>
  110. {
  111. public override void ChangeParameterValue(object value)
  112. {
  113. var intParameter = value as IntParameter;
  114. if (intParameter != null)
  115. {
  116. Value = intParameter.Value;
  117. SavedValue = Value;
  118. }
  119. }
  120. }
  121. }