# Help with group menu [SOLVED]



## Claud9 (Apr 4, 2018)

Hi everyone,
I have an instrument (30 groups) with 3 layers, to each layer I have assigned 10 groups. I have scripted a menu for each layer where I can select which group to play in that specific layer. I would like also to add to each layer the possibility to navigate groups with a "+" and "-" buttons instead of choosing the sound with the menu. The idea is when I hit the "+" button I load the next group and vice versa for the "-" button. Is something possible? Any idea how to do it? Thanks in advance for any help!


----------



## d.healey (Apr 4, 2018)

Yes it's possible. Make two buttons that increment/decrement a counter for each of your sets of groups.


----------



## P.N. (Apr 4, 2018)

You'll still have the menu, right?

If so, you need to get the current selection from the menu in order to increase the index based on that selection. 

Also, special rules need to be applied so that it will properly rotate once it passes your max/minimum number of groups.

If you need help with this, i'll come back with an example.

Cheers.


----------



## Claud9 (Apr 4, 2018)

d.healey said:


> Yes it's possible. Make two buttons that increment/decrement a counter for each of your sets of groups.


Hi, David thanks, I tried this but is not working, is there an example on KSP manual? What I have to look for? 

```
on ui_control ($plus1)
   $i := $LAYER1_FIRST
   while ($i<=$LAYER1_LAST)
   purge_group($i,0)
   inc($i)
   end while
   purge_group($layer1_menu,1)
end on
on ui_control ($minus1)
   $i := $LAYER1_FIRST
   while ($i<=$LAYER1_LAST)
   purge_group($i,0)
   dec($i)
   end while
   purge_group($layer1_menu,1)
end on
```


----------



## Claud9 (Apr 4, 2018)

P.N. said:


> You'll still have the menu, right?
> 
> If so, you need to get the current selection from the menu in order to increase the index based on that selection.
> 
> ...


Thanks a lot an example is what I'm looking for!


----------



## d.healey (Apr 4, 2018)

There's no example that I know of. Get your menu working first, then all you need to do with the buttons is change the value of the menu and update the group purge state.


----------



## P.N. (Apr 4, 2018)

Claud9, remember the script i showed you for group names in a menu, etc?

You could just build up on that...

But if i have time, i'll come back later with a working script.
It's really not too hard... Try to build up on the other script and you'll get there.

All you need to worry about is:
a)Getting the proper index from the menu
b)Updating the menu after a selection is made with your plus/minus signs
c)Be sure the rotation works with "if" statements
d)Have your persistent variables in place.

If you still need help, i'll come back.

Cheers.


----------



## Claud9 (Apr 4, 2018)

P.N. said:


> Claud9, remember the script i showed you for group names in a menu, etc?
> 
> You could just build up on that...
> 
> ...


Hi Thanks, I tried and so far I have this code but it looks like I'm missing some parts because while the menu works fine the "plus" button is not working at all... 

```
on init
    declare $i
    declare $item_idx
    declare @item_txt
 
    declare ui_label $label (1,1)
    declare ui_menu $menu
    declare ui_button $plus

   declare const $LAYER1_FIRST := 0
   declare const $LAYER1_LAST := 3
  
    $i := $LAYER1_FIRST
   while ($i<=$LAYER1_LAST)
     add_menu_item($menu,group_name($i),$i)
     inc($i)
   end while

    $item_idx:=get_control_par(get_ui_id($menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
    @item_txt:=get_menu_item_str(get_ui_id($menu),$item_idx)
    set_text($label,@item_txt)
end on

    on ui_control($menu)
    $i := $LAYER1_FIRST
    while ($i<=$LAYER1_LAST)
    purge_group($i,0)
    inc($i)
    end while
    purge_group($menu,1)
    $item_idx:=get_control_par(get_ui_id($menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
    @item_txt:=get_menu_item_str(get_ui_id($menu),$item_idx)
    set_text($label,@item_txt)
end on

    on ui_control($plus)
    
    $i := $LAYER1_FIRST
    while ($i<=$LAYER1_LAST)
    purge_group($i,0)
    inc($i)
    end while
    purge_group($menu,1)
    $item_idx:=get_control_par(get_ui_id($menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
    @item_txt:=get_menu_item_str(get_ui_id($menu),$item_idx)
    set_text($label,@item_txt)
end on
```


