Skip to content

Commit 8c34e3b

Browse files
committed
refactor(imgui): unify and extend Image/ImageButton overloads
- Consolidate Image and ImageButton methods for textures and render textures into unified overloads with optional parameters for size and UV coordinates. - Add support for displaying and flipping portions of textures using source rectangles. - Remove redundant ImageSize, ImageRect, and ImageRenderTextureFit methods in favor of new overloads. - Update XML documentation to reflect new usage and parameter options. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 8e565ce commit 8c34e3b

1 file changed

Lines changed: 45 additions & 57 deletions

File tree

src/Raylib.NET.ImGui/RlImGui.cs

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -175,36 +175,26 @@ public static void Shutdown()
175175
}
176176

177177
/// <summary>
178-
/// Display a texture as an image in ImGui.
178+
/// Display a texture as an image with custom size and UV coordinates.
179179
/// </summary>
180-
public static void Image(Texture image)
180+
public static void Image(Texture image, Vector2 size = default, Vector2 uv0 = default, Vector2 uv1 = default)
181181
{
182182
var texRef = new ImTextureRef(null, (nint)(ulong)image.Id);
183-
ImGui.Image(texRef, new Vector2(image.Width, image.Height));
183+
ImGui.Image(texRef, size, uv0, uv1);
184184
}
185185

186186
/// <summary>
187-
/// Display a texture as an image with custom size.
187+
/// Display a texture as an image in ImGui.
188188
/// </summary>
189-
public static void ImageSize(Texture image, int width, int height)
190-
{
191-
var texRef = new ImTextureRef(null, (nint)(ulong)image.Id);
192-
ImGui.Image(texRef, new Vector2(width, height));
193-
}
189+
public static void Image(Texture image) =>
190+
Image(image, new Vector2(image.Width, image.Height));
194191

195-
/// <summary>
196-
/// Display a texture as an image with custom size.
197-
/// </summary>
198-
public static void ImageSize(Texture image, Vector2 size)
199-
{
200-
var texRef = new ImTextureRef(null, (nint)(ulong)image.Id);
201-
ImGui.Image(texRef, size);
202-
}
203192

204193
/// <summary>
205-
/// Display a portion of a texture as an image.
194+
/// Display a portion of a texture as an image using source rectangle (x, y, width, height).
195+
/// Negative width/height values will flip the image on that axis.
206196
/// </summary>
207-
public static void ImageRect(Texture image, int destWidth, int destHeight, Vector4 sourceRect)
197+
public static void Image(Texture image, Vector2 size, Vector4 sourceRect)
208198
{
209199
Vector2 uv0 = new();
210200
Vector2 uv1 = new();
@@ -231,64 +221,62 @@ public static void ImageRect(Texture image, int destWidth, int destHeight, Vecto
231221
uv1.Y = uv0.Y + (float)(sourceRect.W / image.Height);
232222
}
233223

234-
var texRef = new ImTextureRef(null, (nint)(ulong)image.Id);
235-
ImGui.Image(texRef, new Vector2(destWidth, destHeight), uv0, uv1);
224+
Image(image, size, uv0, uv1);
236225
}
237226

238227
/// <summary>
239228
/// Display a render texture, automatically flipping Y axis.
240229
/// </summary>
241-
public static void ImageRenderTexture(RenderTexture image)
242-
{
243-
ImageRect(image.Texture, image.Texture.Width, image.Texture.Height, new Vector4(0, 0, image.Texture.Width, -image.Texture.Height));
244-
}
230+
public static void Image(RenderTexture image) =>
231+
Image(image.Texture, new Vector2(image.Texture.Width, image.Texture.Height), new Vector4(0, 0, image.Texture.Width, -image.Texture.Height));
245232

246233
/// <summary>
247-
/// Display a render texture fitted to content area.
234+
/// Display a render texture with custom size, automatically flipping Y axis.
248235
/// </summary>
249-
public static void ImageRenderTextureFit(RenderTexture image, bool center = true)
250-
{
251-
Vector2 area = ImGui.GetContentRegionAvail();
252-
253-
float scale = area.X / image.Texture.Width;
254-
255-
float y = image.Texture.Height * scale;
256-
if (y > area.Y)
257-
{
258-
scale = area.Y / image.Texture.Height;
259-
}
260-
261-
int sizeX = (int)(image.Texture.Width * scale);
262-
int sizeY = (int)(image.Texture.Height * scale);
263-
264-
if (center)
265-
{
266-
ImGui.SetCursorPosX(0);
267-
ImGui.SetCursorPosX(area.X / 2 - sizeX / 2);
268-
ImGui.SetCursorPosY(ImGui.GetCursorPosY() + (area.Y / 2 - sizeY / 2));
269-
}
270-
271-
ImageRect(image.Texture, sizeX, sizeY, new Vector4(0, 0, image.Texture.Width, -image.Texture.Height));
272-
}
236+
public static void Image(RenderTexture image, Vector2 size) =>
237+
Image(image.Texture, size, new Vector4(0, 0, image.Texture.Width, -image.Texture.Height));
273238

274239
/// <summary>
275240
/// Display a texture as a clickable button.
276241
/// </summary>
277-
public static bool ImageButton(string name, Texture image)
242+
public static bool ImageButton(string name, Texture image, Vector2 size = default, Vector2 uv0 = default, Vector2 uv1 = default)
278243
{
279-
return ImageButtonSize(name, image, new Vector2(image.Width, image.Height));
244+
var texRef = new ImTextureRef(null, (nint)(ulong)image.Id);
245+
return ImGui.ImageButton(name, texRef, size, uv0, uv1);
280246
}
281247

282248
/// <summary>
283-
/// Display a texture as a clickable button with custom size.
249+
/// Display a portion of a texture as a clickable button using source rectangle (x, y, width, height).
250+
/// Negative width/height values will flip the image on that axis.
284251
/// </summary>
285-
public static bool ImageButtonSize(string name, Texture image, Vector2 size)
252+
public static bool ImageButton(string name, Texture image, Vector2 size, Vector4 sourceRect)
286253
{
287-
var texRef = new ImTextureRef(null, (nint)(ulong)image.Id);
288-
fixed (byte* namePtr = System.Text.Encoding.UTF8.GetBytes(name + "\0"))
254+
Vector2 uv0 = new();
255+
Vector2 uv1 = new();
256+
257+
if (sourceRect.Z < 0)
258+
{
259+
uv0.X = -((float)sourceRect.X / image.Width);
260+
uv1.X = (uv0.X - (float)(Math.Abs(sourceRect.Z) / image.Width));
261+
}
262+
else
263+
{
264+
uv0.X = (float)sourceRect.X / image.Width;
265+
uv1.X = uv0.X + (float)(sourceRect.Z / image.Width);
266+
}
267+
268+
if (sourceRect.W < 0)
269+
{
270+
uv0.Y = -((float)sourceRect.Y / image.Height);
271+
uv1.Y = (uv0.Y - (float)(Math.Abs(sourceRect.W) / image.Height));
272+
}
273+
else
289274
{
290-
return ImGui.ImageButton(namePtr, texRef, size);
275+
uv0.Y = (float)sourceRect.Y / image.Height;
276+
uv1.Y = uv0.Y + (float)(sourceRect.W / image.Height);
291277
}
278+
279+
return ImageButton(name, image, size, uv0, uv1);
292280
}
293281

294282
//----------------------------------------------------------------------

0 commit comments

Comments
 (0)