{************************** SIPS Module **************************

Module: SAS-Support V2, SXM-SAS

Author: R.D.Villwock aka 'Big Bob'

First Written: September 27, 2007

Current Version: 2.05

Last Modified: July 2, 2008

******************************************************************}
function on_init_aliases  { Shorthand for group-start engine parm stuff }
  declare START_MODE := ENGINE_PAR_START_CRITERIA_MODE

  declare START_ON_CC := ENGINE_PAR_START_CRITERIA_CONTROLLER

  declare START_CC_MIN := ENGINE_PAR_START_CRITERIA_CC_MIN

  declare START_CC_MAX := ENGINE_PAR_START_CRITERIA_CC_MAX
  declare START_KEY_MIN := ENGINE_PAR_START_CRITERIA_KEY_MIN
  declare START_KEY_MAX := ENGINE_PAR_START_CRITERIA_KEY_MAX  

  declare START_CRITERIA := ENGINE_PAR_START_CRITERIA_NEXT_CRIT

  declare ON_NONE := START_CRITERIA_NONE 

  declare ON_CC := START_CRITERIA_ON_CONTROLLER
  declare ON_KEY := START_CRITERIA_ON_KEY

  declare AND_NEXT := START_CRITERIA_AND_NEXT

  declare OR_NEXT := START_CRITERIA_OR_NEXT
  declare AND_NOT := START_CRITERIA_AND_NOT_NEXT
  declare RELEASE_TRIGGER := ENGINE_PAR_RELEASE_TRIGGER
end function { on_init_aliases }