----------



## P.N. (Apr 4, 2018)

```
on init
    make_perfview
   
    declare $count
    declare $item_idx
    declare @item_txt
   
    declare ui_label $label (1,1)
    declare ui_menu $menu
    declare ui_button $minus
    declare ui_button $plus
   
    set_control_par_str(get_ui_id($minus),$CONTROL_PAR_TEXT,"-")
    set_control_par_str(get_ui_id($plus),$CONTROL_PAR_TEXT,"+")

    $count:=0
    while($count<=$NUM_GROUPS-1)
        add_menu_item($menu,group_name($count),$count)
        inc($count)
    end while

end on

function menu_upd
    $item_idx:=get_control_par(get_ui_id($menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
    @item_txt:=get_menu_item_str(get_ui_id($menu),$item_idx)
    set_text($label,@item_txt)
end function

on persistence_changed
    call menu_upd
end on

on ui_control($menu)
    purge_group($item_idx,0)
    call menu_upd
    purge_group($item_idx,1)
end on

on ui_control($minus)
   
    purge_group($item_idx,0)
    $item_idx:=get_control_par(get_ui_id($menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
   
    if($item_idx-1<0)
        $item_idx:=get_control_par(get_ui_id($menu),$CONTROL_PAR_NUM_ITEMS)
    end if
   
    $item_idx:=$item_idx-1
   
    set_control_par(get_ui_id($menu),$CONTROL_PAR_VALUE,$item_idx)
   
    call menu_upd
   
    purge_group($item_idx,1)
    $minus:=0
end on

on ui_control($plus)
   
    purge_group($item_idx,0)
    $item_idx:=get_control_par(get_ui_id($menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
   
    if($item_idx+1>get_control_par(get_ui_id($menu),$CONTROL_PAR_NUM_ITEMS)-1)
        $item_idx:=-1  
    end if
   
    $item_idx:=$item_idx+1
   
    set_control_par(get_ui_id($menu),$CONTROL_PAR_VALUE,$item_idx)
   
    call menu_upd
   
    purge_group($item_idx,1)
    $plus:=0
end on
```


----------



## Claud9 (Apr 4, 2018)

P.N. said:


> ```
> on init
> make_perfview
> 
> ...


Thanks a lot your code works great! I'm trying to insert the code into my instrument scenario but while in "Layer 1" both buttons works great It looks like in "Layer 2" the minus button is not working properly when I click it jumps to the last group "LAYER2_LAST". "Plus" button is working instead. Do you have any suggestion why?

```
{LAYER 1}
  
   on ui_control($minus1)
  
   $i := $LAYER1_FIRST
   while ($i<=$LAYER1_LAST)
   purge_group($i,0)
   inc($i)
   end while
  
    $item_idx:=get_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
  
    if($item_idx-1<$LAYER1_FIRST)
    $item_idx:=get_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_NUM_ITEMS)
    end if
  
    $item_idx:=$item_idx-1
  
    set_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_VALUE,$item_idx)
  
    call menu_upd
  
    purge_group($layer1_menu,1)
    $minus1:=0
    end on

    on ui_control($plus1)
  
    $i := $LAYER1_FIRST
   while ($i<=$LAYER1_LAST)
   purge_group($i,0)
   inc($i)
   end while
  
    $item_idx:=get_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
  
    if($item_idx+1>get_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_NUM_ITEMS)-1)
        $item_idx:=-1 
    end if
  
    $item_idx:=$item_idx+1
  
    set_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_VALUE,$item_idx)
  
    call menu_upd
  
    purge_group($layer1_menu,1)
    $plus1:=0
    
    end on
    
    {LAYER 2}
    
    on ui_control($minus2)
  
   $i := $LAYER2_FIRST
   while ($i<=$LAYER2_LAST)
   purge_group($i,0)
   inc($i)
   end while
  
    $item_idx:=get_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
  
    if($item_idx-1<$LAYER2_FIRST)
    $item_idx:=get_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_NUM_ITEMS)
    end if
  
    $item_idx:=$item_idx-1
  
    set_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_VALUE,$item_idx)
  
    call menu_upd
  
    purge_group($layer2_menu,1)
    $minus2:=0
    end on

    on ui_control($plus2)
  
    $i := $LAYER2_FIRST
   while ($i<=$LAYER2_LAST)
   purge_group($i,0)
   inc($i)
   end while
  
    $item_idx:=get_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
  
    if($item_idx+1>get_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_NUM_ITEMS)-1)
        $item_idx:=-1 
    end if
  
    $item_idx:=$item_idx+1
  
    set_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_VALUE,$item_idx)
  
    call menu_upd
  
    purge_group($layer2_menu,1)
    $plus2:=0
    
    end on
