!******************************************************************************!
! Test a drawing canvas on top of plots (This is experimental is and subject to
! change during any beta release).
!
! Arguments: None
!
! Issue    NXM-2178 PLOT: would like time based line layer on a raster plot
! Since    NeXtMidas 2.9.2
! Author:  J Bauer
!******************************************************************************!
startmacro fn:filename["NULL"]
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)

  call setDatDir

  if "^filename" EQS "NULL"
    set filename "temp_test_feature_time.tmp"
    if filename FEXIST then
      info "^reg.main.name: the file for this test exists, erase if you want&
            to start from scratch."
    else
      set elements 1.2M
      info "^reg.main.name: generating ^elements waveform"
      WAVEFORM,OUT=^filename,FORM=SF,ELEM=elements
      headermod filename fs=100 tc=2008:02:28::16:57:09.657
    endif
  endif
  status filename YD=ydelta
  set deltaT ^ydelta ! 0.013312

  ! get the constants in PlotCanvas for usage later
  invoke i:Y_AS_TIME nxm.sys.libg.PlotCanvas.YASTIME
  invoke i:VERTICAL  nxm.sys.libg.PlotCanvas.VERTICAL

  ! set arrays for use later
  new double[] array {1.83540E9,1.83545E9,1.83550E9,1.83555E9,1.83560E9,1.83565E9,&
    1.83570E9,1.83575E9,1.83580E9,1.83585E9,1.83590E9,1.83595E9,1.83600E9,1.83605E9}
  new double[] array2 {5,10,15,20,25,30,35,40,45,50}
  new double[] ptarray {5,1.83540E9,10,1.83545E9,15,1.83550E9,20,1.83555E9,&
     25,1.83560E9,30,1.83565E9,35,1.83570E9,40,1.83575E9,45,1.83580E9,50,1.83585E9}

  ! the start time of the plot
  timex 2008:02:28::16:57:09.657 wsec=startTime

  ! keep track of the bottom line coords for later use
  res d:bottomLine 0.0

  if interAct then
    set instructions "Click the first button to draw a 100 lines"

    pipe on

      panel/msgid=MAIN/setup/controls=gc
      gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

      gcontrol text  idtext "Instructions" instructions 24 8 /title

      plot/all/id=idplot filename readout="+t|+onAxis|-DateString" /lps=128
      plot/id=mapplot "^{datDir}.world.prm"

      gcontrol label linesLabel "Lines"
        gcontrol button add100Lines "" "Draw 100 lines on raster"
        gcontrol button rem50Lines "" "Remove top 50 lines"
        gcontrol button moveLine "" "Move the bottom line down"
        gcontrol button addMapLines "" "Add line to map"
      gcontrol label pointsLabel "Points"
        gcontrol button addPts "" "Add 10 points to raster"
        gcontrol button remPts "" "Remove points from raster"
      gcontrol label circLabel "Circles"
        gcontrol button addCirc "" "Add Raster Circle"
        gcontrol button moveCirc "" "Move Raster Circle"
        gcontrol button addMapCirc "" "Add Map Circle"
        gcontrol button moveMapCirc "" "Move Map Circle"
      gcontrol label ellLabel "Ellipses"
        gcontrol button addEll "" "Add ellipse to raster"
        gcontrol button addMapEll "" "Add Ellipse to map"
      gcontrol label labelTest "AutoTest"
        gcontrol button runTest "" "Run automated test"
      gcontrol label holdLabel "Refreshes"
        gcontrol button holdButt "Hold Raster Canvas Refreshes" "True,False" "False"
    pipe off

  endif

  !cleanup
  erase/quiet/warn=n ^filename
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqs "ADD100LINES" then
    call add100Lines

  elseif msg.name eqs "REM50LINES" then
    call rem50Lines

  elseif msg.name eqs "MOVELINE" then
    call moveBotLine

  elseif msg.name eqs "ADDMAPLINES" then
    call addMapLines

  elseif msg.name eqs "ADDPTS" then
    call addPoints

  elseif msg.name eqs "REMPTS" then
    call remPoints

  elseif msg.name eqs "ADDCIRC" then
    call addCircle

  elseif msg.name eqs "MOVECIRC" then
    call moveCircle

  elseif msg.name eqs "ADDMAPCIRC" then
    call addMapCirc

  elseif msg.name eqs "MOVEMAPCIRC" then
    call moveMapCirc

  elseif msg.name eqs "ADDELL" then
    call addEllipse

  elseif msg.name eqs "MOVEELL" then
    call moveEllipse

  elseif msg.name eqs "ADDMAPELL" then
    call addMapEllipse

  elseif msg.name eqs "RUNTEST" then
    call runAutoTest

  elseif msg.name eqs "HOLDBUTT" then
    call holdRefresh ^msg.data

  else
    say "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

