init.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. local editors = require'editors'
  2. local modifiers = {ctrl = false, alt = false, shift = false}
  3. -- project directory; expected to have main.lua inside
  4. local projectName = 'sandbox'
  5. -- source file that can be dynamically hotswapped
  6. local hotswapName = 'main'
  7. -- contained callbacks that loaded module tried to register as global
  8. -- init'ed as stubs, overwritten on project module load
  9. local callbacks = {
  10. draw = function () end,
  11. update = function () end,
  12. keypressed = function (key, scancode, isrepeat) end,
  13. keyreleased = function (key, scancode) end,
  14. textinput = function (k) end,
  15. }
  16. function lovr.load()
  17. reloadCode(projectName .. '/main')
  18. local editor = editors.new(1, 1)
  19. editor:listFiles(projectName)
  20. end
  21. function lovr.update(dt)
  22. callbacks.update(dt)
  23. end
  24. function lovr.draw()
  25. lovr.graphics.setShader()
  26. for _, editor in ipairs(editors) do
  27. editor:draw()
  28. end
  29. callbacks.draw()
  30. end
  31. function lovr.keypressed(key, scancode, isrepeat)
  32. if key == 'lctrl' or key == 'rctrl' then
  33. modifiers.ctrl = true
  34. return
  35. elseif key == 'lalt' or key == 'ralt' then
  36. modifiers.alt = true
  37. return
  38. elseif key == 'lshift' or key == 'rshift' then
  39. modifiers.shift = true
  40. return
  41. end
  42. local combo = string.format('%s%s%s%s',
  43. modifiers.ctrl and 'ctrl+' or '',
  44. modifiers.alt and 'alt+' or '',
  45. modifiers.shift and 'shift+' or '',
  46. key)
  47. if combo =='ctrl+r' then
  48. if editors.active then editors.active:saveFile() end
  49. reloadCode(hotswapName)
  50. end
  51. if combo =='ctrl+shift+r' then
  52. lovr.event.push('restart')
  53. end
  54. if combo =='escape' then
  55. lovr.event.push('quit')
  56. end
  57. editors.keypressed(combo)
  58. callbacks.keypressed(key, scancode, isrepeat, combo)
  59. end
  60. function lovr.keyreleased(key, scancode)
  61. if key == 'lctrl' or key == 'rctrl' then
  62. modifiers.ctrl = false
  63. return
  64. elseif key == 'lalt' or key == 'ralt' then
  65. modifiers.alt = false
  66. return
  67. elseif key == 'lshift' or key == 'rshift' then
  68. modifiers.shift = false
  69. return
  70. end
  71. callbacks.keyreleased(key, scancode)
  72. end
  73. function lovr.textinput(k)
  74. if k:match('[^\n]') then
  75. editors.textinput(k)
  76. end
  77. callbacks.textinput(k)
  78. end
  79. function reloadCode(name)
  80. local stored = {
  81. load = lovr.load,
  82. draw = lovr.draw,
  83. update = lovr.update,
  84. keypressed = lovr.keypressed,
  85. keyreleased = lovr.keyreleased,
  86. textinput = lovr.textinput,
  87. }
  88. -- reloading module
  89. lovr.filesystem.setRequirePath(projectName .. '/?.lua;' .. '?.lua;?/init.lua;lua_modules/?.lua;lua_modules/?/init.lua')
  90. package.loaded[name] = nil
  91. require(name)
  92. -- handling lovr callbacks that loaded module has overwritten
  93. if stored.load ~= lovr.load then lovr.load() end -- call once (might as well right now) and forget
  94. for _, fname in ipairs({'draw', 'update', 'keypressed', 'keyreleased', 'textinput'}) do
  95. if stored[fname] ~= lovr[fname] then
  96. lovr[fname], callbacks[fname] = stored[fname], lovr[fname] -- the switch
  97. -- own lovr callbacks are restored and loaded module's callbacks will be called as needed
  98. end
  99. end
  100. end
  101. --[[ Revert to initial development environment (factory reset)
  102. If environment gets messed up beyond repair, use this mechanism to overwrite basic
  103. environment with initial sources. Any additional user source files won't be affected,
  104. just built-in ones (init, editors, pane, buffer, lua-lexer, sandbox).
  105. To proceed, place cursor on line below and press ctrl+shift+enter.
  106. lovr.filesystem.remove('init.lua')
  107. !! last chance to back out: save this file now with ctrl+s !!
  108. To apply the reset, restart the system with f5.
  109. Environment will be reverted to inital condition.
  110. --]]