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

[Question] Mouse clicks!

Started by Stormkrow, 13 November 2012 - 05:20 PM
Stormkrow #1
Posted 13 November 2012 - 06:20 PM
Hi all,

Just playing with the new Mouse Capabilities and i wanna make a type of button, there are some helpful tutorials showing how it all works. But my problem is that they set the conditions so that only if you click on the first character of the option or button then the action takes place.
Is there a way to maybe store the co ords in an array or something so have a that i can click anywhere on the button and not a single specific character?

Thanks!
Shnupbups #2
Posted 13 November 2012 - 06:42 PM

event, button, mouseX, mouseY = os.pullEvent("mouse_click") --Registers mouse click
if button == 1 then --if the left mouse button was used
if mouseY == YofOption and mouseX < X1afterOption and mouseX > X1beforeOption then --if the Y co-ord of the mouse is the Y that the button was on, and the X of the mouse is in a smaller co-ord than one X after the button and a larger co-odrd than one X before the button then do...
--put stuff here
end
end
That should fix it. Sorry if it's a bit complicated, I tried to explain it.
Sammich Lord #3
Posted 13 November 2012 - 06:59 PM
Here is a function I made a while back:

function buttonMenu(table)
  while running do
    term.clear()
    for k,v in ipairs(table) do
	  if table[k].buttonVisible then
	    term.setCursorPos(table[k].buttonStartX, table[k].buttonStartY)
	    print(table[k].buttonText)
	  end
    end
    event, buttonType, x, y = os.pullEvent("mouse_click")
    for k,v in ipairs(table) do
	  if buttonType == table[k].buttonType and table[k].buttonVisible then
	    if x >= table[k].buttonStartX and x <= table[k].buttonEndX and y >= table[k].buttonStartY and y <= table[k].buttonEndY then
		  table[k].buttonFunction()
	    end
	  end
    end
  end
  term.clear()
  term.setCursorPos(1,1)
end

Here is the LINK to a example program I made with it. Just use the same vars as in the table and change the values.
Heracles421 #4
Posted 13 November 2012 - 07:06 PM

event, button, mouseX, mouseY = os.pullEvent("mouse_click") --Registers mouse click
if button == 1 then --if the left mouse button was used
if mouseY == YofOption and mouseX < X1afterOption and mouseX > X1beforeOption then --if the Y co-ord of the mouse is the Y that the button was on, and the X of the mouse is in a smaller co-ord than one X after the button and a larger co-odrd than one X before the button then do...
--put stuff here
end
end
That should fix it. Sorry if it's a bit complicated, I tried to explain it.
Yea, that works but if you format it a bit it'd be easier to read, something like:

local event, button, xPos, yPos = os.pullEvent("mouse_click")
    if button == 1 then
        if (xPos => 1 and xPos <= 6 and yPos =>1 and yPos =< 6) then
            --Put stuff here
        end
    end
Here you're saying, check what button is clicked, if it's left button then check if the cursor is inside the X and Y coords, if it is in those coords do stuff.
You can define your stuff in a function to make this part less crowded
Stormkrow #5
Posted 13 November 2012 - 07:10 PM
Ooooohhh thanks guys!!! ^^
Heracles421 #6
Posted 13 November 2012 - 07:10 PM
SpoilerHere is a function I made a while back:

function buttonMenu(table)
  while running do
    term.clear()
    for k,v in ipairs(table) do
	  if table[k].buttonVisible then
	    term.setCursorPos(table[k].buttonStartX, table[k].buttonStartY)
	    print(table[k].buttonText)
	  end
    end
    event, buttonType, x, y = os.pullEvent("mouse_click")
    for k,v in ipairs(table) do
	  if buttonType == table[k].buttonType and table[k].buttonVisible then
	    if x >= table[k].buttonStartX and x <= table[k].buttonEndX and y >= table[k].buttonStartY and y <= table[k].buttonEndY then
		  table[k].buttonFunction()
	    end
	  end
    end
  end
  term.clear()
  term.setCursorPos(1,1)
