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. />
0
9 replies to this topic
#1
Posted 19 October 2012 - 11:22 AM
#2
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
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() endThe function searchField() will run when you press the screen within the area from x10 to x20 and from y10 to y20 (I think...).
#3
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
Posted 19 October 2012 - 01:25 PM
Ok, this very much helps. In the morning though, I'm in need of sleep...
#6
Posted 19 October 2012 - 03:00 PM
luanub, 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
Posted 19 October 2012 - 03:05 PM
Lyqyd, 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
Posted 19 October 2012 - 04:34 PM
faubiguy, on 19 October 2012 - 03:05 PM, said:
Lyqyd, 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
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
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