# Switch Icon pictures problem..



## Tod (Jan 7, 2019)

I've created many switches along with the pictures in the past and not had this problem. The icon furthest to the left is another switch and it works perfectly in the order I show there.

However, the icons I'm using for my "question and reply" during the exporting of individual presets is totally messed up. The way is should work is when the variable equals 0 it should show the top icon, which it does until I've clicked on the switch for the first time. I've got it set up so that as soon as I've clicked on one of the switches, it sets another variable dependent on whether it's the yes or no switch. Then the switch variable is returned to 0 so that the next time the switch shows up again it should show the top icon. 

However, that's not happening, instead when I export another preset the switch shows up with the 3rd icon and maintains that position from then on. They won't even change when they're hoovered over. However, the switches are still working correctly.

Incidentally, the colors I show on the icons are just for testing purposes. 

Of course I'm showing and hiding the switches as the need arises, but that shouldn't have any affect should it? Also, just so that you know, the text files that go with the picts are correct, they're the same as all my other switch pictures.







It's always possible that I've overlooked something but I'm not sure what. Any ideas?


----------



## EvilDragon (Jan 7, 2019)

I am wondering why are you even complicating things with the additional questioning there... Personally I'd just show the load/save dialog, do the async callback thing, and be done with it. There's a "Cancel" button in that file dialog so export/import can still be prevented like that. This just makes it more tedious to export/import stuff.

That said, not sure if you made a typo, but the order of frames is 0 1 0 1 0 1, not 0 1 1 0 0 1.


----------



## Tod (Jan 7, 2019)

Thanks Mario, I'm not familiar with the load/save dialog. I'll have to check that out, I do have the "on async_complete" call back.

Regarding the icon order, that's the way they showed up when I checked them out. I'll check it again to see for sure.

However, I'm still wondering about my question as to what's going on with the icons for the switches? There's got to be a reason and I'd like to know what it is.


----------



## Tod (Jan 7, 2019)

Okay, regarding the order, I just checked again and you are right, however, it can be a bit confusing.

I was going by the fact that as I hoovered over the 0 icon, then pressed and held the zero button, that's what I considered the 0 pressed but it's actually the 1 pressed.  

Ha ha, I'd still like to know about my original question. Even if I move on to the load/save dialog, I really don't want to leave this question behind.


----------



## EvilDragon (Jan 8, 2019)

Tod said:


> I'm not familiar with the load/save dialog



I'm talking about load/save_array() in mode 0. That's when you get the dialog. See example in KSP reference.

Regarding your question, you're probably doing something unnecessary. For example, there's no need for that extra variable, I presume. You just press Yes or No as per usual. The next time you show those buttons, make sure you set their value to 0 so they will show with the default state...


----------



## Tod (Jan 8, 2019)

Aah, now I remember about the mode 0 on the load/save_array(), I hadn't tried that and forgot about it.

Regarding the extra variable, I started out without it, but put it in while I was working on the problem. 

Thanks much Mario, I'll put some code together and get back.


----------



## Tod (Jan 8, 2019)

Okay, here's the basic coding I'm using, and thankyou for sticking with me Mario. 


```
on ui_control(Imp_Exp_Menu)
  select (Imp_Exp_Menu)
    case 0
    case 2 to 5
      //This loads the array
    case 7 to 10
      call Warning
        Ask_ID->hide := $HIDE_WHOLE_CONTROL
        Yes_ID->hide := $HIDE_WHOLE_CONTROL
        No_ID->hide := $HIDE_WHOLE_CONTROL
      if (cnt2 = 1)
        select (Imp_Exp_Menu)
          case 7
            $save_arr_id:=save_array(%Pre1,1)
          case 8
            $save_arr_id:=save_array(%Pre2,1)
          case 9
            $save_arr_id:=save_array(%Pre3,1)
          case 10
            $save_arr_id:=save_array(%Pre4,1)
        end select
      end if 
  end select
  if (Imp_Exp_Menu>1 and Imp_Exp_Menu<6)
    while load_arr_id > -1
      wait(1)
    end while
    call Load_Preset
  end if}
  Imp_Exp_Menu:=0
end on

function Warning
  Yes_Swch:=0
  No_Swch:=0 
  Ask_ID->hide := $HIDE_PART_NOTHING
  Yes_ID->hide := $HIDE_PART_NOTHING
  No_ID->hide := $HIDE_PART_NOTHING
  cnt2:=0
  cnt1:=0
  while (cnt1 < 1)
    wait(50000)
    if (Yes_Swch = 1)
      cnt2:=1
      exit
    end if
    if (No_Swch = 2)
      cnt2:=2
      exit
    end if
  end while   
end function

on ui_control (Yes_Swch)
end on
on ui_control (No_Swch)
end on
```


