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
349 stars 55 forks source link

Custom drawing doesn't work #32

Closed Hirashi3630 closed 2 years ago

Hirashi3630 commented 2 years ago

I tried recreating what imgui_demo.cpp shows

ImGui.Begin(WindowName, ref IsWindowOpen, ImGuiWindowFlags.None);

ImDrawListPtr drawList = ImGui.GetWindowDrawList();

drawList.AddRect(new Vector2(10,10), new Vector2(20, 20), 1973790, 0.0f, ImDrawFlags.None, 2); 

ImGui.End();

but no luck... I'm not sure if I'm doing something wrong or if it's a bug.

Unity 2020.3.25.f1 ImGUI 1.84 WIP uimgui 4.1.0

psydack commented 2 years ago

Hello there.

Sorry, at the moment I'm trying to make a version to MacOS, so I don't have access to a windows OS and I can't test.

The syntax looks fine. What's happening? Nothing?

Could you test the code below please?

private void OnLayout(UImGui.UImGui obj)
{
    if (!ImGui.Begin("Draw list", ref isOpen)) return;

    var drawList = ImGui.GetWindowDrawList();
    drawList.AddRectFilled(Vector2.zero, Vector2.one * 10, 6528);
    drawList.AddRectFilled(Vector2.one, Vector2.one * 20, 4278190335);
    drawList.AddRectFilled(ImGui.GetCursorPos(), ImGui.GetCursorPos() * 10, 16711680);
    drawList.AddRectFilled(ImGui.GetCursorPos() * - 10, ImGui.GetCursorPos(), 4278190335);

    ImGui.End();
}
Hirashi3630 commented 2 years ago

Sure thing! Thanks for looking into this!

I tried your code but nothing seems appear.

for some reason ImGui.ShowDemoWindow() custom drawing example works just fine though

image

psydack commented 2 years ago

I don't know if it's necessary to use ImGui.InvisibleButton().

I just replicate the whole code showwing the gradient on custom rendering window.

if (!ImGui.Begin("Draw list", ref isOpen)) return;

ImGui.PushItemWidth(-ImGui.GetFontSize() * 15);
var drawList = ImGui.GetWindowDrawList();

ImGui.Text("Gradients");
var gradientSize = new Vector2(ImGui.CalcItemWidth(), ImGui.GetFrameHeight());

//gradient 1
{
    var p0 = ImGui.GetCursorScreenPos();
    var p1 = new Vector2(p0.x + gradientSize.x, p0.y + gradientSize.y);
    var colorA = ImGui.GetColorU32(new Vector4(0, 0, 0, 255));
    var colorB = ImGui.GetColorU32(new Vector4(255, 255, 255, 255));
    drawList.AddRectFilledMultiColor(p0, p1, colorA, colorB, colorB, colorA);
    ImGui.InvisibleButton("##gradient1", gradientSize);
}

// gradient 2
{
    var p0 = ImGui.GetCursorScreenPos();
    var p1 = new Vector2(p0.x + gradientSize.x, p0.y + gradientSize.y);
    var colorA = ImGui.GetColorU32(new Vector4(0, 255, 0, 255));
    var colorB = ImGui.GetColorU32(new Vector4(255, 0, 0, 255));
    drawList.AddRectFilledMultiColor(p0, p1, colorA, colorB, colorB, colorA);
    ImGui.InvisibleButton("##gradient2", gradientSize);
}

const float sz = 36f;
const float spacing = 10f;
const float value = sz + spacing;
ImGui.Dummy(new Vector2(value * 10.2f, value * 3.0f));
ImGui.PopItemWidth();
ImGui.End();

I never tried to do custom rendering stuffs. It appears to be a bug. Later this month I probably have a functional cimgui working on mac. So, after that I will give a shoot to go deeper.

Thanks for reporting it.

Hirashi3630 commented 2 years ago

It seems to need ImGui.InvisibleButton() to render properly

image

Anyway I will try to play with it more and keep you updated if I make it work!

Cheers!

psydack commented 2 years ago

Nice. Awesome to know that works. In the end wasn’t a bug .

;)