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