----------



## Tod (Jan 8, 2019)

Humm, I tried "$save_arr_id:=save_array(%Pre1,0)", and nothing happened. The dialog didn't show, and the "message("File saved") didn't show up either, so I assume the file wasn't saved. I had all other windows closed and also looked behind Kontakt and Reaper, but couldn't find any dialog.

I must be missing something, although I couldn't find anything it the manual to tell me otherwise.


----------



## EvilDragon (Jan 8, 2019)

Test in standalone, always. load/save_array mode 0 works just fine here...


----------



## Tod (Jan 8, 2019)

Okay, I got it to work, however I'm not real keen on doing it that way. Too much room for error and the user has to know exactly what he wants to do. If the save dialog showed up with the correct "File name:" in it, it would be better, but even then I prefer the way I originally planned, it's simpler for the user and there's no room for error. 

Did you look at the code above Mario, to see if there's any indication there that might cause the switch icon to get messed up. I've just never run into this before and don't have a clue what's happening.

I could use just one icon instead of 6, or make them all the same, but then there would be no mouse over and I think that is important.


----------



## P.N. (Jan 8, 2019)

Yeah, mode 0 is not pretty... but it's so simple.

I got a custom preset system to work, with scrolling, move presets, delete presets, categories, etc, and it's definitely a lot nicer to look at and interact with.

I was even creating a way to extend the size of the scroll bar, depending on how many presets were present...

Unfortunately, it's a lot, but i do mean a lot of code which may or not be worth it...

It was probably one of the hardest things i ever did in Kontakt, not because of the amount of code, but the considerable planning that needs to go into it, specially if you decide to add a "delete" option (which is not a real delete, but for the user it is... anyway), and delete any of the presets on the list, no matter where they are and how many are displayed.

The way i did it involves creating all sort of arrays for presets that are active, hidden, preset names... 

I probably over complicated it, but i can't imagine this being a lot simpler.

Paulo


----------



## Tod (Jan 8, 2019)

Hi Paulo, yeah, I've got mine set up very simple, one default preset and 4 user presets that can be saved or loaded.

I've also got 4 Temp presets that can be saved and then loaded with a CC controller.

Then the 4 nka files that can be imported or exported. 

The only thing I'm having problems with are the icons for the "yes/no" switches, the way I describe it above. The switches still work properly, it's just the order of the icons on the switches that's not right, mainly just a visual thing. 

Ha ha, I can imagine what you describe above, no thank you.


----------



## P.N. (Jan 8, 2019)

Tod said:


> Ha ha, I can imagine what you describe above, no thank you.



Oh, it's a similar system to the Output libraries, with some extra features, like deleting, moving, etc.



Tod said:


> The only thing I'm having problems with are the icons for the "yes/no" switches, the way I describe it above. The switches still work properly, it's just the order of the icons on the switches that's not right, mainly just a visual thing.



You probably know this, but the order of the images (pressed state) changes, depending if you declare a switch or a button.


----------



## Tod (Jan 8, 2019)

P.N. said:


> You probably know this, but the order of the images (pressed state) changes, depending if you declare a switch or a button.



Thanks Paulo, and yes, I only use switches unless I'm just putting something very simple together that requires no skinning.

In my situation, after hitting "Apply", the switch icons are working correctly before clicking on the switches. The first state is showing and mouse overs are working correctly.

But if I click on the "Yes" switch, it changes to the 3rd state and is locked there, and doesn't change after that. No mouse over or anything, it remains in the 3rd state on subsequent uses. Then if I click on the "No" switch, the same thing happens.

Right now I can't even imagine what the problem can be.


----------



## P.N. (Jan 8, 2019)

Tod said:


