-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropdownBoxUtility.cs
120 lines (109 loc) · 4.27 KB
/
DropdownBoxUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using ImGuiNET;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
namespace Triggered
{
public class DropdownBoxUtility
{
private static string input = string.Empty;
private static List<string> filteredItems = new List<string>();
private static string[] listNames = {
"Pseudo",
"Explicit",
"Implicit",
"Fractured",
"Enchant",
"Scourge",
"Crafted",
"Crucible",
"Veiled",
"Monster",
"Delve",
"Ultimatum",
};
private static int selectedListIndex = 0;
private static JObject jsonData; // JObject to store the parsed JSON data
public static void DrawDropdownBox()
{
bool isInputTextEnterPressed = ImGui.InputText("##input", ref input, 32, ImGuiInputTextFlags.EnterReturnsTrue);
var min = ImGui.GetItemRectMin();
var size = ImGui.GetItemRectSize();
bool isInputTextActivated = ImGui.IsItemActivated();
if (isInputTextActivated)
{
ImGui.SetNextWindowPos(new Vector2(min.X, min.Y));
ImGui.OpenPopup("##popup");
}
if (ImGui.BeginPopup("##popup", ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoSavedSettings))
{
if (isInputTextActivated)
ImGui.SetKeyboardFocusHere(0);
ImGui.InputText("##input_popup", ref input, 32);
ImGui.SameLine();
ImGui.Combo("##listCombo", ref selectedListIndex, listNames, listNames.Length);
filteredItems.Clear();
// Select items based on the selected list index
string[] selectedItems = GetSelectedListItems(selectedListIndex);
if (string.IsNullOrEmpty(input))
foreach (string item in selectedItems)
filteredItems.Add(item);
else
{
var parts = input.Split(" ", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
foreach (string str in selectedItems)
{
bool allPartsMatch = true;
foreach (string part in parts)
{
if (!str.Contains(part,StringComparison.OrdinalIgnoreCase))
{
allPartsMatch = false;
break;
}
}
if (allPartsMatch)
filteredItems.Add(str);
}
}
ImGui.BeginChild("scrolling_region", new Vector2(size.X * 2, size.Y * 10), false, ImGuiWindowFlags.HorizontalScrollbar);
foreach (string item in filteredItems)
{
if (ImGui.Selectable(item))
{
App.Log("Selected popup");
input = item;
ImGui.CloseCurrentPopup();
break;
}
}
ImGui.EndChild();
if (isInputTextEnterPressed || ImGui.IsKeyPressed(ImGui.GetKeyIndex(ImGuiKey.Escape)))
{
App.Log("Closing popup");
ImGui.CloseCurrentPopup();
}
ImGui.EndPopup();
}
}
private static string[] GetSelectedListItems(int index)
{
// Load JSON data if not already loaded
if (jsonData == null)
LoadJsonData();
return jsonData["result"][index]["entries"]
.Select(entry => entry["text"]?.ToString())
.ToArray();
}
private static void LoadJsonData()
{
// Load the JSON file into a string
string jsonString = File.ReadAllText("stats.json");
// Parse the JSON data into a JObject
jsonData = JObject.Parse(jsonString);
}
}
}