From 6f34d747521bd6012d410826e0556c2b174b00ff Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Wed, 12 Jul 2023 15:04:46 +0200 Subject: [PATCH] Extract and group some header drawing logic. --- Glamourer/Gui/Tabs/ActorTab/ActorPanel.cs | 25 +---- Glamourer/Gui/Tabs/AutomationTab/SetPanel.cs | 21 +--- Glamourer/Gui/Tabs/DesignTab/DesignPanel.cs | 63 +++++------- Glamourer/Gui/Tabs/HeaderDrawer.cs | 100 +++++++++++++++++++ Glamourer/Gui/Tabs/SettingsTab.cs | 1 - 5 files changed, 132 insertions(+), 78 deletions(-) create mode 100644 Glamourer/Gui/Tabs/HeaderDrawer.cs diff --git a/Glamourer/Gui/Tabs/ActorTab/ActorPanel.cs b/Glamourer/Gui/Tabs/ActorTab/ActorPanel.cs index 8ac3fa4..4bb66e0 100644 --- a/Glamourer/Gui/Tabs/ActorTab/ActorPanel.cs +++ b/Glamourer/Gui/Tabs/ActorTab/ActorPanel.cs @@ -67,27 +67,10 @@ public class ActorPanel private void DrawHeader() { - var frameHeight = ImGui.GetFrameHeightWithSpacing(); - var color = !_identifier.IsValid ? ImGui.GetColorU32(ImGuiCol.Text) : - _data.Valid ? ColorId.ActorAvailable.Value() : ColorId.ActorUnavailable.Value(); - var buttonColor = ImGui.GetColorU32(ImGuiCol.FrameBg); - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero) - .Push(ImGuiStyleVar.FrameRounding, 0); - ImGuiUtil.DrawTextButton($"{_actorName}##playerHeader", new Vector2(-frameHeight, ImGui.GetFrameHeight()), buttonColor, color); - ImGui.SameLine(); - style.Push(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); - using (var c = ImRaii.PushColor(ImGuiCol.Text, ColorId.HeaderButtons.Value()) - .Push(ImGuiCol.Border, ColorId.HeaderButtons.Value())) - { - if (ImGuiUtil.DrawDisabledButton( - $"{(_selector.IncognitoMode ? FontAwesomeIcon.Eye : FontAwesomeIcon.EyeSlash).ToIconString()}###IncognitoMode", - new Vector2(frameHeight, ImGui.GetFrameHeight()), string.Empty, false, true)) - _selector.IncognitoMode = !_selector.IncognitoMode; - } - - var hovered = ImGui.IsItemHovered(); - if (hovered) - ImGui.SetTooltip(_selector.IncognitoMode ? "Toggle incognito mode off." : "Toggle incognito mode on."); + var textColor = !_identifier.IsValid ? ImGui.GetColorU32(ImGuiCol.Text) : + _data.Valid ? ColorId.ActorAvailable.Value() : ColorId.ActorUnavailable.Value(); + HeaderDrawer.Draw(_actorName, textColor, ImGui.GetColorU32(ImGuiCol.FrameBg), 0, + HeaderDrawer.Button.IncognitoButton(_selector.IncognitoMode, v => _selector.IncognitoMode = v)); } private (string, Actor) GetHeaderName() diff --git a/Glamourer/Gui/Tabs/AutomationTab/SetPanel.cs b/Glamourer/Gui/Tabs/AutomationTab/SetPanel.cs index 9d02a3d..b680a8a 100644 --- a/Glamourer/Gui/Tabs/AutomationTab/SetPanel.cs +++ b/Glamourer/Gui/Tabs/AutomationTab/SetPanel.cs @@ -61,25 +61,8 @@ public class SetPanel } private void DrawHeader() - { - var buttonColor = ImGui.GetColorU32(ImGuiCol.FrameBg); - var frameHeight = ImGui.GetFrameHeightWithSpacing(); - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero) - .Push(ImGuiStyleVar.FrameRounding, 0); - ImGuiUtil.DrawTextButton(_selector.SelectionName, new Vector2(-frameHeight, ImGui.GetFrameHeight()), buttonColor); - ImGui.SameLine(); - style.Push(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); - using var color = ImRaii.PushColor(ImGuiCol.Text, ColorId.HeaderButtons.Value()) - .Push(ImGuiCol.Border, ColorId.HeaderButtons.Value()); - if (ImGuiUtil.DrawDisabledButton( - $"{(_selector.IncognitoMode ? FontAwesomeIcon.Eye : FontAwesomeIcon.EyeSlash).ToIconString()}###IncognitoMode", - new Vector2(frameHeight, ImGui.GetFrameHeight()), string.Empty, false, true)) - _selector.IncognitoMode = !_selector.IncognitoMode; - var hovered = ImGui.IsItemHovered(); - color.Pop(2); - if (hovered) - ImGui.SetTooltip(_selector.IncognitoMode ? "Toggle incognito mode off." : "Toggle incognito mode on."); - } + => HeaderDrawer.Draw(_selector.SelectionName, 0, ImGui.GetColorU32(ImGuiCol.FrameBg), 0, + HeaderDrawer.Button.IncognitoButton(_selector.IncognitoMode, v => _selector.IncognitoMode = v)); private void DrawPanel() { diff --git a/Glamourer/Gui/Tabs/DesignTab/DesignPanel.cs b/Glamourer/Gui/Tabs/DesignTab/DesignPanel.cs index 637b53a..f514403 100644 --- a/Glamourer/Gui/Tabs/DesignTab/DesignPanel.cs +++ b/Glamourer/Gui/Tabs/DesignTab/DesignPanel.cs @@ -51,43 +51,30 @@ public class DesignPanel _converter = converter; } + private HeaderDrawer.Button LockButton() + => _selector.Selected == null + ? HeaderDrawer.Button.Invisible + : _selector.Selected.WriteProtected() + ? new HeaderDrawer.Button + { + BorderColor = ColorId.HeaderButtons.Value(), + TextColor = ColorId.HeaderButtons.Value(), + Description = "Make this design editable.", + Icon = FontAwesomeIcon.Lock, + OnClick = () => _manager.SetWriteProtection(_selector.Selected!, false), + } + : new HeaderDrawer.Button + { + BorderColor = ColorId.HeaderButtons.Value(), + TextColor = ColorId.HeaderButtons.Value(), + Description = "Write-protect this design.", + Icon = FontAwesomeIcon.LockOpen, + OnClick = () => _manager.SetWriteProtection(_selector.Selected!, true), + }; + private void DrawHeader() - { - var selection = _selector.Selected; - var buttonColor = ImGui.GetColorU32(ImGuiCol.FrameBg); - var frameHeight = ImGui.GetFrameHeightWithSpacing(); - using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero) - .Push(ImGuiStyleVar.FrameRounding, 0); - ImGuiUtil.DrawTextButton(SelectionName, new Vector2(selection != null ? -2 * frameHeight : -frameHeight, ImGui.GetFrameHeight()), - buttonColor); - - ImGui.SameLine(); - style.Push(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); - using var color = ImRaii.PushColor(ImGuiCol.Border, ColorId.HeaderButtons.Value()) - .Push(ImGuiCol.Text, ColorId.HeaderButtons.Value()); - - var hoverText = string.Empty; - if (selection != null) - { - if (ImGuiUtil.DrawDisabledButton( - $"{(selection.WriteProtected() ? FontAwesomeIcon.LockOpen : FontAwesomeIcon.Lock).ToIconString()}###Locked", - new Vector2(frameHeight, ImGui.GetFrameHeight()), string.Empty, false, true)) - _manager.SetWriteProtection(selection, !selection.WriteProtected()); - if (ImGui.IsItemHovered()) - hoverText = selection.WriteProtected() ? "Make this design editable." : "Write-protect this design."; - } - - ImGui.SameLine(); - if (ImGuiUtil.DrawDisabledButton( - $"{(_selector.IncognitoMode ? FontAwesomeIcon.Eye : FontAwesomeIcon.EyeSlash).ToIconString()}###IncognitoMode", - new Vector2(frameHeight, ImGui.GetFrameHeight()), string.Empty, false, true)) - _selector.IncognitoMode = !_selector.IncognitoMode; - if (ImGui.IsItemHovered()) - hoverText = _selector.IncognitoMode ? "Toggle incognito mode off." : "Toggle incognito mode on."; - - if (hoverText.Length > 0) - ImGui.SetTooltip(hoverText); - } + => HeaderDrawer.Draw(SelectionName, 0, ImGui.GetColorU32(ImGuiCol.FrameBg), 0, LockButton(), + HeaderDrawer.Button.IncognitoButton(_selector.IncognitoMode, v => _selector.IncognitoMode = v)); private string SelectionName => _selector.Selected == null ? "No Selection" : _selector.IncognitoMode ? _selector.Selected.Incognito : _selector.Selected.Name.Text; @@ -178,7 +165,9 @@ public class DesignPanel { var set = _customizationService.AwaitedService.GetList(_selector.Selected!.DesignData.Customize.Clan, _selector.Selected!.DesignData.Customize.Gender); - var all = CustomizationExtensions.All.Where(set.IsAvailable).Select(c => c.ToFlag()).Aggregate((a, b) => a | b) | CustomizeFlag.Clan | CustomizeFlag.Gender; + var all = CustomizationExtensions.All.Where(set.IsAvailable).Select(c => c.ToFlag()).Aggregate((a, b) => a | b) + | CustomizeFlag.Clan + | CustomizeFlag.Gender; var flags = (_selector.Selected!.ApplyCustomize & all) == 0 ? 0 : (_selector.Selected!.ApplyCustomize & all) == all ? 3 : 1; if (ImGui.CheckboxFlags("Apply All Customizations", ref flags, 3)) { diff --git a/Glamourer/Gui/Tabs/HeaderDrawer.cs b/Glamourer/Gui/Tabs/HeaderDrawer.cs new file mode 100644 index 0000000..9e6f543 --- /dev/null +++ b/Glamourer/Gui/Tabs/HeaderDrawer.cs @@ -0,0 +1,100 @@ +using System; +using System.Linq; +using System.Numerics; +using Dalamud.Interface; +using ImGuiNET; +using OtterGui; +using OtterGui.Raii; + +namespace Glamourer.Gui.Tabs; + +public static class HeaderDrawer +{ + public struct Button + { + public static readonly Button Invisible = new() + { + Visible = false, + Width = 0, + }; + + public Action? OnClick; + public string Description = string.Empty; + public float Width; + public uint BorderColor; + public uint TextColor; + public FontAwesomeIcon Icon; + public bool Disabled; + public bool Visible; + + public Button() + { + Visible = true; + Width = ImGui.GetFrameHeightWithSpacing(); + Disabled = false; + } + + public readonly void Draw() + { + if (!Visible) + return; + + using var color = ImRaii.PushColor(ImGuiCol.Border, BorderColor) + .Push(ImGuiCol.Text, TextColor, TextColor != 0); + if (ImGuiUtil.DrawDisabledButton(Icon.ToIconString(), new Vector2(Width, ImGui.GetFrameHeight()), Description, Disabled, true)) + OnClick?.Invoke(); + } + + public static Button IncognitoButton(bool current, Action setter) + => current + ? new Button + { + BorderColor = ColorId.HeaderButtons.Value(), + TextColor = ColorId.HeaderButtons.Value(), + Description = "Toggle incognito mode off.", + Icon = FontAwesomeIcon.EyeSlash, + OnClick = () => setter(false), + } + : new Button + { + BorderColor = ColorId.HeaderButtons.Value(), + TextColor = ColorId.HeaderButtons.Value(), + Description = "Toggle incognito mode on.", + Icon = FontAwesomeIcon.Eye, + OnClick = () => setter(true), + }; + } + + public static void Draw(string text, uint textColor, uint frameColor, int leftButtons, params Button[] buttons) + { + using var style = ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero) + .Push(ImGuiStyleVar.FrameRounding, 0) + .Push(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); + + var leftButtonSize = 0f; + foreach (var button in buttons.Take(leftButtons).Where(b => b.Visible)) + { + button.Draw(); + ImGui.SameLine(); + leftButtonSize += button.Width; + } + + var rightButtonSize = buttons.Length > leftButtons ? buttons.Skip(leftButtons).Where(b => b.Visible).Select(b => b.Width).Sum() : 0f; + var midSize = ImGui.GetContentRegionAvail().X - rightButtonSize - ImGuiHelpers.GlobalScale; + + style.Pop(); + style.Push(ImGuiStyleVar.ButtonTextAlign, new Vector2(0.5f + (rightButtonSize - leftButtonSize) / 2 / midSize, 0.5f)); + if (textColor != 0) + ImGuiUtil.DrawTextButton(text, new Vector2(midSize, ImGui.GetFrameHeight()), frameColor, textColor); + else + ImGuiUtil.DrawTextButton(text, new Vector2(midSize, ImGui.GetFrameHeight()), frameColor); + style.Pop(); + style.Push(ImGuiStyleVar.FrameBorderSize, ImGuiHelpers.GlobalScale); + + foreach (var button in buttons.Skip(leftButtons).Where(b => b.Visible)) + { + ImGui.SameLine(); + button.Draw(); + } + } +} diff --git a/Glamourer/Gui/Tabs/SettingsTab.cs b/Glamourer/Gui/Tabs/SettingsTab.cs index 5b21fbb..666b765 100644 --- a/Glamourer/Gui/Tabs/SettingsTab.cs +++ b/Glamourer/Gui/Tabs/SettingsTab.cs @@ -1,5 +1,4 @@ using System; -using System.Numerics; using System.Runtime.CompilerServices; using Dalamud.Interface; using Glamourer.Gui.Tabs.DesignTab;