From 44925263ca2950b861ae5e66000c85d472dfad3c Mon Sep 17 00:00:00 2001 From: Matt Edmondson Date: Tue, 1 Oct 2024 09:42:03 +1000 Subject: [PATCH] Fix typos and renames for clarity --- ImGuiWidgets/Grid.cs | 9 ++-- ImGuiWidgets/Icon.cs | 124 ++++++++++++++++++------------------------- 2 files changed, 56 insertions(+), 77 deletions(-) diff --git a/ImGuiWidgets/Grid.cs b/ImGuiWidgets/Grid.cs index 5b923d3..762c428 100644 --- a/ImGuiWidgets/Grid.cs +++ b/ImGuiWidgets/Grid.cs @@ -113,7 +113,6 @@ public static void Show(IEnumerable items, MeasureGridCell measureDeleg for (int i = 0; i < numColumns * numRows; i++) { - var itemStartCursor = ImGui.GetCursorScreenPos(); int column = i % numColumns; int row = i / numColumns; @@ -152,17 +151,17 @@ public static void Show(IEnumerable items, MeasureGridCell measureDeleg ? (column * numRows) + row : i; - var cellCize = new Vector2(columnWidths[column], rowHeights[row]); + var cellSize = new Vector2(columnWidths[column], rowHeights[row]); if (itemIndex < itemList.Length) { - drawDelegate(itemList[itemIndex], cellCize, itemDimensions[itemIndex]); + drawDelegate(itemList[itemIndex], cellSize, itemDimensions[itemIndex]); } - var advance = new Vector2(marginTopLeftCursor.X, itemStartCursor.Y + cellCize.Y); + var advance = new Vector2(marginTopLeftCursor.X, itemStartCursor.Y + cellSize.Y); if (column < numColumns - 1) { - advance = new Vector2(itemStartCursor.X + cellCize.X, itemStartCursor.Y); + advance = new Vector2(itemStartCursor.X + cellSize.X, itemStartCursor.Y); } ImGui.SetCursorScreenPos(advance); } diff --git a/ImGuiWidgets/Icon.cs b/ImGuiWidgets/Icon.cs index be779cb..555ee4a 100644 --- a/ImGuiWidgets/Icon.cs +++ b/ImGuiWidgets/Icon.cs @@ -56,123 +56,123 @@ public class IconDelegates /// /// The label of the icon. /// The texture ID of the icon. - /// The size of the icon. + /// The size of the image. /// The color of the icon. /// True if the icon was clicked; otherwise, false. - public static bool Icon(string label, uint textureId, float size, Vector4 color) => Icon(label, textureId, new Vector2(size), color, IconAlignment.Horizontal, new()); + public static bool Icon(string label, uint textureId, float imageSize, Vector4 color) => Icon(label, textureId, new Vector2(imageSize), color, IconAlignment.Horizontal, new()); /// /// Renders an icon with the specified parameters. /// /// The label of the icon. /// The texture ID of the icon. - /// The size of the icon. + /// The size of the image. /// The color of the icon. /// The alignment of the icon. /// True if the icon was clicked; otherwise, false. - public static bool Icon(string label, uint textureId, float size, Vector4 color, IconAlignment iconAlignment) => Icon(label, textureId, new Vector2(size), color, iconAlignment, new()); + public static bool Icon(string label, uint textureId, float imageSize, Vector4 color, IconAlignment iconAlignment) => Icon(label, textureId, new Vector2(imageSize), color, iconAlignment, new()); /// /// Renders an icon with the specified parameters. /// /// The label of the icon. /// The texture ID of the icon. - /// The size of the icon. + /// The size of the image. /// The color of the icon. /// The delegates for icon events. /// True if the icon was clicked; otherwise, false. - public static bool Icon(string label, uint textureId, float size, Vector4 color, IconDelegates iconDelegates) => Icon(label, textureId, new Vector2(size), color, IconAlignment.Horizontal, iconDelegates); + public static bool Icon(string label, uint textureId, float imageSize, Vector4 color, IconDelegates iconDelegates) => Icon(label, textureId, new Vector2(imageSize), color, IconAlignment.Horizontal, iconDelegates); /// /// Renders an icon with the specified parameters. /// /// The label of the icon. /// The texture ID of the icon. - /// The size of the icon. + /// The size of the image. /// The color of the icon. /// The alignment of the icon. /// The delegates for icon events. /// True if the icon was clicked; otherwise, false. - public static bool Icon(string label, uint textureId, float size, Vector4 color, IconAlignment iconAlignment, IconDelegates iconDelegates) => Icon(label, textureId, new Vector2(size), color, iconAlignment, iconDelegates); + public static bool Icon(string label, uint textureId, float imageSize, Vector4 color, IconAlignment iconAlignment, IconDelegates iconDelegates) => Icon(label, textureId, new Vector2(imageSize), color, iconAlignment, iconDelegates); /// /// Renders an icon with the specified parameters. /// /// The label of the icon. /// The texture ID of the icon. - /// The size of the icon. + /// The size of the image. /// The color of the icon. /// True if the icon was clicked; otherwise, false. - public static bool Icon(string label, uint textureId, Vector2 size, Vector4 color) => Icon(label, textureId, size, color, IconAlignment.Horizontal, new()); + public static bool Icon(string label, uint textureId, Vector2 imageSize, Vector4 color) => Icon(label, textureId, imageSize, color, IconAlignment.Horizontal, new()); /// /// Renders an icon with the specified parameters. /// /// The label of the icon. /// The texture ID of the icon. - /// The size of the icon. + /// The size of the image. /// The color of the icon. /// The alignment of the icon. /// True if the icon was clicked; otherwise, false. - public static bool Icon(string label, uint textureId, Vector2 size, Vector4 color, IconAlignment iconAlignment) => Icon(label, textureId, size, color, iconAlignment, new()); + public static bool Icon(string label, uint textureId, Vector2 imageSize, Vector4 color, IconAlignment iconAlignment) => Icon(label, textureId, imageSize, color, iconAlignment, new()); /// /// Renders an icon with the specified parameters. /// /// The label of the icon. /// The texture ID of the icon. - /// The size of the icon. + /// The size of the image. /// The color of the icon. /// The delegates for icon events. /// True if the icon was clicked; otherwise, false. - public static bool Icon(string label, uint textureId, Vector2 size, Vector4 color, IconDelegates iconDelegates) => Icon(label, textureId, size, color, IconAlignment.Horizontal, iconDelegates); + public static bool Icon(string label, uint textureId, Vector2 imageSize, Vector4 color, IconDelegates iconDelegates) => Icon(label, textureId, imageSize, color, IconAlignment.Horizontal, iconDelegates); /// /// Renders an icon with the specified parameters. /// /// The label of the icon. /// The texture ID of the icon. - /// The size of the icon. + /// The size of the image. /// The color of the icon. /// The alignment of the icon. /// The delegates for icon events. /// True if the icon was clicked; otherwise, false. - public static bool Icon(string label, uint textureId, Vector2 size, Vector4 color, IconAlignment iconAlignment, IconDelegates iconDelegates) => - IconImpl.Show(label, textureId, size, color, iconAlignment, iconDelegates); + public static bool Icon(string label, uint textureId, Vector2 imageSize, Vector4 color, IconAlignment iconAlignment, IconDelegates iconDelegates) => + IconImpl.Show(label, textureId, imageSize, color, iconAlignment, iconDelegates); /// /// Calculates the size of the icon with the specified parameters. /// /// The label of the icon. - /// The size of the icon. + /// The size of the image. /// The calculated size of the icon. - public static Vector2 CalcIconSize(string label, float size) => CalcIconSize(label, new Vector2(size), IconAlignment.Horizontal); + public static Vector2 CalcIconSize(string label, float imageSize) => CalcIconSize(label, new Vector2(imageSize), IconAlignment.Horizontal); /// /// Calculates the size of the icon with the specified parameters. /// /// The label of the icon. - /// The size of the icon. + /// The size of the image. /// The alignment of the icon. /// The calculated size of the icon. - public static Vector2 CalcIconSize(string label, float size, IconAlignment iconAlignment) => CalcIconSize(label, new Vector2(size), iconAlignment); + public static Vector2 CalcIconSize(string label, float imageSize, IconAlignment iconAlignment) => CalcIconSize(label, new Vector2(imageSize), iconAlignment); /// /// Calculates the size of the icon with the specified parameters. /// /// The label of the icon. - /// The size of the icon. + /// The size of the image. /// The calculated size of the icon. - public static Vector2 CalcIconSize(string label, Vector2 size) => CalcIconSize(label, size, IconAlignment.Horizontal); + public static Vector2 CalcIconSize(string label, Vector2 imageSize) => CalcIconSize(label, imageSize, IconAlignment.Horizontal); /// /// Calculates the size of the icon with the specified parameters. /// /// The label of the icon. - /// The size of the icon. - /// The alignment of the icon. - /// The calculated size of the icon. - public static Vector2 CalcIconSize(string label, Vector2 size, IconAlignment iconAlignment) + /// The size of the image. + /// The alignment of the image and label with respect to each other. + /// The calculated size of the widget. + public static Vector2 CalcIconSize(string label, Vector2 imageSize, IconAlignment iconAlignment) { var style = ImGui.GetStyle(); var framePadding = style.FramePadding; @@ -180,18 +180,18 @@ public static Vector2 CalcIconSize(string label, Vector2 size, IconAlignment ico var labelSize = ImGui.CalcTextSize(label); if (iconAlignment == IconAlignment.Horizontal) { - var sizeWithLabel = size + new Vector2(labelSize.X + itemSpacing.X, 0); - sizeWithLabel.Y = Math.Max(sizeWithLabel.Y, labelSize.Y); - return sizeWithLabel + (framePadding * 2); + var boundingBoxSize = imageSize + new Vector2(labelSize.X + itemSpacing.X, 0); + boundingBoxSize.Y = Math.Max(boundingBoxSize.Y, labelSize.Y); + return boundingBoxSize + (framePadding * 2); } else if (iconAlignment == IconAlignment.Vertical) { - var sizeWithLabel = size + new Vector2(0, labelSize.Y + itemSpacing.Y); - sizeWithLabel.X = Math.Max(sizeWithLabel.X, labelSize.X); - return sizeWithLabel + (framePadding * 2); + var boundingBoxSize = imageSize + new Vector2(0, labelSize.Y + itemSpacing.Y); + boundingBoxSize.X = Math.Max(boundingBoxSize.X, labelSize.X); + return boundingBoxSize + (framePadding * 2); } - return size; + return imageSize; } /// @@ -204,12 +204,12 @@ internal static class IconImpl /// /// The label of the icon. /// The texture ID of the icon. - /// The size of the icon. + /// The size of the image. /// The color of the icon. /// The alignment of the icon. /// The delegates for icon events. /// True if the icon was clicked; otherwise, false. - public static bool Show(string label, uint textureId, Vector2 size, Vector4 color, IconAlignment iconAlignment, IconDelegates iconDelegates) + public static bool Show(string label, uint textureId, Vector2 imageSize, Vector4 color, IconAlignment iconAlignment, IconDelegates iconDelegates) { bool wasClicked = false; @@ -220,29 +220,31 @@ public static bool Show(string label, uint textureId, Vector2 size, Vector4 colo ImGui.PushID(label); var cursorStartPos = ImGui.GetCursorScreenPos(); - var sizeWithLabel = CalcIconSize(label, size, iconAlignment); + var labelSize = ImGui.CalcTextSize(label);// TODO, maybe pass this to an internal overload of CalcIconSize to save recalculating + var boundingBoxSize = CalcIconSize(label, imageSize, iconAlignment); + ImGui.SetCursorScreenPos(cursorStartPos + framePadding); switch (iconAlignment) { case IconAlignment.Horizontal: - HorizontalLayout(label, textureId, size, itemSpacing, color, cursorStartPos); + HorizontalLayout(label, textureId, imageSize, labelSize, boundingBoxSize, itemSpacing, color, cursorStartPos); break; case IconAlignment.Vertical: - VerticalLayout(label, textureId, size, itemSpacing, color, cursorStartPos); + VerticalLayout(label, textureId, imageSize, labelSize, boundingBoxSize, itemSpacing, color, cursorStartPos); break; default: throw new NotImplementedException(); } ImGui.SetCursorScreenPos(cursorStartPos); - ImGui.Dummy(sizeWithLabel); + ImGui.Dummy(boundingBoxSize); bool isHovered = ImGui.IsItemHovered(); if (isHovered) { uint borderColor = ImGui.GetColorU32(ImGui.GetStyle().Colors[(int)ImGuiCol.Border]); var drawList = ImGui.GetWindowDrawList(); - drawList.AddRect(cursorStartPos, cursorStartPos + sizeWithLabel, ImGui.GetColorU32(borderColor)); + drawList.AddRect(cursorStartPos, cursorStartPos + boundingBoxSize, ImGui.GetColorU32(borderColor)); if (ImGui.IsMouseClicked(ImGuiMouseButton.Left)) { @@ -274,44 +276,22 @@ public static bool Show(string label, uint textureId, Vector2 size, Vector4 colo return wasClicked; } - /// - /// Arranges the icon and label in a vertical layout. - /// - /// The label of the icon. - /// The texture ID of the icon. - /// The size of the icon. - /// The spacing between items. - /// The color of the icon. - /// The starting position of the cursor. - private static void VerticalLayout(string label, uint textureId, Vector2 size, Vector2 itemSpacing, Vector4 color, Vector2 cursorStartPos) + private static void VerticalLayout(string label, uint textureId, Vector2 imageSize, Vector2 labelSize, Vector2 boundingBoxSize, Vector2 itemSpacing, Vector4 color, Vector2 cursorStartPos) { - var sizeWithLabel = CalcIconSize(label, size, IconAlignment.Vertical); - var imageTopLeft = cursorStartPos + new Vector2((sizeWithLabel.X - size.X) / 2, 0); + var imageTopLeft = cursorStartPos + new Vector2((boundingBoxSize.X - imageSize.X) / 2, 0); ImGui.SetCursorScreenPos(imageTopLeft); - ImGui.Image((nint)textureId, size, Vector2.Zero, Vector2.One, color); - var labelSize = ImGui.CalcTextSize(label); - var labelTopLeft = cursorStartPos + new Vector2((sizeWithLabel.X - labelSize.X) / 2, size.Y + itemSpacing.Y); + ImGui.Image((nint)textureId, imageSize, Vector2.Zero, Vector2.One, color); + + var labelTopLeft = cursorStartPos + new Vector2((boundingBoxSize.X - labelSize.X) / 2, imageSize.Y + itemSpacing.Y); ImGui.SetCursorScreenPos(labelTopLeft); ImGui.TextUnformatted(label); } - /// - /// Arranges the icon and label in a horizontal layout. - /// - /// The label of the icon. - /// The texture ID of the icon. - /// The size of the icon. - /// The spacing between items. - /// The color of the icon. - /// The starting position of the cursor. - private static void HorizontalLayout(string label, uint textureId, Vector2 size, Vector2 itemSpacing, Vector4 color, Vector2 cursorStartPos) + private static void HorizontalLayout(string label, uint textureId, Vector2 imageSize, Vector2 labelSize, Vector2 boundingBoxSize, Vector2 itemSpacing, Vector4 color, Vector2 cursorStartPos) { - ImGui.Image((nint)textureId, size, Vector2.Zero, Vector2.One, color); - - var labelSize = ImGui.CalcTextSize(label); - var widgetSize = CalcIconSize(label, size, IconAlignment.Horizontal); - var leftAlign = new Vector2(0, widgetSize.Y); - ImGui.SetCursorScreenPos(cursorStartPos + new Vector2(size.X + itemSpacing.X, 0)); + ImGui.Image((nint)textureId, imageSize, Vector2.Zero, Vector2.One, color); + var leftAlign = new Vector2(0, boundingBoxSize.Y); + ImGui.SetCursorScreenPos(cursorStartPos + new Vector2(imageSize.X + itemSpacing.X, 0)); Alignment.CenterWithin(labelSize, leftAlign); ImGui.TextUnformatted(label); }