This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
dcleondc's profile picture

Function to make help GUI's

Started by dcleondc, 18 October 2012 - 04:11 PM
dcleondc #1
Posted 18 October 2012 - 06:11 PM
Hello my name is Dcleondc and im here to show you some functions/code that can help you make your next GUI.

if you are using these functions in your code make sure you put this code at the top

x, y = term.getSize()

Print Center-This function does just what it says, it will print the text that you pass it in the center of the screen
Usage-
 printC("Put any text here", and put what line you want it on here) 
Code
Spoiler

function printC( text, ypos )
  term.setCursorPos(x/2 - #text/2, ypos)
  term.write(text)
end



Print Right- This function prints the text that you pass it in the center of the right section of the screen.
Usage-
 printR("Put any text here", and put what line you want it on here) 
Code
Spoiler

function printR( text, ypos )
	xpos1 = x/2
	xpos2 = xpos1/2
	xpos3 = xpos2+xpos1
	xpos = xpos3-#text/2
	term.setCursorPos(xpos,ypos)
	term.write(text)
end


Print Left- This function prints the text that you pass it in the center of the left section of the screen.
Usage-
 printL("Put any text here", and put what line you want it on here) 
Code
Spoiler

function printL( text, ypos )
	xpos1 = x/2
	xpos2 = xpos1/2
	xpos = xpos2 - #text/2
	term.setCursorPos(xpos,ypos)
	term.write(text)
end

Print menu- this function will print a border around screen.
Usage-
 menu(x,y) 
Code
Spoiler

function menu(x1,y1)
	clear()
	for i=1, x1-1 do
		setPos(i,1)
		write("#")
	end
	for i=1, x1-1 do
		setPos(i,y1)
		write("#")
	end
	for i=1, y1-1 do
		setPos(1,i)
		write("#")
	end
	for i=1, y1-1 do
		setPos(x-1,i)
		write("#")
	end
end


setPos - faster the typing out term.setCursorPos(x,y)
usage- setPos(x,y)
code
Spoiler

function setPos(x,y)
  setCursorPos(x,y)
end
Noodle #2
Posted 18 October 2012 - 06:13 PM
Your right print function is a bit weird, you don't need all the extra info.
Nvm
Good job, I still don't get the printR function though.
Print right of the center of the screen?
jag #3
Posted 18 October 2012 - 06:22 PM
Where does the variable x come from? ← Ignore this, I was just being derpy…

And in your printR() function you could just do
function printR( text, ypos )
	xpos = (x/2+(x/2)/2)-#text/2
	term.setCursorPos(xpos, ypos)
	term.write(text)
end
Or you know, get rid of the xpos variable entirely
function printR( text, ypos )
	term.setCursorPos((x/2+(x/2)/2)-#text/2, ypos)
	term.write(text)
end

EDIT: Also what is your setPos() function?
Either you misspelled term.setCursorPos() or you got a function like this
function setPos(x,y)
	term.setCursorPos(x,y)
end
Or if you go a bit more fancy like this
function setPos(x,y)
	x = tonumber(x)
	y = tonumber(y)
	if not x then x = 1 end
	if not y then y = 1 end
	term.setCursorPos(x,y)
end
Noodle #4
Posted 18 October 2012 - 06:25 PM
Where does the variable x come from?
And in your printR() function you could just do
function printR( text, ypos )
	xpos = (x/2+(x/2)/2)-#text/2
	term.setCursorPos(xpos, ypos)
	term.write(text)
end
Or you know, get rid of the xpos variable entirely
function printR( text, ypos )
	term.setCursorPos((x/2+(x/2)/2)-#text/2, ypos)
	term.write(text)
end
Var x comes from the second sentence.
jag #5
Posted 18 October 2012 - 06:35 PM
Where does the variable x come from?
And in your printR() function you could just do
function printR( text, ypos )
	xpos = (x/2+(x/2)/2)-#text/2
	term.setCursorPos(xpos, ypos)
	term.write(text)
end
Or you know, get rid of the xpos variable entirely
function printR( text, ypos )
	term.setCursorPos((x/2+(x/2)/2)-#text/2, ypos)
	term.write(text)
end
Var x comes from the second sentence.
Oh shoot totally missed that!
dcleondc #6
Posted 18 October 2012 - 07:37 PM
sorry about that i forgot to add my setPos() function that i use in all my code. ill add it now