Search code examples
androidlayoutplayntripleplay

Drag a PlayN/Tripleplay widget without recalculating layout


I like the Tripleplay UI layout for the PlayN framework, but can I sometimes disable the layout calculation so I can move things around or change a widget's text without triggering a layout calculation?

For example, I created a simple screen with a single button in an AxisLayout, and added a listener to the button:

button.layer.addListener(new Adapter() {
    private float startX;

    @Override
    public void onPointerStart(Event event) {
        startX = event.localX();
    }

    @Override
    public void onPointerDrag(Event event) {
        button.layer.transform().translateX(event.localX() - startX);
    }
});

Now dragging the button moves it to the left and right, and I'm happy. You can see the source code on Github.

However, this code has two problems:

  1. When I release the mouse or lift my finger, the button goes back to its starting position.
  2. If I drag the mouse or my finger up so it leaves the button, the button will briefly flash back to its starting position, then continue dragging. If I release the mouse or lift my finger in this state when it's outside the button, the button stays where it is.

What appears to be happening is that whenever the button changes state from pressed to unpressed or the other way, the screen's layout gets recalculated, and the button goes back to its starting position.

Is there some way to tell the screen not to recalculate its layout? Can I tell the button that it's valid?

The only strategy I can think of is to put the button in the axis layout, calculate its position, then move it from the axis layout to an absolute layout while I move it around. I hope there's an easier way.


Solution

  • The idea of tripleplay elements is that they position themselves, based on the layouts, constraints, size, etc, etc.

    If you want to be able to move them around, you need to use an AbsoluteLayout