Jump to content

WidgetSprite setTransform acting weirdly when width and hegiht are manually set.


photo

Recommended Posts

When a WidgetSprite is rotated which has its size set manually by using setWidth() and setHeight() it gives unexpected result. If the size is set to (0, 0) then the rotation works as expected.

Below is the sample code:

 

WidgetSprite sprite;
int init()
{
   Gui gui = engine.getGui();
   sprite = new WidgetSprite(gui,"samples/widgets/textures/background.png");
   sprite.setPosition(400, 300);
   // Uncomment to see the problem.
   //sprite.setWidth(128);
   //sprite.setHeight(128);
   gui.addChild(sprite, GUI_ALIGN_OVERLAP);
   return 1;
}

float fAngle = 1.0f;
int update()
{
   sprite.setTransform(rotateZ(fAngle));
   fAngle += 0.1;
   return 1;
}

int shutdown()
{
   delete sprite;
   return 1;
}

Link to comment

You rotate the background layer which is strongly not recommended. The reason is that the background layer is used to calculate BB of the widget. We have fixed stretching to some extent, but rotating the background layer is still not option. Use other layers for rotation.

Link to comment

If I use a non-background layer, then the background layer will require another dummy(possibly a fully transparent) image to calculate the BB, rt?. Or can we set a blank background layer and set its width and height manually and do the transformations on another layer?

Link to comment

Sure I did and it turns out that setting a non-background layer works like a charm though the getWidth() and getHeight() will return 0 since they are based on the background layer.

This solves my issue.

One small doubt though, will I loose any benefit if I don't use the background texture at all? Looks like one should never use the background layer at all.

Link to comment

Until you are not rotating it, using the background one should be fine.

 

though the getWidth() and getHeight() will return 0 since they are based on the background layer

Have you called arrange() first? It forces to calculate widget size in the current frame. Otherwise, it will be calculated and returned only in the next frame, when all GUI elements are updated.

Link to comment

Yup, I tried arrange() and I also put

log.message("%d, %d\n", sprite.getWidth(), sprite.getHeight());

in the update() function. As long as I don't set a texture to the background layer the size of the sprite is fixed at (1, 1). The moment I set a texture to the background layer, the sprite dimensions are updated to reflect the texture's dimensions.

Link to comment

Then it works as intended :) (1,1) are default values for the background layer unless the texture is set. If you want to get the layer size, use getLayerWidth() and getLayerHeight().

Link to comment
×
×
  • Create New...