Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
fix: safe child ui.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchiDog1998 committed Aug 13, 2023
1 parent fcf27e7 commit 20d59b4
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 27 deletions.
168 changes: 145 additions & 23 deletions RotationSolver/UI/RotationConfigWindowNew.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Dalamud.Interface.Colors;
using Dalamud.Interface.Windowing;
using Dalamud.Logging;
using Dalamud.Utility;
using ECommons.DalamudServices;
using ECommons.ExcelServices;
using ECommons.GameHelpers;
using ECommons.ImGuiMethods;
using FFXIVClientStructs.FFXIV.Client.Game.UI;
using ImGuiScene;
using Lumina.Excel.GeneratedSheets;
using Newtonsoft.Json.Linq;
using RotationSolver.ActionSequencer;
using RotationSolver.Basic.Configuration;
using RotationSolver.Data;
Expand All @@ -19,7 +18,6 @@
using RotationSolver.UI.SearchableSettings;
using RotationSolver.Updaters;
using System.Diagnostics;
using System.Drawing;
using GAction = Lumina.Excel.GeneratedSheets.Action;

namespace RotationSolver.UI;
Expand All @@ -39,6 +37,11 @@ public RotationConfigWindowNew()
{
SizeCondition = ImGuiCond.FirstUseEver;
Size = new Vector2(740f, 490f);
SizeConstraints = new WindowSizeConstraints()
{
MinimumSize = new Vector2(250, 300),
MaximumSize = new Vector2(5000, 5000),
};
RespectCloseHotkey = true;
}

Expand All @@ -62,22 +65,29 @@ public override void OnClose()

public override void Draw()
{
if(ImGui.BeginTable("Rotation Config Table", 2, ImGuiTableFlags.Resizable))
try
{
ImGui.TableSetupColumn("Rotation Config Side Bar", ImGuiTableColumnFlags.WidthFixed, 100 * _scale);
ImGui.TableNextColumn();
DrawSideBar();
if (ImGui.BeginTable("Rotation Config Table", 2, ImGuiTableFlags.Resizable))
{
ImGui.TableSetupColumn("Rotation Config Side Bar", ImGuiTableColumnFlags.WidthFixed, 100 * _scale);
ImGui.TableNextColumn();
DrawSideBar();

ImGui.TableNextColumn();
DrawBody();
ImGui.TableNextColumn();
DrawBody();

ImGui.EndTable();
ImGui.EndTable();
}
}
catch(Exception ex)
{
PluginLog.Warning(ex, "Something wrong with new ui");
}
}

