rgl.setMouseCallbacks          package:rgl          R Documentation

_U_s_e_r _c_a_l_l_b_a_c_k_s _o_n _m_o_u_s_e _e_v_e_n_t_s

_D_e_s_c_r_i_p_t_i_o_n:

     This function sets user callbacks on mouse events.

_U_s_a_g_e:

     rgl.setMouseCallbacks(button, begin = NULL, update = NULL, end = NULL)

_A_r_g_u_m_e_n_t_s:

  button: Which button?  

   begin: Called when mouse down event occurs 

  update: Called when mouse moves 

     end: Called when mouse is released 

_D_e_t_a_i_l_s:

     This function sets an event handler 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.  

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

_V_a_l_u_e:

     This function is called for the side effect of setting the mouse
     event handlers.

_A_u_t_h_o_r(_s):

     Duncan Murdoch

_S_e_e _A_l_s_o:

     'par3d' to set built-in handlers

_E_x_a_m_p_l_e_s:

     ## 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")
      }
      pan3d(3)

