From 1344594b3bae07bee854545be21259fdd268ddc0 Mon Sep 17 00:00:00 2001 From: kalilistic <35899782+kalilistic@users.noreply.github.com> Date: Thu, 8 Apr 2021 19:51:25 -0400 Subject: [PATCH] feat: add icon button and help marker components --- .editorconfig | 4 + .../Components/ComponentDemoWindow.cs | 29 +++++-- .../Components/HelpMarkerComponent.cs | 61 ++++++++++++++ .../Components/IconButtonComponent.cs | 82 +++++++++++++++++++ 4 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 Dalamud/Interface/Components/HelpMarkerComponent.cs create mode 100644 Dalamud/Interface/Components/IconButtonComponent.cs diff --git a/.editorconfig b/.editorconfig index a7ae2aa81..8816a8511 100644 --- a/.editorconfig +++ b/.editorconfig @@ -72,6 +72,8 @@ resharper_align_multiline_for_stmt = true resharper_align_multline_type_parameter_constrains = true resharper_align_multline_type_parameter_list = true resharper_apply_on_completion = true +resharper_auto_property_can_be_made_get_only_global_highlighting = none +resharper_auto_property_can_be_made_get_only_local_highlighting = none resharper_autodetect_indent_settings = true resharper_braces_for_ifelse = required_for_multiline resharper_can_use_global_alias = false @@ -82,6 +84,8 @@ resharper_csharp_int_align_comments = true resharper_csharp_new_line_before_while = true resharper_csharp_wrap_after_declaration_lpar = true resharper_enforce_line_ending_style = true +resharper_member_can_be_private_global_highlighting = none +resharper_member_can_be_private_local_highlighting = none resharper_new_line_before_finally = false resharper_place_accessorholder_attribute_on_same_line = false resharper_place_field_attribute_on_same_line = false diff --git a/Dalamud/Interface/Components/ComponentDemoWindow.cs b/Dalamud/Interface/Components/ComponentDemoWindow.cs index bc25cc1d4..082c13550 100644 --- a/Dalamud/Interface/Components/ComponentDemoWindow.cs +++ b/Dalamud/Interface/Components/ComponentDemoWindow.cs @@ -1,6 +1,9 @@ +using System.Collections.Generic; +using System.Linq; using System.Numerics; using Dalamud.Interface.Windowing; +using Dalamud.Plugin; using ImGuiNET; namespace Dalamud.Interface.Components @@ -10,10 +13,7 @@ namespace Dalamud.Interface.Components /// internal class ComponentDemoWindow : Window { - private readonly IComponent[] components = - { - new TestComponent(), - }; + private List components = new List(); /// /// Initializes a new instance of the class. @@ -23,6 +23,8 @@ namespace Dalamud.Interface.Components { this.Size = new Vector2(600, 500); this.SizeCondition = ImGuiCond.FirstUseEver; + this.AddComponents(); + this.SortComponents(); } /// @@ -32,7 +34,7 @@ namespace Dalamud.Interface.Components ImGui.Text("This is a collection of UI components you can use in your plugin."); - for (var i = 0; i < this.components.Length; i++) + for (var i = 0; i < this.components.Count; i++) { var thisComp = this.components[i]; @@ -44,5 +46,22 @@ namespace Dalamud.Interface.Components ImGui.EndChild(); } + + private void AddComponents() + { + this.components.Add(new TestComponent()); + this.components.Add(new HelpMarkerComponent("help me!") + { + SameLine = false, + }); + var iconButtonComponent = new IconButtonComponent(1, FontAwesomeIcon.Carrot); + iconButtonComponent.OnButtonClicked += id => PluginLog.Log("Button#{0} clicked!", id); + this.components.Add(iconButtonComponent); + } + + private void SortComponents() + { + this.components = this.components.OrderBy(component => component.Name).ToList(); + } } } diff --git a/Dalamud/Interface/Components/HelpMarkerComponent.cs b/Dalamud/Interface/Components/HelpMarkerComponent.cs new file mode 100644 index 000000000..f559c8ef2 --- /dev/null +++ b/Dalamud/Interface/Components/HelpMarkerComponent.cs @@ -0,0 +1,61 @@ +using ImGuiNET; + +namespace Dalamud.Interface.Components +{ + /// + /// HelpMarker component to add a help icon with text on hover. + /// + public class HelpMarkerComponent : IComponent + { + /// + /// Initializes a new instance of the class. + /// + /// The text to display on hover. + public HelpMarkerComponent(string helpText) + { + this.HelpText = helpText; + } + + /// + /// Gets component name. + /// + public string Name { get; } = "HelpMarker Component"; + + /// + /// Gets or sets a value indicating whether the help text should display on same line as previous element. + /// + public bool SameLine { get; set; } = true; + + /// + /// Gets or sets the help text. + /// + public string HelpText { get; set; } + + /// + /// Gets or sets the help marker icon. + /// + public FontAwesomeIcon HelpIcon { get; set; } = FontAwesomeIcon.InfoCircle; + + /// + /// Gets or sets the help text size modifier. + /// + public float HelpTextModifier { get; set; } = 35.0f; + + /// + /// Draw HelpMarker component. + /// + public void Draw() + { + if (this.SameLine) ImGui.SameLine(); + ImGui.PushFont(UiBuilder.IconFont); + ImGui.TextDisabled(this.HelpIcon.ToIconString()); + ImGui.PopFont(); + if (!ImGui.IsItemHovered()) return; + ImGui.BeginTooltip(); + ImGui.PushTextWrapPos(ImGui.GetFontSize() * this.HelpTextModifier); + ImGui.TextUnformatted(this.HelpText); + ImGui.PopTextWrapPos(); + ImGui.EndTooltip(); + } + } +} diff --git a/Dalamud/Interface/Components/IconButtonComponent.cs b/Dalamud/Interface/Components/IconButtonComponent.cs new file mode 100644 index 000000000..7faa4623c --- /dev/null +++ b/Dalamud/Interface/Components/IconButtonComponent.cs @@ -0,0 +1,82 @@ +using System.Numerics; + +using ImGuiNET; + +namespace Dalamud.Interface.Components +{ + /// + /// IconButton component to use an icon as a button. + /// + public class IconButtonComponent : IComponent + { + /// + /// Initializes a new instance of the class. + /// + /// The id for the button. + /// The icon for the button. + public IconButtonComponent(int buttonId, FontAwesomeIcon buttonIcon) + { + this.ButtonId = buttonId; + this.ButtonIcon = buttonIcon; + } + + /// + /// Delegate for the event that occurs when the button is clicked. + /// + /// The id of the button that was clicked. + public delegate void IsButtonClickedDelegate(int buttonId); + + /// + /// Event that occurs when the button is clicked. + /// + public event IsButtonClickedDelegate OnButtonClicked; + + /// + /// Gets component name. + /// + public string Name { get; } = "IconButton Component"; + + /// + /// Gets or sets the id for the button. + /// + public int ButtonId { get; set; } + + /// + /// Gets or sets the icon to use for the button. + /// + public FontAwesomeIcon ButtonIcon { get; set; } + + /// + /// Gets or sets the button color. + /// + public Vector4 ButtonColor { get; set; } = Vector4.Zero; + + /// + /// Gets or sets the active button color. + /// + public Vector4 ButtonColorActive { get; set; } = Vector4.Zero; + + /// + /// Gets or sets the hovered button color. + /// + public Vector4 ButtonColorHovered { get; set; } = Vector4.Zero; + + /// + /// Draw IconButton component. + /// + public void Draw() + { + ImGui.PushStyleColor(ImGuiCol.Button, this.ButtonColor); + ImGui.PushStyleColor(ImGuiCol.ButtonActive, this.ButtonColorActive); + ImGui.PushStyleColor(ImGuiCol.ButtonHovered, this.ButtonColorHovered); + ImGui.PushFont(UiBuilder.IconFont); + if (ImGui.Button($"{this.ButtonIcon.ToIconString()}{this.ButtonId}")) + { + this.OnButtonClicked?.Invoke(this.ButtonId); + } + + ImGui.PopFont(); + ImGui.PopStyleColor(3); + } + } +}