使用libgdx旋转TextButton

时间:2013-07-26 09:56:02

标签: android libgdx matrix-transform

我有一个textButton,我想将它放在旋转90度的屏幕上。

由于某些原因,与TextButton对象相关的所有旋转方法(rotate(),setRotationAngle()等)都无法正常工作。

所以我实现了扩展TextButton和覆盖draw()方法的新类:

@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);
}

rotationAngle等于90.0。由于某种原因,按钮不会旋转90度,但对于某些未知的度数。

enter image description here

UPD

我切换回TextButton对象后做了:

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

它几乎起作用意味着按钮中的文字被正确旋转,但按钮的背景仍保留在它的位置:

enter image description here

所以我的问题是:为什么会发生这种情况,我该如何解决?

3 个答案:

答案 0 :(得分:4)

我根据documentation

实施了旋转窗口小部件

这是我的代码:

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

然后:

rotatingActor.setRotation(newDegree);

即使小部件已旋转,所有点击处理程序等也能按预期工作。

答案 1 :(得分:2)

project issue已关闭,因为无法修复。

  

由于剪切是通过剪裁实现的,因此无法旋转scene2d中的任何UI元素。剪裁需要轴对齐的矩形。

答案 2 :(得分:1)

现有Actors的旋转方法应该有效。可能值得问另一个问题来追踪这个问题。

我至少看到两个问题:

  1. 默认的批量转换矩阵可能不是单位矩阵。也许将rotationMatrix初始化为oldMatrix

  2. 的副本
  3. 您正在围绕一个非常随意的矢量旋转(从原点绘制一条线 - 在左下角到按钮的左上角)。试试Vector3(0, 1, 0)