> But if I click on the "Yes" switch, it changes to the 3rd state and is locked there, and doesn't change after that. No mouse over or anything, it remains in the 3rd state on subsequent uses. Then if I click on the "No" switch, the same thing happens.



If you're resetting them, that shouldn't happen. 
Maybe you can try resetting them at the end of each relevant function, just to test?
Then you can trace back to where the problem is.


----------



## Tod (Jan 8, 2019)

I'm not exactly sure what you mean Paulo, but I did move "Check_Export" so that it comes after everything else, but the results are the same. "Check_Export" hides all the switches and the label, as well as sets the switch values to zero.

Here's a gif showing what's happening.






PS: To better clarify, that yellow is the 3rd state but it should be the 2nd state.


----------



## EvilDragon (Jan 8, 2019)

There appears to be nothing in the callbacks of yes/no buttons in your code. Set the value of those buttons to 0 in those callbacks.


----------



## Tod (Jan 8, 2019)

Actually I've tried that Mario, and that's where the other variable "cnt3" came in. Without cnt3 there's no way to satisfy the if condition in the while statements because the Yes/No switches will go back with a value of 0.

This is the way I had it and of course the while loop was set up for the cnt3.


```
on ui_control (Yes_Swch)
  if (Yes_Swch = 1)
    cnt3:=1
  end if
  Yes_Swch:=0
end on
on ui_control (No_Swch)
  if (No_Swch = 1)
    cnt3:=2
  end if
  No_Swch:=0
end on
```


----------



## P.N. (Jan 8, 2019)

Ah... Sorry, Tod.
I didn't get the explanation and was jumping to conclusions.

Now i see something fishy is going on, because it remains stuck at the mouse down state.

Alright, i think i had a similar issue once. This can happen if you're using Z_LAYER or if your menu extends further from the GUI.

Try changing the YES/NO switches to buttons and let me know if that makes a difference - just as a test.
I'll take a better look at the code you posted to see if there's something that we missed there.

EDIT: Or click anywhere on the GUI (except a control), after it turns yellow, to see if it refreshes the switch. I'm still counting that changing it to a button might fix it, but... i'm not sure.

EDIT2: Another thing to try. Set Z_LAYER to 0 on the menu, and to 1 for the YES/NO switches.

One of these has to work! :D Hopefully...


----------



## Tod (Jan 8, 2019)

Thanks Paulo, I know we've discussed Z_LAYER before but I'm no using it. The label "Are you sure?" is just below the menu and the Yes & No switches are just below the label.

If I change the switches to buttons, I can't skin them can I? Or is it that I can't use index ids with them?

I'd kind of need the answers to those two questions before I proceed changing the switches to buttons.


----------



## P.N. (Jan 8, 2019)

Tod said:


> If I change the switches to buttons, I can't skin them can I? Or is it that I can't use index ids with them?



The skin just behaves differently, akin the the way buttons work when compared to switches - you'll know what i mean when you try it. (switches react instantly, while buttons react on mouse click release)
You really don't need to do anything else.
Just replace ui_switch with ui_button for testing, then you can tweak the image states if you want to.

Before doing this, or after, you could try the Z_LAYER command. Again it's just a 3 line test solution, so it doesn't hurt to try.

Just confirm your variable names for this:


```
set_control_par(get_ui_id($Imp_Exp_Menu), $CONTROL_PAR_Z_LAYER, 0)
set_control_par(get_ui_id($Yes_Swch), $CONTROL_PAR_Z_LAYER, 1)
set_control_par(get_ui_id($No_Swch), $CONTROL_PAR_Z_LAYER, 1)
```

I'm suggesting the Z_LAYER command just because there's a chance it could work considering that the menu extends above the switches. So maybe that, combined with the fact that the menu goes out of bounds, causes some conflict.

My way of thinknig is, if something doesn't cost time/effort to try out, i might was well try it, even if at first it doesn't make a lot of sense.


----------



## P.N. (Jan 8, 2019)

Tod said:


> Or is it that I can't use index ids with them?



What do you mean?

Edit: Caught this in your code above:
if (No_Swch = 2)


----------



## Tod (Jan 8, 2019)

P.N. said:


> What do you mean?



All my switches and knobs have variable or array IDs. Actually most of them are arrays to go along with all the knobs and these arrays are referenced by an index number. For example I have 58 knobs and each of these knobs will have an array ID index going from 0 to 57.



