rgl.setMouseCallbacks {rgl} | R Documentation

## User callbacks on mouse events

### Description

Set and get user callbacks on mouse events.

### Usage

    rgl.setMouseCallbacks(button, begin = NULL, update = NULL, end = NULL)
    rgl.getMouseCallbacks(button)
    rgl.setWheelCallback(rotate)
    rgl.getWheelCallback()

### Arguments

`button` | Which button?
`begin` | Called when mouse down event occurs
`update` | Called when mouse moves
`end` | Called when mouse is released
`rotate` | Called when mouse wheel is rotated

### Details

The set functions set event handlers on mouse events that occur within the current rgl window. The `begin` and `update` events should be functions taking two arguments; these will be the mouse coordinates when the event occurs. The `end` event handler takes no arguments. The `rotate` event takes a single argument, which will be equal to `1` if the user pushes the wheel away by one click, and `2` if the user pulls the wheel by one click.

Alternatively, the handlers may be set to `NULL`, the default value, in which case no action will occur.

If a subscene has multiple listeners, the user action will still only be called for the subscene that received the mouse event. It should consult `par3d("listeners")` if it makes sense to take action on the whole group of subscenes.

The get function retrieves the callbacks that are currently set.

### Value

The set functions are called for the side effect of setting the mouse event handlers.

The `rgl.getMouseCallbacks` function returns a list containing the callback functions or `NULL` if no user callback is set. The `rgl.getWheelCallback` returns the callback function or `NULL`.

### Author(s)

Duncan Murdoch

### See Also

`par3d` to set built-in handlers

### Examples

    ## Not quite right --- this doesn't play well with rescaling

     pan3d <- function(button) {
       start <- list()

       begin <- function(x, y) {
           start$userMatrix <<- par3d("userMatrix")
           start$viewport <<- par3d("viewport")
           start$scale <<- par3d("scale")
           start$projection <<- rgl.projection()
           start$pos <<- rgl.window2user( x/start$viewport[3], 1 - y/start$viewport[4], 0.5,
                                          projection = start$projection)
       }

       update <- function(x, y) {
            xlat <- (rgl.window2user( x/start$viewport[3], 1 - y/start$viewport[4], 0.5,
                                     projection = start$projection) - start$pos)*start$scale
            mouseMatrix <- translationMatrix(xlat[1], xlat[2], xlat[3])
            par3d(userMatrix = start$userMatrix %*% t(mouseMatrix) )
       }
       rgl.setMouseCallbacks(button, begin, update)
       cat("Callbacks set on button", button, "of rgl device", rgl.cur(), "\n")
     }
     shade3d(icosahedron3d(), col = "yellow")
     pan3d(3)

[Package rgl version 0.96.0 Index]