```


----------



## P.N. (Apr 4, 2018)

The code i posted was just an example. If you have "layers", as you call them, you need to take that into account when incorporating my code. You'll need to offset the code accordingly, that's probably why only the first "layer" works.

Do you want to cycle through all the groups? Just some groups? I don't really know what the "layers" represent.
But maybe trying to add the mentioned offset to each "layer" could do it, i don't know. Something, somewhere plus 10 or minus 10 (each layer has 10 groups, right?)

To be quite honest, i apologize but i didn't really understand your instrument structure, i just sent you something that would work as general purpose... Without seeing the whole code it's difficult.

I hope you can sort it out.

Cheers.


----------



## Claud9 (Apr 4, 2018)

P.N. said:


> The code i posted was just an example. If you have "layers", as you call them, you need to take that into account when incorporating my code. You'll need to offset the code accordingly, that's probably why only the first "layer" works.
> 
> Do you want to cycle through all the groups? Just some groups? I don't really know what the "layers" represent.
> But maybe trying to add the mentioned offset to each "layer" could do it, i don't know. Something, somewhere plus 10 or minus 10 (each layer has 10 groups, right?)
> ...


Hello, I made a simple example instrument with 2 layers and 4 groups. This reflects exactly what I have in my instrument and maybe you can figure out what is the problem. As you can see all is working fine except for the "minus" button in Layer 2. Thanks in advance for any help!


```
on init
    make_perfview
  
    declare $i
    declare $item_idx
    declare @item_txt
  
    declare ui_label $label1 (1,1)
    declare ui_label $label2 (1,1)
    declare ui_menu $layer1_menu
    declare ui_menu $layer2_menu
    declare ui_button $minus1
    declare ui_button $plus1
    declare ui_button $minus2
    declare ui_button $plus2
  
   declare const $LAYER1_FIRST := 0
   declare const $LAYER1_LAST := 1
   declare const $LAYER2_FIRST := 2
   declare const $LAYER2_LAST := 3
    

    $i := $LAYER1_FIRST
   while ($i<=$LAYER1_LAST)
     add_menu_item($layer1_menu,group_name($i),$i)
     inc($i)
   end while

    

   $i:= $LAYER2_FIRST
   while ($i<=$LAYER2_LAST)
     add_menu_item($layer2_menu,group_name($i),$i)
     inc($i)
   end while

end on

    function menu_upd
    
    $item_idx:=get_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
    @item_txt:=get_menu_item_str(get_ui_id($layer1_menu),$item_idx)
    set_text($label1,@item_txt)
    
    $item_idx:=get_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
    @item_txt:=get_menu_item_str(get_ui_id($layer2_menu),$item_idx)
    set_text($label2,@item_txt)
end function

on persistence_changed
    call menu_upd
end on

