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

Getting an Image to be Clickable

Started by Persona, 09 January 2013 - 12:38 PM
Persona #1
Posted 09 January 2013 - 01:38 PM

The image above shows you what I am trying to accomplish. I want the whole button (NOT the screen) to be clickable. I've browsed and found stuff on OS.PULLEVENT() and it is CLOSE to what I need, but I've only been able to make the whole screen activate.

I want the button (the 5 by however wide the terminal is)..
Orwell #2
Posted 09 January 2013 - 01:59 PM
This might be a place to start looking: mouse events tutorial
Grim Reaper #3
Posted 09 January 2013 - 02:01 PM
I would suggest using a range to check whether a click that occurred was on your button.

If your button starts at the origin of the screen (1, 1), is 5 high, and extends to the end of the screen (50?), I would suggest doing something like this:


local button_xPos, button_yPos  = 1, 1
local buttonWidth, buttonHeight = 50, 5

-- Checks whether or not the click at the given position was
-- within the range provided.
function wasClickOnRange(xClickPos, yClickPos, xRangeStart, yRangeStart, rangeWidth, rangeHeight)
	return (xClickPos >= xRangeStart or xClickPos <= xRangeStart + rangeWidth) and
		   (yClickPos >= yRangeStart or yClickPos <= yRangeStart + rangeHeight)
end

-- A more specific function to check if a click was on our button.
function wasClickOnButton(xClickPos, yClickPos)
	return wasClickOnRange(xClickPos, yClickPos, button_xPos, button_yPos, buttonWidth, buttonHeight)
end

-- Grabs a click, checks whether or not it was on the button, then
-- performs the appropriate action.
function handleClicks()
	local event, mouseButton, xClickPos, yClickPos = os.pullEvent("mouse_click")

	-- Check if the click we got was on our button.
	if wasClickOnButton(xClickPos, yClickPos) then
		-- Do whatever you want here.
	end
end

I'd be glad to clarify any of the above code if you need some help.
Heracles421 #4
Posted 09 January 2013 - 02:04 PM
Simple, make a condition that checks for the mouse click into the designated area:

local sEvent,button,x,y = os.pullEvent("mouse_click")
if (x >= minX or x <= maxX) and (y >= minY or y <= maxY) then -- Replace min/max values for the corners of the button, in order to make a box 
  Do stuff
end
Persona #5
Posted 09 January 2013 - 04:15 PM
Thank you everyone for your advice. I used the reply by this person
Simple, make a condition that checks for the mouse click into the designated area:

local sEvent,button,x,y = os.pullEvent("mouse_click")
if (x >= minX or x <= maxX) and (y >= minY or y <= maxY) then -- Replace min/max values for the corners of the button, in order to make a box
  Do stuff
end
TheArchitect #6
Posted 10 January 2013 - 07:09 PM
Actually, it should be:

if (x >= minX and x <= maxX) and (y >= minY and y <= maxY)

Because using "or" the condition will be true no matter where you click on the screen.

(no "code" tags because code blocks don't accept bold formatting)