Jump to content




[Question] Mouse clicks!


  • You cannot reply to this topic
9 replies to this topic

#1 Stormkrow

  • Members
  • 50 posts
  • LocationSouth Africa

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!

#2 Shnupbups

  • Members
  • 596 posts
  • LocationThat place over there. Y'know. The one where I am.

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.

#3 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

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.

#4 Heracles421

  • Members
  • 258 posts

Posted 13 November 2012 - 07:06 PM

View PostShnupbups100, on 13 November 2012 - 06:42 PM, said:

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

#5 Stormkrow

  • Members
  • 50 posts
  • LocationSouth Africa

Posted 13 November 2012 - 07:10 PM

Ooooohhh thanks guys!!! ^^

#6 Heracles421

  • Members
  • 258 posts

Posted 13 November 2012 - 07:10 PM

View PostHuman, on 13 November 2012 - 06:59 PM, said:

Spoiler

A little bit hard to understand for somebody new, but it works too

#7 Heracles421

  • Members
  • 258 posts

Posted 13 November 2012 - 07:12 PM

View PostStormkrow, on 13 November 2012 - 07:10 PM, said:

Ooooohhh thanks guys!!! ^^

No problem :P/>

#8 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 13 November 2012 - 07:15 PM

View PostHeracles421, on 13 November 2012 - 07:10 PM, said:

View PostHuman, on 13 November 2012 - 06:59 PM, said:

Spoiler

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.

#9 Heracles421

  • Members
  • 258 posts

Posted 13 November 2012 - 07:18 PM

View PostHuman, on 13 November 2012 - 07:15 PM, said:

View PostHeracles421, on 13 November 2012 - 07:10 PM, said:

View PostHuman, on 13 November 2012 - 06:59 PM, said:

Spoiler

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

#10 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 13 November 2012 - 07:55 PM

View PostHeracles421, on 13 November 2012 - 07:18 PM, said:

View PostHuman, on 13 November 2012 - 07:15 PM, said:

View PostHeracles421, on 13 November 2012 - 07:10 PM, said:

View PostHuman, on 13 November 2012 - 06:59 PM, said:

Spoiler

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users