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

[Question]How do you draw a rectangle with text on it?

Started by denni199, 03 February 2013 - 02:16 AM
denni199 #1
Posted 03 February 2013 - 03:16 AM
Title: [Question]How do you draw a rectangle with text on it?

Hi everyone.

I am learning how to use the advanced monitors option with colors.
I am trying to make a button using the new color options.
So what i'd like to do is specify x and y of the to be square and possible add text on it to specify what it would do.
I would like to do multiple of them just by calling a function like this:


drawSquare(xmin, xmax, ymin, ymax, squareText)

Be easy on me as i am new to lua. Keep it as simple as possible :)/>
denni199 #2
Posted 03 February 2013 - 09:24 AM
bump
Mikee251 #3
Posted 03 February 2013 - 09:39 AM
Here you go! Btw, because of how I centered the text, the rectangle has to be around 1.5 times the length of the word though. Feel free to change it around if you need to


function drawSquare(xmin,xmax,ymin,ymax,text)
numB = ymax - ymin
numA = xmax - xmin

for i = 1,numB do
term.setCursorPos(xmin,(ymin+i))
print("|" .. string.rep(" ",numA-2) .. "|")
end
term.setCursorPos(xmin,ymin)
print(string.rep("=",numA))
term.setCursorPos(xmin,ymax)
print(string.rep("=",numA))
term.setCursorPos((numA/2) + xmin - (string.len(text)/2), ymin + (numB/2))
print(text)
term.setCursorPos(1,ymax+1)
end

drawSquare(1,20,1,3, "It works!")
denni199 #4
Posted 03 February 2013 - 10:01 AM
Here you go! Btw, because of how I centered the text, the rectangle has to be around 1.5 times the length of the word though. Feel free to change it around if you need to


function drawSquare(xmin,xmax,ymin,ymax,text)
numB = ymax - ymin
numA = xmax - xmin

for i = 1,numB do
term.setCursorPos(xmin,(ymin+i))
print("|" .. string.rep(" ",numA-2) .. "|")
end
term.setCursorPos(xmin,ymin)
print(string.rep("=",numA))
term.setCursorPos(xmin,ymax)
print(string.rep("=",numA))
term.setCursorPos((numA/2) + xmin - (string.len(text)/2), ymin + (numB/2))
print(text)
term.setCursorPos(1,ymax+1)
end

drawSquare(1,20,1,3, "It works!")
Sorry but i does that make like a green square?
I meant like make a colored square :/
Kingdaro #5
Posted 03 February 2013 - 10:40 AM
You could either set the background color to green before running the function, or make it so that the function takes an extra color argument and sets the color automatically. I'd do the latter - less typing.
remiX #6
Posted 03 February 2013 - 11:12 AM
Here's a simple function… Read my comments and if I've missed anything, feel free to ask :)/>


--[[ Simple function to draw a square with desired colours, x and y co-ordinates,
	 and text within the function.
	 
	 borderCol = colour of the outside border
	 boxCol	   = colour of the inside of the box
	 textCol   = colour of the text that appears within the box
	 xStart    = starting X position of the box (top left hand corner of the box)
	 xEnd      = ending X position of the box (top right hand corner of the box)
	 yStart    = starting Y positon of the box
	 ...	   = arguments, accepts as many as you want. First one is the title.
--]]

