Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
BSlug committed Dec 9, 2023
1 parent e6205bf commit 362fd41
Showing 1 changed file with 67 additions and 8 deletions.
75 changes: 67 additions & 8 deletions src/lime/_internal/backend/html5/HTML5Window.hx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class HTML5Window
private var currentTouches = new Map<Int, Touch>();
private var isFullscreen:Bool;
private var parent:Window;
private var mouseLockRequest:Bool;
private var mouseLocked:Bool;
private var nextMouseLockRequest:Float = 0;
private var primaryTouch:Touch;
private var renderType:RenderContextType;
private var requestedFullscreen:Bool;
Expand Down Expand Up @@ -238,6 +241,8 @@ class HTML5Window
canvas.addEventListener("webglcontextlost", handleContextEvent, false);
canvas.addEventListener("webglcontextrestored", handleContextEvent, false);
}

Browser.document.addEventListener("pointerlockchange", mouseLockChange);
}

public function alert(message:String, title:String):Void
Expand Down Expand Up @@ -429,7 +434,7 @@ class HTML5Window

public function getMouseLock():Bool
{
return false;
return mouseLocked;
}

public function getOpacity():Float
Expand Down Expand Up @@ -708,17 +713,27 @@ class HTML5Window
}

case "mousemove":
if (x != cacheMouseX || y != cacheMouseY)
if (mouseLocked)
{
parent.onMouseMove.dispatch(x, y);
parent.onMouseMoveRelative.dispatch(x - cacheMouseX, y - cacheMouseY);

parent.onMouseMoveRelative.dispatch(event.movementX, event.movementY);
if ((parent.onMouseMove.canceled || parent.onMouseMoveRelative.canceled) && event.cancelable)
{
event.preventDefault();
}
}
else
{
if (x != cacheMouseX || y != cacheMouseY)
{
parent.onMouseMove.dispatch(x, y);
parent.onMouseMoveRelative.dispatch(x - cacheMouseX, y - cacheMouseY);

if ((parent.onMouseMove.canceled || parent.onMouseMoveRelative.canceled) && event.cancelable)
{
event.preventDefault();
}
}
}
default:
}

Expand Down Expand Up @@ -928,7 +943,22 @@ class HTML5Window

return false;
}


private function mouseLockChange():Void
{
if (Browser.document.pointerLockElement == canvas)
{
canvas.removeEventListener("click", requestMouseLock);
mouseLockRequest = false;
mouseLocked = true;
}
else if (mouseLocked == true)
{
mouseLocked = false;
nextMouseLockRequest = Date.now().getTime() + 1500;
}
}

public function move(x:Int, y:Int):Void {}

public function readPixels(rect:Rectangle):Image
Expand Down Expand Up @@ -963,7 +993,12 @@ class HTML5Window

return null;
}


public function requestMouseLock():Void
{
canvas.requestPointerLock();
}

public function resize(width:Int, height:Int):Void {}

public function setMinSize(width:Int, height:Int):Void {}
Expand Down Expand Up @@ -1153,7 +1188,31 @@ class HTML5Window
return false;
}

public function setMouseLock(value:Bool):Void {}
public function setMouseLock(value:Bool):Void
{
if (value)
{
if (!(mouseLocked || mouseLockRequest))
if (currentTime = Date.now().getTime() > nextMouseLockRequest)
{
canvas.addEventListener("click", requestMouseLock);
mouseLockRequest = true;
}
}
else
{
if (mouseLocked)
{
mouseLocked = false;
Browser.document.exitPointerLock();
}
else if (mouseLockRequest)
{
canvas.removeEventListener("click", requestMouseLock);
mouseLockRequest = false;
}
}
}

public function setOpacity(value:Float):Void {}

Expand Down

0 comments on commit 362fd41

Please sign in to comment.