Skip to content

Commit

Permalink
Hold Shift to display Toggle Dev Menu TSM entry (#1538)
Browse files Browse the repository at this point in the history
Co-authored-by: goat <[email protected]>
  • Loading branch information
Soreepeong and goaaats committed Nov 28, 2023
1 parent d1fad70 commit a71cb81
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 41 deletions.
44 changes: 27 additions & 17 deletions Dalamud/Interface/Internal/DalamudInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Dalamud.Configuration.Internal;
using Dalamud.Game.ClientState;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.ClientState.Keys;
using Dalamud.Game.Gui;
using Dalamud.Game.Internal;
using Dalamud.Interface.Animation.EasingFunctions;
Expand Down Expand Up @@ -151,23 +152,32 @@ private DalamudInterface(

this.interfaceManager.Draw += this.OnDraw;

var tsm = Service<TitleScreenMenu>.Get();
tsm.AddEntryCore(
Loc.Localize("TSMDalamudPlugins", "Plugin Installer"),
dalamudAssetManager.GetDalamudTextureWrap(DalamudAsset.LogoSmall),
this.OpenPluginInstaller);
tsm.AddEntryCore(
Loc.Localize("TSMDalamudSettings", "Dalamud Settings"),
dalamudAssetManager.GetDalamudTextureWrap(DalamudAsset.LogoSmall),
this.OpenSettings);

if (!configuration.DalamudBetaKind.IsNullOrEmpty())
{
tsm.AddEntryCore(
Loc.Localize("TSMDalamudDevMenu", "Developer Menu"),
dalamudAssetManager.GetDalamudTextureWrap(DalamudAsset.LogoSmall),
() => this.isImGuiDrawDevMenu = true);
}
Service<InterfaceManager.InterfaceManagerWithScene>.GetAsync().ContinueWith(
_ =>
{
titleScreenMenu.AddEntryCore(
Loc.Localize("TSMDalamudPlugins", "Plugin Installer"),
dalamudAssetManager.GetDalamudTextureWrap(DalamudAsset.LogoSmall),
this.OpenPluginInstaller);
titleScreenMenu.AddEntryCore(
Loc.Localize("TSMDalamudSettings", "Dalamud Settings"),
dalamudAssetManager.GetDalamudTextureWrap(DalamudAsset.LogoSmall),
this.OpenSettings);
titleScreenMenu.AddEntryCore(
"Toggle Dev Menu",
dalamudAssetManager.GetDalamudTextureWrap(DalamudAsset.LogoSmall),
() => Service<DalamudInterface>.GetNullable()?.ToggleDevMenu(),
VirtualKey.SHIFT);
if (!configuration.DalamudBetaKind.IsNullOrEmpty())
{
titleScreenMenu.AddEntryCore(
Loc.Localize("TSMDalamudDevMenu", "Developer Menu"),
dalamudAssetManager.GetDalamudTextureWrap(DalamudAsset.LogoSmall),
() => this.isImGuiDrawDevMenu = true);
}
});

this.creditsDarkeningAnimation.Point1 = Vector2.Zero;
this.creditsDarkeningAnimation.Point2 = new Vector2(CreditsDarkeningMaxAlpha);
Expand Down
29 changes: 18 additions & 11 deletions Dalamud/Interface/Internal/Windows/TitleScreenMenuWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class TitleScreenMenuWindow : Window, IDisposable
private readonly GameGui gameGui;
private readonly TitleScreenMenu titleScreenMenu;

private readonly IDalamudTextureWrap shadeTexture;
private readonly Lazy<IDalamudTextureWrap> shadeTexture;

private readonly Dictionary<Guid, InOutCubic> shadeEasings = new();
private readonly Dictionary<Guid, InOutQuint> moveEasings = new();
Expand Down Expand Up @@ -77,7 +77,7 @@ public TitleScreenMenuWindow(
this.PositionCondition = ImGuiCond.Always;
this.RespectCloseHotkey = false;

this.shadeTexture = dalamudAssetManager.GetDalamudTextureWrap(DalamudAsset.TitleScreenMenuShade);
this.shadeTexture = new(() => dalamudAssetManager.GetDalamudTextureWrap(DalamudAsset.TitleScreenMenuShade));

framework.Update += this.FrameworkOnUpdate;
}
Expand Down Expand Up @@ -122,15 +122,17 @@ public override void Draw()
return;

var scale = ImGui.GetIO().FontGlobalScale;
var entries = this.titleScreenMenu.Entries.OrderByDescending(x => x.IsInternal).ToList();
var entries = this.titleScreenMenu.Entries;

switch (this.state)
{
case State.Show:
{
for (var i = 0; i < entries.Count; i++)
var i = 0;
foreach (var entry in entries)
{
var entry = entries[i];
if (!entry.IsShowConditionSatisfied())
continue;

if (!this.moveEasings.TryGetValue(entry.Id, out var moveEasing))
{
Expand All @@ -150,7 +152,7 @@ public override void Draw()

moveEasing.Update();

var finalPos = (i + 1) * this.shadeTexture.Height * scale;
var finalPos = (i + 1) * this.shadeTexture.Value.Height * scale;
var pos = moveEasing.Value * finalPos;

// FIXME(goat): Sometimes, easings can overshoot and bring things out of alignment.
Expand All @@ -164,6 +166,7 @@ public override void Draw()
var cursor = ImGui.GetCursorPos();
cursor.Y = (float)pos;
ImGui.SetCursorPos(cursor);
i++;
}

if (!ImGui.IsWindowHovered(ImGuiHoveredFlags.RootAndChildWindows |
Expand Down Expand Up @@ -196,17 +199,20 @@ public override void Draw()

using (ImRaii.PushStyle(ImGuiStyleVar.Alpha, (float)this.fadeOutEasing.Value))
{
for (var i = 0; i < entries.Count; i++)
var i = 0;
foreach (var entry in entries)
{
var entry = entries[i];
if (!entry.IsShowConditionSatisfied())
continue;

var finalPos = (i + 1) * this.shadeTexture.Height * scale;
var finalPos = (i + 1) * this.shadeTexture.Value.Height * scale;

this.DrawEntry(entry, i != 0, true, i == 0, false, false);

var cursor = ImGui.GetCursorPos();
cursor.Y = finalPos;
ImGui.SetCursorPos(cursor);
i++;
}
}

Expand Down Expand Up @@ -280,7 +286,8 @@ private bool DrawEntry(

using (ImRaii.PushStyle(ImGuiStyleVar.Alpha, (float)shadeEasing.Value))
{
ImGui.Image(this.shadeTexture.ImGuiHandle, new Vector2(this.shadeTexture.Width * scale, this.shadeTexture.Height * scale));
var texture = this.shadeTexture.Value;
ImGui.Image(texture.ImGuiHandle, new Vector2(texture.Width, texture.Height) * scale);
}

var isHover = ImGui.IsItemHovered();
Expand Down Expand Up @@ -358,7 +365,7 @@ private bool DrawEntry(
// Drop shadow
using (ImRaii.PushColor(ImGuiCol.Text, 0xFF000000))
{
for (int i = 0, i_ = (int)Math.Ceiling(1 * scale); i < i_; i++)
for (int i = 0, to = (int)Math.Ceiling(1 * scale); i < to; i++)
{
ImGui.SetCursorPos(new Vector2(cursor.X, cursor.Y + i));
ImGui.Text(entry.Name);
Expand Down
65 changes: 54 additions & 11 deletions Dalamud/Interface/TitleScreenMenu/TitleScreenMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
using System.Linq;
using System.Reflection;

using Dalamud.Game.ClientState.Keys;
using Dalamud.Interface.Internal;
using Dalamud.IoC;
using Dalamud.IoC.Internal;
using Dalamud.Plugin.Services;
using ImGuiScene;
using Dalamud.Utility;

namespace Dalamud.Interface;

Expand All @@ -23,14 +24,32 @@ internal class TitleScreenMenu : IServiceType, ITitleScreenMenu
internal const uint TextureSize = 64;

private readonly List<TitleScreenMenuEntry> entries = new();
private TitleScreenMenuEntry[]? entriesView;

[ServiceManager.ServiceConstructor]
private TitleScreenMenu()
{
}

/// <summary>
/// Event to be called when the entry list has been changed.
/// </summary>
internal event Action? EntryListChange;

/// <inheritdoc/>
public IReadOnlyList<TitleScreenMenuEntry> Entries => this.entries;
public IReadOnlyList<TitleScreenMenuEntry> Entries
{
get
{
lock (this.entries)
{
if (!this.entries.Any())
return Array.Empty<TitleScreenMenuEntry>();

return this.entriesView ??= this.entries.OrderByDescending(x => x.IsInternal).ToArray();
}
}
}

/// <inheritdoc/>
public TitleScreenMenuEntry AddEntry(string text, IDalamudTextureWrap texture, Action onTriggered)
Expand All @@ -40,19 +59,23 @@ public TitleScreenMenuEntry AddEntry(string text, IDalamudTextureWrap texture, A
throw new ArgumentException("Texture must be 64x64");
}

TitleScreenMenuEntry entry;
lock (this.entries)
{
var entriesOfAssembly = this.entries.Where(x => x.CallingAssembly == Assembly.GetCallingAssembly()).ToList();
var priority = entriesOfAssembly.Any()
? unchecked(entriesOfAssembly.Select(x => x.Priority).Max() + 1)
: 0;
var entry = new TitleScreenMenuEntry(Assembly.GetCallingAssembly(), priority, text, texture, onTriggered);
entry = new(Assembly.GetCallingAssembly(), priority, text, texture, onTriggered);
var i = this.entries.BinarySearch(entry);
if (i < 0)
i = ~i;
this.entries.Insert(i, entry);
return entry;
this.entriesView = null;
}

this.EntryListChange?.InvokeSafely();
return entry;
}

/// <inheritdoc/>
Expand All @@ -63,15 +86,19 @@ public TitleScreenMenuEntry AddEntry(ulong priority, string text, IDalamudTextur
throw new ArgumentException("Texture must be 64x64");
}

TitleScreenMenuEntry entry;
lock (this.entries)
{
var entry = new TitleScreenMenuEntry(Assembly.GetCallingAssembly(), priority, text, texture, onTriggered);
entry = new(Assembly.GetCallingAssembly(), priority, text, texture, onTriggered);
var i = this.entries.BinarySearch(entry);
if (i < 0)
i = ~i;
this.entries.Insert(i, entry);
return entry;
this.entriesView = null;
}

this.EntryListChange?.InvokeSafely();
return entry;
}

/// <inheritdoc/>
Expand All @@ -80,7 +107,10 @@ public void RemoveEntry(TitleScreenMenuEntry entry)
lock (this.entries)
{
this.entries.Remove(entry);
this.entriesView = null;
}

this.EntryListChange?.InvokeSafely();
}

/// <summary>
Expand All @@ -99,15 +129,19 @@ internal TitleScreenMenuEntry AddEntryCore(ulong priority, string text, IDalamud
throw new ArgumentException("Texture must be 64x64");
}

TitleScreenMenuEntry entry;
lock (this.entries)
{
var entry = new TitleScreenMenuEntry(null, priority, text, texture, onTriggered)
entry = new(null, priority, text, texture, onTriggered)
{
IsInternal = true,
};
this.entries.Add(entry);
return entry;
this.entriesView = null;
}

this.EntryListChange?.InvokeSafely();
return entry;
}

/// <summary>
Expand All @@ -116,28 +150,37 @@ internal TitleScreenMenuEntry AddEntryCore(ulong priority, string text, IDalamud
/// <param name="text">The text to show.</param>
/// <param name="texture">The texture to show.</param>
/// <param name="onTriggered">The action to execute when the option is selected.</param>
/// <param name="showConditionKeys">The keys that have to be held to display the menu.</param>
/// <returns>A <see cref="TitleScreenMenu"/> object that can be used to manage the entry.</returns>
/// <exception cref="ArgumentException">Thrown when the texture provided does not match the required resolution(64x64).</exception>
internal TitleScreenMenuEntry AddEntryCore(string text, IDalamudTextureWrap texture, Action onTriggered)
internal TitleScreenMenuEntry AddEntryCore(
string text,
IDalamudTextureWrap texture,
Action onTriggered,
params VirtualKey[] showConditionKeys)
{
if (texture.Height != TextureSize || texture.Width != TextureSize)
{
throw new ArgumentException("Texture must be 64x64");
}

TitleScreenMenuEntry entry;
lock (this.entries)
{
var entriesOfAssembly = this.entries.Where(x => x.CallingAssembly == null).ToList();
var priority = entriesOfAssembly.Any()
? unchecked(entriesOfAssembly.Select(x => x.Priority).Max() + 1)
: 0;
var entry = new TitleScreenMenuEntry(null, priority, text, texture, onTriggered)
entry = new(null, priority, text, texture, onTriggered, showConditionKeys)
{
IsInternal = true,
};
this.entries.Add(entry);
return entry;
this.entriesView = null;
}

this.EntryListChange?.InvokeSafely();
return entry;
}
}

Expand Down
28 changes: 26 additions & 2 deletions Dalamud/Interface/TitleScreenMenu/TitleScreenMenuEntry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Reflection;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;

using Dalamud.Game.ClientState.Keys;
using Dalamud.Interface.Internal;

namespace Dalamud.Interface;
Expand All @@ -19,13 +23,21 @@ public class TitleScreenMenuEntry : IComparable<TitleScreenMenuEntry>
/// <param name="text">The text to show.</param>
/// <param name="texture">The texture to show.</param>
/// <param name="onTriggered">The action to execute when the option is selected.</param>
internal TitleScreenMenuEntry(Assembly? callingAssembly, ulong priority, string text, IDalamudTextureWrap texture, Action onTriggered)
/// <param name="showConditionKeys">The keys that have to be held to display the menu.</param>
internal TitleScreenMenuEntry(
Assembly? callingAssembly,
ulong priority,
string text,
IDalamudTextureWrap texture,
Action onTriggered,
IEnumerable<VirtualKey>? showConditionKeys = null)
{
this.CallingAssembly = callingAssembly;
this.Priority = priority;
this.Name = text;
this.Texture = texture;
this.onTriggered = onTriggered;
this.ShowConditionKeys = (showConditionKeys ?? Array.Empty<VirtualKey>()).ToImmutableSortedSet();
}

/// <summary>
Expand Down Expand Up @@ -58,6 +70,11 @@ internal TitleScreenMenuEntry(Assembly? callingAssembly, ulong priority, string
/// </summary>
internal Guid Id { get; init; } = Guid.NewGuid();

/// <summary>
/// Gets the keys that have to be pressed to show the menu.
/// </summary>
internal IReadOnlySet<VirtualKey> ShowConditionKeys { get; init; }

/// <inheritdoc/>
public int CompareTo(TitleScreenMenuEntry? other)
{
Expand All @@ -84,6 +101,13 @@ public int CompareTo(TitleScreenMenuEntry? other)
return 0;
}

/// <summary>
/// Determines the displaying condition of this menu entry is met.
/// </summary>
/// <returns>True if met.</returns>
internal bool IsShowConditionSatisfied() =>
this.ShowConditionKeys.All(x => Service<KeyState>.GetNullable()?[x] is true);

/// <summary>
/// Trigger the action associated with this entry.
/// </summary>
Expand Down

0 comments on commit a71cb81

Please sign in to comment.