cbarbu / R-package-zoom

An R package to zoom in and out and more generally navigate a plot or a group of plots in R.
GNU Lesser General Public License v3.0
5 stars 1 forks source link

Keep `zm()` from intercepting left clicks #11

Open mlaparie opened 1 year ago

mlaparie commented 1 year ago

I am working on a fork/PR for the package geomorph and would like to add an optional "interactive.zoom" option for some of its functions, which would depend on zm().

Before integrating the feature directly in geomorph, I have experimented with a simple function in R combining both packages, and reached a satisfactory result with this:

library(jpeg)
library(geomorph)
library(zoom)

digitize2d_zoom <- function(filelist, nlandmarks, scale, tpsfile, MultScale = TRUE, verbose = TRUE) {
    for (i in 1:length(filelist)) {
        # Read image
        img <- readJPEG(filelist[i])
        # Create new plot window
        plot(NA,
             xlim = c(0, ncol(img)),
             ylim = c(0, nrow(img)),
             type = "n",
             asp = 1,
             axes = FALSE,
             ylab = "",
             xlab = "RMB, +, i: zoom in
MMB, -, o: zoom out
left, down, up, right, h, j, k, l: pan
r: reset
s: show limits
q: quit",
             cex.lab = 0.6,
             family = "mono",
             adj = 0)
        rasterImage(img, 0, 0, ncol(img), nrow(img))
        grid()
        # Zoom feature
        zm()
        # Digitize landmarks
        # [Code for digitalization using geomorph's digitize2d here]
    }
}

# Test
test <- list.files("/path/to/images/",
                    pattern = "\\.jpg$",
                    full.names = TRUE,
                    ignore.case = TRUE,
                    recursive = TRUE, all.files = TRUE)

digitize2d_zoom(test,
       nlandmarks = 12,
       scale = NULL,
       tpsfile = "/tmp/test.tps",
       verbose = FALSE)

However, zm() keeps intercepting the left click events, which it uses to pan the image when holding and moving the cursor. This conflicts with geomorph also using the left click to digitize landmarks and save their coordinates. Would there be any way to make zm() ignore left clicks and let them be passed along to the other function? Or can we make it monitor RMB or MMB being held to move the view, instead of LMB?

Thanks for the help!

cbarbu commented 1 year ago

Hi, I'm sorry not be more reactive. I'm not sure how to do what you suggest. In particular, I wonder what you mean by "monitor RMB or MMB being held to move the view, instead of LMB". Bests, Corentin

On Tue, Jan 24, 2023 at 6:40 PM Mathieu Laparie @.***> wrote:

I am working on a fork/PR for the package geomorph and would like to add an optional "interactive.zoom" option for some of its functions, which would depend on zm().

Before integrating the feature directly in geomorph, I have experimented with a simple function in R combining both packages, and reached a satisfactory result with this:

library(jpeg) library(geomorph) library(zoom)

digitize2d_zoom <- function(filelist, nlandmarks, scale, tpsfile, MultScale = TRUE, verbose = TRUE) { for (i in 1:length(filelist)) {

Read image

    img <- readJPEG(filelist[i])
    # Create new plot window
    plot(NA,
         xlim = c(0, ncol(img)),
         ylim = c(0, nrow(img)),
         type = "n",
         asp = 1,
         axes = FALSE,
         ylab = "",
         xlab = "RMB, +, i: zoom in\nMMB, -, o: zoom out\nleft, down, up, right, h, j, k, l: pan\nr: reset\ns: show limits\nq: quit",
         cex.lab = 0.6,
         family = "mono",
         adj = 0)
    rasterImage(img, 0, 0, ncol(img), nrow(img))
    grid()
    # Zoom feature
    zm()
    # Digitize landmarks
    # [Code for digitalization using geomorph's digitize2d here]
}

}

Test

test <- list.files("/path/to/images/", pattern = "\.jpg$", full.names = TRUE, ignore.case = TRUE, recursive = TRUE, all.files = TRUE)

