Skip to content

Commit

Permalink
use Roslynator
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Feb 3, 2024
1 parent 411a560 commit 809c237
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 75 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ dotnet_diagnostic.CA1001.severity = none
dotnet_diagnostic.CA1725.severity = none
dotnet_diagnostic.CA1707.severity = none
dotnet_diagnostic.CA1711.severity = none

# Roslynator
dotnet_diagnostic.rcs0056.severity = warning
roslynator_max_line_length = 100

dotnet_diagnostic.rcs1194.severity = none
dotnet_diagnostic.rcs1129.severity = none
dotnet_diagnostic.rcs1061.severity = none
dotnet_diagnostic.rcs1006.severity = none
dotnet_diagnostic.rcs1141.severity = none
dotnet_diagnostic.rcs1161.severity = none
16 changes: 16 additions & 0 deletions Dear ImGui for Godot Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,21 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.90.1.1" />
<PackageReference Include="Roslynator.Analyzers" Version="4.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Roslynator.CodeAnalysis.Analyzers" Version="4.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Roslynator.CodeFixes" Version="4.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Roslynator.Formatting.Analyzers" Version="4.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
6 changes: 2 additions & 4 deletions addons/imgui-godot/ImGuiGodot/ImGuiGD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ public static bool ToolInit()
nbe.ToolInit();
return true;
}
else
{
return false;
}

return false;
}

internal static bool SubViewportWidget(SubViewport svp)
Expand Down
25 changes: 17 additions & 8 deletions addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ public override void _EnterTree()
_ci = RenderingServer.CanvasItemCreate();
RenderingServer.CanvasItemSetParent(_ci, GetCanvas());

Node cfgScene = ResourceLoader.Load<PackedScene>("res://addons/imgui-godot/Config.tscn").Instantiate();
Resource cfg = (Resource)cfgScene.Get("Config") ?? (Resource)((GDScript)GD.Load("res://addons/imgui-godot/scripts/ImGuiConfig.gd")).New();
Node cfgScene = ResourceLoader.Load<PackedScene>("res://addons/imgui-godot/Config.tscn")
.Instantiate();
Resource cfg = (Resource)cfgScene.Get("Config") ?? (Resource)((GDScript)GD.Load(
"res://addons/imgui-godot/scripts/ImGuiConfig.gd")).New();
cfgScene.Free();

Layer = (int)cfg.Get("Layer");
Expand Down Expand Up @@ -127,11 +129,17 @@ public override void _Process(double delta)
// this is more or less how SubViewportContainer works
_subViewportSize = winSize;
_finalTransform = ft;
RenderingServer.ViewportSetSize(_subViewportRid, _subViewportSize.X, _subViewportSize.Y);
RenderingServer.ViewportSetSize(
_subViewportRid,
_subViewportSize.X,
_subViewportSize.Y);
Rid vptex = RenderingServer.ViewportGetTexture(_subViewportRid);
RenderingServer.CanvasItemClear(_ci);
RenderingServer.CanvasItemSetTransform(_ci, ft.AffineInverse());
RenderingServer.CanvasItemAddTextureRect(_ci, new(0, 0, _subViewportSize.X, _subViewportSize.Y), vptex);
RenderingServer.CanvasItemAddTextureRect(
_ci,
new(0, 0, _subViewportSize.X, _subViewportSize.Y),
vptex);
}

Signaler.EmitSignal("imgui_layout");
Expand All @@ -143,9 +151,9 @@ public override void _Notification(int what)
Internal.Input.ProcessNotification(what);
}

public override void _Input(InputEvent e)
public override void _Input(InputEvent @event)
{
if (Internal.State.Instance.ProcessInput(e, _window))
if (Internal.State.Instance.ProcessInput(@event, _window))
{
_window.SetInputAsHandled();
}
Expand All @@ -166,8 +174,9 @@ private void CheckContentScale()

private void PrintErrContentScale()
{
GD.PrintErr($"imgui-godot only supports content scale modes {Window.ContentScaleModeEnum.Disabled}" +
$" or {Window.ContentScaleModeEnum.CanvasItems}");
GD.PrintErr(
$"imgui-godot only supports content scale modes {Window.ContentScaleModeEnum.Disabled}"
+ $" or {Window.ContentScaleModeEnum.CanvasItems}");
GD.PrintErr($" current mode is {_window.ContentScaleMode}/{_window.ContentScaleAspect}");
}
}
Expand Down
12 changes: 10 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/BackendNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ private sealed class PropertyName
public static readonly StringName Visible = "Visible";
}