function drawSquare( borderCol, boxCol, textCol, xStart, xEnd, yStart, ... )

	--[[ Defines the max amount of characters that can be printed within
		 the box. If exceeded, it will error.
	--]]
	local maxLength = xEnd - xStart - 2
	
	--[[ This part does the border of the box ]]--
	term.setBackgroundColour(borderCol)
	for x = xStart, xEnd do
		term.setCursorPos(x, yStart)
		write(" ")
		term.setCursorPos(x, yStart+#arg+2)
		write(" ")
	end
	for y = yStart + 1, yStart + #arg + 1 do
		term.setCursorPos(xStart, y)
		write(" ")
		term.setCursorPos(xEnd, y)
		write(" ")
	end
	
	--[[ This writes the title, which is the first string within
		 the '...'
	--]]
	term.setTextColour(textCol)
	term.setCursorPos((xEnd - xStart - #arg[1])/2 + xStart + 1, yStart)
	write(arg[1])
	
	--[[ This part fills the insides of the box with the desired
		 colour
	--]]
	term.setBackgroundColour(boxCol)
	for x = xStart + 1, xEnd - 1 do
		for y = yStart + 1, yStart + #arg + 1 do
			term.setCursorPos(x, y)
			write(" ")
		end
	end
	
	--[[ arg is '...' and accepts as many as you want, but of course - having
		 more than 16? would be silly.
		 It centeres each text within the box
	--]]
	for i = 2, #arg do
		if #arg[i] > maxLength then error("Length of arg #" .. i .. " exceeds max limit of " .. maxLength .. " characters.") end
		term.setCursorPos((xEnd - xStart - #arg[i])/2 + xStart + 1, yStart + i)
		write(arg[i])
	end
	
	--[[ Resets the variables and sets the cursorpos to be after the box. --]]
	term.setBackgroundColour(colours.black)
	term.setTextColour(colours.white)
	term.setCursorPos(1, yStart + #arg + 4)
end

term.clear()

drawSquare(colours.lime, colours.lightBlue, colours.red, 10,30,2, "Warning", "It works!", "lssssssssssssstol", "Line 3", "Line 4")
denni199 #7
Posted 03 February 2013 - 11:32 AM
Here's a simple function… Read my comments and if I've missed anything, feel free to ask :)/>


--[[ Simple function to draw a square with desired colours, x and y co-ordinates,
	 and text within the function.
	
	 borderCol = colour of the outside border
	 boxCol	   = colour of the inside of the box
	 textCol   = colour of the text that appears within the box
	 xStart	= starting X position of the box (top left hand corner of the box)
	 xEnd	  = ending X position of the box (top right hand corner of the box)
	 yStart	= starting Y positon of the box
	 ...	   = arguments, accepts as many as you want. First one is the title.
--]]

function drawSquare( borderCol, boxCol, textCol, xStart, xEnd, yStart, ... )

	--[[ Defines the max amount of characters that can be printed within
		 the box. If exceeded, it will error.
	--]]
	local maxLength = xEnd - xStart - 2
	
	--[[ This part does the border of the box ]]--
	term.setBackgroundColour(borderCol)
	for x = xStart, xEnd do
		term.setCursorPos(x, yStart)
		write(" ")
		term.setCursorPos(x, yStart+#arg+2)
		write(" ")
	end
	for y = yStart + 1, yStart + #arg + 1 do
		term.setCursorPos(xStart, y)
		write(" ")
		term.setCursorPos(xEnd, y)
		write(" ")
	end
	
	--[[ This writes the title, which is the first string within
		 the '...'
	--]]
	term.setTextColour(textCol)
	term.setCursorPos((xEnd - xStart - #arg[1])/2 + xStart + 1, yStart)
	write(arg[1])
	
	--[[ This part fills the insides of the box with the desired
		 colour
	--]]
	term.setBackgroundColour(boxCol)
	for x = xStart + 1, xEnd - 1 do
		for y = yStart + 1, yStart + #arg + 1 do
			term.setCursorPos(x, y)
			write(" ")
		end
	end
	
	--[[ arg is '...' and accepts as many as you want, but of course - having
		 more than 16? would be silly.
		 It centeres each text within the box
	--]]
	for i = 2, #arg do
		if #arg[i] > maxLength then error("Length of arg #" .. i .. " exceeds max limit of " .. maxLength .. " characters.") end
		term.setCursorPos((xEnd - xStart - #arg[i])/2 + xStart + 1, yStart + i)
		write(arg[i])
	end
	
	--[[ Resets the variables and sets the cursorpos to be after the box. --]]
	term.setBackgroundColour(colours.black)
	term.setTextColour(colours.white)
	term.setCursorPos(1, yStart + #arg + 4)
end

term.clear()

drawSquare(colours.lime, colours.lightBlue, colours.red, 10,30,2, "Warning", "It works!", "lssssssssssssstol", "Line 3", "Line 4")

Thanks!
I'll try it and fiddle around with the stuff :)/>
remiX #8
Posted 03 February 2013 - 11:37 AM
It looks like a lot because of the comments, it's really fairly simple :P/>