Help with buttons and switches needed

leon59
leon59 Member Posts: 32 Member

I am trying to solve a problem and I am stuck. I have a button matrix (only 2 here) which selects groups and also updates the group menu. I have plus and minus switches which also select the groups however I need the plus and minus buttons to update the matrix. I've been scratching my head on this one as nothing I try seems to work. I would be most grateful if somebody could point me in the right direction. Thank you

on init
 make_perfview
 set_ui_height_px(200)

 declare $i
 declare const $LAYER1_FIRST := 0
 declare const $LAYER1_LAST := 1
 declare ui_menu $menu1 
 declare ui_switch $minus
 declare ui_switch $plus
 declare ui_button $btn_0
 declare ui_button $btn_1
 declare const $BTN_COUNT := 2
 declare %btn_id[$BTN_COUNT]
 %btn_id[0] := get_ui_id($btn_0)
 %btn_id[1] := get_ui_id($btn_1)
 move_control_px($menu1,85,100)
 move_control_px($minus,185,100)
 move_control_px($plus,275,100)
 move_control_px($btn_0,85, 50)
 move_control_px($btn_1,185, 50)

 $i := $LAYER1_FIRST
 while ($i<=$LAYER1_LAST)
  add_menu_item($menu1,group_name($i),$i)
  inc($i)
 end while
 $i := 0
 while ($i<$NUM_GROUPS)
  if (in_range($i,0,$NUM_GROUPS/1-1-2))
   add_menu_item($menu1,group_name($i),$i)
  end if  
  inc($i)
 end while 
 end on

function deactivate_all_btn
   $i := 0
  while($i<num_elements(%btn_id))
  set_control_par(%btn_id[$i],$CONTROL_PAR_VALUE, 0)
  inc($i)
  end while
 end function

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

on ui_control($minus)
 $menu1 := (get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)+$menu1-1) mod get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)
 $minus := 0  
 $i := $LAYER1_FIRST
 while ($i<=$LAYER1_LAST)
  purge_group($i,0)
  inc($i)
 end while
 purge_group($menu1,1)
end on
  
on ui_control($plus)
 $menu1 := ($menu1+1) mod get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)
 $plus := 0
 $i := $LAYER1_FIRST
 while ($i<=$LAYER1_LAST)
  purge_group($i,0)
  inc($i)
 end while
 purge_group($menu1,1)
end on

on ui_control($btn_0)
call deactivate_all_btn
$btn_0 := 1
$menu1 := get_menu_item_value(get_ui_id($menu1),0)
$i := $LAYER1_FIRST
 while ($i<=$LAYER1_LAST)
  purge_group($i,0)
  inc($i)
 end while
 purge_group($menu1,1) 
end on

on ui_control($btn_1)
call deactivate_all_btn
$btn_1 := 1
$menu1 := get_menu_item_value(get_ui_id($menu1),1)
 $i := $LAYER1_FIRST
 while ($i<=$LAYER1_LAST)
  purge_group($i,0)
  inc($i)
 end while 
 purge_group($menu1,1)
end on


