Skip to content

Commit

Permalink
Support AtlasTexture in image widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Jan 11, 2024
1 parent e47f122 commit aa0d44d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
dotnet: ["6.0.x", "7.0.x"]
dotnet: ["6.0.x", "7.0.x", "8.0.x"]

name: .NET ${{ matrix.dotnet }}

Expand Down
4 changes: 2 additions & 2 deletions Dear ImGui for Godot Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Godot.NET.Sdk/4.1.2">
<Project Sdk="Godot.NET.Sdk/4.2.1">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
Expand All @@ -11,6 +11,6 @@
<EmbeddedResource Remove="doc\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.89.9.3" />
<PackageReference Include="ImGui.NET" Version="1.90.0.1" />
</ItemGroup>
</Project>
40 changes: 40 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Widgets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ public static void Image(Texture2D tex, Vector2 size, Vector2 uv0, Vector2 uv1,
ImGuiNative.igImage((IntPtr)tex.GetRid().Id, size, uv0, uv1, tint_col, border_col);
}

public static void Image(AtlasTexture tex, Vector2 size)
{
Image(tex, size, Vector4.One, Vector4.Zero);
}

public static void Image(AtlasTexture tex, Vector2 size, Vector4 tint_col)
{
Image(tex, size, tint_col, Vector4.Zero);
}

public static void Image(AtlasTexture tex, Vector2 size, Vector4 tint_col, Vector4 border_col)
{
(Vector2 uv0, Vector2 uv1) = GetAtlasUVs(tex);
ImGuiNative.igImage((IntPtr)tex.GetRid().Id, size, uv0, uv1, tint_col, border_col);
}

public static bool ImageButton(string str_id, Texture2D tex, Vector2 size)
{
return ImageButton(str_id, tex, size, Vector2.Zero, Vector2.One, Vector4.Zero, Vector4.One);
Expand All @@ -87,5 +103,29 @@ public static bool ImageButton(string str_id, Texture2D tex, Vector2 size, Vecto
{
return ImGui.ImageButton(str_id, (IntPtr)tex.GetRid().Id, size, uv0, uv1, bg_col, tint_col);
}

public static bool ImageButton(string str_id, AtlasTexture tex, Vector2 size)
{
return ImageButton(str_id, tex, size, Vector4.Zero, Vector4.One);
}

public static bool ImageButton(string str_id, AtlasTexture tex, Vector2 size, Vector4 bg_col)
{
return ImageButton(str_id, tex, size, bg_col, Vector4.One);
}

public static bool ImageButton(string str_id, AtlasTexture tex, Vector2 size, Vector4 bg_col, Vector4 tint_col)
{
(Vector2 uv0, Vector2 uv1) = GetAtlasUVs(tex);
return ImGui.ImageButton(str_id, (IntPtr)tex.GetRid().Id, size, uv0, uv1, bg_col, tint_col);
}

private static (Vector2 uv0, Vector2 uv1) GetAtlasUVs(AtlasTexture tex)
{
Godot.Vector2 atlasSize = tex.Atlas.GetSize();
Godot.Vector2 guv0 = tex.Region.Position / atlasSize;
Godot.Vector2 guv1 = tex.Region.End / atlasSize;
return (new(guv0.X, guv0.Y), new(guv1.X, guv1.Y));
}
}
#endif
2 changes: 1 addition & 1 deletion data/icon.svg.import
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
Expand Down
7 changes: 7 additions & 0 deletions data/robot_eye.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="AtlasTexture" load_steps=2 format=3 uid="uid://cqelg04ohkgm5"]

[ext_resource type="Texture2D" uid="uid://2cvgmt2fmpao" path="res://data/icon.svg" id="1_ofydq"]

[resource]
atlas = ExtResource("1_ofydq")
region = Rect2(142, 228, 64, 64)
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ config_version=5

config/name="Dear ImGui for Godot Demo"
run/main_scene="res://data/demo.tscn"
config/features=PackedStringArray("4.1", "C#")
config/features=PackedStringArray("4.2", "C#")
config/icon="res://data/icon.svg"

[autoload]
Expand Down
6 changes: 5 additions & 1 deletion src/MySecondNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public partial class MySecondNode : Node
{
#if !GODOT_MOBILE
private Texture2D _iconTexture = null!;
private AtlasTexture _atlasTexture = null!;
private SubViewport _vp = null!;
private int _iconSize = 64;
private float _scale;
Expand Down Expand Up @@ -59,6 +60,7 @@ public override void _Ready()
{
ImGuiLayer.Connect(OnImGuiLayout);
_iconTexture = GD.Load<Texture2D>("res://data/icon.svg");
_atlasTexture = GD.Load<AtlasTexture>("res://data/robot_eye.tres");
_vp = GetNode<SubViewport>("%SubViewport");
_scale = ImGuiGD.Scale;
GetNode<Button>("%ShowHideButton").Pressed += OnShowHidePressed;
Expand Down Expand Up @@ -106,8 +108,10 @@ private void OnImGuiLayout()
ImGui.PopFont();

ImGui.Separator();
ImGui.Text("Simple texture");
ImGui.Text("Textures");
Widgets.Image(_iconTexture, new(_iconSize, _iconSize));
ImGui.SameLine();
Widgets.Image(_atlasTexture, new(_iconSize, _iconSize));
ImGui.DragInt("size", ref _iconSize, 1.0f, 32, 512);

ImGui.Separator();
Expand Down

0 comments on commit aa0d44d

Please sign in to comment.