Difference between revisions of "Mouse drag (event)"

From ComputerCraft Wiki
Jump to: navigation, search
(Created event page)
 
m (Fixed the second example as it would just clear the screen whilst the background colour was set to red. tl;dr: I moved the term.setBackgroundColor(colors.black) to the start of example two.)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{NeedsWork|A demonstration on the use and handling of this event would be beneficial. ''[[User:AfterLifeLochie|AfterLifeLochie]] 16:00, 30 November 2012 (MSK)''}}
+
The mouse_drag event occurs every time the mouse is moved until the user releases the mouse button. It will always occur after a [[mouse_click_(event)|mouse_click]] event. The event will only be fired when the mouse moves to another character position, dragging within the same character will have no effect.
  
 
{{Event
 
{{Event
 
|name=mouse_drag
 
|name=mouse_drag
 
|desc=Fired when the mouse is dragged (implies that a [[mouse_click_(event)|mouse_click]] event has already occurred).
 
|desc=Fired when the mouse is dragged (implies that a [[mouse_click_(event)|mouse_click]] event has already occurred).
|return1=The new X-coordinate of the mouse (in screen-characters).
+
|return1=The mouse button that was clicked.
|return2=The new Y-coordinate of the mouse (in screen-characters).
+
|return2=The X-coordinate of the mouse (in screen-characters).
 +
|return3=The Y-coordinate of the mouse (in screen-characters).
 +
|examples=
 +
{{Example
 +
|desc=Print the new co-ordinates of the mouse every time a ''mouse_drag'' event occurs.
 +
|code=
 +
while true do
 +
  _, button, xPos, yPos = os.pullEvent("[[mouse_drag_(event)|mouse_drag]]")
 +
  [[print]]("mouse_drag => " .. [[tostring]](button) .. ": " ..
 +
    "X: " .. [[tostring]](xPos) .. ", " ..
 +
    "Y: " .. [[tostring]](yPos))
 +
end
 +
|output=The X and Y position of the mouse during the drag.
 +
}}
 +
{{Example
 +
|desc=Draws a single pixel on the screen, which is moved to the mouse position when mouse is clicked and dragged on the screen
 +
|code=
 +
x = 5
 +
y = 5
 +
while true do
 +
  [[term.setBackgroundColor]]( colors.black )
 +
  [[term.clear]]()
 +
  [[paintutils.drawPixel]]( x, y, colors.red )
 +
 
 +
  event, button, xPos, yPos = os.pullEvent("[[mouse_drag_(event)|mouse_drag]]")
 +
 +
  if button == 1 then -- left button was clicked
 +
    x = xPos
 +
    y = yPos
 +
  end
 +
end
 +
|output=
 +
}}
 
}}
 
}}

Latest revision as of 09:06, 11 November 2014

The mouse_drag event occurs every time the mouse is moved until the user releases the mouse button. It will always occur after a mouse_click event. The event will only be fired when the mouse moves to another character position, dragging within the same character will have no effect.


Grid Modem.png  Event mouse_drag
Fired when the mouse is dragged (implies that a mouse_click event has already occurred).
Returned Object 1 The mouse button that was clicked.
Returned Object 2 The X-coordinate of the mouse (in screen-characters).
Returned Object 3 The Y-coordinate of the mouse (in screen-characters).

Examples

Grid paper.png  Example
Print the new co-ordinates of the mouse every time a mouse_drag event occurs.
Code
while true do
  _, button, xPos, yPos = os.pullEvent("mouse_drag")
  print("mouse_drag => " .. tostring(button) .. ": " ..
    "X: " .. tostring(xPos) .. ", " ..
    "Y: " .. tostring(yPos))
end
Output The X and Y position of the mouse during the drag.



Grid paper.png  Example
Draws a single pixel on the screen, which is moved to the mouse position when mouse is clicked and dragged on the screen
Code
x = 5
y = 5
while true do
  term.setBackgroundColor( colors.black )
  term.clear()
  paintutils.drawPixel( x, y, colors.red )
 
  event, button, xPos, yPos = os.pullEvent("mouse_drag")

  if button == 1 then -- left button was clicked
    x = xPos
    y = yPos
  end
end