!******************************************************************************!
! Test Mouse Position ZOOM IN / OUT.
!
! @bug NXM-3261 - Mouse wheel zoom should center on mouse position 
!      NXM-3339 - zooming in too far was unrecoverable  (3.9.1)
! @author:  K. Esterheld / Jeanette McCall
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE
  switch INTERVAL    interval GET DEF=8

  call setDatDir

  set format sf
  set sr 1e4
  set over 0
  set sfreq sr/10
  set chirpamt sfreq/4
  set amp 1e4
  set noiseamp 1e2
  set fsize 512
  set numelem 200k

  invoke tcur nxm.sys.lib.Time.current()

  set basicInstructions &
      "Should see a plot of the world on top with the nations of the world. &
       Should also see a raster plot on the bottom.\n\n&
       1) The mouse wheel zoom will zoom in and out on the center of the plot by default.\n\n&
       2) Set 'Use Mouse Position'to true to use the zoom on mouse position feature.\n&
       When USEMOUSEPOSITION is set, it will zoom towards the mouse position.\n\n&
       3) Use the mouse wheel to zoom in as far as you can\n&
       Even if the zoom goes beyond the plot resolution, you should be able to successfully unzoom."
    set advancedInstructions &
      "While 'Use Mouse Position' is true,\n\n&
      1) Click the 'Auto-zoom to Africa ...'\n\n&
      2) Then quickly zoom in on another part of the world\n\n&
      3) After the auto-zoom happens, make sure zooming in and out works as expected"
    
  set useMousePosition false !default value of usemouseposition in MPlot

  if interAct then

    waveform/id=wavec ram:psdchirp format elem=numelem cos freq=sfreq amp=amp delta=1/sr chirp=^chirpamt start=^tcur
    fft/dbf/id=fftchirp ram:psdchirp ram:fftchirp fsize over=over

    pipe init
    
    ! Change the keyMap to custom settings
    env set KEYMAP {PLOT={LEFT="L",RIGHT="R",in=i,down=d,up=u,out=o}}

    panel/setup/controls=gc
    set sr     1e5
    set sfreq  sr/10
    waveform _wave sf 10e10 sin freq=sfreq amp=1e4 delta=1/sr /throttle=real
    fft _wave _waveb{form=sb} nfft=1k navg=1 over=0 /dbf

    plot/id=plot1/msgid=MAIN/mousewheelzoom/READOUT="+OnAxis"/zoomadj=.1/DBLCLICKZOOM &
            ^{datDir}.world.shp|nxm.sys.test.points.shp view=latlon 
    plot/id=plot2/mousewheelzoom/all ram:fftchirp options=+crosshairs view=yx


    gcontrol label mainLabel "Main"
    gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
    set gc.main.itemColors {PASS=GREEN,FAIL=RED}
    gcontrol/id=text text idtext "Instructions" "^basicInstructions" 40 17 /title

    gcontrol button USEMOUSEPOSITION "Use Mouse Position"  "True,False" "False"
    
    !Advanced instructions
    gcontrol line spacer1
    gcontrol line spacer2
    gcontrol/id=textadv text idtext2 "Advanced Instructions" "^advancedInstructions" 40 10 /title
    gcontrol/nc=1/id=auto button SETUPAUTOZOOM "" "Auto-zoom to Africa in ^interval seconds"

    pipe off

  endif

endmacro

!******************************************************************************!
procedure processMessage m:msg

  !say "msg.name ^msg.name, data ^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "USEMOUSEPOSITION" then
    if useMousePosition then ! if we are already true, we need to change things to false
      invoke reg.plot1.setUseMousePosition(false)
      invoke reg.plot2.setUseMousePosition(false)

      set useMousePosition false
    else                     ! This means we are currently false and we should set things to true
      invoke reg.plot1.setUseMousePosition(true)
      invoke reg.plot2.setUseMousePosition(true)

      set useMousePosition true
    endif
  elseif msg.name eqs "SETUPAUTOZOOM"
    set this.timer(0) interval   ! turn off the timer
  elseif msg.name eqs "TIMER" then
    set this.timer(0) 0.0   ! turn off the timer
    message send PLOT1  name=zoom DATA={X1=-20,X2=20,Y1=-20,Y2=20}       ! zoom to Africa
  else
  ! do nothing
  !say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif

return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return
