Skip to content

Commit

Permalink
Add 'Insert Special Character' right click action on macros
Browse files Browse the repository at this point in the history
  • Loading branch information
grittyfrog committed Sep 20, 2024
1 parent 9ab3379 commit dafb719
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using Dalamud.Interface.Utility;
using Dalamud.Interface.Windowing;
using ImGuiNET;

namespace MacroMate.Extensions.Dalamud.Interface.CharPicker;

/// Class to pick a character from a list of possible characters
public class CharPickerDialog : Window {
public static readonly string NAME = "Char Picker";

private Action<char>? Callback { get; set; }
private List<char> Choices { get; set; } = new();

/// <summary>
/// Counts the number of inserts since this window gained focus.
///
/// Resets when focus is lost or when the window is opened.
/// </summary>
public int ConsecutiveInserts { get; private set; } = 0;

public CharPickerDialog() : base(NAME) {
this.SizeConstraints = new WindowSizeConstraints {
MinimumSize = new Vector2(375, 330),
MaximumSize = new Vector2(float.MaxValue, float.MaxValue)
};
}

public void Open(
List<char> choices,
Action<char> callback
) {
Choices = choices;
Callback = callback;
ConsecutiveInserts = 0;
if (!IsOpen) {
IsOpen = true;
} else {
ImGui.SetWindowFocus(WindowName);
}
}

public override void Draw() {
var buttonSize = 36 * ImGuiHelpers.GlobalScale;
var columns = (int)((ImGui.GetContentRegionAvail().X - ImGui.GetStyle().WindowPadding.X) / (buttonSize + ImGui.GetStyle().ItemSpacing.X));

if (!IsFocused) { ConsecutiveInserts = 0; }

ImGuiClip.ClippedDraw(Choices, (choice) => {
var text = (choice).ToString();
ImGui.SetWindowFontScale(1.3f);
if (ImGui.Button(text, new Vector2(36 * ImGuiHelpers.GlobalScale))) {
if (Callback != null) {
Callback(choice);
ConsecutiveInserts += 1;
}
}
ImGui.SetWindowFontScale(1);
}, columns, lineHeight: buttonSize);
}
}
11 changes: 9 additions & 2 deletions MacroMate/Extensions/Dalamud/Str/SeStringEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,14 @@ public static SeString InsertAtTextValueIndex(this SeString self, SeString text,
var updatedString = new SeStringBuilder();
var textOffset = 0;

// If we are empty just insert and leave
if (self.Payloads.Count == 0) {
text.Payloads.ForEach(pl => { updatedString.Add(pl); });
return updatedString.Build();
}

var remainingPayloads = new Queue<Payload>(self.Payloads);
while (remainingPayloads.TryDequeue(out var payload)) {
Env.PluginLog.Info($"Checking payload: {payload}, offset: {offset}");
var textPayload = payload as ITextProvider;
if (textPayload == null || textPayload.Text == null) {
updatedString.Add(payload);
Expand All @@ -132,7 +137,9 @@ public static SeString InsertAtTextValueIndex(this SeString self, SeString text,
} else {
var beforeText = textPayload.Text[0..textPayloadInsertOffset];
var afterText = textPayload.Text[textPayloadInsertOffset..];
updatedString.AddText(beforeText);
if (beforeText.Count() > 0) {
updatedString.AddText(beforeText);
}
text.Payloads.ForEach(pl => { updatedString.Add(pl); });
if (afterText.Count() > 0) {
updatedString.AddText(afterText);
Expand Down
2 changes: 2 additions & 0 deletions MacroMate/Extensions/Dotnet/EnumerableExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace MacroMate.Extensions.Dotnet;

public static class EnumerableExt {
public static IEnumerable<int> RangeSE(int start, int end) => Enumerable.Range(start, end - start);

public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> source) {
return source.Select((item, index) => (item, index));
}
Expand Down
40 changes: 40 additions & 0 deletions MacroMate/Windows/MacroWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using MacroMate.Extensions.Dalamud.Str;
using MacroMate.Extensions.Dalamaud.Interface.Components;
using Dalamud.Interface.Utility;
using System.Linq;
using Dalamud.Game.Text;

namespace MacroMate.Windows;

Expand Down Expand Up @@ -253,12 +255,50 @@ private uint DrawMacroTextRightClickPopup(MateNode.Macro macro) {
}
}

if (ImGui.Selectable("Insert Special Character")) {
OpenMacroSpecialCharacterPopup(macro);
}

ImGui.EndPopup();
}

return macroTextRightClickPopupId;
}

private void OpenMacroSpecialCharacterPopup(MateNode.Macro macro) {
var seSpecialChars = Enum.GetValues<SeIconChar>()
.Select(c => c.ToIconChar())
.ToList();

var seUnicodeChars = """
π™′^¿¿‰øØ×∞∩£¥¢Ð€ªº†‡¤ ŒœÅ
ωψ↑↓→←⇔⇒♂♀♪¶§±<>≥≤≡÷½¼¾©®ª¹²³
※⇔「」«»≪≫《》【】℉℃‡。·••‥…¨°º‰
╲╳╱☁☀☃♭♯✓〃¹²³
●◎○■□▲△▼▽∇♥♡★☆◆◇♦♦♣♠♤♧¶
αß∇ΘΦΩδ∂∃∀∈∋∑√∝∞∠∟∥∪∩∨∧∫∮∬
∴∵∽≠≦≤≥≧⊂⊃⊆⊇⊥⊿⌒─━│┃│¦
┗┓└┏┐┌┘┛├┝┠┣┤┥┫┬┯┰┳┴┷┸┻╋
┿╂┼¬ ̄,-./:;<=>[\]_`{|}~@
⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇
⓪①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳
№ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹ
0123456789!?"#$%&'()*+¢¦¥
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
""".Where(c => !Char.IsWhiteSpace(c));

var specialChars = seSpecialChars.Concat(seUnicodeChars).ToList();

Env.PluginWindowManager.CharPicker.Open(specialChars, (choice) => {
macro.Lines = macro.Lines.InsertAtTextValueIndex(
choice.ToString(),
(seStringInputTextMultiline.GetCursorPos() ?? 0) + Env.PluginWindowManager.CharPicker.ConsecutiveInserts
);
Env.MacroConfig.NotifyEdit();
});
}

private void Save() {
if (deleteRequested) {
deleteRequested = false;
Expand Down
3 changes: 3 additions & 0 deletions MacroMate/Windows/PluginWindowManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Dalamud.Interface.Windowing;
using ImGuiNET;
using MacroMate.Extensions.Dalamud.Interface.CharPicker;
using MacroMate.Extensions.Dalamud.Interface.ImGuiIconPicker;
using MacroMate.Windows.Debug;

Expand All @@ -11,6 +12,7 @@ public class PluginWindowManager : IDisposable {
public SettingsWindow SettingsWindow { get; private set; } = new();
public MacroWindow MacroWindow { get; private set; } = new();
public IconPickerDialog IconPicker { get; private set; } = new();
public CharPickerDialog CharPicker { get; private set; } = new();
public MacroLinkPicker MacroLinkPicker { get; private set; } = new();
public BackupWindow BackupWindow { get; private set; } = new();
public SubscriptionStatusWindow SubscriptionStatusWindow { get; private set; } = new();
Expand All @@ -22,6 +24,7 @@ public PluginWindowManager() {
Env.WindowSystem.AddWindow(SettingsWindow);
Env.WindowSystem.AddWindow(MacroWindow);
Env.WindowSystem.AddWindow(IconPicker);
Env.WindowSystem.AddWindow(CharPicker);
Env.WindowSystem.AddWindow(MacroLinkPicker);
Env.WindowSystem.AddWindow(BackupWindow);
Env.WindowSystem.AddWindow(SubscriptionStatusWindow);
Expand Down

0 comments on commit dafb719

Please sign in to comment.