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

Newbi question: Create a "n" line text from which you can choose among options

Discussion in 'Scripting Workshop' started by Alex_Kay, Apr 18, 2015.

  1. Alex_Kay

    Alex_Kay Member

    Messages:
    30
    Hello!

    My name is Alex and it's nice to be part of this forum. First time I involve myself in Kontakt Scripting and Sound Designing.
    I'm creating a new Kalimba Instrument, more as a test and rather as a selling product, and I was wondering if I can create a e.g. 3 line text, in which I will put three option, from which a user can choose.

    E.g.

    On
    Off
    Synced

    The user will be able to click on each of these option (only once choice, and not multiple), and activate the corresponding function.
    I hope I made myself clear and not misguided you!

    Thank you in advance,
    Alex
     
  2. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Each option will need to be a separate button. But in your case a dropdown menu might be a better call.
     
  3. Alex_Kay

    Alex_Kay Member

    Messages:
    30
    Thank you Evil Dragon for your faster-than-a-speeding-bullet answer! I have been reading your answers to other people for quite some time now! :)

    Why I have a feeling that the dropdown menu isn't so functional? Or at least, the menu I'm describing is more functional than the dropdown menu (1 click for my menu, 2 clicks for the dropdown menu).
     
  4. Big Bob

    Big Bob Forum Member

    Messages:
    606
    HI Alex,

    I think what you want is usually called a radio-button set,

    Here is an example of how you can code something like that.

    on init
    ``message("")
    ``declare %radio_set_id[3]
    ``declare ui_switch $On
    ````%radio_set_id[0] := get_ui_id($On)
    ``declare ui_switch $Off
    ````%radio_set_id[1] := get_ui_id($Off)
    ``declare ui_switch $Synced
    ````%radio_set_id[2] := get_ui_id($Synced)
    ``declare $new_active
    ``declare $last_active
    ````read_persistent_var($last_active)
    ````set_control_par(%radio_set_id[$last_active],$CONTROL_PAR_VALUE,1)
    ``make_persistent($last_active)
    end on

    function change_radio_button
    ``set_control_par(%radio_set_id[$last_active],$CONTROL_PAR_VALUE,0) { turn off last active }
    ``set_control_par(%radio_set_id[$new_active],$CONTROL_PAR_VALUE,1)``{ turn on new active }
    ``$last_active := $new_active { update last_active }
    end function

    on ui_control($On)
    ``$new_active := 0
    ``call change_radio_button
    end on

    on ui_control($Off)
    ``$new_active := 1
    ``call change_radio_button
    end on

    on ui_control($Synced)
    ``$new_active := 2
    ``call change_radio_button
    end on


    Rejoice,

    Bob
     
    • Like Like x 1
  5. Alex_Kay

    Alex_Kay Member

    Messages:
    30
    Bob,

    That was exactly the function I wanted to use. Much appreciated!
    Thank you for your time in writing these lines of script!

    Cheers,
    Alex
     
  6. Alex_Kay

    Alex_Kay Member

    Messages:
    30
    Bob,

    I suppose that you are familiar with 8dio products. In particular, if you aren't familiar, take a look at their wallpaper on the, let's say, adagio violins:
    http://i.ytimg.com/vi/zy5lS8ts4sM/maxresdefault.jpg

    What is the way to accomplish that?

    I don't want you to write for me the code. I just wanted to know the way, the mentality behind it. The code it's something I must do! :)

    Cheers,
    Alex

    Edit: Bob, nevermind! I figured it out!
     
    Last edited: Apr 19, 2015
  7. Big Bob

    Big Bob Forum Member

    Messages:
    606
    Hi Alex,

    Looking at the pix you attached, it looks like the selection is changed by clicking the up/down arrow buttons, is that correct?

    The way you worded your original post, I thought you wanted it so that you just click directly on the selection you want (ie random access). The sequential access, up/down arrow thing is a bit easier to code and I guess you already figured it out.

    However, if you want to implement direct selection (or a combination of both direct and scroll selection), let me know and I'll describe how you can do that. Generally, the idea is to use transparent buttons overlayed on a custom, multi-frame label.

    Rejoice,

    Bob
     
  8. Alex_Kay

    Alex_Kay Member

    Messages:
    30
    Hi Bob!

    The selection is changed with three ways:
    a) Clicking the arrows
    b) Keyswitch
    c) On mouse click

    Correct me where I'm wrong:
    On the three choices I have (on, off, synced) I have to make three images as png files, in which I will have the pressed/unpressed state of these choices (e.g. if "on" is selected let's say that the background of the "on" line will change to red, while the others will remain transparent). These pictures will be loaded on the specific position I will set on the script editor. And then with CONTROL_PAR_PICTURE, I will change its state. Am I on the right track?

    Cheers,
    Alex
     
  9. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    With ui_buttons/ui_switches you don't have to use $CONTROL_PAR_PICTURE_STATE (which is what I think you actually meant) at all. Just use the state of the button itself (off or on) to show that it's selected, or not. Basically, use Bob's code and just add custom graphics onto each button and you're done.
     
  10. Alex_Kay

    Alex_Kay Member

    Messages:
    30
    Hi ED!

    I realized it about half an hour ago, while I was scripting and testing stuff! Most probably I will follow your advice, that is borrowing Bob's code with custom images! So thank you again for your advice and your time in replying to my questions!

    Cheers!
    Alex
     
  11. Big Bob

    Big Bob Forum Member

    Messages:
    606
    Hi Alex,

    Sounds like you've pretty much got things under control. But, just for kicks, I'm attaching a little ListBox demo that I just made using the graphic you posted. This should illustrate what Mario and I have been suggesting.

    To run this demo, be sure you put the .nkr file alongside the .nki so the .nki can find the needed graphics.

    This demo uses a separate label for the ListBox but you can just as well make it part of the wallpaper if that's more appropriate. I only used two different switch images so 1,3,5 are pure alpha channel while 2,4,6 are 50% transparent red for the cursor. You can do this fancier of course if you want to indicate the mouse hovering states, etc.

    With the assumption that you are using Nils' Editor, I've included the KSE Source code that I used. I figured you won't have any use for the graphics so I packed them in the .nkr but it you'd like to examine them, let me know and I can post them.

    Rejoice,

    Bob
     

    Attached Files:

  12. Alex_Kay

    Alex_Kay Member

    Messages:
    30
    Thank you Bob for your work!
    I will try it in a bit and come back again to tell you what happened (although I'm sure you are certain that it's working!)
     
  13. Big Bob

    Big Bob Forum Member

    Messages:
    606
    Whoops! I just downloaded what I posted and I must have made the .nkr with absolute paths checked. Sorry about that. I'll attach another copy with relative paths for you with this post.

    Rejoice,

    Bob
     

    Attached Files:

    • Like Like x 1
  14. Alex_Kay

    Alex_Kay Member

    Messages:
    30
    It's working like a charm Bob!
    Thank you very much! Now I have to study it! ;)
     
  15. Big Bob

    Big Bob Forum Member

    Messages:
    606
    OK Alex, after you finish studying the code, let me know if I need to clarify something or if you want me to post the unpacked graphics for study.

    Meanwhile, just to complete the picture, I've added a few lines of code to implement the key-switch selection function which I'll attach as ListBox Demo#2.

    Rejoice,

    Bob
     

    Attached Files:

  16. Big Bob

    Big Bob Forum Member

    Messages:
    606
    Whoops! :( Again I forgot to add the scroll button flashers to the code. Of course it's just a minor detail but it kind of bugs me that I can't seem to get my act together with this:mad: Oh well, maybe I'll get it right this time. :)

    Here's #3
     

    Attached Files:

    • Like Like x 1
  17. Big Bob

    Big Bob Forum Member

    Messages:
    606
    Hi Alex,

    As I checked back in this morning I was about to congratulate myself on finally 'getting it right' when I noticed that the red cursor is fainter than I had intended:(. When I made the graphics my intention was to make the Red 50% transparent but I must have messed it up somehow:mad:. So, I just redid it at 50% and now it looks much more like I had intended. You probably could care less about such picky little details but, I'm attaching the corrected .nkr file if you want to try it.

    If it isn't right this time I think I'll quit :)

    Rejoice,

    Bob
     

    Attached Files:

  18. Big Bob

    Big Bob Forum Member

    Messages:
    606
    Well Alex,

    It's been a few weeks since you went off to study the script example I gave you. And, since I haven't heard back from you, I guess you must have gotten it all sorted out.

    So, I'll close this out by posting the final, commented version of the script for the benefit of anyone else who might read this thread in the future. The demo instrument panel looks like this:

    [​IMG]

    You can change the active index (displayed on Kontakt's status line) by either directly clicking on any line in the list box, by scrolling up/down with the scroll buttons, or by hitting the associated key.

    I'm attaching the KSE source code and companion Resource folder with the unpacked graphics.

    Rejoice,

    Bob

    { List Box Demo #4 }

    on init
    ``message('')
    ``set_ui_height_px(435)
    ``make_perfview
    ``declare const MaxLine := 11``{ max line index }
    ``declare const X := 165```````{ XY panel position for ListBox }
    ``declare const Y := 10
    ``declare active_idx```````````{ index of currently selected ListBox line }
    ``declare idx
    ``declare ArtKey[MaxLine+1] := (24,25,26,27,28,29,30,31,32,33,34,35)
    ``{ Keyswitches corresponding to each ListBox line index }
    ``declare LineID[MaxLine+1]````{ ids of Listbox line switches }

    ``declare ui_label Menu (1,1)``{ Listbox background image }
    ````set_text(Menu,'')``````````{ set properties }
    ````Menu->height := 413
    ````Menu->width := 286
    ````Menu->picture := 'ListBox'
    ````move_control_px(Menu,X,Y)
    ``declare_list_line(0)`````````{ declare the switches for each line }
    ``declare_list_line(1)
    ``declare_list_line(2)
    ``declare_list_line(3)
    ``declare_list_line(4)
    ``declare_list_line(5)
    ``declare_list_line(6)
    ``declare_list_line(7)
    ``declare_list_line(8)
    ``declare_list_line(9)
    ``declare_list_line(10)
    ``declare_list_line(11)
    ``
    ``
    for idx := 0 to MaxLine``````{ configure the switch properties }
    ````LIneID[idx]->picture := 'BoxCursor'
    ````LineID[idx]->text := ''
    ````LineID[idx]->height := 25
    ````LineID[idx]->width := 224
    ````LineID[idx]->pos_x := X + 30
    ````LineID[idx]->pos_y := Y + 35 + 281*idx/10``{ 28.1 pixels per line }
    ``end for

    ``declare ui_switch Down``````{ Down scroll button }
    ````set_text(Down,'')
    ````Down->picture := 'ScrollDown'
    ````move_control_px(Down,X+256,Y+95)
    ``declare ui_switch Up````````{ Up scroll button }
    ````set_text(Up,'')
    ````Up->picture := 'ScrollUp'
    ````move_control_px(Up,X+256,Y+39)
    ``read_persistent_var(active_idx)``{ initialize to last active index }
    ````LineID[active_idx]->value := 1
    ````message('active index = ' & active_idx) { for testing }
    ``make_persistent(active_idx)
    end on

    on note
    ``idx := search(ArtKey,EVENT_NOTE) { line index if key switch }
    ``if idx # -1``{ key switch }
    ````ignore_event(EVENT_ID)
    ````call new_selection { make corresponding selection }
    ``end if
    end on

    on_line_click(0)``{ ListBox direct click handlers }
    on_line_click(1)
    on_line_click(2)
    on_line_click(3)
    on_line_click(4)
    on_line_click(5)
    on_line_click(6)
    on_line_click(7)
    on_line_click(8)
    on_line_click(9)
    on_line_click(10)
    on_line_click(11)

    on ui_control(Down) { Down scroll handler }
    ``idx := active_idx + 1``{ increase current index circularly }
    ``call new_selection
    ``wait(100000)``````{ for visual persistence of state change }
    ``Down := 0
    end on

    on ui_control(Up)```{ Up scroll handler }
    ``idx := active_idx + MaxLine { decrease current index circularly }
    ``call new_selection
    ``wait(100000)``````{ for visual persistence of state change }
    ``Up := 0
    end on

    function new_selection { common function to change the active index }
    ``idx := idx mod (MaxLine+1)``{ keep idx in the 0..MaxLine range }
    ``LineID[active_idx]->value := 0``{ make last active switch transparent }
    ``LineID[idx]->value := 1`````````{ show new switch as cursor }
    ``active_idx := idx```````````````{ update active index }
    ``message('active index = ' & active_idx)``{ for testing purposes }
    end function

    macro declare_list_line(#index#)``{ create a switch named Line_n }
    ``declare ui_switch Line_#index#
    ``LineID[#index#] := get_ui_id(Line_#index#)
    end macro

    macro on_line_click(#index#)``{ create a callback handler for switch n }
    ``on ui_control(Line_#index#)
    ````idx := #index#
    ````call new_selection
    ``end on
    end macro




     

    Attached Files:

    Last edited: May 6, 2015
  19. Alex_Kay

    Alex_Kay Member

    Messages:
    30
    Bob!

    I'm terribly sorry for my late reply and I apologize!
    Your script worked like a charm and I study it quite well, since I used it in another situation. I really appreciate your help! I'm new to Kontakt and KSP ref manual isn't that good in explaining thoroughly every command.

    Is there any way to bring a "label image" in front of the background image? I have troubles succeeding it. Should I make a new background image, with every "label image" I've created?

    The hide_part seems to not work, since the image from the ui_label is in the back of the background picture.
     
  20. Alex_Kay

    Alex_Kay Member

    Messages:
    30
    So I uploaded this image to explain my problem.

    When I apply the script, my label image "No reverb selected" appears normaly, since the background isn't yet loaded.
    As soon as I choose one option from (Mic positions, ADSR and reverb) the label image disappears and the background image seems to come in front of this image.

    Any thoughts?

    Thank you!
     

    Attached Files: