Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
dit-zy committed Nov 22, 2023
1 parent 629d112 commit efc7270
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 50 deletions.
13 changes: 13 additions & 0 deletions ScoutHelper/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using Dalamud.Plugin.Services;
Expand Down Expand Up @@ -82,5 +83,17 @@ public static IReadOnlyDictionary<K, V> VerifyEnumDictionary<K, V>(this IDiction
return enumDict.ToImmutableDictionary();
}

public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action) =>
source.ForEach((_, value) => action.Invoke(value));

public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<int, T> action) {
var values = source as T[] ?? source.ToArray();
for (var i = 0; i < values.Length; ++i) {
action.Invoke(i, values[i]);
}

return values;
}

#endregion
}
70 changes: 21 additions & 49 deletions ScoutHelper/Windows/ImGuiPlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,36 @@
namespace ScoutHelper.Windows;

public static class ImGuiPlus {
private static readonly IDalamudTextureWrap ImCapActive =
Plugin.PluginInterface.UiBuilder.LoadImage(Utils.PluginFilePath(@"Images\toggle-cap-active.png"));

private static readonly IDalamudTextureWrap ImCapInactive =
Plugin.PluginInterface.UiBuilder.LoadImage(Utils.PluginFilePath(@"Images\toggle-cap-inactive.png"));
public static bool ToggleBar(string strId, ref uint selection, Vector2 size, params string[] labels) {
using var sharpCorners = ImRaii.PushStyle(ImGuiStyleVar.FrameRounding, 0f);

private static readonly IDalamudTextureWrap ImBodyActive =
Plugin.PluginInterface.UiBuilder.LoadImage(Utils.PluginFilePath(@"Images\toggle-body-active.png"));
var selectionChanged = false;

private static readonly IDalamudTextureWrap ImBodyInactive =
Plugin.PluginInterface.UiBuilder.LoadImage(Utils.PluginFilePath(@"Images\toggle-body-inactive.png"));
labels.ForEach((i, label) => {
ToggleBarButton(label, (uint)i, ref selection, buttonSize);
});

// the cap textures have a transparent border of 4px and total size of 40px x 72px.
// the border is to prevent color bleeding across the edges because of smooth scaling and texture wrapping.
private static readonly Vector2 CapUvNw = new Vector2(4f / 40, 4f / 72);
private static readonly Vector2 CapUvSe = Vector2.One - CapUvNw;

public static unsafe bool ToggleButton(string strId, ref bool isOn, string offText, string onText, Vector2 size) {
size *= 4;
size.Y *= 3;
var buttonPressed = ImGui.InvisibleButton(strId, size);
var topLeft = ImGui.GetItemRectMin();
var bottomRight = ImGui.GetItemRectMax();
if (buttonPressed) isOn = !isOn;

var capCenter = new Vector2(size.Y / 2);
var bodySize = size with { X = size.X / 2 - capCenter.X };
var buttonColor = ImGui.GetColorU32(*ImGui.GetStyleColorVec4(ImGuiCol.Button));
var thicc = 16f;
var padding = thicc / 2;
var focus1 = topLeft + capCenter;
var focus2 = bottomRight - capCenter;
var radius = size.Y / 2 - padding;
var topCenter = topLeft + (capCenter + bodySize) with { Y = padding };
var bottomCenter = bottomRight - (capCenter + bodySize) with { Y = padding };
var padCenter = new Vector2(0, padding);

ImGui.PushClipRect(topLeft, bottomRight, true);
var drawList = ImGui.GetWindowDrawList();

drawList.PathClear();
return selectionChanged;
}

drawList.PathLineTo(topCenter);
drawList.PathArcToFast(isOn ? focus1 : focus2, radius, 9, isOn ? 3 : 15);
drawList.PathLineTo(bottomCenter);
drawList.PathStroke(buttonColor, ImDrawFlags.None, thicc);
private static unsafe bool ToggleBarButton(string label, uint buttonIndex, ref uint selection, Vector2 size) {
var selected = selection == buttonIndex;

drawList.PathClear();
var baseButtonColor = *ImGui.GetStyleColorVec4(ImGuiCol.Button);
var baseButtonColorActive = *ImGui.GetStyleColorVec4(ImGuiCol.ButtonActive);
var baseButtonColorHovered = *ImGui.GetStyleColorVec4(ImGuiCol.ButtonHovered);

drawList.PathArcToFast(isOn ? focus2 : focus1, radius + padding, isOn ? 15 : 3, 9);
drawList.PathLineTo(topCenter - padCenter);
drawList.PathLineTo(bottomCenter + padCenter);
drawList.PathFillConvex(buttonColor);
var buttonColor = selected ? baseButtonColorActive : baseButtonColor;
var buttonColorHovered = selected ? baseButtonColorActive : baseButtonColorHovered with { W = 0.4f };

using var bc1 = ImRaii.PushColor(ImGuiCol.Button, buttonColor);
using var bc2 = ImRaii.PushColor(ImGuiCol.ButtonHovered, buttonColorHovered);

drawList.PathClear();
var pressed = ImGui.Button(label, size);

ImGui.PopClipRect();
if (pressed) selection = buttonIndex;

return buttonPressed;
return pressed;
}
}
8 changes: 7 additions & 1 deletion ScoutHelper/Windows/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ public override void Draw() {
private unsafe void DrawModeButtons() {
ImGuiHelpers.CenteredText("MODE");

ImGuiPlus.ToggleButton("abc", ref _copyFormattedText, "link", "full text", _buttonSize);
using var sharpCorners = ImRaii.PushStyle(ImGuiStyleVar.FrameRounding, 0f);
var modeButtonSize = _buttonSize with { X = _buttonSize.X / 2 };

ImRaii.PushColor(ImGuiCol.Button)

Check failure on line 70 in ScoutHelper/Windows/MainWindow.cs

View workflow job for this annotation

GitHub Actions / build

; expected

Check failure on line 70 in ScoutHelper/Windows/MainWindow.cs

View workflow job for this annotation

GitHub Actions / build

; expected
if (ImGui.Button("link", modeButtonSize)) {

}
}

private void DrawGeneratorButtons() {
Expand Down

0 comments on commit efc7270

Please sign in to comment.