Skip to content

Commit

Permalink
Add support for dynamic grouping (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorm4 authored Jul 21, 2024
1 parent 4a1d1c1 commit b0e2ff0
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 21 deletions.
32 changes: 32 additions & 0 deletions DelvCD/Config/GroupConfig.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Dalamud.Interface.Utility;
using DelvCD.UIElements;
using DelvCD.Helpers;
using ImGuiNET;
using Newtonsoft.Json;
using System.Numerics;
using System.Collections.Generic;

namespace DelvCD.Config
{
Expand All @@ -12,6 +14,12 @@ public class GroupConfig : IConfigPage
[JsonIgnore] private static Vector2 _screenSize => ImGui.GetMainViewport().Size;

public string Name => "Group";
public bool IsDynamic = false;
public Vector2 DynamicOffset = new Vector2(0, 0);

public int DynamicMaxPerRow = 5;

public int DynamicGrowthDir = 0;

public Vector2 Position = new Vector2(0, 0);

Expand All @@ -21,6 +29,17 @@ public class GroupConfig : IConfigPage
[JsonIgnore] private bool _recusiveResize = false;
[JsonIgnore] private bool _conditionsResize = false;
[JsonIgnore] private bool _positionOnly = false;
[JsonIgnore]
private string[] _DirectionsOptionsValues = {
"Right and Down",
"Right and Up",
"Left and Down",
"Left and Up",
"Centered and Up",
"Centered and Down",
"Centered and Left",
"Centered and Right"
};

public IConfigPage GetDefault() => new GroupConfig();

Expand All @@ -30,6 +49,19 @@ public void DrawConfig(IConfigurable parent, Vector2 size, float padX, float pad
{
ImGui.DragFloat2("Group Position", ref Position);

ImGui.NewLine();
ImGui.Checkbox("Dynamic Grouping", ref IsDynamic);

if (IsDynamic)
{
ImGui.DragFloat2("Dynamic Offset", ref DynamicOffset);
ImGui.Combo(
"Growth Direction", ref DynamicGrowthDir, _DirectionsOptionsValues, _DirectionsOptionsValues.Length
);
ImGui.DragInt("Max elements per row", ref DynamicMaxPerRow, 1, 1, 32);

}

ImGui.NewLine();
ImGui.Text("Resize Icons");
ImGui.DragFloat2("Icon Size##Size", ref _iconSize, 1, 0, _screenSize.Y);
Expand Down
9 changes: 9 additions & 0 deletions DelvCD/Helpers/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,13 @@ public enum DrawAnchor
BottomLeft = 7,
BottomRight = 8
}

public enum GrowthDirections
{
Up = 1,
Down = 2,
Left = 4,
Right = 8,
Centered = 16,
}
}
101 changes: 101 additions & 0 deletions DelvCD/Helpers/LayoutHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System.Collections.Generic;
using System.Numerics;
using System;

namespace DelvCD.Helpers
{
public static class LayoutHelper
{
private static List<GrowthDirections> DirectionOptionsValues = new List<GrowthDirections>()
{
GrowthDirections.Right | GrowthDirections.Down, // grow x axis right, y axis down
GrowthDirections.Right | GrowthDirections.Up, // grow x axis right, y axis up
GrowthDirections.Left | GrowthDirections.Down, // grow x axis left, y axis down
GrowthDirections.Left | GrowthDirections.Up, // grow x axis left, y axis up
GrowthDirections.Centered | GrowthDirections.Up, // center x axis, y axis -> grow up
GrowthDirections.Centered | GrowthDirections.Down, // center x axis, y axis -> grow down
GrowthDirections.Centered | GrowthDirections.Left, // center y axis, x axis -> grow to left
GrowthDirections.Centered | GrowthDirections.Right // center y axis, x axis -> grow to right
};
public static GrowthDirections GrowthDirectionsFromIndex(int index)
{
if (index > 0 && index < DirectionOptionsValues.Count)
{
return DirectionOptionsValues[index];
}

return DirectionOptionsValues[0];
}

public static Vector2 CalculateAxisDirections(
GrowthDirections growthDirections,
int row,
int col)
{
Vector2 direction;
if ((growthDirections & GrowthDirections.Centered) != 0)
{
// if axis is centered -> even elements right/down | odd elements left/up
if ((growthDirections & GrowthDirections.Up) != 0 || (growthDirections & GrowthDirections.Down) != 0)
{
direction.X = col % 2 == 0 ? 1 : -1;
direction.Y = (growthDirections & GrowthDirections.Down) != 0 ? 1 : -1;
}
else
{
direction.X = (growthDirections & GrowthDirections.Right) != 0 ? 1 : -1;
direction.Y = row % 2 == 0 ? 1 : -1;
}
}
else
{
direction.X = (growthDirections & GrowthDirections.Right) != 0 ? 1 : -1;
direction.Y = (growthDirections & GrowthDirections.Down) != 0 ? 1 : -1;
}
return direction;
}

public static Vector2 GetOffsetMultiplier(GrowthDirections directions, int row, int col)
{
// determine if y or x axis is getting centered and scale by .5 for that axis
if ((directions & GrowthDirections.Centered) != 0)
{
if ((directions & GrowthDirections.Up) != 0 || (directions & GrowthDirections.Down) != 0)
{
return new Vector2((float)Math.Ceiling(col / 2f), row);
}
else
{
return new Vector2(col, (float)Math.Ceiling(row / 2f));
}
}
return new Vector2(col, row);
}

public static Vector2 CalculateElementPosition(
GrowthDirections directions,
int maxPerRow,
int index,
Vector2 startingPos,
Vector2 offset)
{
List<Vector2> list = new List<Vector2>();
int actualMaxPerRow = maxPerRow > 0 ? maxPerRow : 1;
int row = index / actualMaxPerRow;
int col = index % actualMaxPerRow;

Vector2 direction = CalculateAxisDirections(
directions,
row,
col
);

Vector2 offsetMultiplier = GetOffsetMultiplier(directions, row, col);

return new Vector2(
startingPos.X + offsetMultiplier.X * direction.X * offset.X,
startingPos.Y + offsetMultiplier.Y * direction.Y * offset.Y
);
}
}
}
13 changes: 7 additions & 6 deletions DelvCD/UIElements/Bar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DelvCD.Config;
using DelvCD.Config;
using DelvCD.Helpers;
using DelvCD.Helpers.DataSources;
using ImGuiNET;
Expand Down Expand Up @@ -121,17 +121,17 @@ public override void ImportPage(IConfigPage page)
}
}

