Question

I have a textButton and I want to place it on the screen rotated by 90deg.

For some reason all methods for rotation(rotate(), setRotationAngle() and so on) assosiated with TextButton object do not work properly.

So I've implemented new class extending the TextButton and overriding draw() method:

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
     Matrix4 rotationMatrix = new Matrix4();
     Matrix4 oldMatrix = batch.getTransformMatrix();
     rotationMatrix.idt();
     rotationMatrix.rotate(new Vector3(this.getX(),this.getY()+this.getHeight(),0),rotationAngle);
     batch.setTransformMatrix(rotationMatrix);
     super.draw(batch, parentAlpha);
     batch.setTransformMatrix(oldMatrix);
}

Where rotationAngle equals to 90.0. And for some reason button isn't rotated by 90degress, but for some unknown amount of degrees.

enter image description here

UPD

After I switched back to TextButton object and did :

newGame.setTransform(true);
newGame.rotate(90);

It almost worked meaning that text in the button was rotated corrently, but the background of the button stayed on it's spot:

enter image description here

So my question is: Why does this happen and how can I solve this ?

Was it helpful?

Solution

I implemented rotating Widgets according to the documentation

Here is my code:

Table buttonContainer = new Table(skin);
buttonContainer.setTransform(true);
buttonContainer.add(button1);
buttonContainer.row().pad(10);
buttonContainer.add(button2);
rotatingActor = buttonContainer;

and then:

rotatingActor.setRotation(newDegree);

All the click handlers etc. work as expected even if the widget are rotated.

OTHER TIPS

There is a project issue that is closed as won't fix.

None of the UI elements in scene2d can be rotated due to the fact that clipping is implemented via scissoring. Scissoring requires axis aligned rectangles.

The rotation methods on existing Actors should work. Probably worth asking another question to track that issue down.

I see at least two problems:

  1. the default batch transorm matrix probably isn't the identity matrix. Maybe initialize rotationMatrix as a copy of oldMatrix?

  2. You're rotating around a really arbitrary vector (draw a line from the origin -- in the bottom left to the top-left corner of your button). Try Vector3(0, 1, 0).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top