public float JoyAxisDeadZone { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
public float JoyAxisDeadZone
{
get => throw new System.NotImplementedException();
set => throw new System.NotImplementedException();
}
public float Scale { get; set; } = 1.0f; // TODO: make property
public bool Visible { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
public bool Visible
{
get => throw new System.NotImplementedException();
set => throw new System.NotImplementedException();
}

public void AddFont(FontFile fontData, int fontSize, bool merge)
{
Expand Down
10 changes: 5 additions & 5 deletions addons/imgui-godot/ImGuiGodot/Internal/BackendNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ public void ResetFonts()
State.Instance.Fonts.ResetFonts();
}

public bool SubViewportWidget(SubViewport vp)
public bool SubViewportWidget(SubViewport svp)
{
Vector2 vpSize = new(vp.Size.X, vp.Size.Y);
Vector2 vpSize = new(svp.Size.X, svp.Size.Y);
var pos = ImGui.GetCursorScreenPos();
var pos_max = new Vector2(pos.X + vpSize.X, pos.Y + vpSize.Y);
ImGui.GetWindowDrawList().AddImage((IntPtr)vp.GetTexture().GetRid().Id, pos, pos_max);
ImGui.GetWindowDrawList().AddImage((IntPtr)svp.GetTexture().GetRid().Id, pos, pos_max);

ImGui.PushID(vp.NativeInstance);
ImGui.PushID(svp.NativeInstance);
ImGui.InvisibleButton("godot_subviewport", vpSize);
ImGui.PopID();

if (ImGui.IsItemHovered())
{
State.Instance.Input.CurrentSubViewport = vp;
State.Instance.Input.CurrentSubViewport = svp;
State.Instance.Input.CurrentSubViewportPos = pos;
return true;
}
Expand Down
14 changes: 12 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/CanvasRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ private void RenderOne(Rid vprid, ImDrawDataPtr drawData)
ViewportData vd = _vpData[vprid];
Rid parent = vd.RootCanvasItem;

var window = (GodotImGuiWindow)GCHandle.FromIntPtr(drawData.OwnerViewport.PlatformHandle).Target!;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(drawData.OwnerViewport.PlatformHandle)
.Target!;

if (!_canvasItemPools.ContainsKey(parent))
_canvasItemPools[parent] = [];
Expand Down Expand Up @@ -168,7 +169,16 @@ private void RenderOne(Rid vprid, ImDrawDataPtr drawData)
drawCmd.ClipRect.W - drawCmd.ClipRect.Y)
);

RenderingServer.CanvasItemAddTriangleArray(child, indices, cmdvertices, cmdcolors, cmduvs, null, null, texrid, -1);
RenderingServer.CanvasItemAddTriangleArray(
child,
indices,
cmdvertices,
cmdcolors,
cmduvs,
null,
null,
texrid,
-1);
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions addons/imgui-godot/ImGuiGodot/Internal/Fonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void ResetFonts()

public void AddFont(FontFile? fontData, int fontSize, bool merge)
{
_fontConfiguration.Add(new FontParams { Font = fontData, FontSize = fontSize, Merge = merge });
_fontConfiguration.Add(
new FontParams { Font = fontData, FontSize = fontSize, Merge = merge });
}

private static unsafe void AddFontToAtlas(FontFile? fontData, int fontSize, bool merge)
Expand Down Expand Up @@ -82,7 +83,8 @@ private static unsafe void AddFontToAtlas(FontFile? fontData, int fontSize, bool

private static unsafe ImVector GetRanges(Font font)
{
var builder = new ImFontGlyphRangesBuilderPtr(ImGuiNative.ImFontGlyphRangesBuilder_ImFontGlyphRangesBuilder());
var builder = new ImFontGlyphRangesBuilderPtr(
ImGuiNative.ImFontGlyphRangesBuilder_ImFontGlyphRangesBuilder());
builder.AddText(font.GetSupportedChars());
builder.BuildRanges(out ImVector vec);
builder.Destroy();
Expand Down Expand Up @@ -146,15 +148,18 @@ public unsafe void RebuildFontAtlas(float scale)
AddFontToAtlas(fontParams.Font, (int)(fontParams.FontSize * scale), fontParams.Merge);
}

io.Fonts.GetTexDataAsRGBA32(out byte* pixelData, out int width, out int height, out int bytesPerPixel);
io.Fonts.GetTexDataAsRGBA32(
out byte* pixelData,
out int width,
out int height,
out int bytesPerPixel);

byte[] pixels = new byte[width * height * bytesPerPixel];
Marshal.Copy((IntPtr)pixelData, pixels, 0, pixels.Length);

var img = Image.CreateFromData(width, height, false, Image.Format.Rgba8, pixels);

var imgtex = ImageTexture.CreateFromImage(img);
_fontTexture = imgtex;
_fontTexture = ImageTexture.CreateFromImage(img);
io.Fonts.SetTexID((IntPtr)_fontTexture.GetRid().Id);
io.Fonts.ClearTexData();

Expand Down
15 changes: 8 additions & 7 deletions addons/imgui-godot/ImGuiGodot/Internal/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ private void UpdateMouse(ImGuiIOPtr io)
if (io.WantSetMousePos)
{
// WarpMouse is relative to the current focused window
int[] windows = DisplayServer.GetWindowList();
foreach (int w in windows)
foreach (int w in DisplayServer.GetWindowList())
{
if (DisplayServer.WindowIsFocused(w))
{
var winPos = DisplayServer.WindowGetPosition(w);
Godot.Input.WarpMouse(new(io.MousePos.X - winPos.X, io.MousePos.Y - winPos.Y));
Godot.Input
.WarpMouse(new(io.MousePos.X - winPos.X, io.MousePos.Y - winPos.Y));
break;
}
}
Expand Down Expand Up @@ -97,7 +97,8 @@ public bool ProcessInput(InputEvent evt, Window window)
var vpEvent = evt.Duplicate() as InputEvent;
if (vpEvent is InputEventMouse mouseEvent)
{
mouseEvent.Position = new Vector2(windowPos.X + mouseEvent.GlobalPosition.X - CurrentSubViewportPos.X,
mouseEvent.Position = new Vector2(
windowPos.X + mouseEvent.GlobalPosition.X - CurrentSubViewportPos.X,
windowPos.Y + mouseEvent.GlobalPosition.Y - CurrentSubViewportPos.Y)
.Clamp(Vector2.Zero, CurrentSubViewport.Size);
}
Expand Down Expand Up @@ -142,7 +143,7 @@ public bool ProcessInput(InputEvent evt, Window window)
case MouseButton.WheelRight:
_mouseWheel.X = mb.Factor;
break;
};
}
consumed = io.WantCaptureMouse;
mb.Dispose();
}
Expand Down Expand Up @@ -209,7 +210,7 @@ public bool ProcessInput(InputEvent evt, Window window)
case JoyAxis.TriggerRight:
io.AddKeyAnalogEvent(ImGuiKey.GamepadR2, pressed, v);
break;
};
}
consumed = true;
jm.Dispose();
}
Expand All @@ -228,7 +229,7 @@ public static void ProcessNotification(long what)
case MainLoop.NotificationApplicationFocusOut:
ImGui.GetIO().AddFocusEvent(false);
break;
};
}
}

