From 6ca91ab7db46a01a333187813a3b5d4e265f7ac4 Mon Sep 17 00:00:00 2001
From: lastbattle <4586194+lastbattle@users.noreply.github.com>
Date: Sat, 14 Sep 2024 23:56:34 +0800
Subject: [PATCH] [HaCreator] Fix searchbox -- prior task is not cancelled as
the user types a new character
-- performance is dramatically faster now~!
---
HaCreator/CustomControls/MapBrowser.cs | 118 ++----------------
.../LoadItemSelector.Designer.cs | 1 -
.../GUI/InstanceEditor/LoadItemSelector.cs | 102 +--------------
.../LoadJobSelector.Designer.cs | 1 -
.../GUI/InstanceEditor/LoadJobSelector.cs | 102 +--------------
.../GUI/InstanceEditor/LoadMapSelector.cs | 6 +-
.../LoadNpcSelector.Designer.cs | 1 -
.../GUI/InstanceEditor/LoadNpcSelector.cs | 102 +--------------
.../LoadQuestSelector.Designer.cs | 1 -
.../GUI/InstanceEditor/LoadQuestSelector.cs | 102 +--------------
.../GUI/InstanceEditor/LoadSearchHelper.cs | 117 +++++++++++++++++
HaCreator/GUI/Load.cs | 6 +-
HaCreator/HaCreator.csproj | 1 +
13 files changed, 146 insertions(+), 514 deletions(-)
create mode 100644 HaCreator/GUI/InstanceEditor/LoadSearchHelper.cs
diff --git a/HaCreator/CustomControls/MapBrowser.cs b/HaCreator/CustomControls/MapBrowser.cs
index 55bb34a7..921ad7d2 100644
--- a/HaCreator/CustomControls/MapBrowser.cs
+++ b/HaCreator/CustomControls/MapBrowser.cs
@@ -18,6 +18,7 @@
using HaSharedLibrary.Wz;
using MapleLib.WzLib.WzStructure;
using System.Data.SQLite;
+using HaCreator.GUI.InstanceEditor;
namespace HaCreator.CustomControls
{
@@ -32,6 +33,13 @@ public partial class MapBrowser : UserControl
private bool _bTownOnlyFilter = false;
private bool _bIsHistoryMapBrowser = false;
+ private LoadSearchHelper _search;
+ public LoadSearchHelper Search
+ {
+ get { return _search; }
+ private set { }
+ }
+
///
/// Constructor
///
@@ -39,6 +47,8 @@ public MapBrowser()
{
InitializeComponent();
+ this._search = new LoadSearchHelper(mapNamesBox, maps);
+
this.minimapBox.SizeMode = PictureBoxSizeMode.Zoom;
}
@@ -232,114 +242,6 @@ public void ClearLoadedMapHistory() {
#endregion
#region UI
- private string _previousSeachText = string.Empty;
- private CancellationTokenSource _existingSearchTaskToken = null;
- ///
- /// On search box text changed
- ///
- ///
- /// May be null
- public void searchBox_TextChanged(object sender, EventArgs e)
- {
- TextBox searchBox = (TextBox)sender;
- string searchText = searchBox.Text.ToLower();
-
- if (_previousSeachText == searchText)
- return;
- _previousSeachText = searchText; // set
-
- // start searching
- searchMapsInternal(searchText);
- }
-
- ///
- /// Search and filters map according to the user's query
- ///
- ///
- public void searchMapsInternal(string searchText) {
- if (!_bMapsLoaded)
- return;
-
- // Cancel existing task if any
- if (_existingSearchTaskToken != null && !_existingSearchTaskToken.IsCancellationRequested) {
- _existingSearchTaskToken.Cancel();
- }
-
- // Clear
- mapNamesBox.Items.Clear();
- if (searchText == string.Empty) {
- var filteredItems = maps.Where(kvp => {
- MapInfo mapInfo = null;
- if (mapsMapInfo.ContainsKey(kvp)) {
- mapInfo = mapsMapInfo[kvp].Item2;
-
- if (this._bTownOnlyFilter) {
- if (!mapInfo.town)
- return false;
- }
- }
- return true;
- }).Select(kvp => kvp) // or kvp.Value or any transformation you need
- .Cast