Skip to content

Commit e869c11

Browse files
committed
Merge branch 'feature_filter'
2 parents 581374d + 59f84e5 commit e869c11

File tree

2 files changed

+144
-39
lines changed

2 files changed

+144
-39
lines changed

Helpers/EnchantmentsHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ namespace OutwardEnchanter.Helpers
1111
{
1212
public class EnchantmentsHelper
1313
{
14+
public static bool ContainsIgnoreCase(string source, string toCheck)
15+
{
16+
return source?.IndexOf(toCheck, StringComparison.OrdinalIgnoreCase) >= 0;
17+
}
18+
1419
public static List<EnchantmentRecipe> GetAvailableEnchantmentRecipies(Item item)
1520
{
1621
List<EnchantmentRecipe> enchantmentRecipes = RecipeManager.Instance.GetEnchantmentRecipes();

Managers/GUIMainCanvasManager.cs

Lines changed: 139 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using OutwardEnchanter.Helpers;
1+
using Mono.Cecil;
2+
using OutwardEnchanter.Helpers;
23
using SideLoader.Helpers;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
7+
using System.Reflection;
68
using UnityEngine;
79
using UnityEngine.UI;
810

@@ -26,6 +28,9 @@ public class GUIMainCanvasManager : MonoBehaviour
2628
private Dropdown _chooseItemDropdown;
2729
private Dropdown _chooseEnchantmentDropdown;
2830

31+
private InputField _itemFilterInput;
32+
private InputField _enchantmentFilterInput;
33+
2934
private GUIMainCanvasManager()
3035
{
3136
try
@@ -63,6 +68,9 @@ public static GUIMainCanvasManager Instance
6368
public Dictionary<string, Equipment> EquipmentDictionary { get => _equipmentDictionary; set => _equipmentDictionary = value; }
6469
public Dictionary<string, EnchantmentRecipe> EnchantmentRecipeDictionary { get => _enchantmentRecipeDictionary; set => _enchantmentRecipeDictionary = value; }
6570

71+
public InputField EnchantmentFilterInput { get => _enchantmentFilterInput; set => _enchantmentFilterInput = value; }
72+
public InputField ItemFilterInput { get => _itemFilterInput; set => _itemFilterInput = value; }
73+
6674
public void Init()
6775
{
6876
string mainPanelPath = "Background-Panel/Main-Panel/";
@@ -135,6 +143,38 @@ public void Init()
135143
{
136144
ResultAndLogMessage("Couldn't find Enchantments Dropdown component");
137145
}
146+
147+
ItemFilterInput = this.transform.Find(mainPanelPath + "Middle-Panel/ItemFilter-Panel/ItemFilter-Input")?.GetComponent<InputField>();
148+
149+
if(ItemFilterInput == null)
150+
{
151+
ResultAndLogMessage("Couldn't find Item Filter Input component");
152+
}
153+
else
154+
{
155+
ItemFilterInput.onEndEdit.AddListener(HandleOnItemFilterEnd);
156+
}
157+
158+
EnchantmentFilterInput = this.transform.Find(mainPanelPath + "Middle-Panel/EnchantmentFilter-Panel/EnchantmentFilter-Input")?.GetComponent<InputField>();
159+
160+
if(EnchantmentFilterInput == null)
161+
{
162+
ResultAndLogMessage("Couldn't find Enchantment Filter Input component");
163+
}
164+
else
165+
{
166+
EnchantmentFilterInput.onEndEdit.AddListener(HandleOnEnchantmentFilterEnd);
167+
}
168+
}
169+
170+
public void HandleOnItemFilterEnd(string text)
171+
{
172+
FilterItemsData(text);
173+
}
174+
175+
public void HandleOnEnchantmentFilterEnd(string text)
176+
{
177+
FilterEnchantmentsData(text);
138178
}
139179

140180
public void HandleOnEnchantButtonClick()
@@ -215,52 +255,114 @@ public void HandleOnChooseItemChange(int index)
215255
{
216256
try
217257
{
218-
string selectedValue = ChooseItemDropdown.options[index].text;
219-
if (!EquipmentDictionary.TryGetValue(selectedValue, out Equipment equipment))
258+
FilterEnchantmentsData(EnchantmentFilterInput.text);
259+
}
260+
catch(Exception ex)
261+
{
262+
OutwardEnchanter.LogMessage("GUIMainCanvasManager@HandleOnChooseItemChange error: " + ex.Message);
263+
}
264+
}
265+
266+
public string GetUniqueEnchantmentsName(EnchantmentRecipe recipe)
267+
{
268+
Enchantment enchantment = null;
269+
string keyName = "";
270+
271+
keyName = recipe.name;
272+
273+
if(keyName == "")
274+
{
275+
enchantment = ResourcesPrefabManager.Instance.GetEnchantmentPrefab(recipe.RecipeID);
276+
277+
if(enchantment == null)
220278
{
221-
return;
279+
return null;
222280
}
223281

224-
List<EnchantmentRecipe> availableRecipes = EnchantmentsHelper.GetAvailableEnchantmentRecipies(equipment);
225-
EnchantmentRecipeDictionary = new Dictionary<string, EnchantmentRecipe>();
226-
List<string> availableRecipesOptions = new List<string>();
227-
Enchantment enchantment = null;
228-
string keyName = "";
282+
keyName = enchantment.PresetID + "_" + enchantment.Name;
283+
}
229284

230-
foreach (EnchantmentRecipe recipe in availableRecipes)
231-
{
285+
//some modders included duplicate names
286+
if(EnchantmentRecipeDictionary.TryGetValue(keyName, out EnchantmentRecipe foundEnchantment))
287+
{
288+
keyName += "_" + Guid.NewGuid();
289+
}
232290

233-
keyName = recipe.name;
291+
return keyName;
292+
}
234293

235-
if(keyName == "")
236-
{
237-
enchantment = ResourcesPrefabManager.Instance.GetEnchantmentPrefab(recipe.RecipeID);
294+
public string GetUniqueEquipmentsName(Equipment equipment)
295+
{
296+
string keyName = "";
297+
298+
keyName = equipment.ItemID + "_" + equipment.Name.Replace(" ", "_");
238299

239-
if(enchantment == null)
240-
{
241-
continue;
242-
}
300+
//some modders included duplicate names
301+
if(EquipmentDictionary.TryGetValue(keyName, out Equipment foundEquipment))
302+
{
303+
keyName += "_" + Guid.NewGuid();
304+
}
243305

244-
keyName = enchantment.PresetID + "_" + enchantment.Name;
245-
}
306+
return keyName;
307+
}
246308

247-
//some modders included duplicate names
248-
if(EnchantmentRecipeDictionary.TryGetValue(keyName, out EnchantmentRecipe foundEnchantment))
249-
{
250-
keyName += "_" + Guid.NewGuid();
251-
}
309+
public void FilterItemsData(string filter)
310+
{
311+
List<string> dropdownOptions = new List<string>();
312+
string keyName = "";
313+
EquipmentDictionary = new Dictionary<string, Equipment>();
252314

253-
EnchantmentRecipeDictionary.Add(keyName, recipe);
254-
availableRecipesOptions.Add(keyName);
315+
foreach (Equipment equipment in AvailableEquipment)
316+
{
317+
keyName = GetUniqueEquipmentsName(equipment);
318+
319+
if (EnchantmentsHelper.ContainsIgnoreCase(keyName, filter))
320+
{
321+
dropdownOptions.Add(keyName);
322+
EquipmentDictionary.Add(keyName, equipment);
255323
}
324+
}
325+
326+
FillDropdownChoices(ChooseItemDropdown, dropdownOptions);
327+
328+
ChooseItemDropdown.value = 0;
329+
ChooseItemDropdown.RefreshShownValue();
330+
ChooseItemDropdown.onValueChanged.Invoke(ChooseItemDropdown.value);
331+
}
256332

257-
if (ChooseEnchantmentDropdown != null)
258-
FillDropdownChoices(ChooseEnchantmentDropdown, availableRecipesOptions);
333+
public void FilterEnchantmentsData(string filter)
334+
{
335+
if(ChooseItemDropdown.value < 0 || ChooseItemDropdown.value >= ChooseItemDropdown.options.Count)
336+
{
337+
ResultAndLogMessage($"Invalid dropdown selection: {ChooseItemDropdown.value}, options count: {ChooseItemDropdown.options.Count}");
338+
return;
259339
}
260-
catch(Exception ex)
340+
341+
string selectedValue = ChooseItemDropdown.options[ChooseItemDropdown.value].text;
342+
if (!EquipmentDictionary.TryGetValue(selectedValue, out Equipment equipment))
261343
{
262-
OutwardEnchanter.LogMessage("GUIMainCanvasManager@HandleOnChooseItemChange error: " + ex.Message);
344+
ResultAndLogMessage($"Item: {selectedValue} is not found! Tried to access: {ChooseItemDropdown.value}");
345+
return;
263346
}
347+
348+
List<EnchantmentRecipe> availableRecipes = EnchantmentsHelper.GetAvailableEnchantmentRecipies(equipment);
349+
EnchantmentRecipeDictionary = new Dictionary<string, EnchantmentRecipe>();
350+
List<string> availableRecipesOptions = new List<string>();
351+
string keyName = "";
352+
353+
foreach (EnchantmentRecipe recipe in availableRecipes)
354+
{
355+
keyName = GetUniqueEnchantmentsName(recipe);
356+
357+
if (keyName == null || !EnchantmentsHelper.ContainsIgnoreCase(keyName, filter))
358+
continue;
359+
360+
EnchantmentRecipeDictionary.Add(keyName, recipe);
361+
availableRecipesOptions.Add(keyName);
362+
}
363+
364+
if (ChooseEnchantmentDropdown != null)
365+
FillDropdownChoices(ChooseEnchantmentDropdown, availableRecipesOptions);
264366
}
265367

266368
public void FillItemsData()
@@ -278,16 +380,14 @@ public void FillItemsData()
278380
OutwardEnchanter.LogMessage("GUIMainCanvasManager@FillItemsData equipment is null!");
279381
#endif
280382

281-
keyName = equipment.ItemID + "_" + equipment.Name;
282-
283-
//some modders included duplicate names
284-
if(EquipmentDictionary.TryGetValue(keyName, out Equipment foundEquipment))
285-
{
286-
keyName += "_" + Guid.NewGuid();
287-
}
383+
keyName = GetUniqueEquipmentsName(equipment);
288384

289385
dropdownOptions.Add(keyName);
290386
EquipmentDictionary.Add(keyName, equipment);
387+
388+
#if DEBUG
389+
OutwardEnchanter.LogMessage($"GUIMainCanvasManager@FillItemsData equipment keyName added: {keyName}!");
390+
#endif
291391
}
292392

293393
#if DEBUG

0 commit comments

Comments
 (0)