Skip to content

Commit cf3810a

Browse files
committed
Add filter to texturedrawer.
1 parent af536b3 commit cf3810a

File tree

3 files changed

+77
-59
lines changed

3 files changed

+77
-59
lines changed

Penumbra/Import/Textures/TextureDrawer.cs

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.IO;
4+
using System.Linq;
35
using System.Numerics;
46
using Dalamud.Interface;
57
using ImGuiNET;
68
using Lumina.Data.Files;
79
using OtterGui;
810
using OtterGui.Raii;
11+
using OtterGui.Widgets;
912
using OtterTex;
13+
using Penumbra.Mods;
1014
using Penumbra.String.Classes;
1115
using Penumbra.UI;
1216
using Penumbra.UI.Classes;
@@ -33,41 +37,6 @@ public static void Draw(Texture texture, Vector2 size)
3337
}
3438
}
3539

36-
public static void PathSelectBox(TextureManager textures, Texture current, string label, string tooltip, IEnumerable<(string, bool)> paths,
37-
int skipPrefix)
38-
{
39-
ImGui.SetNextItemWidth(-0.0001f);
40-
var startPath = current.Path.Length > 0 ? current.Path : "Choose a modded texture from this mod here...";
41-
using var combo = ImRaii.Combo(label, startPath);
42-
if (combo)
43-
foreach (var ((path, game), idx) in paths.WithIndex())
44-
{
45-
if (game)
46-
{
47-
if (!textures.GameFileExists(path))
48-
continue;
49-
}
50-
else if (!File.Exists(path))
51-
{
52-
continue;
53-
}
54-
55-
using var id = ImRaii.PushId(idx);
56-
using (var color = ImRaii.PushColor(ImGuiCol.Text, ColorId.FolderExpanded.Value(), game))
57-
{
58-
var p = game ? $"--> {path}" : path[skipPrefix..];
59-
if (ImGui.Selectable(p, path == startPath) && path != startPath)
60-
current.Load(textures, path);
61-
}
62-
63-
ImGuiUtil.HoverTooltip(game
64-
? "This is a game path and refers to an unmanipulated file from your game data."
65-
: "This is a path to a modded file on your file system.");
66-
}
67-
68-
ImGuiUtil.HoverTooltip(tooltip);
69-
}
70-
7140
public static void PathInputBox(TextureManager textures, Texture current, ref string? tmpPath, string label, string hint, string tooltip,
7241
string startPath, FileDialogService fileDialog, string defaultModImportPath)
7342
{
@@ -136,4 +105,53 @@ private static void DrawData(Texture texture)
136105
break;
137106
}
138107
}
108+
109+
public sealed class PathSelectCombo : FilterComboCache<(string, bool)>
110+
{
111+
private int _skipPrefix = 0;
112+
113+
public PathSelectCombo(TextureManager textures, ModEditor editor)
114+
: base(() => CreateFiles(textures, editor))
115+
{ }
116+
117+
protected override string ToString((string, bool) obj)
118+
=> obj.Item1;
119+
120+
protected override bool DrawSelectable(int globalIdx, bool selected)
121+
{
122+
var (path, game) = Items[globalIdx];
123+
bool ret;
124+
using (var color = ImRaii.PushColor(ImGuiCol.Text, ColorId.FolderExpanded.Value(), game))
125+
{
126+
var equals = string.Equals(CurrentSelection.Item1, path, StringComparison.OrdinalIgnoreCase);
127+
var p = game ? $"--> {path}" : path[_skipPrefix..];
128+
ret = ImGui.Selectable(p, selected) && !equals;
129+
}
130+
131+
ImGuiUtil.HoverTooltip(game
132+
? "This is a game path and refers to an unmanipulated file from your game data."
133+
: "This is a path to a modded file on your file system.");
134+
return ret;
135+
}
136+
137+
private static IReadOnlyList<(string, bool)> CreateFiles(TextureManager textures, ModEditor editor)
138+
=> editor.Files.Tex.SelectMany(f => f.SubModUsage.Select(p => (p.Item2.ToString(), true))
139+
.Prepend((f.File.FullName, false)))
140+
.Where(p => p.Item2 ? textures.GameFileExists(p.Item1) : File.Exists(p.Item1))
141+
.ToList();
142+
143+
public bool Draw(string label, string tooltip, string current, int skipPrefix, out string newPath)
144+
{
145+
_skipPrefix = skipPrefix;
146+
var startPath = current.Length > 0 ? current : "Choose a modded texture from this mod here...";
147+
if (!Draw(label, startPath, tooltip, -0.0001f, ImGui.GetTextLineHeightWithSpacing()))
148+
{
149+
newPath = current;
150+
return false;
151+
}
152+
153+
newPath = CurrentSelection.Item1;
154+
return true;
155+
}
156+
}
139157
}

Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using ImGuiNET;
77
using OtterGui;
88
using OtterGui.Raii;
9-
using OtterGui.Tasks;
109
using OtterTex;
1110
using Penumbra.Import.Textures;
1211