> Edit: Caught this in your code above:
> if (No_Swch = 2)



Oops that was just a transition error, it should have been "if (No_Swch = 1)". 

Okay, I'll check out changing the switches to knobs in the morning and see what happens.

Thanks a lot Paulo, I get back to you tomorrow.


----------



## P.N. (Jan 8, 2019)

Tod said:


> All my switches and knobs have variable or array IDs. Actually most of them are arrays to go along with all the knobs and these arrays are referenced by an index number. For example I have 58 knobs and each of these knobs will have an array ID index going from 0 to 57.



Based on your previous question about button skinning and this one, i take it you have only been using switches until now.

So, buttons are very similar to switches. Differences are:
- They don't allow for CC automation;
- They react on mouse release (not on mouse press like switches)
- They react differently to image states - because of the nature of a switch vs a button (similar to real physical representations of these controls) - this makes more sense when you're interacting with them.

As with any other control element, they can be arranged in arrays, containing either the control's values or their ids. 
So, they will work exactly like switches when it comes to arrays, indexes, ids, etc.

See you tomorrow, Tod.

Paulo


----------



## P.N. (Jan 8, 2019)

I was having difficulty sleeping so i decided to make an example based on your specifications.
I can't seem to reproduce your image state issue with non-skinned controls... :/

In any case, this example is probably how i'd do it, would need to simplify some things here and there...
I added an error message and a "OK" switch for when the user doesn't have the correct container, patchs, etc defined, or when the preset is not present, just because...

Probably errors here. Well, i think i may finally sleep. 
Cheers.



Spoiler: Code





```
on init
    set_ui_height_px(150)
    set_ui_color(9666666h)
    make_perfview
   
    declare %preset_1[1]
    declare %preset_2[1]
    declare %preset_3[1]
    declare %preset_4[1]

    declare $load_arr_id := -1
    declare $save_arr_id := -1
   
    declare ui_menu $in_out_mn
    move_control_px($in_out_mn,266,10)
   
    add_menu_item($in_out_mn, "Import/Export",100)
    add_menu_item($in_out_mn, "______________",99)
    add_menu_item($in_out_mn, "Import Preset 1",1)
    add_menu_item($in_out_mn, "Import Preset 2",2)
    add_menu_item($in_out_mn, "Import Preset 3",3)
    add_menu_item($in_out_mn, "Import Preset 4",4)
    add_menu_item($in_out_mn, "______________",99)
    add_menu_item($in_out_mn, "Export Preset 1",5)
    add_menu_item($in_out_mn, "Export Preset 2",6)
    add_menu_item($in_out_mn, "Export Preset 3",7)
    add_menu_item($in_out_mn, "Export Preset 4",8)
   
    declare ui_switch $yes_sw
    set_control_par(get_ui_id($yes_sw),$CONTROL_PAR_WIDTH,40)
    set_control_par(get_ui_id($yes_sw),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    set_control_par_str(get_ui_id($yes_sw),$CONTROL_PAR_TEXT,"Yes")
    move_control_px($yes_sw,266,50)
   
    declare ui_switch $no_sw
    set_control_par(get_ui_id($no_sw),$CONTROL_PAR_WIDTH,40)
    set_control_par(get_ui_id($no_sw),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    set_control_par_str(get_ui_id($no_sw),$CONTROL_PAR_TEXT,"No")
    move_control_px($no_sw,313,50)
   
    declare ui_switch $ok_sw
    set_control_par(get_ui_id($ok_sw),$CONTROL_PAR_WIDTH,85)
    set_control_par(get_ui_id($ok_sw),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    set_control_par_str(get_ui_id($ok_sw),$CONTROL_PAR_TEXT,"OK")
    move_control_px($ok_sw,266,50)
   
    declare ui_label $conf_lb(1,1)
    set_control_par(get_ui_id($conf_lb),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Are you sure?")
    move_control_px($conf_lb,266,30)
end on

function reset_menu
        set_control_par(get_ui_id($ok_sw),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
        set_control_par(get_ui_id($yes_sw),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
        set_control_par(get_ui_id($no_sw),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
        set_control_par(get_ui_id($conf_lb),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
        set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Are you sure?")
        $in_out_mn := 100
end function

function confirm
    if(not(in_range($in_out_mn,99,100)))
        set_control_par(get_ui_id($yes_sw),$CONTROL_PAR_HIDE, $HIDE_PART_NOTHING)
        set_control_par(get_ui_id($no_sw),$CONTROL_PAR_HIDE, $HIDE_PART_NOTHING)
        set_control_par(get_ui_id($conf_lb),$CONTROL_PAR_HIDE, $HIDE_PART_BG)
        set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Are you sure?")
    else
         $in_out_mn := 100  
         call reset_menu
    end if
end function

function error
    set_control_par(get_ui_id($ok_sw),$CONTROL_PAR_HIDE, $HIDE_PART_NOTHING)
    set_control_par(get_ui_id($yes_sw),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
    set_control_par(get_ui_id($no_sw),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
    $yes_sw := 0
end function

on async_complete
    if ($NI_ASYNC_ID = $load_arr_id)
        $load_arr_id := -1
       
        if ($NI_ASYNC_EXIT_STATUS = 1)
            wait(200000)
            $yes_sw := 0
            call reset_menu
        else
            set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Preset not found!")
            call error
        end if
    end if
   
    if ($NI_ASYNC_ID = $save_arr_id)
        $save_arr_id := -1
       
        if ($NI_ASYNC_EXIT_STATUS = 1)
            wait(200000)
            $yes_sw := 0
            call reset_menu
        else
            set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Path not defined!")
            call error
        end if
    end if
end on

on persistence_changed
    call reset_menu
end on

on ui_control($in_out_mn)
    call confirm
end on

on ui_control($yes_sw)
    select($in_out_mn)
        case 1
            $load_arr_id:=load_array(%preset_1,1)
        case 2
            $load_arr_id:=load_array(%preset_2,1)   
        case 3
            $load_arr_id:=load_array(%preset_3,1)
        case 4
            $load_arr_id:=load_array(%preset_4,1)
        case 5
            $save_arr_id:=save_array(%preset_1,1)
        case 6
            $save_arr_id:=save_array(%preset_2,1)
        case 7
            $save_arr_id:=save_array(%preset_3,1)
        case 8
            $save_arr_id:=save_array(%preset_4,1)
    end select
end on

on ui_control($no_sw)
    wait(200000)
    $no_sw := 0
    call reset_menu
end on

on ui_control($ok_sw)
    wait(200000)
    $ok_sw := 0
    call reset_menu
end on
```


