Skip to content

Commit

Permalink
Sanity check when drawing image in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Oct 2, 2024
1 parent 39d8135 commit 1ab2ea2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
8 changes: 7 additions & 1 deletion CentrED/UI/UIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,13 @@ internal void DrawImage(Texture2D tex, Rectangle bounds)
DrawImage(tex, bounds, new Vector2(bounds.Width, bounds.Height));
}

internal void DrawImage(Texture2D tex, Rectangle bounds, Vector2 size, bool stretch = false)
internal bool DrawImage(Texture2D tex, Rectangle bounds, Vector2 size, bool stretch = false)
{
if (tex == null)
{
ImGui.Dummy(size);
return false;
}
var texPtr = _uiRenderer.BindTexture(tex);
var oldPos = ImGui.GetCursorPos();
var offsetX = (size.X - bounds.Width) / 2;
Expand All @@ -458,6 +463,7 @@ internal void DrawImage(Texture2D tex, Rectangle bounds, Vector2 size, bool stre
var uv0 = new Vector2((bounds.X - uvOffsetX) / fWidth, (bounds.Y - uvOffsetY) / fHeight);
var uv1 = new Vector2((bounds.X + bounds.Width + uvOffsetX) / fWidth, (bounds.Y + bounds.Height + uvOffsetY) / fHeight);
ImGui.Image(texPtr, targetSize, uv0, uv1);
return true;
}

public static void Tooltip(string text)
Expand Down
5 changes: 4 additions & 1 deletion CentrED/UI/Windows/FilterWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ startPos with

if (ImGui.TableNextColumn())
{
CEDGame.UIManager.DrawImage(spriteInfo.Texture, bounds, StaticDimensions);
if (!CEDGame.UIManager.DrawImage(spriteInfo.Texture, bounds, StaticDimensions) && CEDGame.MapManager.DebugLogging)
{
Console.WriteLine($"[FilterWindow] No texture found for tile 0x{index:X4}");
}
if(ImGui.IsItemHovered() && (bounds.Width > StaticDimensions.X || bounds.Height > StaticDimensions.Y))
{
ImGui.BeginTooltip();
Expand Down
9 changes: 1 addition & 8 deletions CentrED/UI/Windows/LandBrushWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,7 @@ protected override void InternalDraw()
{
var brush = landBrushes[names[i]];
var spriteInfo = CEDGame.MapManager.Texmaps.GetTexmap(TileDataLoader.Instance.LandData[brush.Tiles[0]].TexID);
if(spriteInfo.Texture != null)
{
CEDGame.UIManager.DrawImage(spriteInfo.Texture, spriteInfo.UV, PreviewTexSize, true);
}
else
{
ImGui.Dummy(PreviewTexSize);
}
CEDGame.UIManager.DrawImage(spriteInfo.Texture, spriteInfo.UV, PreviewTexSize, true);
}
else
{
Expand Down
12 changes: 10 additions & 2 deletions CentrED/UI/Windows/TilesWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ private void DrawTilesGridRow()
}
else
{
CEDGame.UIManager.DrawImage(tileInfo.Texture, tileInfo.Bounds, TilesDimensions);
if (!CEDGame.UIManager.DrawImage(tileInfo.Texture, tileInfo.Bounds, TilesDimensions)
&& CEDGame.MapManager.DebugLogging)
{
Console.WriteLine($"[TilesWindow] No texture found for tile 0x{tileIndex:X4}");
}
}
}
ImGui.SetCursorPosY(ImGui.GetCursorPosY() - TilesDimensions.Y - ImGui.GetStyle().ItemSpacing.Y);
Expand Down Expand Up @@ -555,7 +559,11 @@ private void DrawTileRow(int index, TileInfo tileInfo)
}
else
{
CEDGame.UIManager.DrawImage(tileInfo.Texture, tileInfo.Bounds, TilesDimensions);
if (!CEDGame.UIManager.DrawImage(tileInfo.Texture, tileInfo.Bounds, TilesDimensions) &&
CEDGame.MapManager.DebugLogging)
{
Console.WriteLine($"[TilesWindow] No texture found for tile 0x{index:X4}");
}
if(ImGui.IsItemHovered() && (tileInfo.Bounds.Width > TilesDimensions.X || tileInfo.Bounds.Height > TilesDimensions.Y))
{
ImGui.BeginTooltip();
Expand Down

0 comments on commit 1ab2ea2

Please sign in to comment.