private void SetValue(T newValue) { T oldValue = _settingVariable; _settingVariable = newValue; ValueChanged.SafeInvoke(this, new ValueChangedArg <T>(_name, oldValue, _settingVariable)); }
private void OnValueChanged() { if (_textChangingIsInProgress) { return; } ValueChanged.SafeInvoke(this); UpdateText(); }
private void UpdateBinding(BindingParty source, BindingParty target, bool useConvertBack) { if (_isUpdatingBinding) { return; } if (!EnsureBindingLifetime()) { Uninitialize(); return; } try { _isUpdatingBinding = true; Log.Debug("Updating binding '{0}' => '{1}'", source, target); var newValue = source.GetPropertyValue(); var converter = Converter; if (converter != null) { if (useConvertBack) { newValue = converter.ConvertBack(newValue, typeof(object), ConverterParameter, CultureInfo.CurrentCulture); } else { newValue = converter.Convert(newValue, typeof(object), ConverterParameter, CultureInfo.CurrentCulture); } } if (ReferenceEquals(newValue, ConverterHelper.UnsetValue)) { Log.Debug("Skipping update because new value is 'ConverterHelper.UnsetValue'"); return; } target.SetPropertyValue(newValue); ValueChanged.SafeInvoke(this); } catch (Exception ex) { Log.Error(ex, "Failed to update binding"); } finally { _isUpdatingBinding = false; } }
/// <summary> /// Sets the option's value. /// </summary> /// <remarks> /// Does nothing when the value type differs or when the value matches the current value. /// </remarks> /// <param name="value">The new value</param> /// <param name="raiseEvents">Whether or not to raise events</param> protected virtual void SetValue(object value, bool raiseEvents) { if (value?.GetType() != Value?.GetType() || Value == value) { return; // Refuse value updates that don't match the option type. } if (raiseEvents && OnValueChanged != null && AmongUsClient.Instance?.AmHost == true && PlayerControl.LocalPlayer) { object lastValue = value; OptionOnValueChangedEventArgs args = OnValueChangedEventArgs(value, Value); foreach (EventHandler <OptionOnValueChangedEventArgs> handler in OnValueChanged.GetInvocationList()) { handler(this, args); if (args.Value.GetType() != value.GetType()) { args.Value = lastValue; args.Cancel = false; EssentialsPlugin.Logger.LogWarning($"A handler for option \"{Name}\" attempted to change value type, ignored."); } lastValue = args.Value; if (args.Cancel) { return; // Handler cancelled value change. } } value = args.Value; } if (OldValue != Value) { OldValue = Value; } Value = value; //if (SendRpc && GameSetting != null && AmongUsClient.Instance?.AmHost == true && PlayerControl.LocalPlayer) Rpc.Send(new (string, CustomOptionType, object)[] { this }); if (SendRpc && GameObject != null && AmongUsClient.Instance?.AmHost == true && PlayerControl.LocalPlayer) { Rpc.Instance.Send(this); } UpdateGameObject(); if (raiseEvents) { ValueChanged?.SafeInvoke(this, ValueChangedEventArgs(value, Value), nameof(ValueChanged)); } if (GameObject == null) { return; // Game object does not exist, menu is closed } // Refresh the value of all options in the menu, in case an option affects another. try { GameOptionsMenu optionsMenu = Object.FindObjectOfType <GameOptionsMenu>(); if (optionsMenu == null) { return; } for (int i = 0; i < optionsMenu.Children.Length; i++) { OptionBehaviour optionBehaviour = optionsMenu.Children[i]; optionBehaviour.enabled = false; optionBehaviour.enabled = true; } } catch { } }
/// <summary> /// Вызвать событие <see cref="ValueChanged"/>. /// </summary> private void RaiseValueChanged() { ValueChanged.SafeInvoke(); this.Notify("Value"); }
protected void RaiseValueChanged(T value) { ValueChanged.SafeInvoke(value); }
/// <summary> /// Raises the <see cref="ValueChanged"/> event. /// </summary> private void RaiseValueChanged() { ValueChanged.SafeInvoke(this); }
/// <summary> /// Sets the option's value, it's not recommended to call this directly, call derivatives instead. /// </summary> /// <remarks> /// Does nothing when the value type differs or when the value matches the current value. /// </remarks> /// <param name="value">The new value</param> /// <param name="raiseEvents">Whether or not to raise events</param> private protected void SetValue(object value, bool raiseEvents) { if (value?.GetType() != Value?.GetType() || Value == value) { return; // Refuse value updates that don't match the option type } if (raiseEvents && OnValueChanged != null && AmongUsClient.Instance && PlayerControl.LocalPlayer && AmongUsClient.Instance.AmHost) { object lastValue = value; OptionOnValueChangedEventArgs args = OnValueChangedEventArgs(value, Value); foreach (EventHandler <OptionOnValueChangedEventArgs> handler in OnValueChanged.GetInvocationList()) { handler(this, args); if (args.Value.GetType() != value.GetType()) { args.Value = lastValue; args.Cancel = false; EssentialsPlugin.Logger.LogWarning($"A handler for option \"{Name}\" attempted to change value type, ignored."); } lastValue = args.Value; if (args.Cancel) { return; // Handler cancelled value change. } } value = args.Value; } if (OldValue != Value) { OldValue = Value; } Value = value; if (GameSetting != null && AmongUsClient.Instance && PlayerControl.LocalPlayer && AmongUsClient.Instance.AmHost) { Rpc.Instance.Send(new Rpc.Data(this)); } try { if (GameSetting is ToggleOption toggle) { bool newValue = (bool)Value; toggle.oldValue = newValue; if (toggle.CheckMark != null) { toggle.CheckMark.enabled = newValue; } } else if (GameSetting is NumberOption number) { float newValue = (float)Value; number.Value = number.Field_3 = newValue; number.ValueText.Text = ToString(); } else if (GameSetting is StringOption str) { int newValue = (int)Value; str.Value = str.oldValue = newValue; str.ValueText.Text = ToString(); } else if (GameSetting is KeyValueOption kv) { int newValue = (int)Value; kv.Selected = kv.oldValue = newValue; kv.ValueText.Text = ToString(); } } catch (Exception e) { EssentialsPlugin.Logger.LogWarning($"Failed to update game setting value for option \"{Name}\": {e}"); } if (raiseEvents) { ValueChanged?.SafeInvoke(this, ValueChangedEventArgs(value, Value)); } /*{ * OptionValueChangedEventArgs args = ValueChangedEventArgs(value, Value); * foreach (EventHandler<OptionValueChangedEventArgs> handler in ValueChanged.GetInvocationList()) handler(this, args); * }*/ try { if (GameSetting != null) { Object.FindObjectOfType <GameOptionsMenu>()?.Method_16(); // RefreshChildren(); } } catch { } }