diff --git a/src/MeoAsstGui/Helper/AsstProxy.cs b/src/MeoAsstGui/Helper/AsstProxy.cs index 1cd9d1e793..f4a585aeaa 100644 --- a/src/MeoAsstGui/Helper/AsstProxy.cs +++ b/src/MeoAsstGui/Helper/AsstProxy.cs @@ -1062,7 +1062,7 @@ namespace MeoAsstGui task_params["stone"] = max_stone; task_params["times"] = max_times; task_params["report_to_penguin"] = true; - if (drops_item_quantity != 0) + if (drops_item_quantity != 0 && !string.IsNullOrWhiteSpace(drops_item_id)) { task_params["drops"] = new JObject(); task_params["drops"][drops_item_id] = drops_item_quantity; diff --git a/src/MeoAsstGui/Helper/CombData.cs b/src/MeoAsstGui/Helper/CombData.cs index 563b10606e..d06aed8d97 100644 --- a/src/MeoAsstGui/Helper/CombData.cs +++ b/src/MeoAsstGui/Helper/CombData.cs @@ -18,5 +18,7 @@ namespace MeoAsstGui /// public class CombData : GenericCombData { + /// + public override string ToString() => Display; } } diff --git a/src/MeoAsstGui/Helper/ComboBoxExtensions.cs b/src/MeoAsstGui/Helper/ComboBoxExtensions.cs new file mode 100644 index 0000000000..28c94583b6 --- /dev/null +++ b/src/MeoAsstGui/Helper/ComboBoxExtensions.cs @@ -0,0 +1,111 @@ +using System.Windows.Controls; +using System.Windows.Input; + +namespace MeoAsstGui.Helper +{ + /// + /// Extensions + /// + public static class ComboBoxExtensions + { + private const string InputTag = "TextInput"; + private const string SelectionTag = "Selection"; + + /// + /// Make searchable + /// + /// Target + public static void MakeComboBoxSearchable(this ComboBox targetComboBox) + { + var targetTextBox = targetComboBox?.Template.FindName("PART_EditableTextBox", targetComboBox) as TextBox; + if (targetTextBox == null) + { + return; + } + + targetComboBox.Items.IsLiveFiltering = true; + targetComboBox.StaysOpenOnEdit = true; + targetComboBox.IsEditable = true; + targetComboBox.IsTextSearchEnabled = false; + + // enter input mode by default + targetComboBox.Tag = InputTag; + + targetTextBox.PreviewKeyDown += (se, ev) => + { + if (ev.Key == Key.Enter || ev.Key == Key.Return || ev.Key == Key.Tab) + { + return; + } + + // switch to input mode + targetComboBox.Tag = InputTag; + + // reset selection + if (targetComboBox.SelectedItem != null) + { + targetComboBox.SelectedItem = null; + targetComboBox.Text = string.Empty; + } + }; + + targetTextBox.TextChanged += (o, args) => + { + if (targetComboBox.Tag is SelectionTag) + { + // text changed in selection mode, switch to input mode + targetComboBox.Tag = InputTag; + } + else + { + // input mode + var searchTerm = targetTextBox.Text; + + // reset selection + if (targetComboBox.SelectionBoxItem != null) + { + targetComboBox.SelectedItem = null; + targetTextBox.Text = searchTerm; + targetTextBox.SelectionStart = targetTextBox.Text.Length; + } + + // filter items + if (string.IsNullOrEmpty(searchTerm)) + { + targetComboBox.Items.Filter = item => true; + targetComboBox.SelectedItem = default; + } + else + { + targetComboBox.Items.Filter = item => item.ToString().Contains(searchTerm); + } + + targetTextBox.SelectionStart = targetTextBox.Text.Length; + } + + targetComboBox.IsDropDownOpen = true; + }; + + targetComboBox.SelectionChanged += (o, args) => + { + // selection changed, switch to selection mode + if (targetComboBox.SelectedItem != null) + { + targetComboBox.Tag = SelectionTag; + } + }; + + targetComboBox.LostFocus += (o, args) => + { + // reset text to the corresponding display text of selected item + var text = targetComboBox.SelectedItem?.ToString(); + if (targetComboBox.Text != text) + { + targetComboBox.Tag = SelectionTag; + targetComboBox.Text = text; + targetComboBox.Items.Filter = item => true; + } + }; + } + } +} diff --git a/src/MeoAsstGui/MeoAsstGui.csproj b/src/MeoAsstGui/MeoAsstGui.csproj index 7590066a14..8652c23353 100644 --- a/src/MeoAsstGui/MeoAsstGui.csproj +++ b/src/MeoAsstGui/MeoAsstGui.csproj @@ -108,6 +108,7 @@ + diff --git a/src/MeoAsstGui/UserControl/FightSettingsUserControl.xaml b/src/MeoAsstGui/UserControl/FightSettingsUserControl.xaml index 74522e6298..af14224fbd 100644 --- a/src/MeoAsstGui/UserControl/FightSettingsUserControl.xaml +++ b/src/MeoAsstGui/UserControl/FightSettingsUserControl.xaml @@ -110,7 +110,8 @@ DisplayMemberPath="Display" IsDropDownOpen="{Binding IsDropDown}" IsEditable="True" - IsTextSearchEnabled="True" + IsTextSearchEnabled="False" + Loaded="{s:Action DropsList_Loaded}" ItemsSource="{Binding DropsList}" SelectedValue="{Binding DropsItemId}" SelectedValuePath="Value" diff --git a/src/MeoAsstGui/ViewModels/TaskQueueViewModel.cs b/src/MeoAsstGui/ViewModels/TaskQueueViewModel.cs index 5dbd2ba288..87857df4bd 100644 --- a/src/MeoAsstGui/ViewModels/TaskQueueViewModel.cs +++ b/src/MeoAsstGui/ViewModels/TaskQueueViewModel.cs @@ -22,6 +22,8 @@ using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; +using System.Windows.Controls; +using MeoAsstGui.Helper; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Stylet; @@ -1487,6 +1489,8 @@ namespace MeoAsstGui } } + #region Drops + private bool _isSpecifiedDrops = Convert.ToBoolean(ViewStatusStorage.Get("MainFunction.Drops.Enable", bool.FalseString)); /// @@ -1552,7 +1556,7 @@ namespace MeoAsstGui /// public ObservableCollection DropsList { get; set; } - private string _dropsItemId = ViewStatusStorage.Get("MainFunction.Drops.ItemId", "0"); + private string _dropsItemId = ViewStatusStorage.Get("MainFunction.Drops.ItemId", string.Empty); /// /// Gets or sets the item ID of drops. @@ -1562,11 +1566,6 @@ namespace MeoAsstGui get => _dropsItemId; set { - if (value == null) - { - return; - } - SetAndNotify(ref _dropsItemId, value); ViewStatusStorage.Set("MainFunction.Drops.ItemId", DropsItemId); } @@ -1599,15 +1598,6 @@ namespace MeoAsstGui } _preSetDropsItemTicks = DateTime.Now.Ticks; - DropsList.Clear(); - foreach (CombData drop in AllDrops) - { - var enumStr = drop.Display; - if (enumStr.Contains(value)) - { - DropsList.Add(drop); - } - } SetAndNotify(ref _dropsItem, value); } @@ -1638,5 +1628,17 @@ namespace MeoAsstGui ViewStatusStorage.Set("MainFunction.Drops.Quantity", DropsQuantity); } } + + /// + /// DropsList ComboBox loaded + /// + /// Event sender + /// Event args + public void DropsList_Loaded(object sender, EventArgs e) + { + (sender as ComboBox)?.MakeComboBoxSearchable(); + } + + #endregion Drops } }