end

Here is the LINK to a example program I made with it. Just use the same vars as in the table and change the values.


A little bit hard to understand for somebody new, but it works too
Heracles421 #7
Posted 13 November 2012 - 07:12 PM
Ooooohhh thanks guys!!! ^^

No problem :P/>/>
Sammich Lord #8
Posted 13 November 2012 - 07:15 PM
SpoilerHere is a function I made a while back:

function buttonMenu(table)
  while running do
	term.clear()
	for k,v in ipairs(table) do
	  if table[k].buttonVisible then
		term.setCursorPos(table[k].buttonStartX, table[k].buttonStartY)
		print(table[k].buttonText)
	  end
	end
	event, buttonType, x, y = os.pullEvent("mouse_click")
	for k,v in ipairs(table) do
	  if buttonType == table[k].buttonType and table[k].buttonVisible then
		if x >= table[k].buttonStartX and x <= table[k].buttonEndX and y >= table[k].buttonStartY and y <= table[k].buttonEndY then
		  table[k].buttonFunction()
		end
	  end
	end
  end
  term.clear()
  term.setCursorPos(1,1)
end

Here is the LINK to a example program I made with it. Just use the same vars as in the table and change the values.


A little bit hard to understand for somebody new, but it works too
It isn't that hard. But I agree, if somebody is starting out, it will be impossible to understand. However, this has a lot more features then a average 4 line function.
Heracles421 #9
Posted 13 November 2012 - 07:18 PM
SpoilerHere is a function I made a while back:

function buttonMenu(table)
  while running do
	term.clear()
	for k,v in ipairs(table) do
	  if table[k].buttonVisible then
		term.setCursorPos(table[k].buttonStartX, table[k].buttonStartY)
		print(table[k].buttonText)
	  end
	end
	event, buttonType, x, y = os.pullEvent("mouse_click")
	for k,v in ipairs(table) do
	  if buttonType == table[k].buttonType and table[k].buttonVisible then
		if x >= table[k].buttonStartX and x <= table[k].buttonEndX and y >= table[k].buttonStartY and y <= table[k].buttonEndY then
		  table[k].buttonFunction()
		end
	  end
	end
  end
  term.clear()
  term.setCursorPos(1,1)
end

Here is the LINK to a example program I made with it. Just use the same vars as in the table and change the values.


A little bit hard to understand for somebody new, but it works too
It isn't that hard. But I agree, if somebody is starting out, it will be impossible to understand. However, this has a lot more features then a average 4 line function.

True, but we don't need many things here, at least for now so the 4 lines code should work like a charm
Sammich Lord #10
Posted 13 November 2012 - 07:55 PM
SpoilerHere is a function I made a while back:

function buttonMenu(table)
  while running do
	term.clear()
	for k,v in ipairs(table) do
	  if table[k].buttonVisible then
		term.setCursorPos(table[k].buttonStartX, table[k].buttonStartY)
		print(table[k].buttonText)
	  end
	end
	event, buttonType, x, y = os.pullEvent("mouse_click")
	for k,v in ipairs(table) do
	  if buttonType == table[k].buttonType and table[k].buttonVisible then
		if x >= table[k].buttonStartX and x <= table[k].buttonEndX and y >= table[k].buttonStartY and y <= table[k].buttonEndY then
		  table[k].buttonFunction()
		end
	  end
	end
  end
  term.clear()
  term.setCursorPos(1,1)
end

Here is the LINK to a example program I made with it. Just use the same vars as in the table and change the values.


A little bit hard to understand for somebody new, but it works too
It isn't that hard. But I agree, if somebody is starting out, it will be impossible to understand. However, this has a lot more features then a average 4 line function.

True, but we don't need many things here, at least for now so the 4 lines code should work like a charm
I like having my code look nice and have a bunch of features. If you look at the example script, you can make buttons visible, change button properties(pos, text) and much more. I was thinking about making a whole library(API) for it.