Jump to content




Making buttons?

help

9 replies to this topic

#1 Mendax

  • Members
  • 366 posts

Posted 19 October 2012 - 11:22 AM

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

#2 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 19 October 2012 - 12: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...).

#3 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 19 October 2012 - 12: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.

#4 Mendax

  • Members
  • 366 posts

Posted 19 October 2012 - 01:25 PM

Ok, this very much helps. In the morning though, I'm in need of sleep...

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 19 October 2012 - 01:30 PM

View Postmrminer84403, on 19 October 2012 - 01:25 PM, said:

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)


#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 October 2012 - 03:00 PM

View Postluanub, on 19 October 2012 - 12:41 PM, said:

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.

#7 faubiguy

  • Members
  • 213 posts

Posted 19 October 2012 - 03:05 PM

View PostLyqyd, on 19 October 2012 - 03:00 PM, said:

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

#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 October 2012 - 04:34 PM

View Postfaubiguy, on 19 October 2012 - 03:05 PM, said:

View PostLyqyd, on 19 October 2012 - 03:00 PM, said:

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.

#9 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 19 October 2012 - 06: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)


#10 Awesomesause200

  • Members
  • 18 posts

Posted 02 May 2014 - 02: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)






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users