!******************************************************************************!
! Test time-based features
!
! Arguments: None
!
!
! @bug      NXM-2090 PLOT / FEATURE: Most of plot disappears after adjusting feature (in BSTORE)
!           NXM-1945 FEATURE: Putting feature into backing store on 2d raster requires manual refresh/resize
! @since    NeXtMidas 2.5.4
! @author:  
!******************************************************************************!
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)

  if interAct then
    switch useALL useALL DEF=true SDEF=true
    switch useY useY DEF=false SDEF=true
    switch useBS useBS DEF=false SDEF=true
    switch help help DEF=false SDEF=true

    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   ! Note: The number of elements is IMPORTANT
                          ! reducing to 1M or lower will NOT show problem!
        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

    if useBS isTRUE then
      set featEnableFlags "GLOBAL|BS"
    else
      set featEnableFlags "GLOBAL"
    endif

    if useY isTRUE then
      set useYorT "Y"
    else
      set useYorT "T"
    endif

    if useAll isTRUE then
      set allSwitch "/ALL"
    else
      set allSwitch ""
    endif
    info "^reg.main.name: all switch '^allSwitch', useY? ^useY, &
                          feature enable flags ^featEnableFlags"
    set tlTable {TIMELINEHANDLER={USEINTERPOLATION=true}}
    invoke bsItemNumber nxm.sys.lib.Parser.find("^featEnableFlags","BS",-1)
    if bsItemNumber > 0 then
      set defBS "On"
    else
      set defBS "Off"
    endif
    pipe on

      panel/msgid=main/setup/controls=gc

      plot/id=psd2/lpb=32/msgid=main^allSwitch filename axis=frame options=+contrast cmap=RAMP readout=SHOW|ONAXIS|T

      gcontrol label macro "MACRO"

      gcontrol Menu  MAIN "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemcolors {PASS=GREEN,FAIL=RED}

      set instructions "1.) With BACKING STORE set to "OFF":\n&
                        a) Click HLINE button and then click on the plot.\n&
                        => plot should show a line feature near where the mouse was clicked.\n&
                        b) Resize the window and the plot will refresh.\n&
                        c) Click on the plot again\n&
                        => feature should move very close to where you click.\n\n&
                        2.) Click the "ON" button in BACKING STORE.\n&
                        The plot does not completely refresh unless you & 
                        resize the window (this is a Known DR 527373-3).\n\n&
                        3.) Click the "OFF" button in BACKING STORE\n&
                        a) Click the "T" button in Y-AXIS or TIME.\n&
                        => Clicking on the plot will move the feature location\n   &
                           based on the plot time.\n&
                        b) Zoom in somewhere in the lower 1/2 of the display\n&
                        c) Click on the plot\n&
                        => make sure the line feature still appears\n   &
                           AND time is NOT t=00:00:00.000\n\n&
                        4.) With BACKING STORE set to "OFF":\n&
                        a) Click the "Y" button in Y-AXIS or TIME.\n&
                        => Clicking on the plot will move the feature location\n   &
                           based on the plot Y-Axis location.\n&
                        b) Zoom in.\n&
                        c: Click on the plot.\n&
                        => The line feature should move very close to where you click."


      gcontrol TEXT idtext "Instructions" "^instructions" 20 29 /title

      gcontrol Button addfeat "Add Feature" "HLINE"
      gcontrol Button featureBS "BACKING STORE" "ON,OFF" "^defBS"
      gcontrol Button featureUSEY "Y-AXIS or TIME" "Y,T" "^useYorT"
      gcontrol button moveFeat "" "Move Feature" 

      if useY isTRUE then
        info "^reg.main.name: NO DT on feature creation..."
        new "nxm.sys.libg.Feature" psdmarker {NAME=AUMARK,TYPE=YLINE|INVERSE,COLOR=BLUE,ENABLE="^featEnableFlags"}
      else
        info "^reg.main.name: using DT..."
        new "nxm.sys.libg.Feature" psdmarker {NAME=AUMARK,TYPE=YLINE|SYMBOL,DT=^deltaT,X=3.98E8,COLOR=BLUE,ENABLE="^featEnableFlags"}
      endif
    pipe off

    !BUG 1744
    erase/quiet/warn=n ^filename
  endif
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 "ADDFEAT" then
    set reg.psd2.addfeature psdmarker

  elseif msg.name eqs "MOVEFEAT" then
    set reg.psd2.addfeature psdmarker   
    set reg.panel.timer(0) 0.5
    
  elseif msg.name eqs "TIMER" then
    set msg.name "MARK"
    calc newY ^psdmarker.y 100 +
    calc newT ^psdmarker.t 10000 +
    set msg.data {y=^newY,t=^newT}

  elseif msg.name eqs "MARK" then
    timex ^msg.data.t S:timeString
    info "^reg.main.name: moving feature to time t=^{timeString}, (X=^msg.data.x, y=^msg.data.y)"
    if useYorT EQS "Y" then
      if psdmarker.dt > 0 then
        set psdmarker.dt -1
      endif
      set psdmarker.y  ^msg.data.y
    else
      if psdmarker.dt < 0 then
        set psdmarker.dt deltaT
      endif
      set psdmarker.t  ^msg.data.t
    endif

  elseif msg.name EQSS "FEATUREBS" then
    if msg.data isTRUE then
      set psdmarker.enable "+BS"
    else
      set psdmarker.enable "-BS"
    endif

  elseif msg.name EQSS "FEATUREUSEY" then
    set useYorT "^msg.data"

  elseif msg.name EQS "ZOOM" then
    ! do nothing

  elseif msg.name EQS "UNZOOM" then
    ! do nothing

  else
    warn "^reg.main.name: unrecognized msg ^msg.name from msg.fid"
  endif
 
  if msg.name eqs "MARK" then
    timex ^msg.data.t S:timeString
    info "^reg.main.name: moving feature to time t=^{timeString}, (X=^msg.data.x, y=^msg.data.y)"
    if useYorT EQS "Y" then
      if psdmarker.dt > 0 then
        set psdmarker.dt -1
      endif
      set psdmarker.y  ^msg.data.y
    else
      if psdmarker.dt < 0 then
        set psdmarker.dt deltaT
      endif
      set psdmarker.t  ^msg.data.t
    endif
  endif
return