----------



## EvilDragon (Jan 8, 2019)

Tod said:


> Actually I've tried that Mario, and that's where the other variable "cnt3" came in. Without cnt3 there's no way to satisfy the if condition in the while statements because the Yes/No switches will go back with a value of 0.



Sure you can do that - you first do the function THEN set the buttons to 0! So your function Warning() needs to set the button values to 0 at the _end_, not at the _beginning_.


----------



## Tod (Jan 9, 2019)

Hi Paulo,

Okay, I simply changed the switches to buttons and they all seem to be working correctly now, so I guess I can just leave them as buttons, they certainly wont be assigned any CCs. 

Ha ha, I'd still like to know what's going on with the switches? 

I checked out your script and it seemed to work okay. I had to comment out the "set_ui_color", Kontakt didn't like that which I assume is because I'm doing this with Kontakt 5.5.

Also, when I clicked on "Yes" I got a "Path not defined" which you had setup in the "on async_complete". However, when I tried to export again a few times, the "Yes" switch was still pressed on, it appears to be stuck on. Actually as I think about it, that's exactly what my switches were doing. 

So, if that wasn't happening to you, could the problem be Kontakt version related? Aah, I just tried the switches in the stand-a-lone mode (5.7) and they worked perfectly. So, does that indicate it's a version problem?

I've got all the versions in the plugin mode but not in the stand-a-lone mode, so I can't test it.


----------



## Tod (Jan 9, 2019)

EvilDragon said:


> Sure you can do that - you first do the function THEN set the buttons to 0! So your function Warning() needs to set the button values to 0 at the _end_, not at the _beginning_.



Thanks Mario, may I ask, at the "end" of what, not sure exactly where you mean?

I've actually reset the switches all over the place. Entering the Warn function before the "while" loop, just before the exit from the while loop, at the end of the menu after execution, in the "on async_complete" after the "if ($NI_ASYNC_ID=$save_arr_id)". Ha ha, I've had them all in place at the same time.