public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVisible = true)
public override bool Draw(Vector2 pos, Vector2? parentSize = null, bool parentVisible = true)
{
if (!TriggerConfig.TriggerOptions.Any())
{
return;
return false;
}

bool visible = VisibilityConfig.IsVisible(parentVisible);
if (!visible && !Preview)
{
return;
return false;
}

bool triggered = TriggerConfig.IsTriggered(Preview, out int triggeredIndex);
Expand All @@ -147,7 +147,7 @@ public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVi
ClipRect? clipRect = Singletons.Get<ClipRectsHelper>().GetClipRectForArea(localPos, size);
if (clipRect.HasValue)
{
return;
return false;
}
}

Expand All @@ -157,7 +157,7 @@ public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVi
{
StartData = null;
StartTime = null;
return;
return false;
}

UpdateStartData(data);
Expand Down Expand Up @@ -304,6 +304,7 @@ public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVi
label.Draw(localPos, size, visible);
}
}
return true;
}

private BarData CalculateBar(Vector2 size, float progress, float max, BarDirection direction)
Expand Down
23 changes: 20 additions & 3 deletions DelvCD/UIElements/Group.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DelvCD.Config;
using DelvCD.Config;
using DelvCD.Helpers;
using System.Collections.Generic;
using System.Numerics;
Expand Down Expand Up @@ -60,11 +60,14 @@ public override void StopPreview()
}
}

