!******************************************************************************!
! Test the plotting of FEATUREs that make sense on a world background
!
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)

  call setDatDir

if interAct then
  res normalTests "None,Ellipse,Update"

  pipe init

    panel/msgid=MAIN/setup/controls=gc
    plot/id=idplot ^{datDir}.world.prm

    gcontrol label macro "MACRO"
    gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
    set gc.main.itemcolors {PASS=GREEN,FAIL=RED}

    gcontrol text idtext "INSTRUCTIONS" "initializing..." 24 21 /title

    gcontrol/nc=3 button nextfeature "FEATURE" ^normalTests none

    gcontrol button move "MOVE FEATURE" "W/ REFRESH,W/O REFRESH"

  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 eqss "NEXT" then
  if msg.data EQS "ELLIPSE" then
    call test_feature_ellipse
  elseif msg.data EQS "UPDATE" then
    call updateFeature
  elseif msg.data EQS "NONE" then
    set reg.idplot.removeFeature "*"
    call setInstructions "All features should have been removed from the plot."
  else
    warning "Do not know ^msg.data feature"
  endif

elseif msg.name eqs "MOVE" then
  if msg.data eqs "W/ REFRESH" then
    call moveFeature TRUE
  else
    call moveFeature FALSE
  endif
else
  ! do nothing
  ! 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

!******************************************************************************!
procedure open
  call setInstructions "Click all the buttons below (starting with 'Ellipse'), &
   and follow the instructions."
return

!******************************************************************************!
procedure setInstructions s:msgString
  set reg.panel.controls.idtext.value msgString
return

!******************************************************************************!
procedure test_feature_ellipse
  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 * ! 15 degrees
  calc semiMin semiMaj 2 /            ! 7.5 degrees
  set tilt 0
  ! add ellipse to plot
  feature label=fellipse table={NAME="ELLIPSE",TYPE="ELPS",X=15,Y=7.5,&
                                SEMIMAJOR=semiMaj,SEMIMINOR=semiMin,TILT=90,&
                                COLOR="GREEN"} plot=idplot

  ! The RED and YELLOW ellipses test the fix for Bug 1338
  feature label=lellipse table={NAME="LEFT",TYPE="ELPS",X=-175,Y=-7.5,&
                                SEMIMAJOR=semiMaj,SEMIMINOR=semiMin,TILT=90,&
                                COLOR="RED"} plot=idplot

  feature label=rellipse table={NAME="RIGHT",TYPE="ELPS",X=175,Y=7.5,&
                                SEMIMAJOR=semiMaj,SEMIMINOR=semiMin,TILT=90,&
                                COLOR="YELLOW"} plot=idplot

  ! add 2nd ellipse and circle to plot
  calc semiMaj2 earthCircumference 5 * ! 5 degrees
  calc semiMin2 semiMaj2 2 /         ! 2.5 degrees
  feature label=fellipse2 table={NAME="ELLIPSECIRC",TYPE="ELPS|CIRC",X=15,Y=7.5,DX=5,&
  		                           SEMIMAJOR=semiMaj2,SEMIMINOR=semiMin2,TILT=45,&
                                 COLOR="WHITE"} plot=idplot

  ! add circle to plot
  ! feature label=fcircle table={NAME="CIRCLE",TYPE="CIRC",X=15,Y=7.5,DX=5,COLOR="RED"} plot=idplot

  call setInstructions "Expected:\n&
                       - 2 ellipses and 1 circle centered at (15,7.5)\n&
                       - GREEN ellipse should span:\n  &
                         30 degrees (two grid lines) in X\n  &
                         15 degrees (1 grid line) in Y.\n&
                        - WHITE circle with radius of 2.5 degrees.\n&
                        - WHITE ellipse surrounds the circle.\n  &
                          and is tilted (rotated) by 45 degrees.\n&
                        - 2 similarly-sized ellipses (RED and YELLOW):\n  &
                           are centered such that they cross +/-180.\n\n&
                        1) Zoom as much as possible to the edges of the WHITE ellipse and circle\n\n&
                        => They should be still be rendered if their coordinates are still on the screen."

return

!*******************************************************************************
procedure updateFeature

  do count 1 500 1
    set fellipse.x 15
    set fellipse.y 7.5
    set fellipse.semiMajor semiMaj
    set fellipse.semiMinor semiMin
    set fellipse.tilt 90
  enddo

  call setInstructions "Prior to fix for BUG 1813, Plot would flicker even if &
                        updating the same values."
return

!*******************************************************************************
procedure moveFeature s:doRefresh

  ! Make sure the features are displayed
  call test_feature_ellipse

  set s:instrFalse "After several seconds: &
                   GREEN ellipse should instantly change locations.\n\n&
                   ************************************\n&
                    Prior to fix for BUG 1813, the ellipse &
                    would get redrawn every .1 seconds and the plot would &
                    flicker even if updating the same values."

   set s:instrTrue "The green ellipse should slowly change locations over several seconds."

  if doRefresh eqs "FALSE" then
    set reg.idplot.holdRefresh features
    call setInstructions "^instrFalse"
  else
    set reg.idplot.holdRefresh NONE  !should not need this but just in case it is in a weird state
    call setInstructions "^instrTrue"
  endif

  do count 1 20 1
    set fellipse.x count*2
    set fellipse.y count
    set fellipse.semiMajor semiMaj
    set fellipse.semiMinor semiMin
    set fellipse.tilt 90
    pause .1
  enddo

  ! The line below calls refresh in feature
  if doRefresh eqs "FALSE" then set reg.idplot.holdRefresh "NONE"

return
