apicici / cimgui-love

LÖVE module for Dear ImGui obtained by wrapping cimgui with LuaJIT FFI.
MIT License
76 stars 6 forks source link

FFI booleans doesn't work with imgui.Begin #12

Closed kithf closed 1 year ago

kithf commented 1 year ago

When I change value of the boolean, or even create new boolean, imgui doesn't react to changes. But when I use FFI booleans with Checkbox, Checkbox works as expected.

love.load = function()
  imgui.love.Init()
  some_bool = ffi.new("bool[1]", true)
end

love.draw = function()
  print(some_bool[0]) -- prints value of boolean, when i click button its false, on startup its true
  if imgui.Begin("Test", some_bool) then
    if imgui.Button "Close" then
      some_bool = ffi.new("bool[1]", false)
      -- same
      -- some_bool[0] = false
    end

    imgui.End()
  end

  imgui.Render()
  imgui.love.RenderDrawLists()
end
apicici commented 1 year ago

That's not how the bool* input of Begin works. From the documentation in imgui.h:

Passing 'bool* p_open != NULL' shows a window-closing widget in the upper-right corner of the window, which clicking will set the boolean to false when clicked.

You should do this to achieve what you want:

love.draw = function()
  if some_bool[0] then
    if imgui.Begin("Test", some_bool) then
      if imgui.Button "Close" then
        some_bool[0] = false -- note that you don't have to create a new bool[1], you can just change the value of the existing one
      end
    end
    imgui.End()
  end

  imgui.Render()
  imgui.love.RenderDrawLists()
end

Two things to note: