Search code examples
playn

PlayN, point [x,y] location with scaled layer


in PlayN, if I have a root layer scaled (eg, scale of 2.0), when I detect a mouse point on screen, 10,10 and draw it point on screen, the point shown is twice the distance as I saw on the OS's mouse point.

What I want is the object I drag move at the exact same distance as I move my OS mouse pointer.

Is there a coordinate map function that map my mouse input X/Y to my logical scaled coordinate system?

Thanks.


Solution

  • You have two choices. You can register your Pointer.Listener on the root layer and use Pointer.Event.localX/Y to obtain the mouse coordinate in the layer's coordinate system:

    graphics().rootLayer().addListener(new Pointer.Adapter() {
        public void onPointerStart(Pointer.Event event) {
            // use event.localX() and event.localY() here
        }
    });
    

    Or you can register a global Pointer.Listener and use Layer.Util to translate the points manually:

    pointer().addListener(new Pointer.Adapter() {
        public void onPointerStart(Pointer.Event event) {
            Point local = Layer.Util.screenToLayer(graphics().rootLayer(), event.x(), event.y());
            // use local.x and local.y here
        }
    });