What do you think Mario, could it be the Kontakt version as I mention above?


----------



## EvilDragon (Jan 9, 2019)

No, it's not about Kontakt version.

You should reset the button values at the end of Warn function. Not at the beginning, or anywhere else. Also you don't really need a wait and exit in that function. Not sure why you did that. If Yes or No was pressed, do the load/save_array(), of course taking care of async, and then set Yes or No (depending on what was pressed) to 0.


----------



## P.N. (Jan 9, 2019)

Tod said:


> Ha ha, I'd still like to know what's going on with the switches?



I'm not sure. Something similar happened to me last year in a different context.
It wasn't related to save/load, but it was a specific situation where using a button instead of a switch solved the issue.
I don't remember which version i was using at the time, so maybe it was fixed later.



Tod said:


> Also, when I clicked on "Yes" I got a "Path not defined" which you had setup in the "on async_complete". However, when I tried to export again a few times, the "Yes" switch was still pressed on, it appears to be stuck on. Actually as I think about it, that's exactly what my switches were doing.



I included the "error" safety net because i was only providing a script.
Kontakt requires the Data folder and resource file structure by default when using mode 1 load/save commands.

You also get the error message at the right corner of the screen, that's why i never use message("") in my scripts.
I always want to know if Kontakt is trying to tell me somtehing.



Tod said:


> Also, when I clicked on "Yes" I got a "Path not defined" which you had setup in the "on async_complete". However, when I tried to export again a few times, the "Yes" switch was still pressed on, it appears to be stuck on. Actually as I think about it, that's exactly what my switches were doing.



This is happening in the script i posted too? Strange.. i thought i took care of that.

But there's the point. The reseting of the buttons must come at the end of the functions/callbacks.
You must be careful it's not inside an if statement that may be never used, otherwise, that reset property will be stopped in its tracks and the button/switch would stay on indefenitely.

In your case, your switch was behaving erratic by being stuck in the mouse pressed state, so there's that "bug" behaviour that's also worth mentioning, since you have no issues when using a button instead.

Edit: Looking at ED's post above, i agree that there's no need for wait statements. I only used them to make the interaction less rough around the edges - this way you can still see the switches turning on before the controls disappear.


----------



## Tod (Jan 9, 2019)

EvilDragon said:


> No, it's not about Kontakt version.
> 
> You should reset the button values at the end of Warn function. Not at the beginning, or anywhere else. Also you don't really need a wait and exit in that function. Not sure why you did that. If Yes or No was pressed, do the load/save_array(), of course taking care of async, and then set Yes or No (depending on what was pressed) to 0.



Okay, since it's not Kontakt version related, then since it works with stand-a-lone it must be a bug with the Kontakt dll files, right?

Regarding the position of the resets, I started off with them right after the Warn fucntion and when that didn't work I also put them in the Warn function just before the while loop. When that didn't work, I created the extra variable and reset them in the "on ui_control" callbacks. I know I also reset them during the while loop right after the "if (Yes/no_swch = 1) just before the"exit".

Aah, I didn't know about the "wait" being unnecessary, I guess it's a carry over from years back.

Thanks much Mario.


----------



## P.N. (Jan 9, 2019)

You can use the wait statements for polishing the interaction.
Here's a quick tweak of my previous script.

It shows text confirming that the preset was loaded or saved, waits 800 miliseconds, then disappears. Definitely not necessary, but... you know. 



Spoiler: Code





