I made a Button API which is working great, you make one like this:
-- Arguments:
-- buttonText, startX or position, endX or auto if position is given, startY, endY, backgroundColor, textColor
local btn = buttons.initButton( ' - Button ', 'center', nil, 1, 3, colours.red, colours.white )
That would make 'btn' the object for this button now. Using btn:blink( col ) will make it blink colour col for a short time.I also have a function to use with os.pullEvent() to check if the clicked X and Y pos matches with the co-ordinates with the button.
local function isWithin( cx, cy, sx, sy, ex, ey )
return cx >= sx and cx <= ex and cy >= sy and cy <= ey
end
I also have a function with the block API called getValue. This will return the value of given data. (works fine)
Now if I want to check if the clicked position is that button, I'd do this:
local _, but, x, y = os.pullEvent( 'mouse_click' )
if isWithin( x, y, btn:getValue( 'startx' ), btn:getValue( 'starty' ), btn:getValue( 'endx' ), btn:getValue( 'endy' ) ) then
-- The button was clicked
end
Now what I want is an easier way to check if a button was clicked using the isWithin function.
I thought the unpack() function will do the job, but I have no clue how unpack() works and it doesn't work anyway :o/>
So I wanted to make a unpackCoords() function for the button api:
function Buttons.unpackCoords( self )
return unpack( { self.startx, self.starty, self.endx, self.endy } )
end
Was calling the isWithin function like so:
if isWithin( x, y, btn:unpackCoords() ) then
but errored with 'attempt to index ? (a function value)If you guys understand what I'm trying to say, is there any way of doing it?