Skip to content

Instantly share code, notes, and snippets.

@akkartik
Last active December 24, 2023 14:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkartik/4a011c4ac2f80c24aa117bea2a02b2e3 to your computer and use it in GitHub Desktop.
Save akkartik/4a011c4ac2f80c24aa117bea2a02b2e3 to your computer and use it in GitHub Desktop.
Edit source code inside LÖVE with almost no constraints or limitations.
Slab = require 'Slab' -- idiomatic GUI: https://github.com/flamendless/Slab
-- default love.run from https://love2d.org/wiki/love.run with various callbacks wrapped in xpcall()
function love.run()
if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
-- We don't want the first frame's dt to include time taken by love.load.
if love.timer then love.timer.step() end
local dt = 0
-- Main loop time.
return function()
-- Process events.
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then
if not love.quit or not love.quit() then
return a or 0
end
end
xpcall(function() love.handlers[name](a,b,c,d,e,f) end, handle_error)
end
end
-- Update dt, as we'll be passing it to update
if love.timer then dt = love.timer.step() end
-- Call update and draw
if love.update then xpcall(function() love.update(dt) end, handle_error) end -- will pass 0 if love.timer is disabled
if love.graphics and love.graphics.isActive() then
love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor())
if love.draw then xpcall(love.draw, handle_error) end
love.graphics.present()
end
if love.timer then love.timer.sleep(0.001) end
end
end
function love.load()
love.keyboard.setKeyRepeat(true)
love.keyboard.setTextInput(true)
Slab.Initialize()
InputText = 'function love.draw() error("aaa") end'
Error_message = nil
LineHeight = Slab.GetTextHeight()
W, H = love.window.getMode()
-- save all MiniIDE handlers
MiniIDE_handlers = {}
for name,def in pairs(love.handlers) do
MiniIDE_handlers[name] = def
end
MiniIDE_handlers.update = love.update
MiniIDE_handlers.draw = love.draw
end
function handle_error(err)
Error_message = debug.traceback('Error: ' .. tostring(err), --[[stack frame]]2):gsub('\n[^\n]+$', '')
-- restore all MiniIDE handlers
for name,def in pairs(MiniIDE_handlers) do
if name == 'draw' then
love.draw = def
elseif name == 'update' then
love.update = def
else
love.handlers[name] = def
end
end
end
function love.update(dt)
Slab.Update(dt)
Slab.BeginWindow('code',{AutoSizeWindow=false, X=0,Y=0, W=W,H=H, IsOpen=true})
if Slab.Input('EditBox',{Text=InputText, MultiLine=true, MultiLineW=W-10, W=W-10, H=H-6*LineHeight}) then
InputText = Slab.GetInputText()
end
if Slab.Button('Run') then
Error_message = nil
load(InputText, 'editor')()
end
Slab.EndWindow()
end
function love.draw()
Slab.Draw()
if Error_message then
love.graphics.setColor(1,0,0)
love.graphics.print(Error_message, 10,H-4*LineHeight)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment