/**************************************************************************** * Copyright (c) 2017 ~ 2020.12 liangxie * * https://qframework.cn * https://github.com/liangxiegame/QFramework * https://gitee.com/liangxiegame/QFramework * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ****************************************************************************/ using System; using UnityEngine; using UnityEngine.Events; namespace QFramework { /// /// /// /// [Serializable] public class Property { /// /// /// public Property() { } /// /// /// protected bool mSetted = false; /// /// /// /// public Property(T initValue) { mValue = initValue; } /// /// /// public T Value { get { return GetValue(); } set { SetValue(value); } } /// /// /// /// protected virtual T GetValue() { return mValue; } /// /// /// /// protected virtual void SetValue(T value) { if (IsValueChanged(value)) { mValue = value; DispatchValueChangeEvent(); mSetted = true; } } /// /// /// /// /// protected virtual bool IsValueChanged(T value) { return value == null || !value.Equals(mValue) || !mSetted; } /// /// /// protected virtual void DispatchValueChangeEvent() { if (mSetter != null) { mSetter.Invoke(mValue); OnValueChanged.Invoke(mValue); } } /// /// /// protected T mValue; /// /// /// /// /// public IDisposable Bind(Action onValueChanged) { mSetter += onValueChanged; return new CustomDisposable(() => { mSetter -= onValueChanged; }); } /// /// /// /// /// public IDisposable BindWithInitialValue(Action onValueChanged) { onValueChanged.Invoke(GetValue()); return Bind(onValueChanged); } /// /// /// public void UnBindAll() { mSetter = null; } private event Action mSetter = t => { }; public UnityEvent OnValueChanged = new OnPropertyChangedEvent(); } /// /// Int 类型的 Property /// [Serializable] public class IntProperty : Property { /// /// 值 /// public new int Value { get { return base.Value; } set { base.Value = value; } } } public class OnPropertyChangedEvent : UnityEvent { } public class PlayerPrefsBooleanProperty : Property { public PlayerPrefsBooleanProperty(string saveKey, bool defaultValue = false) { var initValue = PlayerPrefs.GetInt(saveKey, defaultValue ? 1 : 0) == 1; mValue = initValue; this.Bind(value => PlayerPrefs.SetInt(saveKey, value ? 1 : 0)); } } public class PlayerPrefsFloatProperty : Property { public PlayerPrefsFloatProperty(string saveKey, float defaultValue = 0.0f) { var initValue = PlayerPrefs.GetFloat(saveKey, defaultValue); mValue = initValue; this.Bind(value => PlayerPrefs.SetFloat(saveKey, value)); } } /// /// /// /// public class CustomProperty : Property { private Func mValueGetter = null; private Action mValueSetter = null; /// /// /// /// /// public CustomProperty(Func valueGetter, Action valueSetter = null) { mValueGetter = valueGetter; mValueSetter = valueSetter; } /// /// /// /// protected override T GetValue() { mValue = mValueGetter.Invoke(); return mValue; } /// /// /// /// protected override void SetValue(T value) { if (IsValueChanged(value)) { mValue = value; DispatchValueChangeEvent(); mSetted = true; if (mValueSetter != null) mValueSetter(value); } } } }