Extract and group some header drawing logic.

This commit is contained in:
Ottermandias 2023-07-12 15:04:46 +02:00
parent b00e6bd98b
commit 6f34d74752
5 changed files with 132 additions and 78 deletions

View file

@ -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()

View file

@ -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()
{

View file

@ -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))
{

View file

@ -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<bool> 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();
}
}
}

View file

@ -1,5 +1,4 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using Dalamud.Interface;
using Glamourer.Gui.Tabs.DesignTab;