# How can I make a Unclickable ui_switch?



## Nowhk (Nov 3, 2015)

I'm trying to do a sort of switch-led grid based: when I click on a switch, it toggle a led on/off:


```
on init
    declare ui_switch led
      set_text(led, "Led")
      make_persistent(led)
      move_control_px(led, 100,10)

    declare ui_switch switch
      set_text(switch, "Switch")
      move_control_px(switch, 200,10)    

      set_ui_height_px (200)
      make_perfview
end on

function setToggleButton(button)
  if(button = 1)
    button := 0
    end if
end function

on ui_control(switch)
  setToggleButton(switch)
  if(led=1)
    led:=0
  else if(led=0)
    led:=1
  end if
end on
```
for the moment as "led" I've used a ui_switch; later I'll change its graphics with custom images (green/red for example).

How can I make the led switch "unclickable"? So only the Switch can change its state. Is it possible?

Thanks!


----------



## Mike Greene (Nov 3, 2015)

You could do it with two labels (one for on and one for off), with led pix for graphics. Then use move_control(0,0) and move_control_px(xx,yy) to move the pictures on and off the screen.

Or you could make it so clicking the switch reassigns it's value to whatever the source is. Like this:

```
on ui_control
   $led := $switch
end on
```

Hmmm, as I look at that, method 2 is much easier, but I have to admit that for some reason, I usually use method 1.


----------



## Nowhk (Nov 3, 2015)

I can't make persistant value of Label. Every time I open it I need the led to be on/off due to its previous state. That's why I need some controls.

The second way you are saying, maybe would work, but clicking on it (without releasing) get the impression to somethings that can be clicked! Which I don't want.

there isn't a sort of ignore_on_ui?


----------



## d.healey (Nov 3, 2015)

Just place an invisible button over the switch - buttons are higher than switches in the Z index


----------



## Nowhk (Nov 3, 2015)

d.healey said:


> Just place an invisible button over the switch - buttons are higher than switches in the Z index


What is an "invisible button"? A button is visible and clickable as well, or am I miss somethings?
Can you give to me an example? Thank you


----------



## d.healey (Nov 3, 2015)

Place a button on the exact same x/y position as your switch and make it the same size or slightly larger, then set its image to a blank png. I do it near the end of this video -


----------



## EvilDragon (Nov 3, 2015)

I see no need to use two controls for the purpose of one. You need just one ui_switch and use the graphic you want for it to have the LED states you need (off and on).

What's the point of this separate LED control?


----------



## Nowhk (Nov 4, 2015)

EvilDragon said:


> I see no need to use two controls for the purpose of one. You need just one ui_switch and use the graphic you want for it to have the LED states you need (off and on).
> 
> What's the point of this separate LED control?


If you see, the Switch is "toggle": it has not state on/off. It is always unclicked. I don't know how I can make "persistant" led (using graphic): I guess I need a sort of controls to save the state of each led, don't I need?


----------



## EvilDragon (Nov 4, 2015)

Sure the switch has on/off states. 0 is off, 1 is on. That's all you need. You don't need the switch to behave as a toggle. You simply use the states of the switch to show the LED as off or on. Meaning - the switch IS the LED. You only need one UI control for one field of the grid - not two.


----------



## Nowhk (Nov 4, 2015)

EvilDragon said:


> Sure the switch has on/off states. 0 is off, 1 is on. That's all you need.


But the Switch that "switches" the led is always 0. Can't be 1. Did you see my Toggle function?


----------



## Nowhk (Nov 4, 2015)

EvilDragon said:


> You simply use the states of the switch to show the LED as off or on. Meaning - the switch IS the LED. You only need one UI control for one field of the grid - not two.


No  My switch state is always 0 due to "toggle" mode.


----------



## EvilDragon (Nov 4, 2015)

There's no need for your toggle function at all, from where I stand. You're overcomplicating it.


----------



## Nowhk (Nov 4, 2015)

Yes, there is! That's just an example. I'm doing a function that when I click on a toggle button, it choose 4 different kind of states for leds. So, let say I've 4 leds: when I click on the switches, it toggle on/off each of them randomly.

Once I reload the instruments, I want to store the states of this leds (that doesn't depends of switch, but from led itself).


----------



## Nowhk (Nov 4, 2015)

d.healey said:


> Place a button on the exact same x/y position as your switch and make it the same size or slightly larger, then set its image to a blank png. I do it near the end of this video -



This seems to works well! Things similar to web/css, with z-index! Thank you.

Just a question: is it possible to make a sort of overflow hidden from the button? I mean, if the button is 20x20 and the picture is 30x30, I see a 30x30 picture. Instead, I'd like to see it "cutted". Possible? Or every image must be the same of the controls?


----------



## willbedford (Nov 4, 2015)

If you don't want it to be clickable, there's no point making it a ui_switch. Just use a label and 
$CONTROL_PAR_PICTURE_STATE to change the animation state directly.


----------



## Nowhk (Nov 4, 2015)

NO  As I said I need to remember the state of this "label" in the future. And that's possible only with controls (button and/or switches).


----------



## willbedford (Nov 4, 2015)

Then store the value in a variable when you're setting the picture state. Way simpler than overlaying a blank button.


----------



## Nowhk (Nov 4, 2015)

But that variable value will be lost when I'll reload the Kontakt Instruments in the future :O


----------



## willbedford (Nov 4, 2015)

Not if you use make_persistent().


----------



## Nowhk (Nov 4, 2015)

willbedford said:


> make_persistent()?


Can I really make persistent a variable? I don't think so:


```
on init
    declare myVar:=0
    declare first
    declare after
    make_persistent(myVar)

    first:=myVar

    myVar:=1

    after:=myVar

    message(first&" "&after)
end on
```
every time I open the Instruments, the result is always 0 1, even the second time (which should be 1 1 if the variable become persistent).


----------



## willbedford (Nov 4, 2015)

Nowhk said:


> Can I really make persistent a variable? I don't think so


Of course you can. The persistent value will be applied AFTER the init callback. If you want to use the stored value inside the init callback, you need to use read_persistant_var() first.


```
on init
    declare myVar:=0
    declare first
    declare after
    make_persistent(myVar)
    read_persistent_var(myVar)
    first:=myVar

    myVar:=1

    after:=myVar

    message(first&" "&after)
end on
```


----------



## Nowhk (Nov 4, 2015)

Wow, amazing! So yes I can just store the array of those values and use labels instead. Didn't know this trick! Thank you man


----------



## EvilDragon (Nov 4, 2015)

Seems like you're still missing some understanding of the basics of KSP. Please go through the original KSP manual from Kontakt 2/3. The persistence mechanism is explained much better there (among other things).

www.opasquet.fr/dl/texts/kontakt3_p2.pdf


----------



## Lindon (Nov 5, 2015)

every time you start the program you are forcing myVar to equal 0 with

declare myVar := 0

so you are ALWAYS going to get 0 1


----------



## willbedford (Nov 5, 2015)

Lindon said:


> every time you start the program you are forcing myVar to equal 0 with
> 
> declare myVar := 0
> 
> so you are ALWAYS going to get 0 1


No, because the stored persistent value will overwrite the initial value.


----------



## Lindon (Nov 5, 2015)

oops silly me.


----------

