feat: improve on item search window, async loading, choose multiple

This commit is contained in:
goat 2020-04-20 12:57:46 +02:00
parent ff64c01643
commit a7f5129926
2 changed files with 70 additions and 42 deletions

View file

@ -553,7 +553,7 @@ namespace Dalamud {
private bool isImguiDrawItemSearchWindow; private bool isImguiDrawItemSearchWindow;
private void OnItemLinkCommand(string command, string arguments) { private void OnItemLinkCommand(string command, string arguments) {
this.itemSearchCommandWindow = new ItemSearchWindow(this.Data, new UiBuilder(this.InterfaceManager, "ItemSearcher")); this.itemSearchCommandWindow = new ItemSearchWindow(this.Data, new UiBuilder(this.InterfaceManager, "ItemSearcher"), false);
this.itemSearchCommandWindow.OnItemChosen += (sender, item) => { this.itemSearchCommandWindow.OnItemChosen += (sender, item) => {
var hexData = new byte[] { var hexData = new byte[] {
0x02, 0x13, 0x06, 0xFE, 0xFF, 0xF3, 0xF3, 0xF3, 0x03, 0x02, 0x27, 0x07, 0x03, 0xF2, 0x3A, 0x2F, 0x02, 0x13, 0x06, 0xFE, 0xFF, 0xF3, 0xF3, 0xF3, 0x03, 0x02, 0x27, 0x07, 0x03, 0xF2, 0x3A, 0x2F,

View file

@ -24,6 +24,7 @@ namespace Dalamud.Interface
{ {
private readonly DataManager data; private readonly DataManager data;
private readonly UiBuilder builder; private readonly UiBuilder builder;
private readonly bool closeOnChoose;
private string lastSearchText = string.Empty; private string lastSearchText = string.Empty;
private string searchText = string.Empty; private string searchText = string.Empty;
@ -40,14 +41,16 @@ namespace Dalamud.Interface
public event EventHandler<Item> OnItemChosen; public event EventHandler<Item> OnItemChosen;
public ItemSearchWindow(DataManager data, UiBuilder builder) { public ItemSearchWindow(DataManager data, UiBuilder builder, bool closeOnChoose = true) {
this.data = data; this.data = data;
this.builder = builder; this.builder = builder;
this.closeOnChoose = closeOnChoose;
while (!data.IsDataReady) while (!data.IsDataReady)
Thread.Sleep(1); Thread.Sleep(1);
this.luminaItems = this.data.GetExcelSheet<Item>().GetRows();
Task.Run(() => this.data.GetExcelSheet<Item>().GetRows()).ContinueWith(t => this.luminaItems = t.Result);
} }
public bool Draw() { public bool Draw() {
@ -95,8 +98,11 @@ namespace Dalamud.Interface
ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0, 0)); ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0, 0));
if (!string.IsNullOrEmpty(this.searchText) || this.currentKind != 0) { if (this.luminaItems != null) {
if (this.lastSearchText != this.searchText || this.lastKind != this.currentKind) { if (!string.IsNullOrEmpty(this.searchText) || this.currentKind != 0)
{
if (this.lastSearchText != this.searchText || this.lastKind != this.currentKind)
{
this.lastSearchText = this.searchText; this.lastSearchText = this.searchText;
this.lastKind = this.currentKind; this.lastKind = this.currentKind;
@ -106,7 +112,8 @@ namespace Dalamud.Interface
var asyncEnum = this.luminaItems.ToAsyncEnumerable(); var asyncEnum = this.luminaItems.ToAsyncEnumerable();
if (!string.IsNullOrEmpty(this.searchText)) { if (!string.IsNullOrEmpty(this.searchText))
{
Log.Debug("Searching for " + this.searchText); Log.Debug("Searching for " + this.searchText);
asyncEnum = asyncEnum.Where( asyncEnum = asyncEnum.Where(
x => (x.Name.ToLower().Contains(this.searchText.ToLower()) || x => (x.Name.ToLower().Contains(this.searchText.ToLower()) ||
@ -114,7 +121,8 @@ namespace Dalamud.Interface
parsedId == x.RowId)); parsedId == x.RowId));
} }
if (this.currentKind != 0) { if (this.currentKind != 0)
{
Log.Debug("Searching for C" + this.currentKind); Log.Debug("Searching for C" + this.currentKind);
asyncEnum = asyncEnum.Where(x => x.ItemSearchCategory == this.currentKind); asyncEnum = asyncEnum.Where(x => x.ItemSearchCategory == this.currentKind);
} }
@ -126,9 +134,12 @@ namespace Dalamud.Interface
this.searchTask = asyncEnum.ToListAsync(this.searchCancelTokenSource.Token); this.searchTask = asyncEnum.ToListAsync(this.searchCancelTokenSource.Token);
} }
if (this.searchTask.IsCompletedSuccessfully) { if (this.searchTask.IsCompletedSuccessfully)
for (var i = 0; i < this.searchTask.Result.Count; i++) { {
if (ImGui.Selectable(this.searchTask.Result[i].Name, this.selectedItemIndex == i)) { for (var i = 0; i < this.searchTask.Result.Count; i++)
{
if (ImGui.Selectable(this.searchTask.Result[i].Name, this.selectedItemIndex == i))
{
this.selectedItemIndex = i; this.selectedItemIndex = i;
var iconTex = this.data.GetIcon(this.searchTask.Result[i].Icon); var iconTex = this.data.GetIcon(this.searchTask.Result[i].Icon);
@ -139,13 +150,18 @@ namespace Dalamud.Interface
} }
} }
} }
} else { }
else
{
ImGui.TextColored(new Vector4(0.86f, 0.86f, 0.86f, 1.00f), Loc.Localize("DalamudItemSelectHint", "Type to start searching...")); ImGui.TextColored(new Vector4(0.86f, 0.86f, 0.86f, 1.00f), Loc.Localize("DalamudItemSelectHint", "Type to start searching..."));
this.selectedItemIndex = -1; this.selectedItemIndex = -1;
this.selectedItemTex?.Dispose(); this.selectedItemTex?.Dispose();
this.selectedItemTex = null; this.selectedItemTex = null;
} }
} else {
ImGui.TextColored(new Vector4(0.86f, 0.86f, 0.86f, 1.00f), Loc.Localize("DalamudItemSelectLoading", "Loading item list..."));
}
ImGui.PopStyleVar(); ImGui.PopStyleVar();
@ -153,9 +169,21 @@ namespace Dalamud.Interface
if (ImGui.Button(Loc.Localize("Choose", "Choose"))) { if (ImGui.Button(Loc.Localize("Choose", "Choose"))) {
OnItemChosen?.Invoke(this, this.searchTask.Result[this.selectedItemIndex]); OnItemChosen?.Invoke(this, this.searchTask.Result[this.selectedItemIndex]);
if (this.closeOnChoose) {
this.selectedItemTex?.Dispose(); this.selectedItemTex?.Dispose();
isOpen = false; isOpen = false;
} }
}
if (!this.closeOnChoose) {
ImGui.SameLine();
if (ImGui.Button(Loc.Localize("Close", "Close")))
{
this.selectedItemTex?.Dispose();
isOpen = false;
}
}
ImGui.End(); ImGui.End();