Page 1 of 2

Console -- A love2D console.

Posted: Sat Jun 04, 2011 11:58 pm
by Someguynamedpie
I don't know why this was never done!
This console is a Lua console. When on, it gives all input to the console.
Functions:
Console Console:Init()
Place this at the bottom of your code. Reason for it needing to be at the bottom is because it overwrites some Love2D functions. You can run any other command anywhere else in your code, though.
Console Console:SetEnabled(true/false) (or :Enable() Disable())
The console is disabled by default. This will either enable/disable it.
Console Console:SetOpen(true/false) (or :Open() Close() Toggle())
Sets visibility of the console.
boolean Console:IsOpen()
Returns whether or not the console is open.
Console Console:WriteLine(text[, r,g,b])
Writes text to the console. Note that this doesn't wrap. By default, the color is white.
Console print(text)
Writes white text to the console.
-------
Code:

Code: Select all

Console={}
Console.lines={}
Console.isOpen=false
Console.isEnabled=false
Console.text=""
function Console:WriteLine(txt,r,g,b)
	local height=love.graphics.getHeight()
	r=r or 255 g=g or 255 b=b or 255
	if(#Console.lines>(height/16)-2) then table.remove(Console.lines,1) end
	table.insert(Console.lines,{txt,{r,g,b}})
	return Console
end
function Console:Open()
	if not Console.isEnabled then return Console end
	Console.isOpen=true
	return Console
end
function Console:Close()
	Console.isOpen=false
	return Console
end
function Console:SetOpen(open)
	if not Console.isEnabled then return end
	Console.isOpen=open
	return Console
end
function Console:IsOpen()
	if not Console.isEnabled then return false end
	return Console.isOpen
end
function Console:Toggle()
	if not Console.isEnabled then return end
	Console.isOpen=not Console.isOpen
	return Console
end
function Console:Disable()
	Console.isEnabled=false
	return Console
end
function Console:Enable()
	Console.isEnabled=true
	return Console
end
function Console:SetEnabled(enabled)
	Console.isEnabled=enabled
	return Console
end
function Console:Init()
	local od=love.draw or function() end
	local okp=love.keypressed or function() end
	function love.draw(...)
		local height=love.graphics.getHeight()
		local width=love.graphics.getWidth()
		od(...)
		if(not Console:IsOpen()) then return end
		love.graphics.setColor(126,126,126,126)
		love.graphics.rectangle("fill",0,0,width,height)
		for k,v in pairs(Console.lines) do
			love.graphics.setColor(unpack(v[2]))
			love.graphics.print(v[1] or "",0,k*16-16)
		end
		love.graphics.setColor(255,255,255)
		love.graphics.line(0,height-16,width,height-16)
		love.graphics.setColor(255,255,255)
		love.graphics.print(Console.text,0,height-16)
	end
	function love.keypressed(k,u)
		if(k=="`") then Console:Toggle() return end
		if(not Console:IsOpen()) then okp(k,u) return end
		if(k=="backspace") then Console.text=string.sub(Console.text,1,#Console.text-1) return end
		if(u==13 and #Console.text>0) then
			s,e,r2,r3,r4=pcall(function() loadstring(Console.text)() end)
			if(s) then
				if(e) then
					Console:WriteLine(e,math.random(255),255,math.random(255))
				end
			else
				Console:WriteLine(e,255,0,0)
			end
			Console.text=""
			return
		end
		if(u~=0) then
			Console.text=Console.text..string.char(u)
		end
	end
	return Console
end
function print(txt)
	Console:WriteLine(tostring(txt))
end
If anyone uses this, please tell me. I want to see how its used. :ultrahappy:

Re: Console -- A love2D console.

Posted: Sun Jun 05, 2011 1:36 am
by BlackBulletIV
Someguynamedpie wrote:I don't know why this was never done!
It has been done I believe: http://love2d.org/forums/viewtopic.php?f=5&t=2473

Re: Console -- A love2D console.

Posted: Sun Jun 05, 2011 1:39 am
by Someguynamedpie
Just saw that, but I prefer mine <3 I'm adding various things to mine too :awesome:

Re: Console -- A love2D console.

Posted: Sun Jun 05, 2011 10:57 am
by Ryne
BlackBulletIV wrote:
Someguynamedpie wrote:I don't know why this was never done!
It has been done I believe: http://love2d.org/forums/viewtopic.php?f=5&t=2473
Does that mean it should never be re-created or innovated, especially for a novice looking to practice?

:)

Re: Console -- A love2D console.

Posted: Sun Jun 05, 2011 11:01 am
by BlackBulletIV
Ryne wrote:
BlackBulletIV wrote:
Someguynamedpie wrote:I don't know why this was never done!
It has been done I believe: http://love2d.org/forums/viewtopic.php?f=5&t=2473
Does that mean it should never be re-created or innovated, especially for a novice looking to practice?

:)
I never said any of that, I think exactly the opposite. I was just filling him in, since he obviously didn't know that something like this had been done. :)

Re: Console -- A love2D console.

Posted: Sun Jun 05, 2011 11:20 am
by Robin
Yeah, he literally said he thought it was never done before. :P

Re: Console -- A love2D console.

Posted: Sun Jun 05, 2011 8:56 pm
by BlackBulletIV
Robin wrote:Yeah, he literally said he thought it was never done before. :P
Was that sarcasm? (A :P is present)

Re: Console -- A love2D console.

Posted: Mon Jun 06, 2011 5:21 am
by Robin
I was supporting you by pointing out that you reacted to something Someguynamedpie said.

Re: Console -- A love2D console.

Posted: Mon Jun 06, 2011 5:45 am
by BlackBulletIV
Ah I see, cool stuff. I'm not too good at detecting sarcasm sometimes, so the :P smiley made me wonder.

Re: Console -- A love2D console.

Posted: Mon Jun 06, 2011 4:49 pm
by Robin
Sarcasm can be a bit hard to detect over the internet anyway. :P (No, still no sarcasm.)