public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVisible = true)
public override bool Draw(Vector2 pos, Vector2? parentSize = null, bool parentVisible = true)
{
bool visible = VisibilityConfig.IsVisible(parentVisible);
int idx = 0;
GrowthDirections growthDirections = LayoutHelper.GrowthDirectionsFromIndex(GroupConfig.DynamicGrowthDir);
foreach (UIElement element in ElementList.UIElements)
{

if (!Preview && LastFrameWasPreview)
{
element.Preview = false;
Expand All @@ -76,11 +79,25 @@ public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVi

if (visible || Singletons.Get<PluginManager>().IsConfigOpen())
{
element.Draw(pos + GroupConfig.Position, null, visible);
Vector2 eleOffset = GroupConfig.IsDynamic ? GroupConfig.DynamicOffset : Vector2.Zero;
Vector2 localPos = GroupConfig.Position;
if (GroupConfig.IsDynamic)
{
localPos += LayoutHelper.CalculateElementPosition(growthDirections, GroupConfig.DynamicMaxPerRow, idx, pos, eleOffset);
}
else
{
localPos += pos;
}
if (element.Draw(localPos, null, visible))
{
idx++;
}
}
}

LastFrameWasPreview = Preview;
return idx > 0;
}

public void ResizeIcons(Vector2 size, bool recurse, bool conditions)
Expand Down
18 changes: 9 additions & 9 deletions DelvCD/UIElements/Icon.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Dalamud.Logging;
using DelvCD.Config;
using DelvCD.Helpers;
using DelvCD.Helpers.DataSources;
Expand Down Expand Up @@ -40,7 +39,7 @@ public TriggerConfig TriggerConfig
}
}


[JsonIgnore] private StyleConditions<IconStyleConfig> _styleConditions = null!;
public StyleConditions<IconStyleConfig> StyleConditions
{
Expand Down Expand Up @@ -121,17 +120,17 @@ public override void ImportPage(IConfigPage page)
}
}

public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVisible = true)
public override bool Draw(Vector2 pos, Vector2? parentSize = null, bool parentVisible = true)
{
if (!TriggerConfig.TriggerOptions.Any())
{
return;
return false;
}

bool visible = VisibilityConfig.IsVisible(parentVisible);
if (!visible && !Preview)
{
return;
return false;
}

bool triggered = TriggerConfig.IsTriggered(Preview, out int triggeredIndex);
Expand All @@ -147,15 +146,15 @@ public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVi
ClipRect? clipRect = Singletons.Get<ClipRectsHelper>().GetClipRectForArea(localPos, size);
if (clipRect.HasValue)
{
return;
return false;
}
}

if (!triggered && !Preview)
{
StartData = null;
StartTime = null;
return;
return false;
}

UpdateStartData(data);
Expand Down Expand Up @@ -256,6 +255,7 @@ public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVi
}

LastFrameWasPreview = Preview;
return true;
}

private static void DrawProgressSwipe(
Expand All @@ -271,7 +271,7 @@ private static void DrawProgressSwipe(
{
return;
}

bool invert = style.InvertSwipe;
float percent = (invert ? 0 : 1) - (startValue - triggeredValue) / startValue;
uint progressAlpha = (uint)(style.ProgressSwipeOpacity * 255 * alpha) << 24;
Expand All @@ -288,7 +288,7 @@ private static void DrawProgressSwipe(

ImGui.PushClipRect(pos, pos + size, false);
drawList.PathArcTo(pos + size / 2, radius / 2, startAngle, endAngle, (int)(100f * Math.Abs(percent)));

drawList.PathStroke(progressAlpha, ImDrawFlags.None, radius);
if (style.ShowSwipeLines)
{
Expand Down
5 changes: 3 additions & 2 deletions DelvCD/UIElements/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public override void ImportPage(IConfigPage page)
}
}

public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVisible = true)
public override bool Draw(Vector2 pos, Vector2? parentSize = null, bool parentVisible = true)
{
if (!VisibilityConfig.IsVisible(parentVisible) && !Preview)
{
return;
return false;
}

Vector2 size = parentSize.HasValue ? parentSize.Value : ImGui.GetMainViewport().Size;
Expand All @@ -85,6 +85,7 @@ public override void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVi
style.ShowOutline,
style.OutlineColor.Base);
}
return true;
}

public void UpdateDataSources(DataSource[] data)
Expand Down
2 changes: 1 addition & 1 deletion DelvCD/UIElements/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public UIElement(string name)

public abstract bool IsAlwaysHide { get; }

public abstract void Draw(Vector2 pos, Vector2? parentSize = null, bool parentVisible = true);
public abstract bool Draw(Vector2 pos, Vector2? parentSize = null, bool parentVisible = true);

public abstract IEnumerable<IConfigPage> GetConfigPages();

Expand Down

0 comments on commit b0e2ff0

Please sign in to comment.