digitize2d_zoom(test, nlandmarks = 12, scale = NULL, tpsfile = "/tmp/test.tps", verbose = FALSE)

However, zm() keeps intercepting the left click events, which it uses to pan the image when holding and moving the cursor. This conflicts with geomorph also using the left click to digitize landmarks and save their coordinates. Would there be any way to make zm() ignore left clicks and let them be passed along to the other function? Or can we make it monitor RMB or MMB being held to move the view, instead of LMB?

Thanks for the help!

— Reply to this email directly, view it on GitHub https://github.com/cbarbu/R-package-zoom/issues/11, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEBIBDXU2E7YZNVQOIHTFLWUAHZ7ANCNFSM6AAAAAAUFL67JM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

--


Corentin M. Barbu-Covantes (484) 843-1580 http://scholar.google.com/citations?hl=en&user=sxDMRdQAAAAJ "For what does it profit a man to gain the whole world, and forfeit his soul?" Mark 8:36

mlaparie commented 1 year ago

Hi,

No worries! I used "RMB", "MMB" and "LMB" for "Right mouse button", "Middle mouse button" and "Left mouse button", respectively.

What I meant is, to make zm() work in concert with geomorph, it would be necessary to keep zm() from intercepting LMB events, which it currently does to pan the image. Instead, if I could alter the source of zm() so that it uses RMB or MMB instead of LMB, I think the two package could be used simultaneously, and this would make zm() a pretty convenient addition to any morphometrics workflow in R.

cbarbu commented 1 year ago

Hi, Thanks for the precision. The main difficulty for me is that I don't use any other interfering package. II tried the code you sent but I'm not sure how to test if the LMB is intercepted or not ? In any case, if you have a working alteration of the code (I'm thinking of a zm(mouse=FALSE) option deactivating the LMB+hold) in a fork I'd be glad to see if I can integrate it in the main repository of zoom. Bests, Corentin

On Mon, Mar 6, 2023 at 4:09 PM Mathieu Laparie @.***> wrote:

Hi,

No worries! I used "RMB", "MMB" and "LMB" for "Right mouse button", "Middle mouse button" and "Right mouse button", respectively. What I meant is, to make zm() work in concert with geomorph(), it would be necessary to keep zm() from intercepting LMB events, which it currently does to pan the image. Instead, if I could alter the source of zm() so that it uses RMB or MMB instead of LMB, I think the two package could be used simultaneously, and this would make zm() a pretty convenient addition to any morphometrics workflow in R.

— Reply to this email directly, view it on GitHub https://github.com/cbarbu/R-package-zoom/issues/11#issuecomment-1456312647, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEBIBBRSYYGKLLBPZCYI5DW2X42ZANCNFSM6AAAAAAUFL67JM . You are receiving this because you commented.Message ID: @.***>

--


Corentin M. Barbu-Covantes (484) 843-1580 http://scholar.google.com/citations?hl=en&user=sxDMRdQAAAAJ "For what does it profit a man to gain the whole world, and forfeit his soul?" Mark 8:36

mlaparie commented 1 year ago

You can use the geomorph's vanilla function called digitize2d() to see how it works when not using zm() with it. Typically, it'd add a red dot (or another symbol, I don't remember as I edited on my machine to allow customizing the shape) at the coordinates the user clicked (LMB), and append a .TPS file with the corresponding coordinates. Upon reaching the number of points set when running the command nlandmarks = xx, the next image in the list is shown, and a new set of coordinates is being recorded in the .TPS file.

I haven't re-run the code I posted above because I am another computer at the moment and would need to compile some packages first, but as far as I remember, zm() was taking priority on LMB and therefore digitize2d_zoom() would not do what the original digitize2d() does.

I'd be happy to provide a pull request but I'm not exactly sure how zm() works at the moment. Does it depend upon other packages, like locator()? If so, I think the limitation might be with locator() itself instead of zm(), since its description does say that it uses the "first mouse button". :\