Jump to content




Makeing a colored box


  • You cannot reply to this topic
6 replies to this topic

#1 Zelman89

  • Members
  • 20 posts

Posted 30 June 2013 - 12: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


#2 Apfeldstrudel

  • Members
  • 161 posts

Posted 30 June 2013 - 02: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?

#3 Robert00001

  • Members
  • 15 posts
  • LocationI am in your room, disguised as an object :)

Posted 30 June 2013 - 02:45 PM

View PostXyexs, on 30 June 2013 - 02:00 PM, said:

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

View PostZelman89, on 30 June 2013 - 12:41 PM, said:

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

#4 Zelman89

  • Members
  • 20 posts

Posted 30 June 2013 - 10: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)))


#5 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 01 July 2013 - 02:57 AM

Copied from the string library:

Spoiler


#6 Zelman89

  • Members
  • 20 posts

Posted 01 July 2013 - 10: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])


#7 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 01 July 2013 - 10: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 )






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users