QFramework.BindableProperty.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /****************************************************************************
  2. * Copyright (c) 2017 ~ 2020.12 liangxie
  3. *
  4. * https://qframework.cn
  5. * https://github.com/liangxiegame/QFramework
  6. * https://gitee.com/liangxiegame/QFramework
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. ****************************************************************************/
  26. using System;
  27. using UnityEngine;
  28. using UnityEngine.Events;
  29. namespace QFramework
  30. {
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. /// <typeparam name="T"></typeparam>
  35. [Serializable]
  36. public class Property<T>
  37. {
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. public Property()
  42. {
  43. }
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. protected bool mSetted = false;
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. /// <param name="initValue"></param>
  52. public Property(T initValue)
  53. {
  54. mValue = initValue;
  55. }
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. public T Value
  60. {
  61. get { return GetValue(); }
  62. set { SetValue(value); }
  63. }
  64. /// <summary>
  65. ///
  66. /// </summary>
  67. /// <returns></returns>
  68. protected virtual T GetValue()
  69. {
  70. return mValue;
  71. }
  72. /// <summary>
  73. ///
  74. /// </summary>
  75. /// <param name="value"></param>
  76. protected virtual void SetValue(T value)
  77. {
  78. if (IsValueChanged(value))
  79. {
  80. mValue = value;
  81. DispatchValueChangeEvent();
  82. mSetted = true;
  83. }
  84. }
  85. /// <summary>
  86. ///
  87. /// </summary>
  88. /// <param name="value"></param>
  89. /// <returns></returns>
  90. protected virtual bool IsValueChanged(T value)
  91. {
  92. return value == null || !value.Equals(mValue) || !mSetted;
  93. }
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. protected virtual void DispatchValueChangeEvent()
  98. {
  99. if (mSetter != null)
  100. {
  101. mSetter.Invoke(mValue);
  102. OnValueChanged.Invoke(mValue);
  103. }
  104. }
  105. /// <summary>
  106. ///
  107. /// </summary>
  108. protected T mValue;
  109. /// <summary>
  110. ///
  111. /// </summary>
  112. /// <param name="onValueChanged"></param>
  113. /// <returns></returns>
  114. public IDisposable Bind(Action<T> onValueChanged)
  115. {
  116. mSetter += onValueChanged;
  117. return new CustomDisposable(() => { mSetter -= onValueChanged; });
  118. }
  119. /// <summary>
  120. ///
  121. /// </summary>
  122. /// <param name="onValueChanged"></param>
  123. /// <returns></returns>
  124. public IDisposable BindWithInitialValue(Action<T> onValueChanged)
  125. {
  126. onValueChanged.Invoke(GetValue());
  127. return Bind(onValueChanged);
  128. }
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. public void UnBindAll()
  133. {
  134. mSetter = null;
  135. }
  136. private event Action<T> mSetter = t => { };
  137. public UnityEvent<T> OnValueChanged = new OnPropertyChangedEvent<T>();
  138. }
  139. /// <summary>
  140. /// Int 类型的 Property
  141. /// </summary>
  142. [Serializable]
  143. public class IntProperty : Property<int>
  144. {
  145. /// <summary>
  146. /// 值
  147. /// </summary>
  148. public new int Value
  149. {
  150. get { return base.Value; }
  151. set { base.Value = value; }
  152. }
  153. }
  154. public class OnPropertyChangedEvent<T> : UnityEvent<T>
  155. {
  156. }
  157. public class PlayerPrefsBooleanProperty : Property<bool>
  158. {
  159. public PlayerPrefsBooleanProperty(string saveKey, bool defaultValue = false)
  160. {
  161. var initValue = PlayerPrefs.GetInt(saveKey, defaultValue ? 1 : 0) == 1;
  162. mValue = initValue;
  163. this.Bind(value => PlayerPrefs.SetInt(saveKey, value ? 1 : 0));
  164. }
  165. }
  166. public class PlayerPrefsFloatProperty : Property<float>
  167. {
  168. public PlayerPrefsFloatProperty(string saveKey, float defaultValue = 0.0f)
  169. {
  170. var initValue = PlayerPrefs.GetFloat(saveKey, defaultValue);
  171. mValue = initValue;
  172. this.Bind(value => PlayerPrefs.SetFloat(saveKey, value));
  173. }
  174. }
  175. /// <summary>
  176. ///
  177. /// </summary>
  178. /// <typeparam name="T"></typeparam>
  179. public class CustomProperty<T> : Property<T>
  180. {
  181. private Func<T> mValueGetter = null;
  182. private Action<T> mValueSetter = null;
  183. /// <summary>
  184. ///
  185. /// </summary>
  186. /// <param name="valueGetter"></param>
  187. /// <param name="valueSetter"></param>
  188. public CustomProperty(Func<T> valueGetter, Action<T> valueSetter = null)
  189. {
  190. mValueGetter = valueGetter;
  191. mValueSetter = valueSetter;
  192. }
  193. /// <summary>
  194. ///
  195. /// </summary>
  196. /// <returns></returns>
  197. protected override T GetValue()
  198. {
  199. mValue = mValueGetter.Invoke();
  200. return mValue;
  201. }
  202. /// <summary>
  203. ///
  204. /// </summary>
  205. /// <param name="value"></param>
  206. protected override void SetValue(T value)
  207. {
  208. if (IsValueChanged(value))
  209. {
  210. mValue = value;
  211. DispatchValueChangeEvent();
  212. mSetted = true;
  213. if (mValueSetter != null) mValueSetter(value);
  214. }
  215. }
  216. }
  217. }