Skip to content

Commit

Permalink
Merge pull request #28 from perappu/feature/search
Browse files Browse the repository at this point in the history
Feature/search
  • Loading branch information
erunseelie authored Sep 3, 2023
2 parents 98eb2ba + 0517040 commit e2cb9b6
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 37 deletions.
11 changes: 6 additions & 5 deletions WhoSaidWhatNow/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ public sealed class Plugin : IDalamudPlugin
public static ConfigurationUtils ConfigHelper = null!;

public static Player? SelectedPlayer = null;
public static List<Player> Players = new List<Player>();
public static Dictionary<String, (String NAME, Dictionary<Player, Boolean> PLAYERS)> Groups = new Dictionary<String, (String, Dictionary<Player, Boolean>)> { { "1", ("Group 1", Players.ToDictionary(p => p, p => false)) } };
public static SortedList<DateTime, ChatEntry> ChatEntries = new SortedList<DateTime, ChatEntry>();
public static List<Player> Players = new();
public static string FilterPlayers = "";
public static string FilterSearch = "";
public static Dictionary<String, (String NAME, Dictionary<Player, Boolean> PLAYERS)> Groups = new() { { "1", ("Group 1", Players.ToDictionary(p => p, p => false)) } };
public static SortedList<DateTime, ChatEntry> ChatEntries = new();

private WindowSystem WindowSystem { get; set; }
public MainWindow MainWindow { get; }
Expand Down Expand Up @@ -69,7 +71,7 @@ public sealed class Plugin : IDalamudPlugin
[RequiredVersion("1.0")]
public static GameConfig GameConfig { get; private set; } = null!;

public FileDialogManager FileDialogManager { get; set; } = null!;
public static FileDialogManager FileDialogManager { get; set; } = new FileDialogManager();

internal ChatService ChatListener { get; private set; } = null!;

Expand Down Expand Up @@ -99,7 +101,6 @@ public Plugin()
// setup UI
this.MainWindow = new MainWindow(this);
this.ConfigWindow = new ConfigWindow(this);
this.FileDialogManager = new FileDialogManager();

