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

Makeing a colored box

Started by Zelman89, 30 June 2013 - 11:41 AM
Zelman89 #1
Posted 30 June 2013 - 01:41 PM
I know there is button threads and I always get lost trying to read the code. I just want to be able to make a box with a background color and it not going to be a button. I found this touch button code and can understand half of it but can anyone break it down a little farther with comments so I can understand what is happening. Tables are one thing I still don't fully understand. I inserted some of my comments to show what I get and don't get.


--button on/off color
bactive = colors.cyan
binactive=colors.gray
--text on/off color
tactive=colors.white
tinactive=colors.black
--Background color
bgcolor = colors.black


buttons = {}
	
	
--I understand this is where values are being put into the table

	  function newButton(id,xmin,xmax,ymin,ymax,text,func)
		buttons[id] = {}
		buttons[id]["xmin"] = xmin
		buttons[id]["xmax"] = xmax
		buttons[id]["ymin"] = ymin
		buttons[id]["ymax"] = ymax
		buttons[id]["active"] = false
		buttons[id]["text"] = text
		buttons[id]["func"] = func
	  
	  end
	  ---------------------------------------------------------------------------------
	  function printButton(id)
		ymin = buttons[id]["ymin"]
		ymax = buttons[id]["ymax"]
		xmin = buttons[id]["xmin"]
		xmax = buttons[id]["xmax"]
		text = buttons[id]["text"]
		ia = buttons[id]["active"]

	  

			width = xmax - xmin
			height = ymax - ymin
		  
			if ia then m.setBackgroundColor(bactive) m.setTextColor(tactive)
			else m.setBackgroundColor(binactive) m.setTextColor(tinactive) end

-- what is happening here? This is where I get lost.
			for j = ymin,ymax do
			 m.setCursorPos(xmin,j)
			  for i = xmin,xmax do

				m.write(" ")

			 end

		   end

		
		m.setCursorPos(xmin + width / 2 - #text / 2 + 1,ymin + height / 2)

	   m.write(text)
	   m.setBackgroundColor(bgcolor)
	  
	  end
Apfeldstrudel #2
Posted 30 June 2013 - 03:00 PM
When reading it first i thought it cleared the button box.
Are you sure there is no setCursprPos in the inner for loop?
Robert00001 #3
Posted 30 June 2013 - 03:45 PM
When reading it first i thought it cleared the button box.
Are you sure there is no setCursprPos in the inner for loop?

That is not needed, as write automatically increments the X part of the cursor position
-- what is happening here? This is where I get lost.

						for j = ymin,ymax do
						 m.setCursorPos(xmin,j)
						  for i = xmin,xmax do

								m.write(" ")

						 end

				   end

That part loops through all the X and Y values of the button writing a space to each one making a square
Zelman89 #4
Posted 30 June 2013 - 11:37 PM
Thanks for the info, on a side note can someone explain one more thing to me. I found some code and I don't undstand the "amountString:sub". What does " :sub " do? I understand the whole code to this point and the sub comes out of no where.


term.write(amountString:sub(#amountString - (sizeX - i), #amountString - (sizeX - i)))
Engineer #5
Posted 01 July 2013 - 03:57 AM
Copied from the string library:

Spoiler
string.sub(s, i [, j])

s:sub(i [,j])

Return a substring of the string passed. The substring starts at i. If the third argument j is not given, the substring will end at the end of the string. If the third argument is given, the substring ends at and includes j.

> = string.sub("Hello Lua user", 7) – from character 7 until the end
Lua user
> = string.sub("Hello Lua user", 7, 9) – from character 7 until and including 9
Lua
> = string.sub("Hello Lua user", -8) – 8 from the end until the end
Lua user
> = string.sub("Hello Lua user", -8, 9) – 8 from the end until 9 from the start
Lua
> = string.sub("Hello Lua user", -8, -6) – 8 from the end until 6 from the end
Lua
Zelman89 #6
Posted 01 July 2013 - 11:41 AM
Google has failed me on finding that… I take it these two items are the same thing just one includes the string and the other puts the string infront?


string.sub(s, i [, j])
s:sub(i [,j])
Engineer #7
Posted 01 July 2013 - 11:54 AM
Yeah, you are right. example:


string.sub("Hello wordl!", 1, 2 )
("Hello world!"):sub( 1, 2 ) -- Use the one above if you do this
local s = "Hello World!"
s:sub( 1, 2 )