Console -- A love2D console.

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Someguynamedpie
Citizen
Posts: 71
Joined: Wed Mar 31, 2010 10:59 pm

Console -- A love2D console.

Post 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:
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Console -- A love2D console.

Post 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
User avatar
Someguynamedpie
Citizen
Posts: 71
Joined: Wed Mar 31, 2010 10:59 pm

Re: Console -- A love2D console.

Post by Someguynamedpie »

Just saw that, but I prefer mine <3 I'm adding various things to mine too :awesome:
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Console -- A love2D console.

Post 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?

:)
@rynesaur
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Console -- A love2D console.

Post 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. :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Console -- A love2D console.

Post by Robin »

Yeah, he literally said he thought it was never done before. :P
Help us help you: attach a .love.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Console -- A love2D console.

Post by BlackBulletIV »

Robin wrote:Yeah, he literally said he thought it was never done before. :P
Was that sarcasm? (A :P is present)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Console -- A love2D console.

Post by Robin »

I was supporting you by pointing out that you reacted to something Someguynamedpie said.
Help us help you: attach a .love.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Console -- A love2D console.

Post by BlackBulletIV »

Ah I see, cool stuff. I'm not too good at detecting sarcasm sometimes, so the :P smiley made me wonder.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Console -- A love2D console.

Post by Robin »

Sarcasm can be a bit hard to detect over the internet anyway. :P (No, still no sarcasm.)
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests