1. IMPORTANT:
    We launched a new online community and this space is now closed. This community will be available as a read-only resources until further notice.
    JOIN US HERE

Solved Buttons / Switches Load & Purge

Discussion in 'Scripting Workshop' started by foundation77, Feb 18, 2020.

  1. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    Code:
    declare ui_menu $sample1
    declare $S1_i
    declare const $sample1first := 25
    declare const $sample1last := 50
    $S1_i := 25
    while ($S1_i < $sample1first+$sample1last)
    add_menu_item($sample1, "Sample " & $S1_i + 1, $S1_i)
    inc($S1_i)
    end while
    make_persistent($sample1)
    As above I can easily create a menu that allows me to select samples in different groups.
    I can also load a group using a button / switch

    But is there an easier way to script this in an array of buttons that could load up to 50 samples (each individually) with only one playing at a time.
    assuming that there are at least two layers (simultaneous groups playing)
    Code:
    on init
    
      make_perfview
    
      declare $i
    
      {layer01}
      declare const $layer_01_first := 0
      declare const $layer_01_count := 25
    
      declare ui_switch $sample_01_layer_01
      declare ui_switch $sample_02_layer_01
    
      {layer02}
      declare const $layer_02_first := 26
      declare const $layer_02_count := 24
    
      declare ui_switch $sample_01_layer_01
      declare ui_switch $sample_02_layer_01
    
    end on
    
    on ui_control ($sample_01_layer_01)
      disallow_group($ALL_GROUPS)
      allow_group($sample_01_layer_01)
    end on
    
    on ui_control ($sample_02_layer_01)
      disallow_group($ALL_GROUPS)
      allow_group($sample_02_layer_01)
    end on
    https://i.ibb.co/tH63ChJ/arrayofbuttons.png

    In short we're taking two menus and turning that into two sets of buttons / switches and allowing one to be loaded and the others to be purged
    I hope this makes sense... I'll elaborate if it's not understood.
    I'm just looking to see if there's a shorter way of doing this rather than purging groups on every single buttons 'off state'
    :)
     
  2. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    Code:
    on init
        declare $i
    
        declare const $group1_1st := 1
        declare $group1_last := 50
    
        declare ui_switch $s1
        declare ui_switch $s2
        declare ui_switch $s3
    
    end on
        on note
        disallow_group($ALL_GROUPS)
            $i := 0
            if ($i < $group1_1st+$group1_last)
                if($s1 = 1)
                disallow_group($group1_1st+$group1_last)
                allow_group(0)
            end if
            end if
            if ($i < $group1_1st+$group1_last)
                if($s2 = 1)
                disallow_group($group1_1st+$group1_last)
                allow_group(1)
            end if
            end if
            if ($i < $group1_1st+$group1_last)
                if($s3 = 1)
                disallow_group($group1_1st+$group1_last)
                allow_group(2)
            end if
            end if
            inc($i)
        
    
    end on   
    
    on ui_control ($s1)
        $s2 := 0
        $s3 := 0
    end on
    
    on ui_control ($s2)
        $s1 := 0
        $s3 := 0
    end on
    
    on ui_control ($s3)
        $s1 := 0
        $s2 := 0
    end on
    Possibly solved, would you agree Mario?
     
  3. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    You'd really want to use a function here. I hope you use SublimeKSP because it's much faster to write this stuff there:

    Code:
    on init
        define NUM_SOUNDS := 16
    
        declare i
        declare pers sel_sound
    
        declare ui_switch Sound[NUM_SOUNDS]
    end on
    
    function SelButton()
        for i := 0 to NUM_SOUNDS - 1
            if i = sel_sound
                Sound[i] -> value := 1
            else
                Sound[i] -> value := 0
            end if
        end for
    end function
    
    macro ButtonCB(#id#)
    on ui_control (Sound#id#)
            for i := 0 to NUM_SOUNDS - 1
                purge_group(i, 0)
            end for
    
            sel_sound := #id#
            purge_group(sel_sound, 1)
            call SelButton()
    end on
    end macro
    
    iterate_macro(ButtonCB) := 0 to NUM_SOUNDS - 1
     
  4. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    I've gotten this far - the only issue I'm facing now is that some waveforms don't display and some do.
    Am I missing something here?
    And yes I use Sublime with the KSP package

    Code:
    on init
        make_perfview
        set_ui_width_px(1000)
    
        declare $i
        
        declare const $num_wt_switches := 25
        
        declare $old_wt_btn
        declare $sel_wt_btn
    
        declare %ID[$num_wt_switches]
    
        declare ui_switch $s1
        declare ui_switch $s2
        declare ui_switch $s3
        declare ui_switch $s4
        declare ui_switch $s5
        declare ui_switch $s6
        declare ui_switch $s7
        declare ui_switch $s8
        declare ui_switch $s9
        declare ui_switch $s10
        declare ui_switch $s11
        declare ui_switch $s12
        declare ui_switch $s13
        declare ui_switch $s14
        declare ui_switch $s15
        declare ui_switch $s16
        declare ui_switch $s17
        declare ui_switch $s18
        declare ui_switch $s19
        declare ui_switch $s20
        declare ui_switch $s21
        declare ui_switch $s22
        declare ui_switch $s23
        declare ui_switch $s24
        declare ui_switch $s25
    
        while ($i < $num_wt_switches)
            %ID[$i] := get_ui_id($s1) + $i
            inc($i)
        end while
        make_persistent($sel_wt_btn)
    
    declare ui_wavetable $wavetable
        set_control_par(get_ui_id($wavetable), $CONTROL_PAR_WIDTH, 200)
        set_control_par(get_ui_id($wavetable), $CONTROL_PAR_HEIGHT, 40)
        set_control_par(get_ui_id($wavetable), $CONTROL_PAR_WAVE_COLOR, 0FFFFFFH)
        set_control_par(get_ui_id($wavetable), $CONTROL_PAR_WAVE_END_COLOR, 0FFFFFFH)
        move_control_px($wavetable, 250, 90)
    
    declare ui_slider $wt_pos (0, 1000)
        make_persistent($wt_pos)
    
    
    end on
    
    function load_wt()
        set_control_par(get_ui_id($wavetable), $CONTROL_PAR_WT_ZONE, $sel_wt_btn)   
    end function
    
    function wt_switch_cb()
        set_control_par(%ID[$old_wt_btn], $CONTROL_PAR_VALUE, 0)
        set_control_par(%ID[$sel_wt_btn], $CONTROL_PAR_VALUE, 1)
        $old_wt_btn := $sel_wt_btn
    end function
    
    on ui_control ($s1)
        $sel_wt_btn := 0
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s2)
        $sel_wt_btn := 1
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s3)
        $sel_wt_btn := 2
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s4)
        $sel_wt_btn := 3
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s5)
        $sel_wt_btn := 4
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s6)
        $sel_wt_btn := 5
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s7)
        $sel_wt_btn := 6
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s8)
        $sel_wt_btn := 7
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s9)
        $sel_wt_btn := 8
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s10)
        $sel_wt_btn := 9
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s11)
        $sel_wt_btn := 10
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s12)
        $sel_wt_btn := 11
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s13)
        $sel_wt_btn := 12
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s14)
        $sel_wt_btn := 13
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s15)
        $sel_wt_btn := 14
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s16)
        $sel_wt_btn := 15
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s17)
        $sel_wt_btn := 16
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s18)
        $sel_wt_btn := 17
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s19)
        $sel_wt_btn := 18
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s20)
        $sel_wt_btn := 19
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s21)
        $sel_wt_btn := 20
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s22)
        $sel_wt_btn := 21
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s23)
        $sel_wt_btn := 22
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s24)
        $sel_wt_btn := 23
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($s25)
        $sel_wt_btn := 24
        call wt_switch_cb()
        call load_wt()
    end on
    
    on ui_control ($wt_pos)
        $i := 0
        while ($i < $num_wt_switches)
           set_engine_par($ENGINE_PAR_WT_POSITION, $wt_pos * 1000, $i, -1, -1)
           inc($i)
       end while
    end on
    
    on persistence_changed
        call wt_switch_cb()
        call load_wt()
    end on
    
    on note
        disallow_group($ALL_GROUPS)
        allow_group($sel_wt_btn)
    end on    
     
  5. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Are you sure that zone IDs match to what you're trying to show? Check Expert->Zones tab.
     
  6. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    Sure did, I was doing it slightly differently I was using a function that wouldn't work on callback properly - it worked on it's own but not with the rest of the script.
    Code:
    function LoadWT()
       set_control_par(get_ui_id($WT), $CONTROL_PAR_WT_ZONE, $menu)
    end function
    What I ended up doing was writing it out from scratch (thank god for sublime)

    Code:
    on ui_control($wt0)
      $i := 0
      while ($i<16)
        purge_group($i,0)
        inc($i)
      end while
      $wt_group := 0
      set_control_par(get_ui_id($wavetable), $CONTROL_PAR_WT_ZONE, 0)
      purge_group($wt_group,1)
      call wt_selected
     
    end on
    Now it all loads correctly, I just have to expand on it now to reach 50 wavetables and adjust the zones as when I've added the 'samples'

    Thanks for the input on the script, your version purges which is always better, not that wavetables use many resources but always good practice :D
     
  7. foundation77

    foundation77 NI Product Owner

    Messages:
    147
    FYI - I had loaded (to test) and deleted some zones as I was cycling through the zones rather than the above
    Code:
    function load_wt()
        set_control_par(get_ui_id($wavetable), $CONTROL_PAR_WT_ZONE, $sel_wt_btn)  
    end function
    As we know once you do this you have to start all over again.
    Not a big problem. I shall mark as solved :)