fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosMake classes a bit easier to understand and setup. Moved GUI related textures to their own folder.7180fb3

settings-overhaul
3.0 KiB131 linesraw
1local utf8 = require("utf8")
2local log = require("log")
3 
4local function error_printer(msg, layer)
5	log.error((debug.traceback("Error: " .. tostring(msg), 1+(layer or 1)):gsub("\n[^\n]+$", "")))
6end
7
8function love.errorhandler(msg)
9	msg = tostring(msg)
10 
11	error_printer(msg, 2)
12 
13	if not love.window or not love.graphics or not love.event then
14		return
15	end
16 
17	if not love.graphics.isCreated() or not love.window.isOpen() then
18		local success, status = pcall(love.window.setMode, 800, 600)
19		if not success or not status then
20			return
21		end
22	end
23 
24	-- Reset state.
25	if love.mouse then
26		love.mouse.setVisible(true)
27		love.mouse.setGrabbed(false)
28		love.mouse.setRelativeMode(false)
29		if love.mouse.isCursorSupported() then
30			love.mouse.setCursor()
31		end
32	end
33	if love.joystick then
34		-- Stop all joystick vibrations.
35		for i,v in ipairs(love.joystick.getJoysticks()) do
36			v:setVibration()
37		end
38	end
39	if love.audio then love.audio.stop() end
40 
41	love.graphics.reset()
42	local font = love.graphics.setNewFont(14)
43 
44	love.graphics.setColor(255, 255, 255, 255)
45 
46	local trace = debug.traceback()
47 
48	love.graphics.origin()
49 
50	local sanitizedmsg = {}
51	for char in msg:gmatch(utf8.charpattern) do
52		table.insert(sanitizedmsg, char)
53	end
54	sanitizedmsg = table.concat(sanitizedmsg)
55 
56	local err = {}
57 
58	table.insert(err, "Error\n")
59	table.insert(err, sanitizedmsg)
60 
61	if #sanitizedmsg ~= #msg then
62		table.insert(err, "Invalid UTF-8 string in error message.")
63	end
64 
65	table.insert(err, "\n")
66 
67	for l in trace:gmatch("(.-)\n") do
68		if not l:match("boot.lua") then
69			l = l:gsub("stack traceback:", "Traceback\n")
70			table.insert(err, l)
71		end
72	end
73 
74	local p = table.concat(err, "\n")
75 
76	p = p:gsub("\t", "")
77	p = p:gsub("%[string \"(.-)\"%]", "%1")
78 
79	local function draw()
80		local pos = 8
81		love.graphics.clear(89, 157, 220)
82		love.graphics.printf(p, pos, pos, love.graphics.getWidth() - pos)
83		love.graphics.present()
84	end
85 
86	local fullErrorText = p
87	local function copyToClipboard()
88		if not love.system then return end
89		love.system.setClipboardText(fullErrorText)
90		p = p .. "\nCopied to clipboard!"
91		draw()
92	end
93 
94	if love.system and love.keyboard then
95		p = p .. "\n\nPress Ctrl+C or tap to copy this error"
96	end
97 
98	return function()
99		love.event.pump()
100 
101		for e, a, b, c in love.event.poll() do
102			if e == "quit" then
103				return 1
104			elseif e == "keypressed" and a == "escape" then
105				return 1
106			elseif e == "keypressed" and a == "c" and love.keyboard and love.keyboard.isDown("lctrl", "rctrl") then
107				copyToClipboard()
108			elseif e == "touchpressed" then
109				local name = love.window.getTitle()
110				if #name == 0 or name == "Untitled" then name = "Game" end
111				local buttons = {"OK", "Cancel"}
112				if love.system then
113					buttons[3] = "Copy to clipboard"
114				end
115				local pressed = love.window.showMessageBox("Quit "..name.."?", "", buttons)
116				if pressed == 1 then
117					return 1
118				elseif pressed == 3 then
119					copyToClipboard()
120				end
121			end
122		end
123 
124		draw()
125 
126		if love.timer then
127			love.timer.sleep(0.1)
128		end
129	end
130 
131end