function on_init_general_controls
  declare n  { General Loop index }
             { Strings }
  declare !FootCapTxt[3]
    FootCapTxt[0] := 'Foot Switch OFF'             
    FootCapTxt[1] := ' Foot Switch ON'
    FootCapTxt[2] := '  ****************'
  declare @NoRange
    NoRange := '       Not Set'
  declare !SetIRCap[3]
    SetIRCap[0] := ' Set Inst. Range'
    SetIRCap[1] := ' Hit Lowest Key'
    SetIRCap[2] := ' Hit Highest Key' 
  declare !SetKR1Cap[2]
    SetKR1Cap[0] := 'Set Art/Var Keys'
    SetKR1Cap[1] := 'Hit Key for Art#1'
  declare !SetKR2Cap[3] 
    SetKR2Cap[0] := ' Set Bank Keys'
    SetKR2Cap[1] := ' Hit Low Key'
    SetKR2Cap[2] := ' Hit High Key'
      { Buttons (besides MBtns) }
  declare ui_button AudArt
    move_control(AudArt,0,0)
    set_text(AudArt,'  Audition Art #')
  declare ui_button AudGroup
    move_control(AudGroup,0,0)
    set_text(AudGroup,'  Audition Group')
    AudGroup := true    
  declare ui_button AutoVar
    move_control(AutoVar,0,0)
    set_text(AutoVar,' Auto-Step Var#')
  declare ui_button ManVar
    move_control(ManVar,0,0)  

    set_text(ManVar,'Stay On Set Var#')
    ManVar := true    
  declare ui_button Monitor
    move_control(Monitor,0,0)  

    set_text(Monitor,'      Monitor')
  declare ui_button SetIR
    move_control(SetIR,0,0)  

    set_text(SetIR,SetIRCap[0])

    SetIR := false     
  declare ui_button SetKR1
    move_control(SetKR1,0,0)  
    set_text(SetKR1,SetKR1Cap[0])
    SetKR1 := false
  declare ui_button SetKR2
    move_control(SetKR2,0,0)  
    set_text(SetKR2,SetKR2Cap[0])
    SetKR2 := false
         { Edit Boxes }
  declare ui_value_edit ZoneSize (1,12,1)
    move_control(ZoneSize,0,0)  
    set_text(ZoneSize,'Semitones')
    ZoneSize := 1
         { Knobs }  
  declare ui_knob ArtSel (0,63,1) { Default 1-1 }
    move_control(ArtSel,0,0)  
    set_text(ArtSel,'  Art #')
  declare ui_knob VarSel (0,8,1)  { Default = 0 }
    move_control(VarSel,0,0)  

    set_text(VarSel,' Var #')
          { Menus }
  declare ui_menu ArtMode
    move_control(ArtMode,0,0)  
    declare const amKeys := 100
    declare const amPC := 101
    declare const amBoth := 102
    add_menu_item(ArtMode,' Key-Switches       (100)',amKeys)
    add_menu_item(ArtMode,' Program Chg        (101)',amPC)
    add_menu_item(ArtMode,' Both KS & PC      (102)',amBoth)
    ArtMode := amKeys
  declare ui_menu Assign
    move_control(Assign,0,0)  
    declare const NoAssign := -1
    declare const asnFixed  := 1
    declare const asnChain  := 2
    declare const asnSeries := 3
    declare const asnDFD := 8
    declare const asnRT  := 16
    declare const asnClrArt := 32  { Don't change this assignment }
    declare const asnFormat := 33
    declare const asnVerify := 34
    add_menu_item(Assign,'  Assign Groups',NoAssign)
    add_menu_item(Assign,'***************',NoAssign)        

    add_menu_item(Assign,'----Fixed Art----',NoAssign)    
    add_menu_item(Assign,' Normal Groups',asnFixed)    
    add_menu_item(Assign," 'Inside' Groups",asnFixed+asnDFD)
    add_menu_item(Assign,' Release Groups',asnFixed+asnRT)
    add_menu_item(Assign,' ',NoAssign)    
    add_menu_item(Assign,'---Chained Art---',NoAssign)    
    add_menu_item(Assign,' Normal Groups',asnChain)    
    add_menu_item(Assign," 'Inside' Groups",asnChain+asnDFD)
    add_menu_item(Assign,' Release Groups',asnChain+asnRT)    
    add_menu_item(Assign,' ',NoAssign)        
    add_menu_item(Assign,'----Multi Art----',NoAssign)     
    add_menu_item(Assign,' Normal Series',asnSeries)
    add_menu_item(Assign," 'Inside' Series",asnSeries+asnDFD)
    add_menu_item(Assign,' Release Series',asnSeries+asnRT)    
    add_menu_item(Assign,' ',NoAssign)        
    add_menu_item(Assign,'====Utility====',NoAssign)
    add_menu_item(Assign,'Clear Articulation',asnClrArt)

    add_menu_item(Assign,'Format & Verify',asnFormat)
    add_menu_item(Assign,'Check All Groups',asnVerify)
  declare ui_menu AttachVar

    move_control(AttachVar,0,0)  

    declare const NoAttach   := -1

    declare const atAttach   := 1

    declare const atRemove   := 2
    declare const atClearAll := 3
    add_menu_item(AttachVar,' Keep Settings',NoAttach)    
    add_menu_item(AttachVar,'=================',NoAttach)

    add_menu_item(AttachVar,' Attach to Articulation',atAttach) 

    add_menu_item(AttachVar,'Sever from Articulation',atRemove)
    add_menu_item(AttachVar,'  Detach All Settings',atClearAll)        
  declare ui_menu MaxVars
    move_control(MaxVars,0,0)  
    declare const mv2 := 115
    declare const mv4 := 116
    declare const mv6 := 117
    declare const mv8 := 118
    add_menu_item(MaxVars,' 2 - Variations      (115)',2)  

    add_menu_item(MaxVars,' 4 - Variations      (116)',4)

    add_menu_item(MaxVars,' 6 - Variations      (117)',6)

    add_menu_item(MaxVars,' 8 - Variations      (118)',8) 
    MaxVars := 4     
  declare ui_menu SASMode
    move_control(SASMode,0,0)  
    declare const SAS_On := 1
    declare const SAS_Off := 2
    add_menu_item(SASMode,' SAS  Active',SAS_On)

    add_menu_item(SASMode,' - SAS  OFF -',SAS_Off)
    SASMode := SAS_On
  declare ui_menu Setup
    move_control(Setup,0,0)  

    declare const PlayMode := 1
    declare const AudSetup := 2
    declare const ChgPrefs := 3
    add_menu_item(Setup,'  Play Mode',PlayMode)
    add_menu_item(Setup,'Setup/Audition',AudSetup)
    add_menu_item(Setup,' Preferences',ChgPrefs)
    Setup := PlayMode

  declare ui_menu TrigMode
    move_control(TrigMode,0,0)  
    declare const tmNever := 110
    declare const tmMatch2 := 111
    declare const tmMatch3 := 112
    declare const tmAlways := 113
    add_menu_item(TrigMode,' Never                (110)',tmNever)  
    add_menu_item(TrigMode,' Match in 2           (111)',tmMatch2)
    add_menu_item(TrigMode,' Match in 3           (112)',tmMatch3)
    add_menu_item(TrigMode,' Always               (113)',tmAlways)
    TrigMode := tmNever  { Default }
  declare ui_menu VarMode
    move_control(VarMode,0,0)  
    declare const vmAutoFCR := 120
    declare const vmAutoRR  := 121
    declare const vmKeys    := 122
    add_menu_item(VarMode,' Auto FC Rand     (120)',vmAutoFCR)  { Default }
    add_menu_item(VarMode,' Auto Rd.Robin     (121)',vmAutoRR)

    add_menu_item(VarMode,' KeySW-Select     (122)',vmKeys)

  declare ui_menu VarType
    move_control(VarType,0,0)  

    declare const vtChnOnly := 123

    declare const vtChn_TKT := 124

    declare const vtTKT_Chn := 125 
    add_menu_item(VarType,'   All Natural        (123)',vtChnOnly)  { Default }

    add_menu_item(VarType,' Natural + TKT     (124)',vtChn_TKT)

    add_menu_item(VarType,' TKT + Natural     (125)',vtTKT_Chn)     
          { Labels }
  declare ui_label About (3,1)
    move_control(About,0,0)  
    set_text(About,'    SIPS 2   Articulation and Alternation Script     by RDV')
  declare ui_label About2 (2,1)

    move_control(About2,0,0)  

    set_text(About2,' SIPS 2  Articulation Script    by RDV')    
  declare ui_label ArtBox (2,2)
    move_control(ArtBox,0,0)  
  declare ui_label ArtCap1 (2,1)
    move_control(ArtCap1,0,0)  
    set_text(ArtCap1,' Last Played Note: Information Monitor')
  declare ui_label ArtCap2 (2,1)
    move_control(ArtCap2,0,0)  
    set_text(ArtCap2,'  Monitor On/Off    -    Artic. Control')
  declare ui_label ArtCap3 (2,1)

    move_control(ArtCap3,0,0)  

    set_text(ArtCap3,'   Last Played Note - Assignment Info')    
  declare ui_label FootCap (1,1)
    move_control(FootCap,0,0)  
  declare ui_label IR_Box (1,2)
    move_control(IR_Box,0,0)
  declare ui_label KRBox1 (1,2)
    move_control(KRBox1,0,0)  
  declare ui_label KRBox2 (1,2)
    move_control(KRBox2,0,0)  
  declare ui_label KRCap1 (1,1)
    move_control(KRCap1,0,0)  
    set_text(KRCap1,'  Art/Var Keys')
  declare ui_label KRCap2 (1,1)
    move_control(KRCap2,0,0)  
    set_text(KRCap2,'    Bank Keys')
  declare ui_label VarCap1 (2,1)
    move_control(VarCap1,0,0)  
    set_text(VarCap1,' Auto TKT Chain   -  Sample Zone Size')
  declare ui_label VarCap2 (2,1)
    move_control(VarCap2,0,0)  
    set_text(VarCap2,'  Variation Mode   -    Auto Triggering')
  declare ui_label VarCap3 (2,1)
    move_control(VarCap3,0,0)  
    set_text(VarCap3,'      -- Audition TKT Variations --')
       { Control Handles }
  declare const uAttachVar := 10
  declare const uAudArt    := 11
  declare const uAudGroup  := 12
  declare const uAutoVar   := 13
  declare const uManVar    := 14
  declare const uMonitor   := 15
  declare const uSetKR1    := 16
  declare const uSetKR2    := 17
  declare const uSetup     := 18
  declare const uZoneSize  := 19
  declare const uArtSel    := 20
  declare const uVarSel    := 21
  declare const uArtMode   := 22
  declare const uAssign    := 23
  declare const uMaxVars   := 24
  declare const uSASMode   := 25
  declare const uTrigMode  := 26
  declare const uVarMode   := 27
  declare const uVarType   := 28
  declare const tAbout     := 29
  declare const tAbout2    := 30
  declare const tArtBox    := 31
  declare const tArtCap1   := 32
  declare const tArtCap2   := 33
  declare const tArtCap3   := 34
  declare const tFootCap   := 35
  declare const tIRBox     := 36
  declare const uSetIR     := 37  
  declare const tKRBox1    := 38
  declare const tKRBox2    := 39
  declare const tKRCap1    := 40
  declare const tKRCap2    := 41
  declare const tVarCap1   := 42
  declare const tVarCap2   := 43
  declare const tVarCap3   := 44
  declare const uPrefABtn  := 45
  declare const uPrefCBtn  := 46
  declare const uPrefSw    := 47

  declare const tPrefBox   := 48
  declare const tPrefHdr   := 49
  declare const MaxControl := tPrefHdr  { Highest Control Index }     
  declare const xxx        := -1        { Unused panel anchor position }       
end function { on_init_general_controls }

function on_init_MBtns    { Build MC assignment buttons }
  declare const PASize  := 10  { Persistent array size, never make any smaller }

  declare const mMode   := 0   { CC Btn/Knob reference indices }

  declare const mVTrig  := 1

  declare const mLast   := 1   { Highest MIDI button/knob index }  
  family MBtn   { MIDI CC assignment button system }

    declare ui_button VTrig    { Assignment buttons }

    declare ui_button Mode
    declare Actv  { Flag set when menu is active }

                       { Indexed Constants }
    declare SCMenu[mLast+1] := (0) { 1 = Can Use special MCs, 0 = CCs only }                       

    declare Knob[mLast+1] := (0) { 1 = Uses MIDI Knob, 0 = No Knob }

    declare !Cap[mLast+1]  { Button prefix captions }    

                       { Indexed Variables }

    declare CC[PASize]     { Assigned CC number, -1 = NoCC (unassigned) }

    declare CCV[PASize]    { Last value of assigned CC, normalized 0..8191 }

    declare State[PASize]  { Current button state }

    declare x[mLast+1]     { Button/Menu Panel position }

    declare y[mLast+1]

  end family 
  move_control(MBtn.VTrig,0,0) { Initially hide all MBtns }
  move_control(MBtn.Mode,0,0)
  family MKnob  { MIDI 'Knobs', controlled with associated CC }

    declare DKmin[mLast+1] := (0)    { Default MIDI range limits }

    declare DKmax[mLast+1] := (100)

    declare Kmin[PASize]   { Current MIDI range }

    declare Kmax[PASize]                         

  end family
  MBtn.Cap[mMode]   := 'Mode '  { Initialize button prefix captions }

  MBtn.Cap[mVTrig]  := 'Foot  '

  InzParms    { Initialize to prior-version or default settings }     
end function { on_init_MBtns }

{---------------- Support routines for MIDI Btns/Knobs ----------------}

function HideMenu(bx)

  move_btn(bx,MBtn.x[bx],MBtn.y[bx])

  move_control(MCMenu,0,0)  

end function { HideMenu }

function InzParms

  declare bx

  { Convert from prior version or use 'fresh' install defaults }  

  for bx := 0 to mLast

    MBtn.CC[bx] := NoCC                { Default to CCs unassigned }

    MKnob.Kmin[bx] := MKnob.DKmin[bx]  { Set default MIDI knob ranges }

    MKnob.Kmax[bx] := MKnob.DKmax[bx]      

  end for
  if in_range(Version[vPFC],V2UP,V200) { Could be newer so leave defaults } 

    _read_persistent_var(MBtn.CC)      { If updating from V200 or V2UP }

    _read_persistent_var(MBtn.CCV)

  end if

  for bx := 0 to mLast

    UpdateBtn(bx)   { Display prior version or default CC assignment } 

  end for

end function { InzParms }



function move_btn(bx,x,y)  { Move button bx to position x,y }

  select bx
    case mMode

      move_control(MBtn.Mode,x,y)

    case mVTrig

      move_control(MBtn.VTrig,x,y)

  end select  

end function { move_btn }



function SetBtn(bx,val) { 'bx' = button index, 'val' = button value }

  select bx

    case mMode

      MBtn.Mode := val

    case mVTrig

      MBtn.VTrig := val

  end select

end function { SetBtn }



function SetBtnText(bx,txt)  { 'bx' = btn index, 'txt' = btn text }

  select bx

    case mMode

      set_text(MBtn.Mode,txt)

    case mVTrig

      set_text(MBtn.VTrig,txt)

  end select

end function { SetBtnText }

function SetMKnob(bx,val)  { Set knob indexed with 'bx' to 'val' }
  NOP

end function { SetMKnob }

function ShowMenu(bx)

  move_btn(bx,0,0)

  move_control(MCMenu,MBtn.x[bx],MBtn.y[bx])   

end function { ShowMenu }

function on_init_Std_PanelMap { Control Layout for the Standard SAS Panel }

  declare StdPanel_Map[36] := ( ...

   tArtCap1,       xxx,   tVarCap1,        xxx,   tKRCap1,    uSetIR, ...

    tArtBox,       xxx,   uMaxVars,  uZoneSize,   tKRBox1,    tIRBox, ...

        xxx,       xxx,   tVarCap2,        xxx,       xxx,       xxx, ...

   tArtCap2,       xxx,   uVarMode,  uTrigMode,   tKRCap2,    uSetup, ...

   uMonitor,  uArtMode,   uVarType,     mVTrig,   tKRBox2,  uSASMode, ...

    tAbout2,       xxx, uAttachVar,   tFootCap,       xxx,     mMode)

end function { on_init_Std_PanelMap }

function on_init_Setup_PanelMap { Control Layout for the SAS Setup Panel }

  declare SetupPanel_Map[36] := ( ...

   tArtCap3,       xxx,  tVarCap1,        xxx,   uSetKR1,    uSetIR, ...

    tArtBox,       xxx,  uMaxVars,  uZoneSize,   tKRBox1,    tIRBox, ...

        xxx,       xxx,  tVarCap3,        xxx,       xxx,       xxx, ...

    uAudArt, uAudGroup,   uManVar,    uVarSel,   uSetKR2,    uSetup, ...

    uArtSel,   uAssign,  uAutoVar,        xxx,   tKRBox2,  uSASMode, ...
        xxx,    tAbout,       xxx,        xxx,       xxx,     mMode)

end function { on_init_Setup_PanelMap }

{ ---------------- SAS Version of Other Common Routines --------------- }
function AssignCC(bx)  { Special handling of certain CC assignments }
  if bx = mVTrig
    ShowFootSw         { Show current status of FootSw MC }
  end if

end function { AssignCC }

function DeAssignCC(bx) { Special handling of certain CC de-assignments }
  if bx = mVTrig
    Var.VTrig := true       { Make always true when de-assigned }
    set_text(FootCap,FootCapTxt[2])     { Display '***********' }
  end if
end function { DeAssignCC }

function Reformat(oldpfc)

   { Since this script is V200, 'oldpfc' can only be }

   {  V2UP or V200 and no reformatting is required   }
end function { Reformat }

function ShowFootSw { Display the current state of the VTrig MC }

  declare tx

  

  if MBtn.cc[mVTrig] # NoCC

    tx := Var.VTrig  { Display ON/OFF state }

  else { No MC is assigned }

    tx := 2   { Display a blank label }

  end if

  set_text(FootCap,FootCapTxt[tx])

end function { ShowFootSw }

function SetSASMode(ccv) { MIDI Controller mode change }

  select ccv             { Update Mode }

    case 0 to 63         { Disable SAS when MC in lower half }

      SASMode := SAS_Off

    case 64 to 127       { Enable SAS when MC in upper half }

      SASMode := SAS_On

  end select  

end function { SetSASMode }

function SetXY(h,x,y)

{ Moves the Control whose assigned 'handle' is 'h', to position 'x','y' on panel.

  If x = 0 or y = 0, the Control will be hidden. This case statement ties the

  previously defined index constants to the corresponding control. NOTE: SetXY,

  also sets the 'internal' x,y values for 'overlay' buttons when x > 0 }

  select h

    case mMode                      { MIDI-controlled Knobs & Menus }

      move_control(MBtn.Mode,x,y)   { SASMode MC assign button }
      SetBtnXY(mMode,x,y)             

    case mVTrig

      move_control(MBtn.VTrig,x,y)  { VTrig MC assign button }
      SetBtnXY(mVTrig,x,y)        

    case uManVar   { Buttons }                 

      move_control(ManVar,x,y)
    case uAudArt
      move_control(AudArt,x,y)
    case uAudGroup
      move_control(AudGroup,x,y)
    case uAutoVar
      move_control(AutoVar,x,y)

    case uMonitor

      move_control(Monitor,x,y)
    case uSetIR

      move_control(SetIR,x,y)       
    case uSetKR1
      move_control(SetKR1,x,y)
    case uSetKR2

      move_control(SetKR2,x,y)      

    case uSetup

      move_control(Setup,x,y)

    case uZoneSize

      move_control(ZoneSize,x,y)

    case uArtSel        { Knobs }

      move_control(ArtSel,x,y)

    case uVarSel

      move_control(VarSel,x,y)
    case uArtMode    { Menus }

      move_control(ArtMode,x,y)

    case uAssign

      move_control(Assign,x,y)
    case uAttachVar
      move_control(AttachVar,x,y)
    case uMaxVars

      move_control(MaxVars,x,y)      

    case uSASMode

      move_control(SASMode,x,y)

    case uTrigMode

      move_control(TrigMode,x,y)

    case uVarMode

      move_control(VarMode,x,y)
    case uVarType

      move_control(VarType,x,y)      
    case tAbout     { Labels }

      move_control(About,x,y)
    case tAbout2

      move_control(About2,x,y)      
    case tArtBox

      move_control(ArtBox,x,y)       

    case tArtCap1

      move_control(ArtCap1,x,y)

    case tArtCap2

      move_control(ArtCap2,x,y)
    case tArtCap3

      move_control(ArtCap3,x,y)      
    case tFootCap
      move_control(FootCap,x,y)

    case tIRBox

      move_control(IR_Box,x,y)       

    case tKRBox1

      move_control(KRBox1,x,y)

    case tKRBox2

      move_control(KRBox2,x,y)
    case tKRCap1

      move_control(KRCap1,x,y)

    case tKRCap2

      move_control(KRCap2,x,y)          

    case tVarCap1

      move_control(VarCap1,x,y)

    case tVarCap2

      move_control(VarCap2,x,y)

    case tVarCap3

      move_control(VarCap3,x,y)
    case uPrefABtn                  { Preferences Popup-Panel Controls }

      move_control(Pref.ABtn,x,y)

    case uPrefCBtn

      move_control(Pref.CBtn,x,y)

    case uPrefSw

      move_control(Pref.Switch,x,y)
    case tPrefBox

      move_control(Pref.Box,x,y)

    case tPrefHdr

      move_control(Pref.Hdr,x,y)         
  end select

end function { SetXY }

function Startup  { Extended ICB routine }
  move_control(BigLabel,0,0)     { Hide ICB Prompting Label }

  ICall(ShowRanges,none,none)    { Initial Keyswitch Range Displays }
  if MBtn.CC[mVTrig] = NoCC      { If no FootSw MC is assigned }
    Var.VTrig := 1               { Set VTrig to default on state }
  else   { If FootSw MC is assigned. set VTrig per last MC value }
    Var.VTrig := sh_right(MBtn.CCV[mVTrig],6)    
  end if
  asp.warn := 0 

  ICall(SetAttr,none,none)       { Build attribute table for instrument }
  Art.GX := REF_GROUP_IDX        { Get initial highlighted group }  
  Send_IR                        { Broadcast IR to rest of suite }
  ICall(AcceptPrefs,none,none)   { Copy & broadcast DIP Switch preferences }
  Redraw := true                 { Display standard panel (ie Setup = PlayMode) }

  ICall(UpdatePanel,none,none)   { Update all dynamic controls }  
  if asp.warn # 0
    StatMsg := asp.Msg
    BlinkStatMsg(10)    { Warn user if instrument is not properly configured }
  else                  { Otherwise Update the KS extensions }
    wait(100000)        { Allow other scripts to finish before updating KS }
    ICall(UpdateKS,none,none) { K2 won't allow this inline function in the CCB }
  end if
end function { Startup }

function Update_Format

{ Checks for any prior loaded version of this same script to see if data can be

  imported. If so, any User Preset Data is re-formatted as needed and Fail is

  set to false. If the prior script bears a different SID or if the prior script

  is a later version than this script (or if it is from the V1 series), Fail is

  set to true so the Failure Panel will be displayed at the end of the ICB.}

  declare Fail

  declare OldSID

  declare OldPFC

  

  Fail := true               { Assume Upgrade will fail }

  OldSID := Version[vSID]    { If these aren't the default for this script, they }

  OldPFC := Version[vPFC]    {  belong to a previously loaded, prior V2 script   }

  if OldSID = SID and in_range(OldPFC,V2UP,PFC) 

    { Prior script has same SID, is in V2 series, and is not newer than this one }

    Fail := false            { Upgrade may proceed }

    Version[vSID] := SID     { Update codes to defaults for this script }

    Version[vPFC] := PFC

    Reformat(OldPFC)         { Reformat presets from a prior version }

  end if

  if Fail = true   { Update has failed report error and stop }

    MsgText[3] := ' Automatic Import of Presets FAILED'

    MsgText[4] := "   See Page xx of V2.0 User's Guide"

    Cancel_Startup   { Cancel any XICB 'startup' function }

  else { Update was successful, write prompter and prepare panel }

    MsgText[2] := 'If you see this instead of the Control Panel'

    MsgText[3] := '            Move a MIDI Controller'

    MsgText[4] := " (See Section 1.1 of SIPS 2 User's Guide)"

  end if

end function { Update_Format }

{ ------------------------- SAS Support Stuff ---------------------------}
function UpdatePanel_ { Update dynamic controls & optionally Redraw panel }
  declare bx
  declare ax
  declare artchg

  if Redraw = true    { If overtly requested, first redraw the required panel }

    if Setup = AudSetup

      CopyArray(PMap,SetupPanel_Map)
    else if Setup = PlayMode
      CopyArray(PMap,StdPanel_Map)
    else
      CopyArray(PMap,PrefPanel_Map)
    end if
    ShowPanel(PMap)

    Redraw := false   { Don't do it again unless flag is overtly set again }

  end if
  artchg := true

  set_text(ArtBox,'')        { Erase ArtBox } 
  if Setup = AudSetup
    if UserPrefs .and. ShowBX # 0
      message('')            { Erase any KSw stat display }
    end if      
    bx := ArtSel/8 + 1

    ax := ArtSel mod 8 + 1

    set_knob_label(ArtSel,'  ' & bx & '-' & ax) 
    Art.ArtX := bx*10 + ax   { Update ArtX if changed }
    if AudGroup = true       { Auditioning a Group }
      artchg := false
      set_controller(ArtCC,StartAll)  { Enable all groups for allow/disallow control }
      XSwitch                { Keep KSw group starts 'alive' for groups 0 and 1 }
    end if
  else if Setup = ChgPrefs
    artchg := false
    Pref.ShowHelpCap         { Force initial Help display }     
  end if
  ShowFootSw  { Show status of FootSw MC }
  if artchg = true
    if Setup = PlayMode
      Art.ArtX := Art.Play
      if UserPrefs .and. ShowBX # 0

        ShowKeyState         { Display Art & KSw Bank-Index on K2's status line }

      end if
    end if
    NewArt
  end if
end function { UpdatePanel_ }

{------------------------------------------------------------------

  The following routines provide the User Preferences Panel

  but have no counterparts in the SLS or SVS Support modules

------------------------------------------------------------------}



function on_init_PrefPanel { Build the Change Preferences Panel }

  declare local n



  family Pref

    declare ui_table Switch[16] (2,2,-100)

    declare ISwitch[16]        { DIP Switch images }

    declare ui_button ABtn     { Accept Btn }

    declare ui_button CBtn     { Cancel Btn }

    declare ui_label Hdr (2,1)

    declare ui_label Box (2,2)

    declare @Help1

    declare @Help2

    declare !TipTxt[16]

  end family
  move_control(Pref.Switch,0,0) { Initially Hide these controls }
  move_control(Pref.ABtn,0,0)

  move_control(Pref.CBtn,0,0)

  move_control(Pref.Hdr,0,0)

  move_control(Pref.Box,0,0)    

  for n := 0 to 15             { Default is all switches off }

    Pref.Switch[n] := -100 

    Pref.ISwitch[n] := -100

  end for
  Pref.Switch[5] := 100        { Change default of Sw5, B-X state On }

  set_text(Pref.ABtn,'Accept  Settings')

  set_text(Pref.CBtn,' Cancel Changes')

  Pref.TipTxt[0] := 'Sw1 ^ Disable CC64 Sustain Function'

  Pref.TipTxt[1] := 'Sw2 ^ Disable SAS MIDI Menu Control'

  Pref.TipTxt[2] := 'Sw3 ^ Increment MIDI Program Change Number from Keyboard'
  Pref.TipTxt[3] := 'Sw4 ^ Limit playback of Release Samples to 3 secs'
  Pref.TipTxt[4] := 'Sw5 ^ Reset random generator when' ...
                  & ' Articulation or Var Mode is changed'
  Pref.TipTxt[5] := 'Sw6 ^ Display Bank-Index Key State' ...

                  & " on K2's Status Line"
  Pref.TipTxt[6] := "Sw7 ^ Hide Keyswitch Display on K2's" ...
                  & ' Keyboard (Pink Keys)' 
  for n := 7 to 15

    Pref.TipTxt[n] := '    This Switch Currently Not In Use'

  end for
  move_control(Pref.Hdr,0,0)  

  set_text(Pref.Hdr,'  SIPS 2    User Preference Switches')
  Pref.Help1 := '  Drag Up or Down to Change Setting'

  Pref.Help2 := "  Click any Switch to See It's Function"

  declare PrefSub_Map[15] := { Preferences subpanel Map } ( ...

              tPrefHdr,  xxx,        xxx, ...

               uPrefSw,  xxx,        xxx, ...

                   xxx,  xxx,  uPrefABtn, ...

              tPrefBox,  xxx,  uPrefCBtn, ...

                   xxx,  xxx,        xxx)

  declare PrefPanel_Map[36] := (xxx) { Start out with a blank panel }

    AddSubPanel(PrefPanel_Map,PrefSub_Map,3,3,2)  { Add the Pref subpanel }
end function { on_init_PrefPanel }



{----------------- Support Routines for Preferences Panel ----------------}

function AcceptPrefs_  { Update UserPrefs from Dip Switches & Broadcast }

  UpdtBits(UserPrefs)

  SendOmniMsg(new_prefs,od1(UserPrefs)) { Broadcast bit-encoded Prefs }
end function { AcceptPrefs_ }



function Pref.ScanSwitches  { Change Switch setting and/or display tips }

  declare I

  

  for I := 0 to 15

    if Pref.Switch[I] # Pref.ISwitch[I] { When clicking or dragging }

      set_text(Pref.Box,Pref.TipTxt[I])

    end if

    if Pref.Switch[I] > 0       { Draging above center }

      Pref.Switch[I] := 100     { Turn on Switch }

    else { Dragging below center }

      Pref.Switch[I] := -100    { Turn off Switch }

    end if

    Pref.ISwitch[I] := Pref.Switch[I]

  end for

end function { Pref.ScanSwitches }



function Pref.ShowHelpCap   { Displays Help caption lines in Tip Box }

  set_text(Pref.Box,Pref.Help1)

  add_text_line(Pref.Box,Pref.Help2)

end function { Pref.ShowHelpCap }


function UpdtBits(pref) { Copy DIPs to 'pref' bits }

  declare n

  

  pref := 0             { Clear all bits }

  for n := 0 to 15      { Set 'pref' bits to match Dip Switches }

    pref := pref .or. sh_left(sh_right(Pref.Switch[n],7) + 1,n)

  end for

end function { UpdtBits }



function UpdtDips(pref) { Set Dip Switches to match pref bits } 

  declare n

  

  for n := 0 to 15

    Pref.Switch[n] := (sh_right(pref,n) .and. 1)*200 - 100

  end for

end function { UpdtDips }

