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

Making buttons?

Started by Mendax, 19 October 2012 - 10:22 AM
Mendax #1
Posted 19 October 2012 - 12:22 PM
Ok, so, I just finished my API for an internet protocol, and now I have to move on to the UI
I want advanced computers to be able to press buttons. As in, search bar (more like ids but eh), meny, quit etc. The problem is, I don't know how to make buttons, so I was hoping someone here could help me. Thank you. :P/>/>
jag #2
Posted 19 October 2012 - 01:18 PM
The adv. computers got a pressed button event.
The event will return the x and y pos of where the user pressed.
So if you want to check if the user pressed in a certain area you can do something like
if x > 9 and x < 21
and y > 9 and y < 21 then
  searchField()
end
The function searchField() will run when you press the screen within the area from x10 to x20 and from y10 to y20 (I think…).
Luanub #3
Posted 19 October 2012 - 01:41 PM
So you'll want to use the following line to capture the mouse clicks

local event, button, mouseX, mouseY = os.pullEvent("mouse_click")
You can of coarse name the vars anything you want. Just named that way so you will know what the event returns.
Mendax #4
Posted 19 October 2012 - 02:25 PM
Ok, this very much helps. In the morning though, I'm in need of sleep…
remiX #5
Posted 19 October 2012 - 02:30 PM
Ok, this very much helps. In the morning though, I'm in need of sleep…


print("Goodnight, see you in 11 hours :P/>/>"
sleep(39600)
Lyqyd #6
Posted 19 October 2012 - 04:00 PM
So you'll want to use the following line to capture the mouse clicks

local event, button, mouseX, mouseY = os.pullEvent("mouse_click")
You can of coarse name the vars anything you want. Just named that way so you will know what the event returns.

Did they change the order in the newer pre-releases? As far as I know, it is still event, mouseX, mouseY, button.
faubiguy #7
Posted 19 October 2012 - 04:05 PM
Did they change the order in the newer pre-releases? As far as I know, it is still event, mouseX, mouseY, button.

Yes, they changed the parameter order to button, X, Y, and they also changed the event from "click" to "mouse_click" ("mouse_scroll" for the mouse wheel up or down.)
Lyqyd #8
Posted 19 October 2012 - 05:34 PM
Did they change the order in the newer pre-releases? As far as I know, it is still event, mouseX, mouseY, button.

Yes, they changed the parameter order to button, X, Y, and they also changed the event from "click" to "mouse_click" ("mouse_scroll" for the mouse wheel up or down.)

I suppose such a distinction is useful, and I can sort of see a justification for switching the order. Time to change a bunch of code and update to the newest prerelease. Such are the downfalls of coding for unstable features.
Sammich Lord #9
Posted 19 October 2012 - 07:40 PM
Here is a good function to have multiple buttons:

local running = true

local function hello1()
  print("Hello1")
end

local function hello2()
  print("Hello2")
end

local buttons = {
  [1] = {buttonType = 1, startX = 1, endX = 1, startY = 1, endY = 1, buttonText = "#", command = hello1}, --Creates button at top left
  [2] = {buttonType = 1, startX = 1, endX = 1, startY = 2, endY = 2, buttonText = "@", command = hello2} --Creates button at one down from the other one
}

local function buttonMenu(table)
  for k,v in ipairs(table) do
    term.setCursorPos(table[k].startX, table[k].startY)
  end
  while running do
    for k,v in ipairs(table) do
      event, button, x, y = os.pullEvent()
      if event == "mouse_click" then
        if button = table[k].buttonType then
          if x >= table[k].startX and x <= table[k].endX and y >= table[k].startY and y <= table[k].endY then
            table[k].command()
          end
        end
      end
    end
  end
  term.clear()
end

buttonMenu(buttons)
Awesomesause200 #10
Posted 02 May 2014 - 03:15 AM
I'm glad I found this page so I can find code to easily make buttons, but I have to revise this code a little bit to display buttons and not have a "bios:338: [string "<program name>"]:25: 'then' expected" error inside of it

local running = true
local function hello1()
  print("Hello1")
end
local function hello2()
  print("Hello2")
end
local buttons = {
  [1] = {buttonType = 1, startX = 1, endX = 1, startY = 1, endY = 1, buttonText = "#", command = hello1}, --Creates button at top left
  [2] = {buttonType = 1, startX = 1, endX = 1, startY = 2, endY = 2, buttonText = "@", command = hello2} --Creates button at one down from the other one
}
local function buttonMenu(table)
  for k,v in ipairs(table) do
	term.setCursorPos(table[k].startX, table[k].startY)
	print(table[k].buttonText)
  end
  while running do
	for k,v in ipairs(table) do
	  event, button, x, y = os.pullEvent()
	  if event == "mouse_click" then
		if button == table[k].buttonType then
		  if x >= table[k].startX and x <= table[k].endX and y >= table[k].startY and y <= table[k].endY then
			table[k].command()
		  end
		end
	  end
	end
  end
  term.clear()
end
term.clear()
buttonMenu(buttons)