# Custom images



## RiffWraith (May 21, 2011)

Howdy.

So, I want a custom image to turn on and off stuff. I figured rather than make my own right away, I would just start with an NI-provided one. So, I added a comp into the insert fx rack, just to have something to use.

This is where I am at as of right now:


```
on init 

set_ui_height_px(350)
make_perfview 

declare ui_button $comp_onoff
$comp_onoff := 0 
declare $id 
$id := get_ui_id($comp_onoff)

set_control_par($id,$CONTROL_PAR_MOUSE_BEHAVIOUR,2000) 
set_control_par($id,$CONTROL_PAR_DEFAULT_VALUE,0) 
set_control_par_str($id,$CONTROL_PAR_PICTURE,"pv_world_switch_on_off")

move_control_px($comp_onoff,360,200)
$comp_onoff := (_get_engine_par($ENGINE_PAR_EFFECT_BYPASS,-1,0,1)+1) 
if ($comp_onoff = 0)

end if
end on

on ui_control ($comp_onoff)
if ($comp_onoff = 0)
_set_engine_par ($ENGINE_PAR_EFFECT_BYPASS,1,-1,0,1)
else
_set_engine_par ($ENGINE_PAR_EFFECT_BYPASS,0,-1,0,1)

end if
end on
```

And it works. Thing is, I get the 'comp.' text as part of the image. Where is that coming from? I am not using the 'set_text' command... :? 

Furthermore, Mario had said previously that buttons need to have six states:

1 off 
2 on 
3 off, mouse down 
4 on, mouse down 
5 off, mouse over 
6 on, mouse over

Does that really apply here? I mean it's working as-is...:lol: If so, where do I put 'em?

Thanks bunches!


----------



## EvilDragon (May 21, 2011)

Yes, all buttons and switches should have 6 animations. The picture you've used is a factory image and it has the necessary 6 animations.

The "comp" text you're getting is probably the name of the variable, but trimmed because the button image is not as wide as the full variable name. If you use set_text($comp_on_off,""), you'll make it go away, of course.


----------



## RiffWraith (May 21, 2011)

EvilDragon @ Sat May 21 said:


> Yes, all buttons and switches should have 6 animations. The picture you've used is a factory image and it has the necessary 6 animations.



Ah, the txt file needs to have those anims, not the code....got it!



EvilDragon @ Sat May 21 said:


> If you use set_text($comp_on_off,""), you'll make it go away, of course.



Of course! I knew that! :lol: Yeah - forgot about that.... :? 

Ok, So I have this:


```
on init 

set_ui_height_px(350)
make_perfview 

declare ui_button $comp_onoff
$comp_onoff := 0
declare $id 
$id := get_ui_id($comp_onoff)
set_text ($comp_onoff,"")

move_control_px($comp_onoff,490,190)
$comp_onoff := (_get_engine_par($ENGINE_PAR_EFFECT_BYPASS,-1,1,1)+1) mod 2 
if ($comp_onoff = 0)

end if
end on

on ui_control ($comp_onoff)
if ($comp_onoff = 0)
_set_engine_par ($ENGINE_PAR_EFFECT_BYPASS,1,-1,1,1)
else
_set_engine_par ($ENGINE_PAR_EFFECT_BYPASS,0,-1,1,1)

end if

end on
```

Which allows me to save the instrument with the button in the off position, which is what I want. So when the inst loads, the button is off. Now when I work in the other code to have the custom button:


```
on init 

set_ui_height_px(350)
make_perfview 

declare ui_button $comp_onoff
$comp_onoff := 0
declare $id 
$id := get_ui_id($comp_onoff)
set_text ($comp_onoff,"")

set_control_par($id,$CONTROL_PAR_MOUSE_BEHAVIOUR,2000) 
set_control_par($id,$CONTROL_PAR_DEFAULT_VALUE,0) 
set_control_par_str($id,$CONTROL_PAR_PICTURE,"pv_world_switch_on_off")

move_control_px($comp_onoff,490,190)
$comp_onoff := (_get_engine_par($ENGINE_PAR_EFFECT_BYPASS,-1,1,1)+1) mod 2 
if ($comp_onoff = 0)

end if
end on

on ui_control ($comp_onoff)
if ($comp_onoff = 0)
_set_engine_par ($ENGINE_PAR_EFFECT_BYPASS,1,-1,1,1)
else
_set_engine_par ($ENGINE_PAR_EFFECT_BYPASS,0,-1,1,1)

end if

end on
```

