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

Getting X and Y coordinates when clicked on window created by window.create

Started by thenextntnick, 09 December 2014 - 12:02 AM
thenextntnick #1
Posted 09 December 2014 - 01:02 AM
Hello.
I'm developing a game, and the game uses windows created by the window.create method.

While developing it, I came across a problem; how can I obtain the X and Y coordinates of a mouse click when the user clicks on a window made by the window.create method?

I've tried this:

ev, btn, x, y = os.pullEvent("mouse_click")
if x / windowSizeH == 1 and y / windowSizeW then
  print("clicked in window")
end

but it dosen't work.
Bomb Bloke #2
Posted 09 December 2014 - 02:30 AM
local myWindow = window.create(term.current(), 10, 10, 5, 5)

term.setTextColour(colours.white)
term.setBackgroundColour(colours.black)
term.clear()

myWindow.setBackgroundColour(colours.white)
myWindow.clear()

local winPosX, winPosY = myWindow.getPosition()
local winSizeX, winSizeY = myWindow.getSize()

while true do
	local myEvent = {os.pullEvent("mouse_click")}
	
	term.setCursorPos(1,1)
	term.clearLine()
	print("Clicked at ("..myEvent[3]..","..myEvent[4]..")")
	
	term.clearLine()
	if myEvent[3] >= winPosX and myEvent[3] < winPosX + winSizeX and myEvent[4] >= winPosY and myEvent[4] < winPosY + winSizeY then
		print("Click was inside window ("..(myEvent[3] - winPosX + 1)..","..(myEvent[4] - winPosY + 1)..").")
	else 
		print("Click was outside window.")
	end
end
Edited on 09 December 2014 - 01:31 AM
KingofGamesYami #3
Posted 09 December 2014 - 02:32 AM

function isClicked( window, x, y )
  local posx, posy = window.getPosition()
  local sizex, sizey = window.getSize()
  if x > posx and x < posx + sizex and y > posy and y < posy + sizey then
    return true
  end
  return false
end

Something along those lines will work.

Ninja'd by BB
Edited on 09 December 2014 - 01:32 AM
thenextntnick #4
Posted 09 December 2014 - 03:16 AM
Yep, it works! Thanks for your help, guys!