Comments

  • corbo-billy
    corbo-billy Member Posts: 83 Helper

    Is it your soluce ?



    on ui_control($minus)
    $btn_0 := 1
    $btn_1 := 0
     $menu1 := (get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)+$menu1-1) mod get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)
     $minus := 0  
     $i := $LAYER1_FIRST
     while ($i<=$LAYER1_LAST)
      purge_group($i,0)
      inc($i)
     end while
     purge_group($menu1,1)
    end on
     
    on ui_control($plus)
    $btn_0 := 0
    $btn_1 := 1
     $menu1 := ($menu1+1) mod get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)
     $plus := 0
     $i := $LAYER1_FIRST
     while ($i<=$LAYER1_LAST)
      purge_group($i,0)
      inc($i)
     end while
     purge_group($menu1,1)
    end on
    
  • leon59
    leon59 Member Posts: 32 Member

    Thank you for the quick response, yes that approach works but i have 96 btns and there would be a lot of lines of code. I have been trying to work out a formula along the lines of :-

    $menu1 := (get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)+$menu1-1) mod get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)
    

    and:-

    $menu1 := ($menu1+1) mod get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)
    

    by using the array method used in the function to deactivate the btns :-

      $i := 0
      while($i<num_elements(%btn_id))
      set_control_par(%btn_id[$i],$CONTROL_PAR_VALUE, 0)
      inc($i)
    

    But I can't get a grip on it :(

  • leon59
    leon59 Member Posts: 32 Member

    Not sure if it's even worth posting on here anymore as it would appear newcomers are overlooked by those in the know? Anyway I already had my problem solved with a long winded solution of if statements x96 to address the group menu and %btn array[96](see below) but instead of repeating the code 96 times in a function and then calling it in the plus,minus switches, I was hoping a senior member could hold out an olive branch or throw a dog a bone and show how to do the same in one line of code to a brain dead newcomer + 60 years of age who has been struggling with it for a couple of days!... lol

    Thanks anyway

    if($menu1=0)
     set_control_par(%btn_id[0],$CONTROL_PAR_VALUE, 1) 
     end if
    
  • darb1
    darb1 Member Posts: 22 Member

    Is the button matrix a sublime ui control array?

  • leon59
    leon59 Member Posts: 32 Member

    Hi darb1 , I'm not sure what you mean but maybe? it's just an %(interger array) for the btn_ids. ie:-

    declare %btn_id[96] :=(0,1,2,3...etc)
    declare %btnB_id[96]:=(0,1,2,3...etc)
    
    

    Then I have the plus minus buttons and menus calling back 6 separate functions as I have 2 menus and 2 sets for plus minus btns. I have it sorted but it's very long winded:-

    function grptobtnMenu1
    if($menu1=0)
     set_control_par(%btn_id[0],$CONTROL_PAR_VALUE, 1) 
     end if
     if($menu1=1)
     set_control_par(%btn_id[1],$CONTROL_PAR_VALUE, 1) 
     end if  
     if($menu1=2)
    etc....menu1=95
    end function
    

    Thank you for asking

  • noY_T
    noY_T Member Posts: 37 Member
    edited August 2023

    Sublime is a text editor, via a plugin you can compile KSP code in there. Sounds complicated but it is not and absolutely worth it 100000000% (in sublime you can search for keywords, the font size isnt 2 etc it just make your life so much easier).

    Anyways, in your example it seems that your menu goes up by 1 for each entry, and the menu entry values correspond to the same button id number in the btn_id array. I guess you also want to set the button selected to 1 but all others to 0?

    $i := 0
     while ($i<95)
       set_control_par(%btn_id[$i],$CONTROL_PAR_VALUE,0)
       inc($i)
     end while
     set_control_par(%btn_id[$menu1],$CONTROL_PAR_VALUE,1)
    

    The <95 is obviously dependent on how many controls you have, I think I misread and it should be 96

  • leon59
    leon59 Member Posts: 32 Member

    Hi noY_T!

    I can't thank you enough!, I've learned a lot from this with regards to:-

    %btn_id[$menu1]
    

    I do have sublime, my main problem is I overthink things and the penny doesn't drop like it used to. You've just saved me close to 1k lines of code!...lol

  • noY_T
    noY_T Member Posts: 37 Member

    This is absolutely normal. You just gotta keep going at it, then more and more things will become easier over time.

    Another tip:

    In sublime you can create ui variable arrays. This is basically just a compiler trick where x number of a control with the same name are created, and their ui id are AUTOMATICALLY stored in an array. So no need to manually create an array like btn_id in most cases.

    on init 
       declare ui_button myButton[3]
    end on
    

    This compiles to:

    on init
     // preprocessor stuff ... 
     // What the ui array looks like
     declare %myButton[3]
     declare ui_button $myButton0
     declare ui_button $myButton1
     declare ui_button $myButton2
     $preproc_i := 0
     while ($preproc_i<=2)
       %myButton[$preproc_i] := get_ui_id($myButton0)+$preproc_i
       inc($preproc_i)
     end while
    end on
    
    /* Adressing a button via ID:
       myButton[menu1] -> value := 0
       Adressing a button itself:
       myButton0 := 0
       myButton1 := 1
    */
    
  • leon59
    leon59 Member Posts: 32 Member

    That is mind blowing, what a time saver. Thanks.

    I am currently stuck on another issue. :(

    I have 6 groups divided into 2 two menus with groups 1-3 menu 1, 4-6 menu 2. There are two sets of - + buttons one for each menu. The problem I'm having is, on the second menu the plus button will continue to increment into oblivion and the label text disappears. You have to minus it back the same amount of presses to get it back to group 6. I don't get it because there are only 6 groups, but on the first menu when the plus button reaches group 3 it will cycle back around to group 1. I can't see the issue nor find any info in the KSP ref manual. I have been banging my head with it for hours, if you or somebody could take a look then I would be most grateful , not sure if i need to start a new thread for this ikssue but here goes anyway :-


    on init
    
    set_ui_height_px(300)
       
    declare $i
    declare $item_idx
    declare @item_txt
    declare $item_idxB
    declare @item_txtB
    
    
    declare ui_label $label (1,1)
    declare ui_menu $menu1
    declare ui_button $minus_1
    declare ui_button $plus_1
    declare ui_label $labelB (1,1)
    declare ui_menu $menu2
    declare ui_button $minus_2
    declare ui_button $plus_2
    
    
    set_control_par(get_ui_id($menu1),$CONTROL_PAR_WIDTH,60)
    set_control_par(get_ui_id($minus_1),$CONTROL_PAR_WIDTH,50)
    set_control_par(get_ui_id($plus_1),$CONTROL_PAR_WIDTH,50)
    set_control_par(get_ui_id($menu2),$CONTROL_PAR_WIDTH,60)
    set_control_par(get_ui_id($minus_2),$CONTROL_PAR_WIDTH,50)
    set_control_par(get_ui_id($plus_2),$CONTROL_PAR_WIDTH,50)
    
    set_control_par_str(get_ui_id($minus_1),$CONTROL_PAR_TEXT,"- minus1")
    set_control_par_str(get_ui_id($plus_1),$CONTROL_PAR_TEXT,"+ plus1")
    set_control_par_str(get_ui_id($minus_2),$CONTROL_PAR_TEXT,"- minus2")
    set_control_par_str(get_ui_id($plus_2),$CONTROL_PAR_TEXT,"+ plus2")
    
    
    move_control_px($label,160,25)
    move_control_px($menu1,75,50)
    move_control_px($minus_1,160,50)
    move_control_px($plus_1,220,50)
    move_control_px($labelB,395,25)
    move_control_px($menu2,310,50)
    move_control_px($minus_2,395,50)
    move_control_px($plus_2,455,50)
    
    
    declare const $LAYER1_FIRST := 0
    declare const $LAYER1_LAST := 2
    declare const $LAYER2_FIRST := 3
    declare const $LAYER2_LAST := 5
    
    $i := $LAYER1_FIRST
    while ($i<=$LAYER1_LAST)
    add_menu_item($menu1,group_name($i),$i)
    inc($i)
    end while
    
    $i := $LAYER2_FIRST
    while ($i<=$LAYER2_LAST)
    add_menu_item($menu2,group_name($i),$i)
    inc($i)
    end while
    
    end on
    
    
    function menu1_upd
    $item_idx:=get_control_par(get_ui_id($menu1),$CONTROL_PAR_SELECTED_ITEM_IDX)
    @item_txt:=get_menu_item_str(get_ui_id($menu1),$item_idx)
    set_text($label,@item_txt)
    end function
    
    function menu2_upd
    $item_idxB:=get_control_par(get_ui_id($menu2),$CONTROL_PAR_SELECTED_ITEM_IDX)
    @item_txtB:=get_menu_item_str(get_ui_id($menu2),$item_idxB)
    set_text($labelB,@item_txtB)
    end function
    
    
    
    on ui_control($menu1)
    $i := $LAYER1_FIRST
    while ($i<=$LAYER1_LAST)
    purge_group($i,0)
    inc($i)
    end while
    call menu1_upd
    purge_group($menu1,1)
    end on
    
    on ui_control($minus_1)
    $i := $LAYER1_FIRST
    while ($i<=$LAYER1_LAST)
    purge_group($i,0)
    inc($i)
    end while
    $item_idx := get_control_par(get_ui_id($menu1),$CONTROL_PAR_SELECTED_ITEM_IDX)
    if ($item_idx-1<0)
    $item_idx := get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)
    end if
    $item_idx := $item_idx-1
    set_control_par(get_ui_id($menu1),$CONTROL_PAR_VALUE,$item_idx)
    call menu1_upd
    purge_group($menu1,1)
    $minus_1 := 0 
    end on
    
    on ui_control($plus_1)
    $i := $LAYER1_FIRST
    while ($i<=$LAYER1_LAST)
    purge_group($i,0)
    inc($i)
    end while
    $item_idx := get_control_par(get_ui_id($menu1),$CONTROL_PAR_SELECTED_ITEM_IDX)
    if ($item_idx+1>(get_control_par(get_ui_id($menu1),$CONTROL_PAR_NUM_ITEMS)-1))
    $item_idx := -1
    end if
    $item_idx := $item_idx+1
    set_control_par(get_ui_id($menu1),$CONTROL_PAR_VALUE,$item_idx)
    call menu1_upd
    purge_group($menu1,1)
    $plus_1:= 0   
    end on
    
    on ui_control($menu2)
    $i := $LAYER2_FIRST
    while ($i<=$LAYER2_LAST)
    purge_group($i,0)
    inc($i)
    end while
    call menu2_upd
    purge_group($menu2,1) 
    end on
    
    on ui_control($minus_2)
    $item_idxB := get_control_par(get_ui_id($menu2),$CONTROL_PAR_SELECTED_ITEM_IDX)
    if ($item_idxB-1<0)
    $item_idxB:= get_control_par(get_ui_id($menu2),$CONTROL_PAR_NUM_ITEMS)
    end if
    $item_idxB := $item_idxB-1
    set_control_par(get_ui_id($menu2),$CONTROL_PAR_VALUE,$item_idxB)
    $i := $LAYER2_FIRST
    while ($i<=$LAYER2_LAST)
    purge_group($i,0)
    inc($i)
    end while
    call menu2_upd
    purge_group($menu2,1)
    $minus_2 := 0
    end on
    
    on ui_control($plus_2)
    $item_idxB := get_control_par(get_ui_id($menu2),$CONTROL_PAR_SELECTED_ITEM_IDX)
    if ($item_idxB+1>(get_control_par(get_ui_id($menu2),$CONTROL_PAR_NUM_ITEMS)-1))
    $item_idx := -1
    end if
    $item_idxB := $item_idxB+1
    set_control_par(get_ui_id($menu2),$CONTROL_PAR_VALUE,$item_idxB)
    $i := $LAYER2_FIRST
    while ($i<=$LAYER2_LAST)
    purge_group($i,0)
    inc($i)
    end while
    call menu2_upd
    purge_group($menu2,1)
    $plus_2 := 0  
    end on
    


  • corbo-billy
    corbo-billy Member Posts: 83 Helper
    edited August 2023

    Just adapt what you wrote for "plus1" to "plus2" and it's work fine .



    on ui_control($plus_2)
    $i := $LAYER2_FIRST
    while ($i<=$LAYER2_LAST)
    purge_group($i,0)
    inc($i)
    end while
    $item_idxB := get_control_par(get_ui_id($menu2),$CONTROL_PAR_SELECTED_ITEM_IDX)
    if ($item_idxB+1>(get_control_par(get_ui_id($menu2),$CONTROL_PAR_NUM_ITEMS)-1))
    $item_idxB := -1
    end if
    $item_idxB := $item_idxB+1
    set_control_par(get_ui_id($menu2),$CONTROL_PAR_VALUE,$item_idxB)
    call menu2_upd
    purge_group($menu2,1)
    $plus_2:= 0   
    end on
    
  • leon59
    leon59 Member Posts: 32 Member

    Corbo-billy,

    I thought I did!, what an absolute.... I am.

    Many many thanks.

Back To Top