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

mouse related question-or two

Started by makerimages, 19 December 2012 - 01:58 AM
makerimages #1
Posted 19 December 2012 - 02:58 AM
hi, i`ve got a quick-mouse related question.

Lets say I have a piece of code to check if a letter of a word was pressed


event, p1, p2, p3 = os.pullEvent(mouse_click)
if p2==21 and p3==18 then
  shell.run("programs/programMenu")
end

how can i make it so that the full word ("PROGRAMS") is clickable? and when I curently click any other mouse button, the program stops running, how to prevent that?
theoriginalbit #2
Posted 19 December 2012 - 03:14 AM
the easiest way I can think of off the top of my head would be to know the x and y of where programs is written, and then check if the x and y of where they clicked y = wordY and x >= wordX and x <= wordX + wordLength

as for the other issue I'm not too sure as I haven't messed around too much with the mouse event, give me a few minutes and ill have a look.
theoriginalbit #3
Posted 19 December 2012 - 03:19 AM
well i've looked into the events so I could see exactly what you were doing there. now. when they click on what I'm assuming is an icon at pixel 21, 18 it runs another program. and which program is it that stops running? the original program or this new one?
Kingdaro #4
Posted 19 December 2012 - 03:37 AM
First off, "mouse_click" should be in quotes.

event, p1, p2, p3 = os.pullEvent("mouse_click")

To answer your first question, just do what TheOriginalBIT said. Check if the x and y coordinates are within bounds of the words. I personally would use a word "object" to make things easier in the future. By object, I mean a table with various values.

To answer your second question, just keep pulling the event in a loop so that it goes back to pull the event again. You should also draw your clickable object in said loop.


obj = {
  text = "PROGRAMS";
  x = 5;
  y = 5;
  w = 8; -- the length of the word
  h = 1; -- the height of the word
}

while true do
  -- draw the object's text, "PROGRAMS" at the object's x and y position
  term.setCursorPos(obj.x, obj.y)
  write(obj.text)

  -- get a mouse click
  event, button, x, y = os.pullEvent("mouse_click")

  -- check it against the object
  if x >= obj.x and x <= obj.x + obj.w - 1 -- minus 1 because of how the coordinate system works
  and y >= obj.y and y <= obj.y + obj.h - 1 then
    obj.text = "Clicked!"
    sleep(0.5)
    obj.text = "PROGRAMS"
  end
end

The example code here simply changes the object's text to "Clicked!" when you click it.
makerimages #5
Posted 20 December 2012 - 02:42 AM
First off, "mouse_click" should be in quotes.

event, p1, p2, p3 = os.pullEvent("mouse_click")

To answer your first question, just do what TheOriginalBIT said. Check if the x and y coordinates are within bounds of the words. I personally would use a word "object" to make things easier in the future. By object, I mean a table with various values.

To answer your second question, just keep pulling the event in a loop so that it goes back to pull the event again. You should also draw your clickable object in said loop.


obj = {
  text = "PROGRAMS";
  x = 5;
  y = 5;
  w = 8; -- the length of the word
  h = 1; -- the height of the word
}

while true do
  -- draw the object's text, "PROGRAMS" at the object's x and y position
  term.setCursorPos(obj.x, obj.y)
  write(obj.text)

  -- get a mouse click
  event, button, x, y = os.pullEvent("mouse_click")

  -- check it against the object
  if x >= obj.x and x <= obj.x + obj.w - 1 -- minus 1 because of how the coordinate system works
  and y >= obj.y and y <= obj.y + obj.h - 1 then
	obj.text = "Clicked!"
	sleep(0.5)
	obj.text = "PROGRAMS"
  end
end

The example code here simply changes the object's text to "Clicked!" when you click it.

hmm…I left click on it and it does NOTHING, how to fix?
Kingdaro #6
Posted 20 December 2012 - 03:11 AM
Yeah, made a derp there. The text is being set but not being drawn. Simple fix:

  if x >= obj.x and x <= obj.x + obj.w - 1 -- minus 1 because of how the coordinate system works
  and y >= obj.y and y <= obj.y + obj.h - 1 then
    obj.text = "Clicked!"
    term.setCursorPos(obj.x, obj.y)
    write(obj.text)
    sleep(0.5)
    obj.text = "PROGRAMS"
  end