private void DrawSideBar()
{
if (ImGui.BeginChild("Rotation Solver Side bar", Vector2.Zero))
if (BeginChild("Rotation Solver Side bar"))
{
var wholeWidth = ImGui.GetWindowSize().X;

Expand Down Expand Up @@ -336,9 +346,8 @@ private static void DrawItemMiddle(System.Action drawAction, float wholeWidth, f

private void DrawBody()
{
var margin = 8 * _scale;
ImGui.SetCursorPos(ImGui.GetCursorPos() + Vector2.One * margin);
if (ImGui.BeginChild("Rotation Solver Body", Vector2.One * -margin))
ImGui.SetCursorPos(ImGui.GetCursorPos() + Vector2.One * 8 * _scale);
if (BeginChild("Rotation Solver Body", -Vector2.One))
{
//Search box
if (_searchResults != null && _searchResults.Any())
Expand Down Expand Up @@ -368,8 +377,8 @@ private void DrawBody()
DrawRotations();
break;

case RotationConfigWindowTab.IDs:
DrawIDs();
case RotationConfigWindowTab.List:
DrawList();
break;

case RotationConfigWindowTab.Basic:
Expand Down Expand Up @@ -864,14 +873,14 @@ private static void DrawActions()
ImGui.TableSetupColumn("Action Column", ImGuiTableColumnFlags.WidthFixed, ImGui.GetWindowWidth() / 2);
ImGui.TableNextColumn();

if (_actionsList != null && ImGui.BeginChild("Rotation Solver Action List"))
if (_actionsList != null && BeginChild("Rotation Solver Action List"))
{
_actionsList.ClearCollapsingHeader();

if (RotationUpdater.RightNowRotation != null)
{
var size = 30 * _scale;
var count = (int)MathF.Floor(ImGui.GetWindowWidth() / (size + ImGui.GetStyle().ItemSpacing.X));
var count = Math.Max(1, (int)MathF.Floor(ImGui.GetWindowWidth() / (size + ImGui.GetStyle().ItemSpacing.X)));
foreach (var pair in RotationUpdater.AllGroupedActions)
{
_actionsList.AddCollapsingHeader(() => pair.Key, () =>
Expand Down Expand Up @@ -915,7 +924,7 @@ private static void DrawActions()

ImGui.TableNextColumn();

if (_sequencerList != null && _activeAction != null && ImGui.BeginChild("Rotation Solver Sequencer List"))
if (_sequencerList != null && _activeAction != null && BeginChild("Rotation Solver Sequencer List"))
{
var enable = _activeAction.IsEnabled;
if (ImGui.Checkbox($"{_activeAction.Name}##{_activeAction.Name} Enabled", ref enable))
Expand Down Expand Up @@ -1248,15 +1257,100 @@ internal static ActionTexture[] AllActions
return _allActions;
}
}
private static readonly CollapsingHeaderGroup _idsHeader = new(new()
{

});
private static void DrawIDs()
private static void DrawList()
{
ImGui.TextWrapped(LocalizationManager.RightLang.ConfigWindow_List_Description);
_idsHeader?.Draw();
}
private static readonly CollapsingHeaderGroup _idsHeader = new(new()
{
{ () => "Status", DrawIDsStatus},
});
private static void DrawIDsStatus()
{
if(ImGui.BeginTable("Rotation Solver List Status", 2, ImGuiTableFlags.Borders | ImGuiTableFlags.Resizable | ImGuiTableFlags.SizingStretchSame))
{
ImGui.TableSetupScrollFreeze(0, 1);
ImGui.TableNextRow(ImGuiTableRowFlags.Headers);

ImGui.TableNextColumn();
ImGui.TableHeader(LocalizationManager.RightLang.ConfigWindow_List_Invincibility);

ImGui.TableNextColumn();
ImGui.TableHeader(LocalizationManager.RightLang.ConfigWindow_List_DangerousStatus);

ImGui.TableNextRow();

ImGui.TableNextColumn();
DrawInvincibility();

ImGui.TableNextColumn();
DrawDangerousStatus();

ImGui.EndTable();
}
}

private static void DrawInvincibility()
{
uint removeId = 0;


if(BeginChild("Rotation Solver InvincibleStatus"))
{
var count = Math.Max(1, (int)MathF.Floor(ImGui.GetWindowWidth() / (24 * _scale + ImGui.GetStyle().ItemSpacing.X)));

var index = 0;
foreach (var statusId in OtherConfiguration.InvincibleStatus)
{
void Reset() => removeId = statusId;

var status = Service.GetSheet<Status>().GetRow(statusId);
var key = "Status" + statusId;
if (ImGui.BeginPopup(key))
{
if (ImGui.BeginTable(key, 2, ImGuiTableFlags.BordersOuter))
{
Searchable.DrawHotKeys("Remove", Reset, "Delete");
ImGui.EndTable();
}

ImGui.EndPopup();
}
if (IconSet.GetTexture(status.Icon, out var texture))
{
if (index++ % count != 0)
{
ImGui.SameLine();
}
NoPaddingNoColorImageButton(texture.ImGuiHandle, new Vector2(24, 32) * _scale, "Status");

if (ImGui.IsItemHovered())
{
ImguiTooltips.ShowTooltip($"{status.Name} ({status.RowId})");
if (ImGui.IsMouseClicked(ImGuiMouseButton.Right) && !ImGui.IsPopupOpen(key))
{
ImGui.OpenPopup(key);
}
Searchable.ExecuteHotKeys(Reset, Dalamud.Game.ClientState.Keys.VirtualKey.DELETE);
}
}
}
ImGui.EndChild();
}

if (removeId != 0)
{
OtherConfiguration.InvincibleStatus.Remove(removeId);
OtherConfiguration.SaveInvincibleStatus();
}
}
private static void DrawDangerousStatus()
{

}

#endregion

private static readonly CollapsingHeaderGroup _debugHeader = new(new()
Expand Down Expand Up @@ -1395,4 +1489,32 @@ internal static void DrawActionOverlay(Vector2 cursor, float width, float percen
}
}
#endregion

#region Child
private static bool BeginChild(string str_id)
{
if (IsFailed()) return false;
return ImGui.BeginChild(str_id);
}

private static bool BeginChild(string str_id, Vector2 size)
{
if (IsFailed()) return false;
return ImGui.BeginChild(str_id, size);
}

private static bool IsFailed()
{
var style = ImGui.GetStyle();
var min = style.WindowPadding.X + style.WindowBorderSize;
var columnWidth = ImGui.GetColumnWidth();
var windowSize = ImGui.GetWindowSize();
var cursor = ImGui.GetCursorPos();

return columnWidth > 0 && columnWidth <= min
|| windowSize.Y - cursor.Y <= min
|| windowSize.X - cursor.X <= min;

}
#endregion
}
2 changes: 1 addition & 1 deletion RotationSolver/UI/RotationConfigWindowNew_Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public partial class RotationConfigWindowNew
private ISearchable[] _searchResults = Array.Empty<ISearchable>();
private void SearchingBox()
{
if (ImGui.InputTextWithHint("##Rotation Solver Search Box", "Searching is not available", ref _searchText, 128, ImGuiInputTextFlags.AutoSelectAll))
if (ImGui.InputTextWithHint("##Rotation Solver Search Box", "Searching...", ref _searchText, 128, ImGuiInputTextFlags.AutoSelectAll))
{
if (!string.IsNullOrEmpty(_searchText))
{
Expand Down
2 changes: 1 addition & 1 deletion RotationSolver/UI/RotationConfigWindowTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal enum RotationConfigWindowTab : byte

[TabIcon(Icon = 4)] Actions,
[TabIcon(Icon = 47)] Rotations,
[TabIcon(Icon = 21)] IDs,
[TabIcon(Icon = 21)] List,
[TabIcon(Icon = 14)] Basic,
[TabIcon(Icon = 42)] UI,
[TabIcon(Icon = 29)] Auto,
Expand Down
4 changes: 2 additions & 2 deletions RotationSolver/UI/SearchableConfigs/Searchable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private static void CopyCommand(string command)
Notify.Success($"\"{command}\" copied to clipboard.");
}

private static void DrawHotKeys(string name, Action action, params string[] keys)
public static void DrawHotKeys(string name, Action action, params string[] keys)
{
if (action == null) return;

Expand All @@ -127,7 +127,7 @@ private static void DrawHotKeys(string name, Action action, params string[] keys
}

private static readonly SortedList<string, bool> _lastChecked = new();
private static void ExecuteHotKeys(Action action, params VirtualKey[] keys)
public static void ExecuteHotKeys(Action action, params VirtualKey[] keys)
{
if (action == null) return;
var name = string.Join(' ', keys);
Expand Down

0 comments on commit 20d59b4

Please sign in to comment.