How to make a centered label with Icon?

What’s wrong with this?

public class AButton extends ActionButton {

    ColorRGBA disabledColor;

    public AButton(Action action) {
        super(action);
    }

    @StyleAttribute("disabledColor")
    public void setDisabledColor(ColorRGBA disabledColor) {
        this.disabledColor = disabledColor;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        ColorRGBA c = ColorRGBA.Gray;
        if (enabled) {
            c = getColor();
        }
        setColor(c);
        ((TbtQuadBackgroundComponent) getBackground()).setColor(c);
    }
}

error: cannot find symbol
@StyleAttribute(“disabledColor”)
symbol: class StyleAttribute
location: class AButton

I mean: is there a way for the groovy style to invoke a method on Abutton? Something like

def highlightOn = new Command<Button>() {
    public void execute ( Button source ) {
        ((AButton)source).invokeAButtonMethod();
    }
}

I mean… did you import the StyleAttribute class?

Yes… only you don’t even need the cast.

Ok, thanks. But:

ClassCastException: com.simsilica.lemur.ActionButton cannot be cast to com.pesegato.p8s.sdk.amethyst.AButton

I did

    import com.pesegato.p8s.sdk.amethyst.AButton;
//...
   def highlightOn = new Command<AButton>() {
    public void execute ( AButton source ) {
        ((AButton)source).callBack()
        //source.background = defWhiteBackground.clone();
    }
}

Also tried by replacing Button with AButton on different places, didn’t help.

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.simsilica.lemur.ActionButton[text=Campaign mode, color=Color[1.0, 0.5, 1.0, 1.0], elementId=ElementId[small.button]]' with class 'com.simsilica.lemur.ActionButton' to class 'com.pesegato.p8s.sdk.amethyst.AButton'

or

MissingMethodException: No signature of method: com.simsilica.lemur.ActionButton.callBack() is applicable for argument types: () values: [] Possible solutions: click(), collect(), collect(groovy.lang.Closure), collect(java.util.Collection, groovy.lang.Closure)

This sort of worked for me.

def pressedCommand = new Command<Button>() {
    public void execute( Button source ) {
        if (source.isEnabled()){
            if( source.isPressed() ) {
                source.background = defPinkBackground.clone();
                source.move(1, -1, 0);
            } else {
                source.move(-1, 1, 0);
            }
        }
    }       
};
    
def highlightOn = new Command<Button>() {
    public void execute ( Button source ) {
        //source.callBack();
        if (source.isEnabled()){
            source.background = defWhiteBackground.clone();
        }
    }
}

def highlightOff = new Command<Button>() {
    public void execute ( Button source ) {
        if (source.isEnabled()){
            source.background = defBackground.clone();
        }
    }
}

Yes, because you must be attaching it to all kinds of buttons that aren’t just AButton.

Groovy is basically Java here… there’s no magic. If you get an error about something you’d get in Java then it’s a Java error. Here your attaching a thing that’s trying to cast its source to AButton to things that are not AButton.

So do an instanceof check if you want to operate that way.

And then you never need the case either way. If the code is valid in the first place, you NEVER HAVE TO CAST in groovy. Dynamic dispatch.

1 Like

Note: in Lemur master I’ve added ButtonAction.Enabled/Disabled:

…and the appropriate code to call those when enabling/disabling a button. (I’ve also added runEffect calls for the new EFFECT_ENABLE/EFFECT_DISABLE, for what it’s worth.)

These will be in the next release… or if you build Lemur from source you can have them already.

1 Like