Here, the custom button isn't visible when I save and load the instr in the off position. In fact, it becomes invisible when I 'apply'. It _is_ visible, however, when I save in the on position.

What am I missing here?


----------



## EvilDragon (May 21, 2011)

You forgot to set the height of the image in the init callback:

set_control_par($id,$CONTROL_PAR_HEIGHT,51)


Oh, and you can remove MOUSE_BEHAVIOUR line - it doesn't make sense for buttons and switches.


----------



## RiffWraith (May 21, 2011)

Awesomeness! Thanks so much.  

But now I have another Q. That code is not in the KSP Reference Manual. If it weren't for you, how would I have known that?


----------



## RiffWraith (May 21, 2011)

Ok, I am stuck. :? Can't do multiples here. 


```
on init 

set_ui_height_px(350) 
make_perfview 

declare ui_button $comp_onoff
$comp_onoff := 0 
declare $id 
$id := get_ui_id($comp_onoff)
set_text ($comp_onoff,"")

set_control_par($id,$CONTROL_PAR_DEFAULT_VALUE,0) 
set_control_par_str($id,$CONTROL_PAR_PICTURE,"pv_synth_button_small")
set_control_par($id,$CONTROL_PAR_HEIGHT,51)

move_control_px($comp_onoff,75,290)
$comp_onoff := (_get_engine_par($ENGINE_PAR_EFFECT_BYPASS,-1,1,1)+1) mod 2
if ($comp_onoff = 0)

end if
end on
```

Works just fine. So, to the comp, I want to add, say, delay. So now I have:


```
on init 

set_ui_height_px(350) 
make_perfview 

declare ui_button $comp_onoff
$comp_onoff := 0 
declare $id 
$id := get_ui_id($comp_onoff)
set_text ($comp_onoff,"")

set_control_par($id,$CONTROL_PAR_DEFAULT_VALUE,0) 
set_control_par_str($id,$CONTROL_PAR_PICTURE,"pv_synth_button_small")
set_control_par($id,$CONTROL_PAR_HEIGHT,51)

move_control_px($comp_onoff,75,290)
$comp_onoff := (_get_engine_par($ENGINE_PAR_EFFECT_BYPASS,-1,0,1)+1) mod 2
if ($comp_onoff = 0)

end if

declare ui_button $d_onoff
$d_onoff := 0 
declare $id1 
$id1 := get_ui_id1($d_onoff)
set_text ($d_onoff,"")

set_control_par($id1,$CONTROL_PAR_DEFAULT_VALUE,0) 
set_control_par_str($id1,$CONTROL_PAR_PICTURE,"pv_synth_button_small")
set_control_par($id1,$CONTROL_PAR_HEIGHT,51)

move_control_px($d_onoff,205,290)
$d_onoff := (_get_engine_par($ENGINE_PAR_EFFECT_BYPASS,-1,1,1)+1) mod 2
if ($d_onoff = 0)

end if
end on
```

I am getting "epression expected" in the delay code - the *$id1 := get_ui_id1($d_onoff)* line. Why? :?:


----------



## EvilDragon (May 21, 2011)

I think you meant get_ui_id, not get_ui_id1. 


Also, why using empty if clause? You also don't need the $d_onoff := 0 and $comp_onoff := 0 lines, as this is the default state of the control when it's declared...


----------



## RiffWraith (May 21, 2011)

EvilDragon @ Sun May 22 said:


> Also, why using empty if clause? You also don't need the $d_onoff := 0 and $comp_onoff := 0 lines, as this is the default state of the control when it's declared...