! Add 100 horozontal lines to the raster plot
procedure add100Lines
  res d:x1 reg.idplot.xmin
  res d:x2 reg.idplot.xmax
  calc t1 startTime 1000 +
  res t2 ^t1
  res s:objColor "green"
  new java.awt.Color(0,0,255) col   ! default
  res t:lData {}
  res t:lTab {}
  invoke ,, reg.idplot.addCanvas()

  set gc.idtext.value "You should see 100 horizontal lines that alternate between green and blue."
  do count 1 100 1
    !invoke reg.idplot.addToCanvas("",x1,t1,x2,t2,true,objColor)
    set lData.X1 x1
    set lData.Y1 t1
    set lData.X2 x2
    set lData.Y2 t2
    set lData.ISTIME true
    set lTab.TYPE "LINE"
    set lTab.COLOR col
    set lTab.DATA lData
    invoke ,, reg.idplot.canvas.addToCanvas(lTab)

    calc t1 ^t1 10000 +
    set t2 ^t1
    if objColor eqs "green" then
      set objColor "blue"
       new java.awt.Color(0,255,0) col
    else
      set objColor "green"
      new java.awt.Color(0,0,255) col
    endif
  enddo
  calc bottomLine ^t1 10000 - ! keep track of the bottom line
  invoke reg.idplot.refresh()  ! call paint method (can also call idplot.refresh() )

return

! remove the top 50 lines
procedure rem50Lines
  calc t1 startTime 1000 +
  set t2 ^t1
  set gc.idtext.value "The top half (50) of the horizontal lines should be gone."
  do count 1 50 1
    invoke ,, reg.idplot.canvas.removeLine(x1,t1,x2,t2)
    calc t1 ^t1 10000 +
    set t2 ^t1
  enddo

  invoke reg.idplot.refresh()  ! remove artifacts from plot

return

! Add lines to the map Canvas
procedure addMapLines
  res t:mapLine1 {}
  res t:mapData1 {}
  set mapLine1.TYPE "LINE"
  set mapLine1.COLOR "YELLOW"
  set mapData1.X1 -77 ! lon
  set mapData1.Y1 38  ! lat
  set mapData1.X2 135 ! lon
  set mapData1.Y2 -22 ! lat
  set mapData1.ISTIME false
  set mapLine1.DATA mapData1

  invoke ,, reg.mapplot.addCanvas()
  invoke ,, reg.mapplot.canvas.addToCanvas(mapLine1)
  invoke reg.mapplot.refresh()  ! call refresh rather than paint since 3.7.2 for NXM-2841

  set gc.idtext.value "There should be a yellow line on the map from app from New York to Australia."

return

! Move the bottom line down a bit
procedure moveBotLine
  calc newLine bottomLine 10000 +
  invoke myres reg.idplot.canvas.moveObject(x1,bottomLine,x2,bottomLine,x1,newLine,x2,newLine,true)
  set bottomLine ^newLine
  set gc.idtext.value "The bottom line should re-position lower each time the button is pressed."

return

! Add Points
procedure addPoints
  ! draw a set of points diagonally across the plot
  !invoke ,, reg.idplot.setCanvasColor(blue)
  invoke ,, reg.idplot.addCanvas()
  invoke canvas reg.idplot.getCanvas()
  invoke ,, canvas.setPointSize(20)
  invoke ,, canvas.setPointSymbol("Diamond")
  set gc.idtext.value "You should see 10 blue diamonds on the plot."
  invoke ,, canvas.draw(ptarray,1,0,10,"TEST",^{Y_AS_TIME},"blue")
  invoke ,, reg.idplot.refresh()  ! call paint method (can also call idplot.refresh() )

return

!remove the points previously added
procedure remPoints
  set xPos 5
  set yPos 1.83540E9

  do count 1 10 1
   invoke reg.idplot.canvas.removePoint(xPos,yPos)
   calc xPos xPos 5 +
   calc yPos yPos 50000 +
  enddo
  set gc.idtext.value "The diamonds should be gone."
  invoke reg.idplot.refresh()  ! remove artifacts from plot

return

! Add a circle on the plot
procedure addCircle
  set xPos 20
  set yPos 1.83590E9
  set radius 150000
  res t:circTab {}
  res t:circData {}
  set circData.X1 ^xPos
  set circData.Y1 ^yPos
  set circData.RADIUS ^radius
  set circData.ISTIME true
  set circData.NPTS -1
  set circTab.color "red"
  set circTab.TYPE "CIRCLE"
  set circTab.DATA circData

  set gc.idtext.value "You should see a red circle appear on the plot at about 20,600e3."

  invoke ,, reg.idplot.addCanvas()
  invoke canvas reg.idplot.getCanvas()
  invoke ,, canvas.addToCanvas(circTab)
  invoke reg.idplot.refresh()  ! call refresh rather than paint since 3.7.2 for NXM-2841
return

! Move the circle on the raster plot to a different location
procedure moveCircle
  set xPos    20
  set newXPos 60
  set yPos 1.83590E9
  invoke canvas reg.idplot.getCanvas()
  invoke ,, canvas.moveObject(xPos,yPos,xPos,yPos,newXPos,yPos,newXPos,yPos,true)
  set gc.idtext.value "The red circle should move to 60,600e3."

