So you want to create buttons?
We first have to collect some information before continueing:
xMin = the first position on the horizontal axis
xMax = the last postion on the horizontal axis
yMin = the first position on the vertical axis
yMax = the second position on the vertical axis
Here is a tool-program to help you out, it is straight forward what you should do..
The program ( Obviously run on an advanced computer ) can be downloaded here. Or you can do pastebin get ywa52Eh4
Now you have your values you have to think ifyou want to get multiple buttons. If you want multiple buttons you need their values as well.
If I look at myself, I use multiple buttons or none, so I will start with multiple buttons.
Note: If you are making one button, also read multiple buttons because I am recalling items from them
Multiple buttons
Let's go and process your values. I like them to put them in a table so you have later an easier way of determining which button is clicked.
So I tend als to use a "template" so you can easily look up what you have done
local tCoords = {
-- xMin, xMax, yMin, yMax, representable string
-- That string is easier for debugging, thats for later
[1] = { 12, 16, 8, 10, "X" }, -- button 1: xMin = 12, xMax = 16, yMin = 8, yMax = 10, representable string = X
[2] = { 1, 2, 1, 19, "OK" },
-- etc..
}
What you always have to do next is use this:
local evt, button, x, y = os.pullEvent("mouse_click") -- In the terminal
local evt, side, x, y = os.pullEvent("monitor_touch") -- on your monitor
You have to do this because it sits and wait until you touch the monitor or click your terminal. You also get your values to compare with of this.
So now we have our values and we can see our positions we clicked or touched on, we can start determining wich button we actually clicked.
So first we want to do a for-loop, and that for the length of the table tCoords. We will do this because then we can get all values of all the current buttons.
You get the length of a table by doing:
#<your table name>
If you don't know how to do a for-loop, here it is:
for i = 1, #tCoords do
--code
end
The variable i is the most used variable for this. Than you say from <number>, <tonumber>, <step or default 1> do myCode endSo in our for-loop we loop through number 1 to 2, because we have 2 buttons in our table.
This is the total code we will use for determining our button, I will explain the loop later.
local tCoords = {
-- xMin, xMax, yMin, yMax, representable string
-- That string is easier for debugging, thats for later
[1] = { 12, 16, 8, 10, "X" }, -- button 1: xMin = 12, xMax = 16, yMin = 8, yMax = 10, representable string = X
[2] = { 1, 2, 1, 19, "OK" },
-- etc..
}
local evt, button, x, y = os.pullEvent("mouse_click") -- In the terminal
for i = 1, #tCoords do
if x >= tCoords[i][1] and x <= tCoords[i][2] and y >= tCoords[i][3] and y <= tCoords[i][4] then
button = i
break
end
end
So basicly we say in the for loop:
if the x is bigger or equal than our xMin and smaller or equal to our xMax ( also for yMin and yMax )
then we say, our button is the current loopnumber if that makes sense
so we break out of this loop because we have found our button.
If no button has been found then nothing happens. Then we have not found our button.
Now for debugging, now we have variable button ( if we found a button that matches our xMin, xMax, yMin, yMax ) so we can easily fill in button:
if debug and button then -- only if button is not nil and debug is enabled
print( "Found button: " .. tCoords[button][5] )
end
And at this point the representable string makes sense, right? :)/>/>A good example is my Basic Calculator, it uses the same code for determining buttons! You can check the code overthere.
Single button
This time we don't have to put our values in a table, we can use an good old if statement. So your code should look like this:
local evt, button, x, y = os.pullEvent("mouse_click")
if x >= [your xMin] and x <= [your xMax] and y >= [your yMin] and y <= [your yMax] then
-- your code
end
Well I should not have to explain much about this, because you have read multiple buttons aswell right?
This should wrap up my tutorial,
If you find any spelling errors or if you have a question post it below!
Also feedback is appreciated.
Engineer
Also check out LBPHacker's button API, now you know the logic behind it and can use this:
http://www.computerc...9382#entry99382