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

Button API

Started by grand_mind1, 05 April 2013 - 01:03 PM
grand_mind1 #1
Posted 05 April 2013 - 03:03 PM
I'm trying to make a button API that I can use for my menus and games. It worked the first time I used it but I've changed something that makes the button be drawn quite oddly. Instead of making the rectangle button it makes a rectangle with the last line of it being colored all the way to the edge of the terminal. I can't seem to figure out what I've changed or done to the code to make it behave like this. I believe that it only has to do with the drawing part of the createButton() function but I'm going to post the entirety of the code just in case. Some of the code may not make sense as much of it is not finished and there are many unused variables. Sorry about that!


m = term
--m = peripheral.wrap("top")
if m == term then
  detect = "click"
else
  detect = "touch"
end

local btns = {
  ["btn1"] ={
	["xMin"] = 1,
	["xMax"] = 10,
	["yMin"] = 1,
	["yMax"] = 5,
	["text"] = "Start!",
	["active"] = false,
  },
-- ["btn2"] = {
   -- ["xMin"] = 11,
   -- ["xMax"] = 10,
   -- ["yMin"] = 1,
   -- ["yMax"] = 5,
   -- ["text"] = "Quit",
   -- ["active"] = false,
-- }
}

m.clear()
m.setCursorPos(1,7)
m.setBackgroundColor(colors.green)
function findCenter()
  xPos = math.floor((btns["btn1"]["xMax"]- btns["btn1"]["xMin"] - string.len(btns["btn1"]["text"])) /2)+1
  yPos = math.floor((btns["btn1"]["yMin"] + btns["btn1"]["yMax"])/2)
end
function createBtn(btn)
xMin = btns["btn1"]["xMin"]
xMax = btns["btn1"]["xMax"]
yMin = btns["btn1"]["yMin"]
yMax = btns["btn1"]["yMax"]
if btns[btn]["active"] == false then
  color = colors.red
else
  color = colors.lime
end

  m.setBackgroundColor(color)
  for i = yMin, yMax do
	m.setCursorPos(xMin, i)
	if i == yPos then
	  for k = 0,xMax-xMin-string.len(text)+1 do
		if k == xPos then
		  m.write(text)
		else
		  m.write(" ")
		end
	  end
	else
	  for o = xMin, xMax do
		m.write(" ")
	  end
	end
  end
  m.setBackgroundColor(colors.black)
end
function click()
  if detect == "click" then
	event, button, x, y = os.pullEvent("mouse_click")
  elseif detect == "touch" then
	event, button, x, y = os.pullEvent("monitor_touch")
  end
  if x >= btns["btn1"]["xMin"] and x <= btns["btn1"]["xMax"] and y >= btns["btn1"]["yMin"] and y <= btns["btn1"]["yMax"] then
	clicked = Start
  --elseif x >=
  end
end
findCenter("btn1")
createBtn("btn1")
term.setCursorPos(1,10)

If someone could locate the problem that would be great!
Help is appreciated!
Thanks!
oeed #2
Posted 05 April 2013 - 03:55 PM
At first glance, I think you may want to change how this works.
But anyway, continuing with this code.

Change line 42 to another, that's not red. What is actually happening is that an error is being thrown, but you don't notice it because the error is in red text.
The error is on line 51.

for k = 0,xMax-xMin-string.len(text)+1 do
This is because text is not defined, change it to something like:

btns[btn]['text']
grand_mind1 #3
Posted 05 April 2013 - 04:28 PM
Ah! Right, that would make sense, them both being red. Also, how should I change how it works? I want it to be good, but I also want to understand everything that is going on so some of it is more complicated than it needs to be.
Thanks for your help! :D/>
oeed #4
Posted 05 April 2013 - 08:31 PM
Ah! Right, that would make sense, them both being red. Also, how should I change how it works? I want it to be good, but I also want to understand everything that is going on so some of it is more complicated than it needs to be.
Thanks for your help! :D/>

I'll try to explain how PearOS buttons work. I'll start writing it now, might take a while.
In fact, I might make it a tutorial if its long enough.
Smiley43210 #5
Posted 06 April 2013 - 07:02 PM
Being a Java coder, I like a different approach. Just a suggestion.
Create a "super" object, or "class". It would be the parent object. Then, create a new instance of the object that inherits everything from the parent without having to be redefined.
I'll post an example later on tonight.

Edit: Wait, let me rephrase that. I'll post an example in a few hours, after I get back home. (My timezone is probably nowhere near yours)
Edit 2: I'll post it if it's not the same as oeed's and/or he hasn't posted it yet.