123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- using System.Collections.Generic;
- using UnityEngine.Events;
- using UnityEngine.UI;
- namespace WS
- {
-
- public class BindablePropertyBase : WSComponent
- {
- }
-
- public class BindableProperty<T> : BindablePropertyBase
- {
-
- public UnityAction<T> OnValueChanged;
- private T _value;
-
- public T Value
- {
- get { return _value; }
- set
- {
- if (!Equals(_value, value))
- {
- _value = value;
- OnValueChanged?.Invoke(_value);
- }
- }
- }
- public override void Dispose()
- {
- base.Dispose();
- OnValueChanged = null;
- _value = default(T);
- }
- }
-
- public abstract class Model : WSComponent
- {
-
- private List<BindablePropertyBase> Propertys = new List<BindablePropertyBase>();
-
- private List<UnityEventBase> UnityEvents = new List<UnityEventBase>();
-
- public BindableProperty<T> CreateProperty<T>()
- {
- BindableProperty<T> property = ComponentFactory.CreateWithParent<BindableProperty<T>>(this);
- Propertys.Add(property);
- return property;
- }
-
- public abstract void InitProperty();
-
-
-
-
- public void Bind(InputField inputfield, BindableProperty<string> property, UnityAction<string> valueChanged = null)
- {
- property.OnValueChanged += delegate
- {
- inputfield.text = property.Value;
- valueChanged?.Invoke(property.Value);
- };
- inputfield.onValueChanged.AddListener((x) =>
- {
- property.Value = x;
- });
- UnityEvents.Add(inputfield.onValueChanged);
- }
-
-
-
-
- public void Bind(TMPro.TMP_InputField inputfield, BindableProperty<string> property, UnityAction<string> valueChanged = null)
- {
- property.OnValueChanged += delegate
- {
- inputfield.text = property.Value;
- valueChanged?.Invoke(property.Value);
- };
- inputfield.onValueChanged.AddListener((x) =>
- {
- property.Value = x;
- });
- UnityEvents.Add(inputfield.onValueChanged);
- }
-
-
-
-
- public void Bind(InputField inputfield, BindableProperty<int> property, UnityAction<int> valueChanged = null)
- {
- property.OnValueChanged += delegate
- {
- inputfield.text = property.Value.ToString();
- valueChanged?.Invoke(property.Value);
- };
- inputfield.onValueChanged.AddListener((x) =>
- {
- int num = int.Parse(x);
- property.Value = num;
- });
- UnityEvents.Add(inputfield.onValueChanged);
- }
-
-
-
-
- public void Bind(InputField inputfield, BindableProperty<long> property, UnityAction<long> valueChanged = null)
- {
- property.OnValueChanged += delegate
- {
- inputfield.text = property.Value.ToString();
- valueChanged?.Invoke(property.Value);
- };
- inputfield.onValueChanged.AddListener((x) =>
- {
- long num = long.Parse(x);
- property.Value = num;
- });
- UnityEvents.Add(inputfield.onValueChanged);
- }
-
-
-
- public void Bind(Text text, BindableProperty<string> property)
- {
- text.text = property.Value;
- property.OnValueChanged += (x) =>
- {
- text.text = property.Value.ToString();
- };
- }
-
-
-
-
- public void Bind<T>(Text text, BindableProperty<T> property)
- {
- text.text = property.Value.ToString();
- property.OnValueChanged += (x) =>
- {
- text.text = property.Value.ToString();
- };
- }
-
-
-
-
- public void Bind(Toggle toggle, BindableProperty<bool> property, UnityAction<bool> valueChanged = null)
- {
- property.OnValueChanged += delegate
- {
- toggle.isOn = property.Value;
- valueChanged?.Invoke(property.Value);
- };
- toggle.onValueChanged.AddListener((x) =>
- {
- property.Value = x;
- });
- UnityEvents.Add(toggle.onValueChanged);
- }
-
-
-
-
- public void Bind(Slider slider, BindableProperty<float> property, UnityAction<float> valueChanged = null)
- {
- property.OnValueChanged += delegate
- {
- slider.value = property.Value;
- valueChanged?.Invoke(property.Value);
- };
- slider.onValueChanged.AddListener((x) =>
- {
- property.Value = x;
- });
- UnityEvents.Add(slider.onValueChanged);
- }
-
-
-
-
- public void Bind(Scrollbar scrollbar, BindableProperty<float> property, UnityAction<float> valueChanged = null)
- {
- property.OnValueChanged += delegate
- {
- scrollbar.value = property.Value;
- valueChanged?.Invoke(property.Value);
- };
- scrollbar.onValueChanged.AddListener((x) =>
- {
- property.Value = x;
- });
- UnityEvents.Add(scrollbar.onValueChanged);
- }
-
-
-
-
- public void Bind(Dropdown dropdown, BindableProperty<int> property, UnityAction<int> valueChanged = null)
- {
- property.OnValueChanged += delegate
- {
- dropdown.value = property.Value;
- valueChanged?.Invoke(property.Value);
- };
- dropdown.onValueChanged.AddListener((x) =>
- {
- property.Value = x;
- });
- UnityEvents.Add(dropdown.onValueChanged);
- }
-
-
-
- public void Bind(Button button, UnityAction onClick)
- {
- button.onClick.AddListener(delegate
- {
- UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(null);
- onClick.Invoke();
- });
- UnityEvents.Add(button.onClick);
- }
- public override void Dispose()
- {
- base.Dispose();
- for (int i = 0; i < UnityEvents.Count; i++)
- {
- UnityEvents[i].RemoveAllListeners();
- }
- UnityEvents.Clear();
- for (int i = 0; i < Propertys.Count; i++)
- {
- Propertys[i].Dispose();
- }
- Propertys.Clear();
- }
- }
- }
|