private static void UpdateKeyMods(ImGuiIOPtr io)
Expand Down
28 changes: 20 additions & 8 deletions addons/imgui-godot/ImGuiGodot/Internal/RdRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ internal class RdRenderer : IRenderer
private readonly ArrayPool<byte> _bufPool = ArrayPool<byte>.Create();

private Rid _idxBuffer;
private int _idxBufferSize = 0; // size in indices
/// <summary>
/// size in indices
/// </summary>
private int _idxBufferSize = 0;
private Rid _vtxBuffer;
private int _vtxBufferSize = 0; // size in vertices
/// <summary>
/// size in vertices
/// </summary>
private int _vtxBufferSize = 0;

private readonly Dictionary<IntPtr, Rid> _uniformSets = new(8);
private readonly HashSet<IntPtr> _usedTextures = new(8);
Expand All @@ -50,7 +56,8 @@ public RdRenderer()

// set up everything to match the official Vulkan backend as closely as possible

using var shaderFile = ResourceLoader.Load<RDShaderFile>("res://addons/imgui-godot/data/ImGuiShader.glsl");
using var shaderFile = ResourceLoader.Load<RDShaderFile>(
"res://addons/imgui-godot/data/ImGuiShader.glsl");
_shader = RD.ShaderCreateFromSpirV(shaderFile.GetSpirV());
if (!_shader.IsValid)
throw new RdRendererException("failed to create shader");
Expand Down Expand Up @@ -82,7 +89,10 @@ public RdRenderer()
Offset = sizeof(float) * 4
};