Err, ok. Took those out. Still have the 'expression expected'.



EvilDragon @ Sun May 22 said:


> I think you meant get_ui_id, not get_ui_id1.



Nope - the get_ui_id1 is on purpose. If d_onoff and comp_onoff are seperate controls, they need seperate ids, so comp_onoff is id, and d_onoff is id1. Right? Because if they are both id, I get the 'id already declared' message.

So, what am I missing?

Thanks!

--edit--

ok, just deleted $comp_onoff, and tried just the $d_onoff with id2, and that doesn't work either - still get the 'expression expected'. That tells me that the 'expression expected' error is due to id*2*. But if I leave $d_onoff as id, I still get ''id already declared' , which, techinically, it has been with the $comp_onoff.

What to do?


----------



## RiffWraith (May 21, 2011)

Ok, GOT IT!

I had *declare $id * 2x instead of just 1x. THAT is what was causing the 'id already declared' - _not_ the fact that *$id := get_ui_id* was used 2x, which is what I thought the problem was. Ok, with *declare $id * 1x, all is well.

BUT....

I have one more dilemma. I want both buttons to start in the off position. Currently, they start in the on position. I can get one button to start off, with:

(_get_engine_par($ENGINE_PAR_EFFECT_BYPASS,-1,4,1)+1) *mod 2*

