/// <inheritdoc /> /// <summary> /// </summary> /// <param name="gameTime"></param> public override void Update(GameTime gameTime) { if (WaitingForKeyPress && Dialog.IsOnTop) { var keys = KeyboardManager.CurrentState.GetPressedKeys(); if (keys.Length != 0) { if (KeyboardManager.IsUniqueKeyPress(keys.First())) { Bindable.Value = keys.First(); var key = XnaKeyHelper.GetStringFromKey(keys.First()); if (Button.Text.Text != key) Button.Text.Text = key; if (keys.First() == Keys.Escape) Dialog.PreventExitOnEscapeKeybindPress = true; Button.Selected = false; WaitingForKeyPress = false; } } } else { // Important. Makes it so the dialog doesn't close when the user presses escape as a keybind. Dialog.PreventExitOnEscapeKeybindPress = false; } base.Update(gameTime); }
/// <inheritdoc /> /// <summary> /// </summary> /// <param name="key"></param> public SettingsKeybindSprite(Bindable <Keys> key) { Key = key; Image = UserInterface.BlankBox; Tint = Color.Transparent; AddBorder(Color.White, 2); Size = new ScalableVector2(54, 54); KeyText = new SpriteText(Fonts.Exo2Regular, XnaKeyHelper.GetStringFromKey(Key.Value), 13) { Parent = this, Alignment = Alignment.MidCenter }; Key.ValueChanged += OnKeybindChanged; }
/// <inheritdoc /> /// <summary> /// </summary> /// <param name="dialog"></param> /// <param name="name"></param> /// <param name="bindable"></param> public SettingsKeybind(SettingsDialog dialog, string name, Bindable <Keys> bindable) : base(dialog, name) { Dialog = dialog; Bindable = bindable; Button = new SelectableBorderedTextButton(XnaKeyHelper.GetStringFromKey(bindable.Value), Color.White, Colors.MainAccent, false) { Parent = this, Alignment = Alignment.MidRight, X = -20, UsePreviousSpriteBatchOptions = true, Border = { UsePreviousSpriteBatchOptions = true }, Text = { UsePreviousSpriteBatchOptions = true } }; Button.Height -= 6; Button.Clicked += (o, e) => { Button.Text.Text = "Press Key"; Button.Selected = true; WaitingForKeyPress = true; }; Button.ClickedOutside += (o, e) => { if (!dialog.IsOnTop) { return; } var key = XnaKeyHelper.GetStringFromKey(Bindable.Value); if (Button.Text.Text != key) { Button.Text.Text = key; } Button.Selected = false; WaitingForKeyPress = false; }; }
/// <summary> /// Updates the text when the key is changed. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void OnKeybindChanged(object sender, BindableValueChangedEventArgs <Keys> args) => KeyText.Text = XnaKeyHelper.GetStringFromKey(args.Value);