```
on init
    set_ui_height_px(150)
    set_ui_color(9666666h)
    make_perfview
   
    declare %preset_1[1]
    declare %preset_2[1]
    declare %preset_3[1]
    declare %preset_4[1]

    declare $load_arr_id := -1
    declare $save_arr_id := -1
   
    declare ui_menu $in_out_mn
    move_control_px($in_out_mn,266,10)
   
    add_menu_item($in_out_mn, "Import/Export",100)
    add_menu_item($in_out_mn, "______________",99)
    add_menu_item($in_out_mn, "Import Preset 1",1)
    add_menu_item($in_out_mn, "Import Preset 2",2)
    add_menu_item($in_out_mn, "Import Preset 3",3)
    add_menu_item($in_out_mn, "Import Preset 4",4)
    add_menu_item($in_out_mn, "______________",99)
    add_menu_item($in_out_mn, "Export Preset 1",5)
    add_menu_item($in_out_mn, "Export Preset 2",6)
    add_menu_item($in_out_mn, "Export Preset 3",7)
    add_menu_item($in_out_mn, "Export Preset 4",8)
   
    declare ui_switch $yes_sw
    set_control_par(get_ui_id($yes_sw),$CONTROL_PAR_WIDTH,40)
    set_control_par(get_ui_id($yes_sw),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    set_control_par_str(get_ui_id($yes_sw),$CONTROL_PAR_TEXT,"Yes")
    move_control_px($yes_sw,266,50)
   
    declare ui_switch $no_sw
    set_control_par(get_ui_id($no_sw),$CONTROL_PAR_WIDTH,40)
    set_control_par(get_ui_id($no_sw),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    set_control_par_str(get_ui_id($no_sw),$CONTROL_PAR_TEXT,"No")
    move_control_px($no_sw,313,50)
   
    declare ui_switch $ok_sw
    set_control_par(get_ui_id($ok_sw),$CONTROL_PAR_WIDTH,85)
    set_control_par(get_ui_id($ok_sw),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    set_control_par_str(get_ui_id($ok_sw),$CONTROL_PAR_TEXT,"OK")
    move_control_px($ok_sw,266,50)
   
    declare ui_label $conf_lb(1,1)
    set_control_par(get_ui_id($conf_lb),$CONTROL_PAR_TEXT_ALIGNMENT,1)
    set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Are you sure?")
    move_control_px($conf_lb,266,30)
end on

function reset_menu
        set_control_par(get_ui_id($ok_sw),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
        set_control_par(get_ui_id($yes_sw),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
        set_control_par(get_ui_id($no_sw),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
        set_control_par(get_ui_id($conf_lb),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
        set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Are you sure?")
        $in_out_mn := 100
end function

function confirm
    if(not(in_range($in_out_mn,99,100)))
        set_control_par(get_ui_id($yes_sw),$CONTROL_PAR_HIDE, $HIDE_PART_NOTHING)
        set_control_par(get_ui_id($no_sw),$CONTROL_PAR_HIDE, $HIDE_PART_NOTHING)
        set_control_par(get_ui_id($conf_lb),$CONTROL_PAR_HIDE, $HIDE_PART_BG)
        set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Are you sure?")
    else
         $in_out_mn := 100  
         call reset_menu
    end if
end function

function error
    set_control_par(get_ui_id($ok_sw),$CONTROL_PAR_HIDE, $HIDE_PART_NOTHING)
    set_control_par(get_ui_id($yes_sw),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
    set_control_par(get_ui_id($no_sw),$CONTROL_PAR_HIDE, $HIDE_WHOLE_CONTROL)
    $yes_sw := 0
end function

on async_complete
    if ($NI_ASYNC_ID = $load_arr_id)
        $load_arr_id := -1
       
        if ($NI_ASYNC_EXIT_STATUS = 1)
            set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Preset " & $in_out_mn & " loaded!")
            wait(800000)
            $yes_sw := 0
            call reset_menu
        else
            set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Preset not found!")
            call error
        end if
    end if
   
    if ($NI_ASYNC_ID = $save_arr_id)
        $save_arr_id := -1
       
        if ($NI_ASYNC_EXIT_STATUS = 1)
            set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Preset " & $in_out_mn-4 & " saved!")
            wait(800000)
            $yes_sw := 0
            call reset_menu
        else
            set_control_par_str(get_ui_id($conf_lb),$CONTROL_PAR_TEXT,"Path not defined!")
            call error
        end if
    end if
end on

on persistence_changed
    call reset_menu
end on

on ui_control($in_out_mn)
    call confirm
end on

on ui_control($yes_sw)
    select($in_out_mn)
        case 1
            $load_arr_id:=load_array(%preset_1,1)
        case 2
            $load_arr_id:=load_array(%preset_2,1)   
        case 3
            $load_arr_id:=load_array(%preset_3,1)
        case 4
            $load_arr_id:=load_array(%preset_4,1)
        case 5
            $save_arr_id:=save_array(%preset_1,1)
        case 6
            $save_arr_id:=save_array(%preset_2,1)
        case 7
            $save_arr_id:=save_array(%preset_3,1)
        case 8
            $save_arr_id:=save_array(%preset_4,1)
    end select
end on

on ui_control($no_sw)
    wait(200000)
    $no_sw := 0
    call reset_menu
end on

on ui_control($ok_sw)
    wait(200000)
    $ok_sw := 0
    call reset_menu
end on
```


