public static IObservable <ChangeEvent <T> > OnValueChange <T>(this INotifyValueChanged <T> source) { return(Observable.FromEvent <EventCallback <ChangeEvent <T> >, ChangeEvent <T> >( h => new EventCallback <ChangeEvent <T> >(h), h => source.RegisterValueChangedCallback(h), h => source.UnregisterValueChangedCallback(h))); }
public static IDisposable Bind <TValue>(this VisualElement target, INotifyValueChanged <TValue> targetAccessor, object source, string propertyName, INotifyValueChanged <TValue> accessor, BindingMode mode = BindingMode.TwoWay) { if (target == null) { throw new ArgumentNullException(nameof(target)); } if (targetAccessor == null) { throw new ArgumentNullException(nameof(targetAccessor)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } if (propertyName == null) { throw new ArgumentNullException(nameof(propertyName)); } var binding = new CustomBinding <TValue> .AccessorPropertyBinding2(); binding.mode = mode; binding.target = target; binding.targetAccessor = targetAccessor; binding.source = source; binding.propertyName = propertyName; binding.accessor = accessor; binding.Bind(); return(binding); }
protected override void SyncPropertyToField(INotifyValueChanged <TValue> c, SerializedProperty p) { if (!propCompareValues(lastFieldValue, p, propGetValue)) { lastFieldValue = propGetValue(p); c.value = lastFieldValue; } }
public static bool UnregisterValueChangedCallback <T>(this INotifyValueChanged <T> control, EventCallback <ChangeEvent <T> > callback) { var handler = control as CallbackEventHandler; if (handler != null) { handler.UnregisterCallback(callback); return(true); } return(false); }
public VisualElementPropertyProxy(object target, IProxyPropertyInfo propertyInfo) : base(target, propertyInfo) { //不能所有的属性都自动绑定,可能绑定的其他值,与事件不匹配的值 if (target is INotifyValueChanged <TValue> ) { this.notifyValueChanged = (INotifyValueChanged <TValue>)target; } else { notifyValueChanged = null; } }
public static void CreateBind(INotifyValueChanged <TValue> field, SerializedObjectUpdateWrapper objWrapper, SerializedProperty property, Func <SerializedProperty, TValue> propGetValue, Action <SerializedProperty, TValue> propSetValue, Func <TValue, SerializedProperty, Func <SerializedProperty, TValue>, bool> propCompareValues) { var newBinding = s_Pool.Get(); newBinding.isReleased = false; newBinding.SetBinding(field, objWrapper, property, propGetValue, propSetValue, propCompareValues); }
public static void CreateBind <ValueType>(INotifyValueChanged <ValueType> field, SerializedProperty property, Func <SerializedProperty, ValueType> getter, Action <SerializedProperty, ValueType> setter, Func <ValueType, SerializedProperty, Func <SerializedProperty, ValueType>, bool> comparer) { if (!_createBindMethods.TryGetValue(typeof(ValueType), out var createBindMethod)) { var serializedObjectBindingType = _serializedObjectBindingType?.MakeGenericType(typeof(ValueType)); createBindMethod = serializedObjectBindingType?.GetMethod(_createBindName, BindingFlags.Public | BindingFlags.Static); _createBindMethods.Add(typeof(ValueType), createBindMethod); } var wrapper = Activator.CreateInstance(_serializedObjectUpdateWrapperType, property.serializedObject); createBindMethod.Invoke(null, new object[] { field, wrapper, property, getter, setter, comparer }); }
// TODO: See if we can use `this INotifyValueChanged<T> ve` without `element` argument public static void InitField <T>(this VisualElement ve, INotifyValueChanged <T> element, EventCallback <ChangeEvent <T> > onValueChange, Func <T> getInitialValue) { element.RegisterValueChangedCallback(onValueChange); ve.RegisterCallback <AttachToPanelEvent>(InitValue); void InitValue(AttachToPanelEvent e) { element.value = getInitialValue(); ve.UnregisterCallback <AttachToPanelEvent>(InitValue); } }
public static void DefaultEnumBind(INotifyValueChanged <Enum> field, SerializedProperty property) { // 2019.3 only supports flags on EnumFlagsField specifically var type = field.value.GetType(); var wrapper = Activator.CreateInstance(_serializedObjectUpdateWrapperType, property.serializedObject); Func <SerializedProperty, Enum> getter = p => Enum.ToObject(type, p.intValue) as Enum; Action <SerializedProperty, Enum> setter = (p, v) => p.intValue = (int)Enum.Parse(type, v.ToString()); Func <Enum, SerializedProperty, Func <SerializedProperty, Enum>, bool> comparer = (v, p, g) => g(p).Equals(v); _defaultBindEnumMethod.Invoke(null, new object[] { field, wrapper, property, getter, setter, comparer }); }
private void RegistEvent <T>(INotifyValueChanged <T> notify, FieldInfo fieldInfo) { #if UNITY_2019_1_OR_NEWER || UNITY_2019_OR_NEWER notify.RegisterValueChangedCallback((val) => { fieldInfo.SetValue(this.target, val.newValue); this.onDirty?.Invoke(); }); #else notify.OnValueChanged((val) => { fieldInfo.SetValue(this.target, val.newValue); this.onDirty?.Invoke(); }); #endif }
public static bool RegisterValueChangedCallback <T>(this INotifyValueChanged <T> control, EventCallback <ChangeEvent <T> > callback) { CallbackEventHandler callbackEventHandler = control as CallbackEventHandler; bool flag = callbackEventHandler != null; bool result; if (flag) { callbackEventHandler.RegisterCallback <ChangeEvent <T> >(callback, TrickleDown.NoTrickleDown); result = true; } else { result = false; } return(result); }
private void SetBinding(INotifyValueChanged <TValue> c, SerializedObjectUpdateWrapper objWrapper, SerializedProperty property, Func <SerializedProperty, TValue> getValue, Action <SerializedProperty, TValue> setValue, Func <TValue, SerializedProperty, Func <SerializedProperty, TValue>, bool> compareValues) { this.field = c; property.unsafeMode = true; this.boundPropertyPath = property.propertyPath; this.boundObject = objWrapper; this.boundProperty = property; this.propGetValue = getValue; this.propSetValue = setValue; this.propCompareValues = compareValues; this.lastFieldValue = c.value; Update(); }
public static IDisposable Bind <TValue>(this VisualElement target, INotifyValueChanged <TValue> targetAccessor, object source, string path, BindingMode mode = BindingMode.TwoWay) { if (target == null) { throw new ArgumentNullException(nameof(target)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } if (path == null) { throw new ArgumentNullException(nameof(path)); } Binding <TValue> binding = new Binding <TValue>(target, source, path); binding.TargetAccessor = targetAccessor; binding.Bind(); return(binding); }
static INotifyValueChanged <F> CreateFieldS <F>(string _label, F _value, Type _realFieldType, Action <object> _onValueChanged) { INotifyValueChanged <F> fieldDrawer = null; // 如果没有Drawer创建方法,返回空 if (!fieldDrawerCreatorMap.TryGetValue(typeof(F), out Func <Type, BindableElement> drawerCreator)) { return(null); } // 创建Drawer,设置初始值,注册onValueChanged方法 fieldDrawer = drawerCreator(_realFieldType) as INotifyValueChanged <F>; fieldDrawer.SetValueWithoutNotify(_value); fieldDrawer.RegisterValueChangedCallback((e) => { _onValueChanged(e.newValue); }); BaseField <F> tDrawer = fieldDrawer as BaseField <F>; tDrawer.label = _label; return(fieldDrawer); }
public ValueChangedEventProxy(INotifyValueChanged <T> target) : base(target) { this.target = target; this.BindEvent(); }
public static void InitField <T>(this VisualElement ve, INotifyValueChanged <T> element, EventCallback <ChangeEvent <T> > onValueChange, T initialValue) { InitField(ve, element, onValueChange, () => initialValue); }
public static IDisposable Bind <TValue>(this VisualElement target, object source, string propertyName, INotifyValueChanged <TValue> accessor, BindingMode mode = BindingMode.TwoWay) { if (target == null) { throw new ArgumentNullException(nameof(target)); } var targetAccessor = target as INotifyValueChanged <TValue>; if (targetAccessor == null) { throw new Exception("target not INotifyValueChanged<T>"); } return(Bind(target, targetAccessor, source, propertyName, accessor, mode)); }
public static IObservable <T> OnValueChanged <T>(this INotifyValueChanged <T> source) { return(source.OnValueChange().Select(x => x.newValue)); }
public static void BindManagedReference <ReferenceType>(INotifyValueChanged <ReferenceType> field, SerializedProperty property, Action onSet) { CreateBind(field, property, GetManagedReference <ReferenceType>, (p, v) => { SetManagedReference(p, v); onSet?.Invoke(); }, CompareManagedReferences); }
public static void RegisterValueChangedCallback <T>(this INotifyValueChanged <T> control, EventCallback <ChangeEvent <T> > callback) { control.OnValueChanged(callback); }