this.WindowSystem = new WindowSystem("WhoSaidWhatNow");
this.WindowSystem.AddWindow(this.ConfigWindow);
Expand Down
37 changes: 17 additions & 20 deletions WhoSaidWhatNow/Utils/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class FileUtils
/// </summary>
/// <param name="plugin">the plugin</param>
/// <param name="playerName">player name</param>
public static void OpenFileDialog(Plugin plugin, string playerName)
public static void OpenFileDialog(string playerName)
{
plugin.FileDialogManager.SaveFileDialog("Save log...", "Text File{.txt}",
Plugin.FileDialogManager.SaveFileDialog("Save log...", "Text File{.txt}",
Regex.Replace(playerName, "[^a-zA-Z0-9]", string.Empty) + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt",
".txt", (isOk, selectedFile) =>
{
Expand All @@ -30,17 +30,16 @@ public static void OpenFileDialog(Plugin plugin, string playerName)
/// <summary>
/// OpenFileDialog for groups
/// </summary>
/// <param name="plugin">the plugin</param>
/// <param name="group">KeyValuePair string for group name, Dictionary<Player, Boolean> for contents of group</param>
public static void OpenFileDialog(Plugin plugin, KeyValuePair<string, (string NAME, Dictionary<Player, bool> PLAYERS)> group)
public static void DialogSaveGroup(string name, Dictionary<Player, bool> group)
{
plugin.FileDialogManager.SaveFileDialog("Save log...", "Text File{.txt}",
Regex.Replace(group.Value.NAME, "[^a-zA-Z0-9]", string.Empty) + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt",
Plugin.FileDialogManager.SaveFileDialog("Save log...", "Text File{.txt}",
Regex.Replace(name, "[^a-zA-Z0-9]", string.Empty) + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt",
".txt", (isOk, selectedFile) =>
{
if (isOk)
{
SaveGroupLog(selectedFile, group.Value.PLAYERS);
SaveGroupLog(selectedFile, group);
}
});
}
Expand Down Expand Up @@ -81,25 +80,23 @@ public static void SaveGroupLog(string path, Dictionary<Player, bool> players)
{
try
{
using (var file = new System.IO.StreamWriter(path, false))
using var file = new System.IO.StreamWriter(path, false);
foreach (var c in Plugin.ChatEntries)
{
foreach (var c in Plugin.ChatEntries)
// if we are displaying this type of message;
if (Plugin.Config.ChannelToggles[c.Value.Type] == true)
{
// if we are displaying this type of message;
if (Plugin.Config.ChannelToggles[c.Value.Type] == true)
// and if the player is among the tracked;
var p = Plugin.Players.Find(p => c.Value.Sender.Name.Contains(p.Name));
if (players[p!])
{
// and if the player is among the tracked;
var p = Plugin.Players.Find(p => c.Value.Sender.Name.Contains(p.Name));
if (players[p!])
{
var tag = ConfigurationUtils.ChatTypeToFormat(c.Value.Type);
file.WriteLine(c.Value.CreateMessage(tag));
}
var tag = Plugin.Config.Formats[c.Value.Type];
file.WriteLine(c.Value.CreateMessage(tag));
}
}

Plugin.ChatGui.PluginPrint($"Successfully saved log: {path}");
}

Plugin.ChatGui.PluginPrint($"Successfully saved log: {path}");
}
catch
{
Expand Down
3 changes: 3 additions & 0 deletions WhoSaidWhatNow/Windows/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ public override void Draw()
//GROUP TAB
var groups = new TabGroups(this, plugin);

// chatlog search tab.
new TabSearch(this);

ImGui.EndTabBar();

}
Expand Down
27 changes: 18 additions & 9 deletions WhoSaidWhatNow/Windows/TabGroups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
using System.Linq;
using System.Numerics;
using WhoSaidWhatNow;
using WhoSaidWhatNow.Objects;
using WhoSaidWhatNow.Utils;
using WhoSaidWhatNow.Windows;


public class TabGroups
{
private static int counter = 1;
private static int Counter = 1;

public TabGroups(MainWindow main, Plugin plugin)
{
Expand All @@ -28,6 +30,8 @@ public TabGroups(MainWindow main, Plugin plugin)
var players = group.PLAYERS;
if (ImGui.BeginTabItem($"{name}###Tab_{index}"))
{
var filtered = new Player[Plugin.Players.Count];

if (ImGui.BeginPopupContextItem())
{
var input = String.Empty;
Expand All @@ -42,12 +46,17 @@ public TabGroups(MainWindow main, Plugin plugin)
ImGui.EndPopup();
}
ImGui.BeginChild(MainWindow.ID_PANEL_LEFT, new Vector2(205 * ImGuiHelpers.GlobalScale, 0), true);
foreach (var p in Plugin.Players)
ImGui.InputTextWithHint("", "Filter by name...", ref Plugin.FilterPlayers, 40, ImGuiInputTextFlags.EnterReturnsTrue);
Plugin.Players.Where(p => p.Name.ToLower().Contains(Plugin.FilterPlayers.ToLower())).ToList().CopyTo(filtered);
foreach (var p in filtered)
{
bool isActive;
players.TryGetValue(p, out isActive);
ImGui.Checkbox(p.Name, ref isActive);
players[p] = isActive;
try
{
players.TryGetValue(p, out var isActive);
ImGui.Checkbox(p.Name, ref isActive);
players[p] = isActive;
}
catch { }
}
ImGui.EndChild();
ImGui.SameLine();
Expand All @@ -62,7 +71,7 @@ public TabGroups(MainWindow main, Plugin plugin)
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.MenuItem(FontAwesomeIcon.Save.ToIconString()))
{
FileUtils.OpenFileDialog(plugin, g);
FileUtils.DialogSaveGroup(name, players);
}
ImGui.PopFont();
if (ImGui.IsItemHovered())
Expand Down Expand Up @@ -109,8 +118,8 @@ public TabGroups(MainWindow main, Plugin plugin)

if (ImGui.TabItemButton("+", ImGuiTabItemFlags.Trailing | ImGuiTabItemFlags.NoTooltip))
{
TabGroups.counter++;
Plugin.Groups.Add($"{TabGroups.counter}", ($"Group {TabGroups.counter}", Plugin.Players.ToDictionary(p => p, p => false)));
TabGroups.Counter++;
Plugin.Groups.Add($"{TabGroups.Counter}", ($"Group {TabGroups.Counter}", Plugin.Players.ToDictionary(p => p, p => false)));
ImGui.EndTabItem();
}

Expand Down
18 changes: 15 additions & 3 deletions WhoSaidWhatNow/Windows/TabIndividual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public TabIndividual(MainWindow mainWindow, Plugin plugin)

if (ImGui.BeginTabItem("Individual"))
{
var players = new Player[Plugin.Players.Count];

//janky handling for if we're coming back to this tab from the groups tab
if (Plugin.SelectedPlayer is null)
Expand All @@ -28,6 +29,9 @@ public TabIndividual(MainWindow mainWindow, Plugin plugin)
// you can redeclare BeginChild() with the same ID to add things to them, which we do for chatlog
ImGui.BeginChild(MainWindow.ID_PANEL_LEFT, new Vector2(230 * ImGuiHelpers.GlobalScale, 0), true, ImGuiWindowFlags.MenuBar);

ImGui.InputTextWithHint("", "Filter by name...", ref Plugin.FilterPlayers, 40, ImGuiInputTextFlags.EnterReturnsTrue);
Plugin.Players.Where(p => p.Name.ToLower().Contains(Plugin.FilterPlayers.ToLower())).ToList().CopyTo(players);

if (ImGui.BeginMenuBar())
{
//i can not believe i have to wrap this in a group to show the hover when the button is disabled
Expand Down Expand Up @@ -98,7 +102,7 @@ public TabIndividual(MainWindow mainWindow, Plugin plugin)
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.MenuItem(FontAwesomeIcon.Save.ToIconString()))
{
FileUtils.OpenFileDialog(plugin, Plugin.SelectedPlayer.Name);
FileUtils.OpenFileDialog(Plugin.SelectedPlayer.Name);
}
ImGui.PopFont();

Expand Down Expand Up @@ -129,10 +133,18 @@ public TabIndividual(MainWindow mainWindow, Plugin plugin)


//Reopen left window, populate selectable list
foreach (var p in Plugin.Players)
foreach (var p in players)
{
ImGui.BeginChild(MainWindow.ID_PANEL_LEFT);
mainWindow.AddPlayerSelectable(p);
// catch NPEs silently
try
{
mainWindow.AddPlayerSelectable(p);
}
catch
{

}
ImGui.EndChild();
}

Expand Down
50 changes: 50 additions & 0 deletions WhoSaidWhatNow/Windows/TabSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using ImGuiNET;
using WhoSaidWhatNow;
using System.Linq;
using System.Numerics;
using Dalamud.Interface;
using WhoSaidWhatNow.Windows;
using WhoSaidWhatNow.Utils;



public class TabSearch
{
public TabSearch(MainWindow main)
{

main.toggleWindow(true);

if (ImGui.BeginTabItem("Chat Search"))
{
ImGui.InputTextWithHint("", "Filter by message content...", ref Plugin.FilterSearch, 40);

ImGui.BeginChild(MainWindow.ID_PANEL_RIGHT, new Vector2(0, 0), true);
ImGui.BeginGroup();

// for all chat entries where the Sender or Message contain a match for our filter;
foreach (var c in Plugin.ChatEntries.Where(c => c.Value.Message.ToLower().Contains(Plugin.FilterSearch.ToLower()) || c.Value.Sender.ToString()!.ToLower().Contains(Plugin.FilterSearch.ToLower())))
{
try
{
// if we are displaying this type of message;
if (Plugin.Config.ChannelToggles[c.Value.Type] == true)
{
ChatUtils.ShowMessage(c);
}
}
catch
{
// trying to access a player that doesn't exist, just ignore
}
}
ImGui.EndGroup();
ImGui.EndChild();

ImGui.EndTabItem();

}


}
}

0 comments on commit e2cb9b6

Please sign in to comment.