----------



## Tod (Jan 9, 2019)

P.N. said:


> In your case, your switch was behaving erratic by being stuck in the mouse pressed state, so there's that "bug" behaviour that's also worth mentioning, since you have no issues when using a button instead.



Yeah, I think I will proceed, this was kind of some finishing touches for my new steel guitar instrument I hope to put out soon.



> Edit: Looking at ED's post above, i agree that there's no need for wait statements. I only used them to make the interaction less rough around the edges - this way you can still see the switches turning on before the controls disappear.



Ha ha, maybe in the back of my mind the while loop would be whirling around to fast I was afraid something would break.  

Thanks Paulo.


----------



## Tod (Jan 9, 2019)

Okay guys, I hate to say it but I think a "wait" might have some pertinence after all. When I took the wait out of the while loop, this is what I got.

*SCRIPT WARNING: while loop terminated (too much iterations, no wait statement)
*
As soon as I put the wait back in, the script warning disappeared.


----------



## P.N. (Jan 9, 2019)

You don't need wait statements, exits or while loops for the yes/no confirmation script, which was where i was focosing on.

The only while loop you'd possibly need for this exercise as a whole (and again this depends how your script is setup) would be in the async load portion of it - where you copy the loaded array to its intended destination, preferably after confirming that the async exit status is true.

There are situations where creating arrays for async ids can come handy, but you don't need any of that at all for this script. So, using while loops for the async variables is just complicating, in my opinion.

Take a look at my script. It could still be simplified, and compacted in some ways, but it's simpler and doesn't need while loops, waits (which again, were only for interactivity reasons), or exits.


----------



## Tod (Jan 9, 2019)

Okay, thanks Paulo, I'm afraid my head is still way back on this, maybe as far as K2, not sure, I think I've only used a warning once before some time ago, and that was a while loop. 

In my head I'm thinking linear & sequential, and have to pause some how, is this wrong? I did look your script over and noticed you didn't use a while loop, but I didn't have the time to figure it out.

I'll take a look at it again and see if I can figure it out. If I can't I'll be back.


----------



## P.N. (Jan 9, 2019)

Here's a rundown of what i did.

- The menu, doesn't do anything related to load/save.
It just calls up the confirmation assets (yes/no switches and the question label), or resets itself if you select a menu separator.

- The "no" switch really doesn't do anything else either besides hidding the confirmation assets and reseting the menu, and itself.

- It's the "yes" switch that does all the work depending on what's currently selected by the menu.
Once pressed, it triggers the appropriate command which is sent over to the async callback.

- The async callback decides what to do based on its return status, hides the confirmation assets and resets the menu.

There are the extras "File not found" or somthing, but those are just optional and don't really matter here.


----------



## Tod (Jan 9, 2019)

Okay I've pretty much figured it out as you explain above, and I understand pretty much how it works. Ha ha, I've never thought of doing it that way before.

I also understand that it's just waiting for the yes or no switch to be clicked. However, what happens if somehow some of the other controls get moved before the yes/no switches get licked on? (there's 58 knobs and several bypass switchs)


----------



## P.N. (Jan 9, 2019)

Tod said:


> I also understand that it's just waiting for the yes or no switch to be clicked. However, what happens if somehow some of the other controls get moved before the yes/no switches get licked on? (there's 58 knobs and several bypass switchs)



Don't touch other controls while the script is asking you an important question! It's rude! :D

Nah, seriously, you can just add the reset function to those control callbacks, so no worries there.

If you think that's too much code flowing everytime you touch a control, you can flag the confirmation task, and only trigger the reset function if the confirmation task is set to "true". It's probably overkill, but... well.


----------



## Tod (Jan 9, 2019)

I've got to admit, it would be foolish to not pay attention to what's in front of you, but things happen. 

Thanks Paulo, I think I'm good to go and I'll see about incorporating your idea into my script.


----------

