init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. --[[
  2. Debug Overlay module
  3. @author : Eiyeron Fulmincendii
  4. The debug overlay is targeting to be a relatively agnositc debug framework for Love2D based on love-imgui.
  5. Requiring for now a custom class implementation and love-imgui.
  6. To turn on the debug module, set the global variable ENABLE_DEBUG_OVERLAY to true before requiring the debug module for the first time.
  7. It is very suggested to use imgui-love module as this module has been designed with it in mind.
  8. Modulable, th overlay create a menubar on the top of the game screen containing the different debug menus registered to
  9. the system. One being already registered containing
  10. - FPS and CPU/MEM usage graphs
  11. - an interactive console à-la Quake
  12. - a variable watch.
  13. Usage
  14. -----
  15. Create an instance like this:
  16. debug_overlay = require("debug_overlay"):new()
  17. In love.draw, before imgui.Render(), call your instance's render function:
  18. function love.draw()
  19. <snip>
  20. debug_overlay:render()
  21. imgui.Render();
  22. end
  23. To register a new window, create a class derived from DebugWindow (check debugwindow.lua and debug_overlay/graphs/init.lua for an example)
  24. local MyDebugWindow = class(DebugWindow)
  25. Note : to add a shortcut to the window in the menubar, add this line in your window's init function:
  26. function MyDebugWindow:init()
  27. <snip>
  28. self.shortcut = "f4"
  29. <snip>
  30. end
  31. And to register your debug windows in a menu:
  32. debug_overlay:register_menu("Menu button name", {WindowName=MyDebugWindow_Instance})
  33. (I use somemthing like this : )
  34. debug_overlay:register_menu("Menu button name", require("mydebugwindow"):new())
  35. ]]--
  36. -- If not debug then returning a blank class.
  37. if not _G.ENABLE_DEBUG_OVERLAY then
  38. local void = {}
  39. local mt = {
  40. __index = function(self, key) return self end,
  41. __newindex = function(self, key) return self end,
  42. __metatable = {},
  43. __call = function() return void end
  44. }
  45. setmetatable(void, mt)
  46. return void
  47. end
  48. require("imgui")
  49. local class, object = require("class"), require("class.object")
  50. local Console = require("debug_overlay.console")
  51. local Graphs = require("debug_overlay.graphs")
  52. local Watch = require("debug_overlay.watch")
  53. local Debug = class(object)
  54. function Debug:init()
  55. self.debug_menus = {
  56. Debug = {
  57. Console = Console:new(),
  58. Watch = Watch:new(),
  59. Graphs = Graphs:new(),
  60. }
  61. }
  62. self.console = self.debug_menus.Debug.Console
  63. self.watch = self.debug_menus.Debug.Watch
  64. end
  65. function Debug:register_menu(menu_name, windows)
  66. if self.debug_menus[menu_name] then
  67. self.console:error('Debug : '..menu_name.." debug menu is already registered.")
  68. return
  69. end
  70. self.debug_menus[menu_name] = windows
  71. end
  72. function Debug:render()
  73. self.debug_menus.Debug.Graphs:update()
  74. if imgui.BeginMainMenuBar() then
  75. for menu,windows in pairs(self.debug_menus) do
  76. -- Menu
  77. if imgui.BeginMenu(menu) then
  78. for k,v in pairs(windows) do
  79. if imgui.MenuItem(k, v.shortcut, v.visible) then
  80. v:toggle()
  81. end
  82. end
  83. imgui.EndMenu()
  84. end
  85. end
  86. imgui.EndMainMenuBar()
  87. for menu,windows in pairs(self.debug_menus) do
  88. for k, window in pairs(windows) do
  89. if window.visible then
  90. window:render()
  91. end
  92. end
  93. end
  94. end
  95. end
  96. function Debug:keypressed(key)
  97. for _, windows in pairs(self.debug_menus) do
  98. for __, window in pairs(windows) do
  99. if window.shortcut and key == window.shortcut then
  100. window:toggle()
  101. end
  102. end
  103. end
  104. end
  105. return Debug