I haven't worked with LibGdx in a couple of years, but I remember figuring this out at some point. I'm trying to create a button, and use ShapeRenderer to draw a Rectangle around the edge of the button (to outline it).
So far, I've attempted to create my own custom Actor and use ShapeRenderer in the draw method, but it won't update the position of the rectangle, opacity etc. In other words, it doesn't take advantage of any of the Actor class benefits. Is there any way to draw a line or shape using the Actor classes which will then get updated with applied Actions etc?
Here is an example of what I was doing in my custom Actor class:
public class Shape extends Actor {
ShapeRenderer sr;
float x, y, w, h;
public Shape (float x, float y, float w, float h) {
sr = new ShapeRenderer();
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
batch.end();
sr.begin(ShapeRenderer.ShapeType.Line);
sr.setColor(Color.BLACK);
sr.rect(x, y, w, h);
sr.end();
batch.begin();
}}
Any help would be greatly appreciated; thanks in advance!
EDIT 4/21 2:39PM EST Made recommended changes, shape still doesn't move / rotate with actions. Any other suggestions?
public class Shape extends Actor {
ShapeRenderer sr;
public Shape (ShapeRenderer sr, float x, float y, float w, float h) {
this.sr = sr;
setX(x);
setY(y);
setWidth(w);
setHeight(h);
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
batch.end();
sr.begin(ShapeRenderer.ShapeType.Line);
sr.setColor(Color.BLACK);
sr.rect(getX(), getY(), getWidth(), getHeight());
sr.end();
batch.begin();
}}
EDIT 4/21 2:46PM EST Here's how I'm implementing my custom actor, I add it to a parent group and set actions on the group...
//Generate buttons
ImageTextButton.ImageTextButtonStyle style = new ImageTextButton.ImageTextButtonStyle();
parameter.size = game.labelButtonSize;
style.font = generator.generateFont(parameter);
style.fontColor = Color.BLACK;
buttonYes = new ImageTextButton(Consts.CAMERA_STRING_BUTTON_YES, style);
buttonYes.align(Align.left);
buttonYes.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.log("TAG", "Clicked Yes");
}
});
Group bY = new Group();
bY.addActor(buttonYes);
bY.addActor(new Shape(sr, buttonYes.getX(), buttonYes.getY(), buttonYes.getWidth(), buttonYes.getHeight()));
bY.addAction(Actions.rotateTo(90, 2));