Jump to content




Getting an Image to be Clickable


5 replies to this topic

#1 Persona

  • Members
  • 12 posts

Posted 09 January 2013 - 01:38 PM

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

#2 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 09 January 2013 - 01:59 PM

This might be a place to start looking: mouse events tutorial

#3 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

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.

#4 Heracles421

  • Members
  • 258 posts

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


#5 Persona

  • Members
  • 12 posts

Posted 09 January 2013 - 04:15 PM

Thank you everyone for your advice. I used the reply by this person

View PostHeracles421, on 09 January 2013 - 02:04 PM, said:

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


#6 TheArchitect

  • Members
  • 68 posts

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)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users