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 Select Group Ranges

Discussion in 'Scripting Workshop' started by StephanS90, Nov 16, 2021.

  1. StephanS90

    StephanS90 New Member

    Messages:
    10
    Hello I hope someone can help me.

    I've been trying to define the Group Ranges for a while now, but unfortunately it doesn't really work.

    I have 60 switches and the first 20 should be routed to the groups 0-19,
    the next 20 should be routed to the groups 20-39 etc.

    Then the switches must toggle between each other (1-20)(21-41)(42-62)
    and purge the group

    Unfortunately I don't know what to do.

    It would be nice to get help

    Thanks a lot

    Code:
    {\\\-------------------------------------------------------------------------------------------LAYER A SELECT BUTTONS}
    
        define NUM_SOUNDS := 20
        declare  sel_sound
        declare ui_switch $Layer1[NUM_SOUNDS]
    
        %Layer1[$count] := 0
    
        $count := 0
    
    while($count < num_elements(%Layer1))
    
        
        %Layer1[$count] -> parent_panel := get_ui_id($FundamentPanel)
        set_control_par(%Layer1[$count],$CONTROL_PAR_POS_X,$count*100+300)
        set_control_par(%Layer1[$count],$CONTROL_PAR_POS_Y,130)
        set_control_par(%Layer1[$count],$CONTROL_PAR_HEIGHT,50)
        set_control_par (%Layer1[$count],$CONTROL_PAR_WIDTH,100)
        set_control_par_str(%Layer1[$count] ,$CONTROL_PAR_TEXT,"")
        set_control_par(%Layer1[1],$CONTROL_PAR_DEFAULT_VALUE,1)
    
        %Layer1[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
        $count := 5
        
    while($count < num_elements(%Layer1))
    
        
        %Layer1[$count] -> parent_panel := get_ui_id($FundamentPanel)
        set_control_par(%Layer1[$count],$CONTROL_PAR_POS_X,$count*100-200)
        set_control_par(%Layer1[$count],$CONTROL_PAR_POS_Y,180)
    
        %Layer1[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
        $count := 10
        
    while($count < num_elements(%Layer1))
    
        
        %Layer1[$count] -> parent_panel := get_ui_id($FundamentPanel)
        set_control_par(%Layer1[$count],$CONTROL_PAR_POS_X,$count*100-700)
        set_control_par(%Layer1[$count],$CONTROL_PAR_POS_Y,230)
    
        %Layer1[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
        $count := 15
        
    while($count < num_elements(%Layer1))
    
        
        %Layer1[$count] -> parent_panel := get_ui_id($FundamentPanel)
        set_control_par(%Layer1[$count],$CONTROL_PAR_POS_X,$count*100-1200)
        set_control_par(%Layer1[$count],$CONTROL_PAR_POS_Y,280)
    
        %Layer1[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
    {\\\-------------------------------------------------------------------------------------------LAYER B SELECT BUTTONS}
    
    define NUM_SOUNDS1 := 20
    declare  sel_sound1
    
        declare ui_switch $Layer2[NUM_SOUNDS1]
    
        %Layer2[$count] := 0
    
        $count := 0
    
    while($count < num_elements(%Layer2))
    
        
        %Layer2[$count] -> parent_panel := get_ui_id($TexturePanel)
        set_control_par(%Layer2[$count],$CONTROL_PAR_POS_X,$count*100+300)
        set_control_par(%Layer2[$count],$CONTROL_PAR_POS_Y,130)
        set_control_par(%Layer2[$count],$CONTROL_PAR_HEIGHT,50)
        set_control_par (%Layer2[$count],$CONTROL_PAR_WIDTH,100)
        set_control_par_str(%Layer2[$count] ,$CONTROL_PAR_TEXT,"")
        set_control_par(%Layer2[2],$CONTROL_PAR_DEFAULT_VALUE,1)
        
    
        %Layer2[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
        $count := 5
        
    while($count < num_elements(%Layer2))
    
        
        %Layer2[$count] -> parent_panel := get_ui_id($TexturePanel)
        set_control_par(%Layer2[$count],$CONTROL_PAR_POS_X,$count*100-200)
        set_control_par(%Layer2[$count],$CONTROL_PAR_POS_Y,180)
    
        %Layer1[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
        $count := 10
        
    while($count < num_elements(%Layer2))
    
        
        %Layer2[$count] -> parent_panel := get_ui_id($TexturePanel)
        set_control_par(%Layer2[$count],$CONTROL_PAR_POS_X,$count*100-700)
        set_control_par(%Layer2[$count],$CONTROL_PAR_POS_Y,230)
    
        %Layer2[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
        $count := 15
        
    while($count < num_elements(%Layer2))
    
        
        %Layer2[$count] -> parent_panel := get_ui_id($TexturePanel)
        set_control_par(%Layer2[$count],$CONTROL_PAR_POS_X,$count*100-1200)
        set_control_par(%Layer2[$count],$CONTROL_PAR_POS_Y,280)
    
        %Layer2[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
    {\\\-------------------------------------------------------------------------------------------LAYER C SELECT BUTTONS}
    
    define NUM_SOUNDS2 := 20
    declare  sel_sound2
    
        declare ui_switch $Layer3[NUM_SOUNDS2]
    
        %Layer3[$count] := 0
    
        $count := 0
    
    while($count < num_elements(%Layer3))
    
        
        %Layer3[$count] -> parent_panel := get_ui_id($MovementPanel)
        set_control_par(%Layer3[$count],$CONTROL_PAR_POS_X,$count*100+300)
        set_control_par(%Layer3[$count],$CONTROL_PAR_POS_Y,130)
        set_control_par(%Layer3[$count],$CONTROL_PAR_HEIGHT,50)
        set_control_par (%Layer3[$count],$CONTROL_PAR_WIDTH,100)
        set_control_par_str(%Layer3[$count] ,$CONTROL_PAR_TEXT,"")
        set_control_par(%Layer3[3],$CONTROL_PAR_DEFAULT_VALUE,1)
        
    
        %Layer3[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
        $count := 5
        
    while($count < num_elements(%Layer3))
    
        
        %Layer3[$count] -> parent_panel := get_ui_id($MovementPanel)
        set_control_par(%Layer3[$count],$CONTROL_PAR_POS_X,$count*100-200)
        set_control_par(%Layer3[$count],$CONTROL_PAR_POS_Y,180)
    
        %Layer3[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
        $count := 10
        
    while($count < num_elements(%Layer3))
    
        
        %Layer3[$count] -> parent_panel := get_ui_id($MovementPanel)
        set_control_par(%Layer3[$count],$CONTROL_PAR_POS_X,$count*100-700)
        set_control_par(%Layer3[$count],$CONTROL_PAR_POS_Y,230)
    
        %Layer3[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
        $count := 15
        
    while($count < num_elements(%Layer3))
    
        
        %Layer3[$count] -> parent_panel := get_ui_id($MovementPanel)
        set_control_par(%Layer3[$count],$CONTROL_PAR_POS_X,$count*100-1200)
        set_control_par(%Layer3[$count],$CONTROL_PAR_POS_Y,280)
    
        %Layer3[$count] -> picture := "SELECTBUTTON"
        
        inc($count)
    end while
    
    
    {-------------------------------------------------------------------------------------------------------------------------------------------SELECT BUTTON FUNCTIONS}
    
    function SelButtonLayer1()
        for i := 0 to NUM_SOUNDS - 1
            if i = sel_sound
                Layer1[i] -> value := 1
            else
                Layer1[i] -> value := 0
            end if
        end for
    end function
    
    function SelButtonLayer2()
        for i := 0 to NUM_SOUNDS - 1
            if i = sel_sound1
                Layer2[i] -> value := 1
            else
                Layer2[i] -> value := 0
            end if
        end for
    end function
    
    function SelButtonLayer3()
        for i := 0 to NUM_SOUNDS - 1
            if i = sel_sound2
                Layer3[i] -> value := 1
            else
                Layer3[i] -> value := 0
            end if
        end for
    end function
    
    macro ButtonCB(#id#)
    on ui_control (Layer1#id#)
    
            for $i := 0 to NUM_SOUNDS - 1
              if (%Layer1[$i] # #id#)
                purge_group(i, 0)
              end if
            end for
    
            sel_sound := #id#
            purge_group(sel_sound, 1)
            purge_group(sel_sound1, 1)
            purge_group(sel_sound2, 1)
            call SelButtonLayer1()
    end on
    end macro
    
    iterate_macro(ButtonCB) := 0 to NUM_SOUNDS - 1
    
    macro ButtonCB1(#id#)
    on ui_control (Layer2#id#)
    
            for $i := 0 to NUM_SOUNDS1 - 1
              if (%Layer2[$i] # #id#)
                purge_group(i, 0)
              end if
            end for
    
            sel_sound1 := #id#
            purge_group(sel_sound, 1)
            purge_group(sel_sound1, 1)
            purge_group(sel_sound2, 1)
            call SelButtonLayer2()
    end on
    end macro
    
    iterate_macro(ButtonCB1) := 0 to NUM_SOUNDS1 - 1
    
    macro ButtonCB2(#id#)
    on ui_control (Layer3#id#)
    
            for $i := 0 to NUM_SOUNDS2 - 1
              if (%Layer3[$i] # #id#)
                purge_group(i, 0)
              end if
            end for
    
            sel_sound2 := #id#
            purge_group(sel_sound, 1)
            purge_group(sel_sound1, 1)
            purge_group(sel_sound2, 1)
            call SelButtonLayer3()
    end on
    end macro
    
    iterate_macro(ButtonCB2) := 0 to NUM_SOUNDS2 - 1
    
    on persistence_changed
     
    call SelButtonLayer1()
    call SelButtonLayer2()
    call SelButtonLayer3()
    
    end on
    
    on note
       disallow_group($ALL_GROUPS)
       allow_group(sel_sound)
       allow_group(sel_sound1)
       allow_group(sel_sound2)
     
    
    
    end on
     
  2. StephanS90

    StephanS90 New Member

    Messages:
    10
    is there no one here who can maybe help me?
    It would be great if someone could give me a tip please.

    Thank you very much
     
  3. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Here's how I'd do it. I see you're using SublimeKSP syntax so...

    Code:
    on init
        make_perfview
        set_ui_height_px(500)
    
        tcm.init(100)
    
        define NUM_SOUNDS := 20
        define NUM_LAYERS := 3
    
        declare pers sel_sound[NUM_LAYERS] := (0, NUM_SOUNDS, NUM_SOUNDS * 2)
        declare ui_switch SoundSelect[NUM_LAYERS, NUM_SOUNDS]
    
        declare i
    end on
    
    on persistence_changed
        for i := 0 to NUM_LAYERS - 1
            SoundSelect(i)
        end for
    end on
    
    
    taskfunc SoundSelect(layer)
        declare i
    
        for i := 0 to NUM_SOUNDS - 1
            if i = sel_sound[layer] mod NUM_SOUNDS
                SoundSelect[layer, i] -> value := 1
            else
                SoundSelect[layer, i] -> value := 0
            end if
        end for
    end taskfunc
    
    function PurgeGroups()
        declare local i
    
        for i := 0 to NUM_GROUPS - 1
            if search(sel_sound, i) # -1
                purge_group(i, 1)
            else
                purge_group(i, 0)
            end if
        end for
    end function
    
    
    macro SelBtnCB(#id#)
    on ui_control (_SoundSelect#id#)
        sel_sound[#id# / NUM_SOUNDS] := #id#
        SoundSelect(#id# / NUM_SOUNDS)
        PurgeGroups()
    end on
    end macro
    
    iterate_macro(SelBtnCB) := 0 to (NUM_SOUNDS * NUM_LAYERS) - 1
     
  4. StephanS90

    StephanS90 New Member

    Messages:
    10
    Hi EvilDragon

    first of all thx a lot for your help!

    i tried to include and understand your code but i get an error in the taskfunc:

    $fp has not been declared.

    it marks the "declare i" in the taskfunc.

    without the "declare i" the error comes:

    $sp has not been declared

    could you help me again?
    that would be really great.
    Thank you already
     
  5. StephanS90

    StephanS90 New Member

    Messages:
    10
    Hi EvilDragon,

    ok i found my mistake and get it to work.

    Thank you for the input with the TCM Modul. I have never worked with that.

    I have now a new question;

    How i can set the Layout for the Switches by its own?

    I would like to have different switches on different panels but it didnt work.

    can you tell me whats wrong?

    Thank you very much for the help
    Cheers

    Code:
        $count := 0
    
        while($count < num_elements(_SoundSelect))
                
        _SoundSelect[$count] -> parent_panel := get_ui_id($FundamentPanel)
        set_control_par(_SoundSelect[$count],$CONTROL_PAR_POS_X,$count*100+300)
        set_control_par(_SoundSelect[$count],$CONTROL_PAR_POS_Y,130)
        set_control_par(_SoundSelect[$count],$CONTROL_PAR_HEIGHT,50)
        set_control_par (_SoundSelect[$count],$CONTROL_PAR_WIDTH,100)
        set_control_par_str(_SoundSelect[$count] ,$CONTROL_PAR_TEXT,"")
        set_control_par(_SoundSelect[0],$CONTROL_PAR_DEFAULT_VALUE,1)
        _SoundSelect[$count]-> picture := "SELECTBUTTON"
    
    inc($count)
    end while
     
     $count := 20
    
        while($count < num_elements(_SoundSelect))
                
        _SoundSelect[$count] -> parent_panel := get_ui_id($TexturePanel)
        set_control_par(_SoundSelect[$count],$CONTROL_PAR_POS_X,$count*100+300)
        set_control_par(_SoundSelect[$count],$CONTROL_PAR_POS_Y,130)
        set_control_par(_SoundSelect[$count],$CONTROL_PAR_HEIGHT,50)
        set_control_par (_SoundSelect[$count],$CONTROL_PAR_WIDTH,100)
        set_control_par_str(_SoundSelect[$count] ,$CONTROL_PAR_TEXT,"")
        set_control_par(_SoundSelect[19],$CONTROL_PAR_DEFAULT_VALUE,1)
        _SoundSelect[$count]-> picture := "SELECTBUTTON"
    
    inc($count)
    end while
     
  6. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    I would suggest just using UI arrays for the three panels you need then loop through:

    Code:
    declare ui_panel Page[NUM_LAYERS]
    
    for i := 0 to num_elements(_SoundSelect) - 1
        _SoundSelect[i] -> parent_panel := Page[i / NUM_SOUNDS]
    end for
     
  7. medusa

    medusa NI Product Owner

    Messages:
    239
    Just out of interest, why a taskfunction here and not simply a normal function?
     
  8. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Shortcut to having a callable function with arguments, instead of using a macro+function workaround.
     
  9. StephanS90

    StephanS90 New Member

    Messages:
    10
    Hi EvilDragon,

    yes thats really nice. Thx. i would never have thought of making a page array :)

    Now i would like to make a switch matrix out of the buttons on the pages but it didnt work well. Do you have some other workarounds for that?
    Because the arrangement of the switches leads unfortunately. I tried out this:
    Code:
       define NUM_SOUNDS := 20
        define NUM_LAYERS := 3
        declare pers sel_sound[NUM_LAYERS] := (0, NUM_SOUNDS, NUM_SOUNDS * 2)
        declare ui_switch SoundSelect[NUM_LAYERS, NUM_SOUNDS]
    
    
    
        declare ui_panel Pages[NUM_LAYERS]
        
        Pages[NUM_LAYERS] -> pos_y := 0
        Pages[NUM_LAYERS] -> pos_x := 0
        Pages[0] -> parent_panel := get_ui_id($FundamentPanel)
        Pages[1] -> parent_panel := get_ui_id($TexturePanel)
        Pages[2] -> parent_panel := get_ui_id($MovementPanel)
    
        for i := 0 to num_elements(_SoundSelect) - 1
        _SoundSelect[i] -> parent_panel := Pages[i / NUM_SOUNDS]
        _SoundSelect[i] -> picture := "SELECTBUTTON"
    
    
         if(_SoundSelect[i] mod $BTN_COUNT_ROW = 0)
          inc($y)
          $x := 0
          end if
          _SoundSelect[i] -> pos_y := $MARGIN_TOP + ($BTN_HEIGHT + $PADDING_RIGHT) * $y
          _SoundSelect[i] -> pos_x := (880 - ($BTN_COUNT_ROW * ($BTN_WIDTH + $PADDING_RIGHT) - $PADDING_RIGHT)) / 2 + ($BTN_WIDTH + $PADDING_RIGHT) * $x
          inc($x)
        
        end for
    
     
  10. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    You should use modulo to reset the layout every $NUM_SOUNDS.
     
  11. StephanS90

    StephanS90 New Member

    Messages:
    10
    OK I will try that. Thx a lot
     
  12. StephanS90

    StephanS90 New Member

    Messages:
    10
    Hi EvilDragon, a have a problem with setup the switch layout. I have tried different workarounds with the modulo but i cant get it to work.

    Can you give me just an example what you mean with reset the layout every $NUM_SOUNDS?
    Do you mean a $count While loop?

    Thank you very much
     
  13. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    For example if you want to put your buttons in a matrix with a certain number of columns, you would do:

    Code:
    for i := 0 to num_elements(Button) - 1
        Button[i] -> x := (i mod NUM_COLS) * X_SPACING
        Button[i] -> y := (i / NUM_COLS) * Y_SPACING
    end for[/quote]
    In your case you would need to reset i every 20 iterations with modulo, so that your 3 pages look the same. So instead of (i mod NUM_COLS) or (i / NUM_COLS), you do ((i mod NUM_SOUNDS) mod NUM_COLS) etc.
     
  14. StephanS90

    StephanS90 New Member

    Messages:
    10
    Hi EvilDragon,

    this works like a charm thank you very much :)

    Now i am trying to create a text array and on a button press the text of the button shows up in a label. The whole thing as a function via the SelBtnCB macro but it didnt work out yet. Can you tell me whats wrong?

    Code:
    declare !layer_names[60]
    !layer_names[0] := etc,etc,etc
    declare ui_label LayerA_Info (1,1)
    set_text(LayerA_Info,"Layer A")
    
    macro SelBtnCB(#id#)
    on ui_control (_SoundSelect#id#)
        sel_sound[#id# / NUM_SOUNDS] := #id#
        SoundSelect(#id# / NUM_SOUNDS)
        PurgeGroups()
        layerA_select
    
    
    end on
    end macro
    
    iterate_macro(SelBtnCB) := 0 to (NUM_SOUNDS * NUM_LAYERS) - 1
    
    function layerA_select
    
    $count := 0
    while ($count < 20)
    
      if _SoundSelect[$count] # _SoundSelect[$count]
     
      set_text ($LayerA_Info, !layer_names[$count])
      _SoundSelect[$count] := _SoundSelect[$count]
    
      end if
      inc($count)
    end while
    
    end function
     
  15. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    There's no need to iterate through all the buttons. You're changing sel_sound when you press on a button, right? So just use that as a pointer in a single line of code, out of loop:

    set_text($LayerA_Info, layer_names[sel_sound[0]]

    This doesn't even need to be in a function, you can just have it in your macro, that one line. Then just use #id / NUM_SOUNDS instead of 0, and you should probably also use UI array for the layer info, so that you can easily target it with that macro in the same way, regardless of whicih layer.
     
  16. StephanS90

    StephanS90 New Member

    Messages:
    10
    Hi EvilDragon,

    i have set up the my text array in an UI Array but
    i get an syntax error with this line in the macro:

    Code:
    set_text($LayerA_Info, layer_names[sel_sound[#id# / NUM_SOUNDS]]
     
  17. StephanS90

    StephanS90 New Member

    Messages:
    10
    Ok i get it to work :)
    Thank you very much for your help EvilDragon.

    Here the code for everyone:
    Code:
    for i := STARTGRP to STARTGRP1 - 1
      if i = sel_sound[#id# / NUM_SOUNDS]
        set_control_par_str(get_ui_id($LayerA_Info),$CONTROL_PAR_TEXT,layer_names[sel_sound[#id# / NUM_SOUNDS]])
      end if
    end for
    
    for i := STARTGRP1 to STARTGRP2 - 1
      if i = sel_sound[#id# / NUM_SOUNDS]
        set_control_par_str(get_ui_id($LayerB_Info),$CONTROL_PAR_TEXT,layer_names[sel_sound[#id# / NUM_SOUNDS]])
      end if
    end for 
        
    for i := STARTGRP2 to STARTGRP3 - 1
      if i = sel_sound[#id# / NUM_SOUNDS]
        set_control_par_str(get_ui_id($LayerC_Info),$CONTROL_PAR_TEXT,layer_names[sel_sound[#id# / NUM_SOUNDS]])
      end if
    end for 
     
  18. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    I forgot a closing parentheses there :)

    It will work, you don't really need to all those loops...


    Code:
    macro SelBtnCB(#id#)
    on ui_control (_SoundSelect#id#)
       sel_sound[#id# / NUM_SOUNDS] := #id#
       LayerInfo[#id# / NUM_SOUNDS] := layer_names[sel_sound[#id# / NUM_SOUNDS]]
       SoundSelect(#id# / NUM_SOUNDS)
       PurgeGroups()
    end on
    end macro