psydack / uimgui

UImGui (Unity ImGui) is an UPM package for the immediate mode GUI library using ImGui.NET. This project is based on RG.ImGui project.
MIT License
320 stars 50 forks source link

[FEATURE] BeginTabItem missing possibility to set Flags without open/close ref bool. #34

Open CaptainTimberTim opened 2 years ago

CaptainTimberTim commented 2 years ago

Hi, I just had the problem that the BeginTabItem cannot be called easily with flags enabled, if you don't want the open/close tab feature. In the C version of ImGui this can be done by passing a nullpointer as the p_open param. But as C# does not allow this in normal code (as far as I know?), there would need to be an additional overload without the "ref bool p_open" param.

For me I now solved this by copying the generated implementation of the imguiNET and changing it by just passing null.

public static bool BeginTabItem(string label, ImGuiTabItemFlags flags)
{
    byte* native_label;
    int label_byteCount = 0;
    if (label != null)
    {
        label_byteCount = Encoding.UTF8.GetByteCount(label);
        if (label_byteCount > 2048)
        {
            native_label = (byte*)Marshal.AllocHGlobal(label_byteCount + 1);
        }
        else
        {
            byte* native_label_stackBytes = stackalloc byte[label_byteCount + 1];
            native_label = native_label_stackBytes;
        }
        int native_label_offset = GetUtf8(label, native_label, label_byteCount);
        native_label[native_label_offset] = 0;
    }
    else { native_label = null; }
    byte ret = ImGuiNative.igBeginTabItem(native_label, null, flags);
    if (label_byteCount > 2048)
    {
        Marshal.FreeHGlobal((IntPtr)native_label);
    }
    return ret != 0;
}

private static int GetUtf8(string s, byte* utf8Bytes, int utf8ByteCount)
{
    fixed (char* utf16Ptr = s)
    {
        return Encoding.UTF8.GetBytes(utf16Ptr, s.Length, utf8Bytes, utf8ByteCount);
    }
}
psydack commented 2 years ago

Hello there.

This is awesome.

If you want to make a merge request in our custom ImGui.Net project , I would really appreciate.

Edit: I will use that to remember to change the code generator.

Thank you.