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

Finding data In A Table

Started by WojmusTheGreat, 10 December 2014 - 08:36 PM
WojmusTheGreat #1
Posted 10 December 2014 - 09:36 PM
Hello I'm working on a touch API for terminals and when reading the x and y of the mouseclick I want to be able to see if a button is located at those coordinates.

my code for setting up the table is:


function setButton(ID, text, xMin, xMax, y, Index, Function)
	button[ID] = {}
	button[ID] ["text"] = text
	button[ID] ["xMin"] = xMin
   button[ID] ["xMax"] = xMax
   button[ID] ["y"] = y
   button[ID] ["Function"] = Function
   button[Index] = {}
   button[Index] ["ID"] = ID
   ButtonAmount = ButtonAmount + 1
end

The ID is what the button is reffered to and everything else is self explanatory, Ignore the Index part its a test.

Then to create the button:


function createButton(ID)
   local data = button[ID]
   term.setBackgroundColor(ButtonOffC)
   term.setCursorPos(data["xMin"], data["y"])
   local side = math.ceil((data["xMax"] - data["xMin"]) - data["text"]:len())
   local todraw = side
   while todraw > 0 do
	 term.write(" ")
	 todraw = todraw - 1
   end
   todraw = side
   term.write(data["text"])
   while todraw > 0 do
	 term.write(" ")
	 todraw = todraw - 1
   end
end

This with all my other code give's this: http://imgur.com/rZgvquO

So when the mouse is pressed I want to be able to find out if a button is drawn at those coordinates, I already know how to look for these events and such and have created a simple Menu API which uses arrow keys and Index but this has quite stumped me.

Any help is appreciated.
KingofGamesYami #2
Posted 10 December 2014 - 10:12 PM
Here's how to check if an area was clicked:

function check( mousex, mousey, areax, areay, areamaxx, areamaxy )
  return mousex >= areax and mousex <= areamaxx and mousey >= areay and mousey <= areamaxy
end

As for finding data in the table, I think you might be looking for

for key, value in pairs( tbl ) do
  --#stuff
end
valithor #3
Posted 10 December 2014 - 10:13 PM
-snip

You could probably do something like this.

function check(x,y) -- input x,y coords of where was clicked
  for k,v in pairs(button) do -- checking all of the entires in the button table
	if ((v["y"] == y) and (v["xMin"] >= x) and (v["xMax"]<=x)) then -- checking to see if the area was where the button is
	  return k -- returning the button id
	end
  end
  return false -- none of the entires matched
end

edit: ninja'd
Edited on 10 December 2014 - 09:28 PM
WojmusTheGreat #4
Posted 11 December 2014 - 05:18 PM
-snip

You could probably do something like this.

function check(x,y) -- input x,y coords of where was clicked
  for k,v in pairs(button) do -- checking all of the entires in the button table
	if ((v["y"] == y) and (v["xMin"] >= x) and (v["xMax"]<=x)) then -- checking to see if the area was where the button is
	  return k -- returning the button id
	end
  end
  return false -- none of the entires matched
end

edit: ninja'd

Thankyou for your help This will work perfectly.

Here's how to check if an area was clicked:

function check( mousex, mousey, areax, areay, areamaxx, areamaxy )
  return mousex >= areax and mousex <= areamaxx and mousey >= areay and mousey <= areamaxy
end

As for finding data in the table, I think you might be looking for

for key, value in pairs( tbl ) do
  --#stuff
end

Thankyou for your help.
Edited on 11 December 2014 - 04:22 PM