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

Getting buttons to work

Started by lennyitb, 19 January 2014 - 06:32 PM
lennyitb #1
Posted 19 January 2014 - 07:32 PM
I am in the process of creating a nifty GUI, and it (of course) uses buttons. I created a function which draws buttons:

function button(cap, xpos, ypos, sizex, sizey)
  term.setTextColor(colors.white)
  term.setBackgroundColor(colors.blue)
  caplen = string.len(cap)
  term.setCursorPos(xpos, ypos)
  for ydraw = 1, sizey do
  for xdraw = 1, sizex do
  term.write(" ")
  end
  term.setCursorPos(xpos, ypos + ydraw)
  end
  term.setCursorPos((sizex/2-caplen/2)+xpos, sizey/2+ypos)
  term.write(cap)
end
Now I have absolutely no idea how to get these buttons to do anything and I need help. -Lenny
CometWolf #2
Posted 20 January 2014 - 12:18 AM
Buttons rely on os.pullEvent() to function.

while true do
  local event,mouseButton,clickX,clickY = os.pullEvent("mouse_click")
  if clickX >= buttonStartX and clickX <= buttonEndX
  and clickY >= buttonStartY and clickY <= buttonEndY then
    --button function
  end
end
http://computercraft.info/wiki/Os.pullEvent
theoriginalbit #3
Posted 20 January 2014 - 12:33 AM
Also I would like to point out a little function that will help you with your drawing and it is string.rep


for ydraw = 1, sizey do
  term.write(string.rep(' ', sizex))
  term.setCursorPos(xpos, ypos + ydraw)
end
Engineer #4
Posted 20 January 2014 - 03:13 AM
Buttons rely on os.pullEvent() to function.

while true do
  local event,mouseButton,clickX,clickY = os.pullEvent("mouse_click")
  if clickX >= buttonStartX and clickX <= buttonEndX
  and clickY >= buttonStartY and clickY <= buttonEndY then
	--button function
  end
end
http://computercraft...ki/Os.pullEvent
Here is more "in-depth" tutorial:
http://www.computercraft.info/forums2/index.php?/topic/11046-clickable-buttons-in-cc/
[/shameless_self_plug]