{LAYER 1}

   on ui_control ($layer1_menu)
   $i := $LAYER1_FIRST
   while ($i<=$LAYER1_LAST)
   purge_group($i,0)
   inc($i)
   call menu_upd
   end while
   purge_group($layer1_menu,1)
    
   end on

   {LAYER 2}

   on ui_control ($layer2_menu)
   $i := $LAYER2_FIRST
   while ($i<=$LAYER2_LAST)
   purge_group($i,0)
   inc($i)
   call menu_upd
   end while
   purge_group($layer2_menu,1)
  
      
   end on
 on ui_control($minus1)
 
   $i := $LAYER1_FIRST
   while ($i<=$LAYER1_LAST)
   purge_group($i,0)
   inc($i)
   end while
 
    $item_idx:=get_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
 
    if($item_idx-1<$LAYER1_FIRST)
    $item_idx:=get_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_NUM_ITEMS)
    end if
 
    $item_idx:=$item_idx-1
 
    set_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_VALUE,$item_idx)
 
    call menu_upd
 
    purge_group($layer1_menu,1)
    $minus1:=0
    end on

    on ui_control($plus1)
 
    $i := $LAYER1_FIRST
   while ($i<=$LAYER1_LAST)
   purge_group($i,0)
   inc($i)
   end while
 
    $item_idx:=get_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
 
    if($item_idx+1>get_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_NUM_ITEMS)-1)
        $item_idx:=-1
    end if
 
    $item_idx:=$item_idx+1
 
    set_control_par(get_ui_id($layer1_menu),$CONTROL_PAR_VALUE,$item_idx)
 
    call menu_upd
 
    purge_group($layer1_menu,1)
    $plus1:=0
  
    end on
  
    {LAYER 2}
  
    on ui_control($minus2)
 
   $i := $LAYER2_FIRST
   while ($i<=$LAYER2_LAST)
   purge_group($i,0)
   inc($i)
   end while
 
    $item_idx:=get_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
 
    if($item_idx-1<$LAYER2_FIRST)
    $item_idx:=get_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_NUM_ITEMS)
    end if
 
    $item_idx:=$item_idx-1
 
    set_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_VALUE,$item_idx)
 
    call menu_upd
 
    purge_group($layer2_menu,1)
    $minus2:=0
    end on

    on ui_control($plus2)
 
    $i := $LAYER2_FIRST
   while ($i<=$LAYER2_LAST)
   purge_group($i,0)
   inc($i)
   end while
 
    $item_idx:=get_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_SELECTED_ITEM_IDX)
 
    if($item_idx+1>get_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_NUM_ITEMS)-1)
        $item_idx:=-1
    end if
 
    $item_idx:=$item_idx+1
 
    set_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_VALUE,$item_idx)
 
    call menu_upd
 
    purge_group($layer2_menu,1)
    $plus2:=0
  
    end on
```


----------



## P.N. (Apr 5, 2018)

Hi.
Listen, before anything else, the code i posted would need to be seriously edited for a proper, easy to understand implementation, and i advise you to do so.

That being said, let's put a quick band-aid on it.

Your "on ui_control" for both the minus buttons are not correct.

Let's adress this. The original code states that if the current selection is smaller than 0, the current selection should be the total number of menu elements, effectively cycling counter clockwise to the last item of the menu.

There's a little offset trick at play to make sure you don't get a "non-existing group" in the list, but let's ignore that.

So, your on_ui_control for both minus controls should be:

```
if($item_idx-1<0)
```

The fact that it works for your "$minus1" is simply because you wrote:

```
if($item_idx-1<$LAYER1_FIRST)
```

and your $LAYER1_FIRST constant happens to be equal to 0.

Your "on_ui_control" for $minus2 reads as:


```
if($item_idx-1<$LAYER2_FIRST)
```

- $LAYER2_FIRST is equal to 2.

so the menu would always cycle to the end of the menu as seen here:


```
if($item_idx-1<$LAYER2_FIRST)
    $item_idx:=get_control_par(get_ui_id($layer2_menu),$CONTROL_PAR_NUM_ITEMS)
end if
 
$item_idx:=$item_idx-1
```

So, to summarize:
On your minus controls use:


```
if($item_idx-1<0)
```

not


```
if($item_idx-1<$ANY_LAYER)
```

Cheers.

PS: Please take the time to better organize the code as it will prove helpful later on.
There are things that can be simplified.


----------



## Claud9 (Apr 5, 2018)

P.N. said:


> Hi.
> Listen, before anything else, the code i posted would need to be seriously edited for a proper, easy to understand implementation, and i advise you to do so.
> 
> That being said, let's put a quick band-aid on it.
> ...


Thanks a lot now it works great. I will try to simplify it as you suggested.


----------

