apicici / cimgui-love

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

love.graphics.setColor affects cimgui-love #10

Closed BinarySpike closed 2 years ago

BinarySpike commented 2 years ago

If you call love.graphics.setColor it will affect the color that cimgui-love renders on the next frame.

example issue image

See line 13 of the below code:

-- Make sure the shared library can be found through package.cpath before loading the module.
-- For example, if you put it in the LÖVE save directory, you could do something like this:
local lib_path = love.filesystem.getWorkingDirectory() .. "/lib"
local extension = jit.os == "Windows" and "dll" or jit.os == "Linux" and "so" or jit.os == "OSX" and "dylib"
package.cpath = string.format("%s;%s/?.%s", package.cpath, lib_path, extension)

local imgui = require "cimgui" -- cimgui is the folder containing the Lua module (the "src" folder in the github repository)

love.load = function()
  love.window.setMode(1920, 1080)
  imgui.love.Init() -- or imgui.love.Init("RGBA32") or imgui.love.Init("Alpha8")

  love.graphics.setBackgroundColor(0.41, 0.53, 0.97) --set the background color to a nice blue
end

love.draw = function()
  -- example window
  imgui.ShowDemoWindow()

  -- code to render imgui
  imgui.Render()
  imgui.love.RenderDrawLists()

  love.graphics.setColor(1,0,0,1) -- Causes weird issues
end

love.update = function(dt)
  imgui.love.Update(dt)
  imgui.NewFrame()
end

love.mousemoved = function(x, y, ...)
  imgui.love.MouseMoved(x, y)
  if not imgui.love.GetWantCaptureMouse() then
    -- your code here
  end
end

love.mousepressed = function(x, y, button, ...)
  imgui.love.MousePressed(button)
  if not imgui.love.GetWantCaptureMouse() then
    -- your code here 
  end
end

love.mousereleased = function(x, y, button, ...)
  imgui.love.MouseReleased(button)
  if not imgui.love.GetWantCaptureMouse() then
    -- your code here 
  end
end

love.wheelmoved = function(x, y)
  imgui.love.WheelMoved(x, y)
  if not imgui.love.GetWantCaptureMouse() then
    -- your code here 
  end
end

love.keypressed = function(key, ...)
  imgui.love.KeyPressed(key)
  if not imgui.love.GetWantCaptureKeyboard() then
    -- your code here 
  end
end

love.keyreleased = function(key, ...)
  imgui.love.KeyReleased(key)
  if not imgui.love.GetWantCaptureKeyboard() then
    -- your code here 
  end
end

love.textinput = function(t)
  imgui.love.TextInput(t)
  if imgui.love.GetWantCaptureKeyboard() then
    -- your code here 
  end
end

love.quit = function()
  return imgui.love.Shutdown()
end

-- for gamepad support also add the following:

love.joystickadded = function(joystick)
  imgui.love.JoystickAdded(joystick)
  -- your code here 
end

love.joystickremoved = function(joystick)
  imgui.love.JoystickRemoved()
  -- your code here 
end

love.gamepadpressed = function(joystick, button)
  imgui.love.GamepadPressed(button)
  -- your code here 
end

love.gamepadreleased = function(joystick, button)
  imgui.love.GamepadReleased(button)
  -- your code here 
end

-- choose threshold for considering analog controllers active, defaults to 0 if unspecified
local threshold = 0.2 

love.gamepadaxis = function(joystick, axis, value)
  imgui.love.GamepadAxis(axis, value, threshold)
  -- your code here 
end
apicici commented 2 years ago

This is expected, the call to imgui.love.RenderDrawLists() works like any other LÖVE draw call and is affected by the current colour. Some people may want to be able to affect the colour (for example to dim everything, UI included) so I'm not planning on changing that.

If you want to avoid tinting the UI you can do something like this:

local oldcolor = {love.graphics.getColor()}
love.graphics.setColor(1,0,0)
imgui.love.RenderDrawLists()
love.graphics.setColor(oldcolor)