init.lua 3.9 KB

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