var vattrs = new Godot.Collections.Array<RDVertexAttribute>() { attrPoints, attrUvs, attrColors };
var vattrs = new Godot.Collections.Array<RDVertexAttribute>() {
attrPoints,
attrUvs,
attrColors };
_vtxFormat = RD.VertexFormatCreate(vattrs);

// blend state
Expand Down Expand Up @@ -198,8 +208,7 @@ private void SetupBuffers(ImDrawDataPtr drawData)
uniform.AddId(_sampler);
uniform.AddId(texrid);
_uniformArray[0] = uniform;
Rid uniformSet = RD.UniformSetCreate(_uniformArray, _shader, 0);
_uniformSets[texid] = uniformSet;
_uniformSets[texid] = RD.UniformSetCreate(_uniformArray, _shader, 0);
}
}
}
Expand All @@ -217,7 +226,8 @@ protected static void ReplaceTextureRids(ImDrawDataPtr drawData)
for (int cmdi = 0; cmdi < cmdList.CmdBuffer.Size; ++cmdi)
{
ImDrawCmdPtr drawCmd = cmdList.CmdBuffer[cmdi];
drawCmd.TextureId = (IntPtr)RenderingServer.TextureGetRdTexture(Util.ConstructRid((ulong)drawCmd.TextureId)).Id;
drawCmd.TextureId = (IntPtr)RenderingServer.TextureGetRdTexture(
Util.ConstructRid((ulong)drawCmd.TextureId)).Id;
}
}
}
Expand Down Expand Up @@ -277,7 +287,9 @@ protected void RenderOne(Rid fb, ImDrawDataPtr drawData)
{
if (_idxBuffer.Id != 0)
RD.FreeRid(_idxBuffer);
_idxBuffer = RD.IndexBufferCreate((uint)drawData.TotalIdxCount, RenderingDevice.IndexBufferFormat.Uint16);
_idxBuffer = RD.IndexBufferCreate(
(uint)drawData.TotalIdxCount,
RenderingDevice.IndexBufferFormat.Uint16);
_idxBufferSize = drawData.TotalIdxCount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;

using SharedList = ImGuiGodot.Internal.DisposableList<Godot.Rid, ImGuiGodot.Internal.ClonedDrawData>;
using SharedList = ImGuiGodot.Internal.DisposableList<Godot.Rid,
ImGuiGodot.Internal.ClonedDrawData>;

namespace ImGuiGodot.Internal;

Expand Down Expand Up @@ -50,7 +51,7 @@ public unsafe void Dispose()

internal sealed class DisposableList<T, U> : List<Tuple<T, U>>, IDisposable where U : IDisposable
{
public DisposableList() : base() { }
public DisposableList() { }
public DisposableList(int capacity) : base(capacity) { }

public void Dispose()
Expand All @@ -70,7 +71,7 @@ internal sealed class RdRendererThreadSafe : RdRenderer, IRenderer
private readonly object _sharedDataLock = new();
private SharedList? _dataToDraw;

public RdRendererThreadSafe() : base()
public RdRendererThreadSafe()
{
// draw on the renderer thread to avoid conflicts
RenderingServer.FramePreDraw += OnFramePreDraw;
Expand Down
Loading

0 comments on commit 809c237

Please sign in to comment.