But if I put that 'mod 2' at the end of the other (_get_engine_par($ENGINE_PAR_EFFECT_BYPASS...it screws everything up.

How to get both buttons to start in the off position? Changing the $CONTROL_PAR_DEFAULT_VALUE,*0*) to *1* and *-1* didnt do the trick.

Thanks again!


----------



## EvilDragon (May 22, 2011)

You didn't understand me. You have a TYPO: the command is called get_ui_id(), NOT get_ui_id*1*(). It doesn't matter if it's a different UI ID - the command MUST be typed get_ui_id() - no added numbers!

Also, as I told you already, using $CONTROL_PAR_DEFAULT_VALUE has no meaning with switches.

Usually, when managing controls via UI IDs, I have ONE array that has IDs for all the controls:

declare %ID[<number of controls>]
%ID[0] := get_ui_id($Control1)
%ID[1] := get_ui_id($Control2)
%ID[2] := get_ui_id($Control3)
etc.

If you don't use make_persistent() for the controls you've declared, they will always start in off position.

Here's the code that works. And it's simple!


```
on init
	make_perfview
	set_ui_height_px(350)
	
	declare ui_button $comp_onoff
	declare ui_button $d_onoff

	declare %ID[2]	{ 2 - total number of controls }
	%id[0] := get_ui_id($comp_onoff)
	%id[1] := get_ui_id($d_onoff)

	declare $i
	while ($i < 2)	{ 2 - total number of controls }
		set_control_par_str(%ID[$i],$CONTROL_PAR_TEXT,"")
		set_control_par_str(%ID[$i],$CONTROL_PAR_PICTURE,"pv_synth_button_small")
		set_control_par(%ID[$i],$CONTROL_PAR_WIDTH,25)
		set_control_par(%ID[$i],$CONTROL_PAR_HEIGHT,25)
		inc($i)
	end while

	move_control_px($comp_onoff,75,290)
	move_control_px($d_onoff,205,290)

	message("")
end on
```


How to find out the width and height for buttons: open the custom image file you want to use, see the dimensions of the image. For pv_synth_button_small, you have that orange button, and the dimensions are 23x138 pixels. So, width is 23, and 138 / 6 = 23 for height. I used 25 because Kontakt has a strange way of dealing with button images, it "needs" additional pixels declared otherwise it doesn't show the picture fully, it crops it a bit. For knobs and sliders I use 1 additional pixel.

WHY is this important? Simple - if you declare a bigger button than the image is, even if the image is smaller than the button, the button will STILL have the size you declared! Meaning that you can press the button EVEN IF YOU DID NOT CLICK ON THE IMAGE DIRECTLY. This is very important.


----------



## RiffWraith (May 22, 2011)

Hey, thanks again for your time.  



EvilDragon @ Sun May 22 said:


> You didn't understand me. You have a TYPO: the command is called get_ui_id(), NOT get_ui_id*1*().



No - I did understand you. Maybe I did a poor job at making myself clear, or perhaps you missed part of my post. Initially _I thought _that each object needed a different id, but as I alluded to earlier, I found that to not be the case, and have already changed everything back to get_ui_id(). 

Ok, got it. Wasn't aware that you can have one array that has IDs for all the controls. Used your example as a guide - THANKS!

"if you declare a bigger button than the image is, even if the image is smaller than the button, the button will STILL have the size you declared! Meaning that you can press the button EVEN IF YOU DID NOT CLICK ON THE IMAGE DIRECTLY. This is very important."

Yep - found that out the hard way...:lol:


----------



## stevemazzaro (May 22, 2011)

I dont mean to hi-jack the post - but I'm trying to do the same thing only using a label with a static image. Is there a reason Riff's ui_button is working, yet my ui_label script is not?


```
On init

declare ui_label $pan1 (3, 7)
move_control ($pan1, 3, 10)
set_text ($pan1, "")
set_control_par_str(get_ui_id ($pan1),$CONTROL_PAR_PICTURE, "pictures/Standard")

End on
```

The label is just the standard label background color...


----------



## RiffWraith (May 22, 2011)

stevemazzaro @ Mon May 23 said:


> I dont mean to hi-jack the post - but I'm trying to do the same thing only using a label with a static image. Is there a reason Riff's ui_button is working, yet my ui_label script is not?



ED knows better than I do, but from what I know, buttons need to have 6 states:

1 off 
2 on 
3 off, mouse down 
4 on, mouse down 
5 off, mouse over 
6 on, mouse over

These are declared in the associated .txt file, which resides in the same dir as your pic, and I believe gets created auto. The pic must be in .png format, and if the png is transparent, the text file has to be edited so that Has Alpha Channel: says "yes" instead of "no"..."no" being created auto by default.

I think I have that correct....hope that helps.


----------



## stevemazzaro (May 22, 2011)

Well this is just to display the static image - no button/animations. I also created the txt file myself, copied and pasted from the other txt files (changing the name, of course). I've tried everything - changing the root folder, changing the name, changing the ui_object, placing the image in both locations (WINDOWS). The file is in png format. 

I have another $CONTROL_PAR_PICTURE line in the same script for the icon, and that line works fine.

Can I not use a label?


----------



## RiffWraith (May 22, 2011)

Ok, I just had an actual glance at your script.


```
On init 

declare ui_label $pan1 (3, 7) 
move_control ($pan1, 3, 10) 
set_text ($pan1, "") 
set_control_par_str(get_ui_id ($pan1),$CONTROL_PAR_PICTURE, "pictures/Standard") 

End on
```

You have *On init *- needs to be *on init *
You have *End on* - needs to be *end on*

Next, get rid of the path. It would be *PAR_PICTURE, "Standard") *

Next, in order to see the label once you close the instrument, you need the *make_perfview* command, which goes after *on init *.

Finally, your *move_control ($pan1, 3, 10) * won't work. It's *move_control_px ($pan1, 3, 10)* - whereas the 'px' is for pixels.

So now you have:


```
on init 

make_perfview
declare ui_label $pan1 (3, 7) 
move_control_px($pan1, 3, 10) 
set_text ($pan1, "") 
set_control_par_str(get_ui_id ($pan1),$CONTROL_PAR_PICTURE, "Standard") 

end on
```

That works here.

Cheers.


----------



## stevemazzaro (May 22, 2011)

The directory fix did it - I don't know why I thought I had to set the pictures folder. Thanks!


----------



## EvilDragon (May 22, 2011)

If you're in Kontakt 4.2, create a Resource container and put all the images in Resources/pictures folder. Then you can reference all the images directly by only typing their name.


And just a slight correction of RiffWraith: move_control ($pan1, 3, 10) is perfectly legitimate command - it just depends on how large you have set_ui_height() set.


----------