return

! Add a circle to the map plot
procedure addMapCirc
  res t:mapCirc {}
  res t:mapData {}
  set mapData.X1 -77  ! lon
  set mapData.Y1 38 ! lat
  set mapData.RADIUS 20  ! in degrees
  set mapData.ISTIME false
  set mapData.NPTS -1
  set mapCirc.TYPE "CIRC"
  set mapCirc.COLOR "red"
  set mapCirc.DATA mapData

  invoke ,, reg.mapplot.addCanvas()
  invoke mapcanvas reg.mapplot.getCanvas()
  invoke ,, mapcanvas.addToCanvas(mapCirc)
  invoke ,, reg.mapplot.refresh() ! call refresh rather than paint since 3.7.2 for NXM-2841
  set gc.idtext.value "You should see a red circle with a radius of 20 deg on the map centered near New York."

return

! Move the circle on the map plot
procedure moveMapCirc
  set xPos -77
  set yPos 38
  set newXPos 135
  set newYPos -22
  invoke canvas reg.mapplot.getCanvas()
  invoke ,, canvas.moveObject(xPos,yPos,xPos,yPos,newXPos,newYPos,newXPos,newYPos,true)

  set gc.idtext.value "The circle on the map should have moved to Australia."

return

! Add an ellipse to the plot
procedure addEllipse
  set xPos   40
  set yPos 1.83590E9
  res t:ellTab {}
  res t:ellData {}
  set ellData.ALT 0
  set ellData.X1 ^xPos
  set ellData.Y1 ^yPos
  set ellData.SEMIMAJOR 15
  set ellData.SEMIMINOR 50000
  set ellData.TILT 45
  set ellData.DIV -1.0
  set ellTab.COLOR "aqua"
  set ellData.ISTIME true
  set ellTab.DATA ellData
  set ellTab.TYPE "ELL"

  set gc.idtext.value "You should see an aqua-colored ellipse on the raster tilted at 45 degrees."

  invoke ,, reg.idplot.addCanvas()
  invoke canvas reg.idplot.getCanvas()
  invoke ,, canvas.addToCanvas(ellTab)
  invoke reg.idplot.refresh()  ! call refresh rather than paint since 3.7.2 for NXM-2841
return

! Add an ellipse to the map plot
procedure addMapEllipse
  invoke pi nxm.sys.inc.Constants.PI
  invoke radiusEarth nxm.sys.inc.Constants.EARTH_EQUATORIAL_RADIUS
  calc earthCircumference 2 pi * radiusEarth * 360 /
  calc semiMaj earthCircumference 15 *
  calc semiMin semiMaj 2 /
  res t:mapEll {}
  res t:mapEllData {}
  set mapEllData.X1 20
  set mapEllData.Y1 40
  set mapEllData.SEMIMAJOR ^semiMaj
  set mapEllData.SEMIMINOR ^semiMin
  set mapEllData.TILT 80
  set mapEllData.DIV 200
  set mapEllData.ISTIME false
  set mapEllData.ALT 0
  set mapEll.DATA mapEllData
  set mapEll.COLOR "aqua"
  set mapEll.TYPE "ELLIPSE"

  invoke ,, reg.mapplot.addCanvas()
  invoke canvas reg.mapplot.getCanvas()
  invoke ,, canvas.addToCanvas(mapEll)
  invoke reg.mapplot.refresh()  ! call refresh rather than paint since 3.7.2 for NXM-2841
  set gc.idtext.value "You should see an aqua ellipse in Eastern Europe tilted at ^mapEllData.TILT degrees."
return

! Draw 14 horizontal lines and 10 vertical lines
procedure runAutotest
  set gc.idtext.value "You should see 14 red horizontal lines and 10 green vertical lines on the raster"
  !invoke ,, reg.idplot.setCanvasColor(green)
  invoke ,, reg.idplot.addCanvas()
  invoke ,, reg.idplot.canvas.draw(array,2,0,14,"TEST",^{Y_AS_TIME},"red")  ! hor lines
  invoke ,, reg.idplot.canvas.draw(array2,2,0,10,"TEST",^{VERTICAL},"green")  ! vert lines

  invoke ,, reg.idplot.refresh()  ! call refresh rather than paint since 3.7.2 for NXM-2841
return

! Hold refreshes of the Canvas
procedure holdRefresh s:holdRef

  if holdRef eqs "TRUE" then
    set gc.idtext.value "No raster canvas drawing should take place."
    set reg.idplot.holdRefresh("+canvas")
  else
    set gc.idtext.value "The raster canvas should display any canvas objects."
    set reg.idplot.holdRefresh("-canvas")
    invoke ,, reg.idplot.refresh()  ! call refresh rather than paint since 3.7.2 for NXM-2841
  endif

return