@@ -16,9 +15,10 @@ public partial class ModEditWindow
1615
{
1716
private readonly TextureManager _textures;
1817

19-
private readonly Texture _left = new();
20-
private readonly Texture _right = new();
21-
private readonly CombinedTexture _center;
18+
private readonly Texture _left = new();
19+
private readonly Texture _right = new();
20+
private readonly CombinedTexture _center;
21+
private readonly TextureDrawer.PathSelectCombo _textureSelectCombo;
2222

2323
private bool _overlayCollapsed = true;
2424
private bool _addMipMaps = true;
@@ -47,11 +47,10 @@ private void DrawInputChild(string label, Texture tex, Vector2 size, Vector2 ima
4747

4848
TextureDrawer.PathInputBox(_textures, tex, ref tex.TmpPath, "##input", "Import Image...",
4949
"Can import game paths as well as your own files.", _mod!.ModPath.FullName, _fileDialog, _config.DefaultModImportPath);
50-
var files = _editor.Files.Tex.SelectMany(f => f.SubModUsage.Select(p => (p.Item2.ToString(), true))
51-
.Prepend((f.File.FullName, false)));
52-
TextureDrawer.PathSelectBox(_textures, tex, "##combo",
53-
"Select the textures included in this mod on your drive or the ones they replace from the game files.", files,
54-
_mod.ModPath.FullName.Length + 1);
50+
if (_textureSelectCombo.Draw("##combo",
51+
"Select the textures included in this mod on your drive or the ones they replace from the game files.", tex.Path,
52+
_mod.ModPath.FullName.Length + 1, out var newPath) && newPath != tex.Path)
53+
tex.Load(_textures, newPath);
5554

5655
if (tex == _left)
5756
_center.DrawMatrixInputLeft(size.X);

Penumbra/UI/AdvancedWindow/ModEditWindow.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -520,22 +520,22 @@ private FullPath FindBestMatch(Utf8GamePath path)
520520

521521
public ModEditWindow(PerformanceTracker performance, FileDialogService fileDialog, ItemSwapTab itemSwapTab, IDataManager gameData,
522522
Configuration config, ModEditor editor, ResourceTreeFactory resourceTreeFactory, MetaFileManager metaFileManager,
523-
StainService stainService, ActiveCollections activeCollections, UiBuilder uiBuilder, DalamudServices dalamud, ModMergeTab modMergeTab,
523+
StainService stainService, ActiveCollections activeCollections, DalamudServices dalamud, ModMergeTab modMergeTab,
524524
CommunicatorService communicator, TextureManager textures)
525525
: base(WindowBaseLabel)
526526
{
527-
_performance = performance;
528-
_itemSwapTab = itemSwapTab;
529-
_config = config;
530-
_editor = editor;
531-
_metaFileManager = metaFileManager;
532-
_stainService = stainService;
533-
_activeCollections = activeCollections;
534-
_dalamud = dalamud;
535-
_modMergeTab = modMergeTab;
536-
_communicator = communicator;
537-
_textures = textures;
538-
_fileDialog = fileDialog;
527+
_performance = performance;
528+
_itemSwapTab = itemSwapTab;
529+
_config = config;
530+
_editor = editor;
531+
_metaFileManager = metaFileManager;
532+
_stainService = stainService;
533+
_activeCollections = activeCollections;
534+
_dalamud = dalamud;
535+
_modMergeTab = modMergeTab;
536+
_communicator = communicator;
537+
_textures = textures;
538+
_fileDialog = fileDialog;
539539
_materialTab = new FileEditor<MtrlTab>(this, gameData, config, _fileDialog, "Materials", ".mtrl",
540540
() => _editor.Files.Mtrl, DrawMaterialPanel, () => _mod?.ModPath.FullName ?? string.Empty,
541541
bytes => new MtrlTab(this, new MtrlFile(bytes)));
@@ -544,8 +544,9 @@ public ModEditWindow(PerformanceTracker performance, FileDialogService fileDialo
544544
_shaderPackageTab = new FileEditor<ShpkTab>(this, gameData, config, _fileDialog, "Shaders", ".shpk",
545545
() => _editor.Files.Shpk, DrawShaderPackagePanel, () => _mod?.ModPath.FullName ?? string.Empty,
546546
bytes => new ShpkTab(_fileDialog, bytes));
547-
_center = new CombinedTexture(_left, _right);
548-
_quickImportViewer = new ResourceTreeViewer(_config, resourceTreeFactory, 2, OnQuickImportRefresh, DrawQuickImportActions);
547+
_center = new CombinedTexture(_left, _right);
548+
_textureSelectCombo = new TextureDrawer.PathSelectCombo(textures, editor);
549+
_quickImportViewer = new ResourceTreeViewer(_config, resourceTreeFactory, 2, OnQuickImportRefresh, DrawQuickImportActions);
549550
_communicator.ModPathChanged.Subscribe(OnModPathChanged, ModPathChanged.Priority.ModEditWindow);
550551
}
551552

0 commit comments

Comments
 (0)