Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

iconButton that behaves like a checkBox

Explorer ,
Dec 03, 2021 Dec 03, 2021

Hey,

I was just wondering, is there a way to have an iconButton that behaves like a checkBox in the UI of a script?

Thanks in advance,
Redy

TOPICS
Error or problem , How to , Scripting
468
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Dec 09, 2021 Dec 09, 2021

For anyone wondering, this is the code I ended up using ( I'm really not a coder, so this might not be the best way to do it, the best way is probably with an object or an array including all of the images )

It's basically an image, that by default is clicked and you can click on it to turn it on or off. It also detects when the mouse is over the button to put an image that looks like it's selected.



* Make sure you use the actual path to the image, not a binary string because it won't work *

var th
...
Translate
Engaged ,
Dec 03, 2021 Dec 03, 2021

Should be doable, it you add a bool to the button function. 

 

On init, the bool is false. On click, use a if statement to do the switch:

 

if bool true 

   bool false

else 

   bool true

 

*Martin

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 04, 2021 Dec 04, 2021

Why didn't I think about that 🤦‍:male_sign:

That technically works just fine but the button doesn't stay clicked in the UI

Video go for example at 7:00, as you can see the blue buttons on his script stay clicked, that's pretty much what I want to do

Thank you so much,
Redy

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 04, 2021 Dec 04, 2021

The button don't stay clicked, technically spoken. But like you apply the bool, you just switch the color of the button, so it appears highlighted.

 

*Martin

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 05, 2021 Dec 05, 2021

Excatly what I thought! What would be the best way to change the color tho? By changing the image?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 06, 2021 Dec 06, 2021

I never used image buttons before, but from what I have seen in the source folders of other script, this is the workflow. They use a normal button image and a highlight button image.

 

*Martin

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 06, 2021 Dec 06, 2021

Thank you so much for all of those answers! Works just fine

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 09, 2021 Dec 09, 2021

For anyone wondering, this is the code I ended up using ( I'm really not a coder, so this might not be the best way to do it, the best way is probably with an object or an array including all of the images )

It's basically an image, that by default is clicked and you can click on it to turn it on or off. It also detects when the mouse is over the button to put an image that looks like it's selected.



* Make sure you use the actual path to the image, not a binary string because it won't work *

var thisFolder = File($.fileName).path

// MY STYLES
var myStylesNotClicked = File(""+thisFolder+"/Styles/Images/MyStylesNotClicked.png");
var myStylesMouseOver = File(""+thisFolder+"/Styles/Images/MyStylesMouseOver.png");
var myStylesClicked = File(""+thisFolder+"/Styles/Images/MyStylesClicked.png");
var myStylesClickedMouseOver = File(""+thisFolder+"/Styles/Images/MyStyleClickedMouseOver.png");

var bool = true;

var image2 = group1.add("image", undefined, File.decode(myStylesClicked), {name: "image2"}); 
    image2.addEventListener("mouseover", image2MouseOver, false);
    image2.addEventListener("mouseout", image2MouseOut, false);
    image2.addEventListener("click", image2Click, false);

    function image2MouseOver() {
        if (bool == true) {
            updateImage(image2, myStylesClickedMouseOver);
        } else {
            updateImage(image2, myStylesMouseOver);
        }
    }
    function image2MouseOut() {
        if (bool == true) {
            updateImage(image2, myStylesClicked);
        } else {
            updateImage(image2, myStylesNotClicked);
        }
    }
    function image2Click() {
        if (bool == true) {
            bool = false;
            updateImage(image2, myStylesMouseOver);
        } else {
            updateImage(image2, myStylesClickedMouseOver);
            bool = true;
        }
    }

function updateImage(icon, state) {
    icon.image = ScriptUI.newImage(state);
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2021 Dec 10, 2021

Nice!

 

Indeed, using an array can simplify this. You can name your files like 0,1,2,3, knowing that 0 is notclicked, 1 is mouseover and so on. Than you just increase or decrease a counter which work as array index to point to the imagefile path. On the other hand: if it works, it works 🙂

 

You should not name a variable "bool". I used "bool" in my example code to show you the data type. You won't know what your "bool" is for in a few weeks. I wouldn't use "image2" or "group1" either. This will be hard to manage in the future or for others.

 

I think instead of "if (bool == true)" you can write "if (bool)", because bool is true or false already. This way, you can create good to ready code like: "if (button_activated)". I don't know what your "bool" actualy does, so this is just an example.

 

*Martin

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 10, 2021 Dec 10, 2021
LATEST

Hey,

This "bool" was just tempopary ( it's already changed, of course )

Thank you so much for all those tips, this is really gonna help me!

RedyMotion

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines