startDrag for windows

I want to be able to move a window around without having a title bar. Is this possible with lime or openfl? If I naively have a listener on mouse move update the position it doesn’t work right because of course the positions I get from mouse move are relative to the window.

class Main extends Sprite
{
    var mouseDown:Bool = false;

    public function new()
    {	
        super();        

        stage.window.onMouseDown.add(function(_, _, mouseButton) {
            if (mouseButton == MouseButton.LEFT) mouseDown = true;
        });

        stage.window.onMouseUp.add(function(_, _, mouseButton) {
            if (mouseButton == MouseButton.LEFT)  mouseDown = false;
        });

        stage.window.onMouseMove.add(function(_x:Float, _y:Float) {
            if (mouseDown) {
                trace('$_x,$_y');
                stage.window.move(Std.int(x + _x), Std.int(y + _y));
                // stage.window.move(Std.int(_x), Std.int(_y));
            }
        });
    }
}

Hi, never player with that yet but can’t you just store mouse x and y and use the difference ?

Store lastMouseX and lastMouseY on mouseDown then on mouseMove xMove = mouseX-lastMouseX, same for y and store lastMouseX/Y again.

onMouseMoveRelative already gives you the difference between the last time (I believe). But the problem is nothing to my knowledge gives you the coordinates with respect to the window manager, yet window.move() is based on those same absolute coordinates.

It was a dumb mistake, I was using the sprite’s x instead of the window’s. This works:

    stage.addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent) {
        startClickX = event.localX;
        startClickY = event.localY;
        trace('startClick: $startClickX,$startClickY');
    });

    stage.addEventListener(MouseEvent.MOUSE_MOVE, function(event:MouseEvent) {
        if (event.buttonDown) {
            trace('${event.localX},${event.localY}');
            stage.window.move(stage.window.x + Std.int(event.localX - startClickX), stage.window.y + Std.int(event.localY - startClickY));
        }
    });
1 Like