implement ingame menu

This commit is contained in:
LabyStudio
2022-02-03 16:02:55 +01:00
parent 64b84c3f64
commit 5eb1d03e5d
14 changed files with 179 additions and 69 deletions
+29 -7
View File
@@ -28,8 +28,23 @@ window.GameWindow = class {
// Mouse motion
document.addEventListener('mousemove', event => this.onMouseMove(event), false);
// Losing focus event
window.addEventListener("mouseout", function () {
if (minecraft.currentScreen === null) {
minecraft.displayScreen(new GuiIngameMenu());
}
});
// Mouse buttons
document.addEventListener('click', event => minecraft.onMouseClicked(event.button), false);
document.addEventListener('click', function (event) {
// Handle in-game mouse click
minecraft.onMouseClicked(event.button);
// Handle mouse click on screen
if (!(minecraft.currentScreen === null)) {
minecraft.currentScreen.mouseClicked(event.x, event.y, event.code);
}
}, false);
// Keyboard interaction with screen
window.addEventListener('keydown', function (event) {
@@ -41,6 +56,9 @@ window.GameWindow = class {
// Cancel browser interaction
event.preventDefault();
}
} else if (event.code === 'Escape') {
event.preventDefault();
minecraft.displayScreen(new GuiIngameMenu());
}
});
@@ -49,7 +67,12 @@ window.GameWindow = class {
}
requestFocus() {
this.renderer.canvasElement.requestPointerLock();
let scope = this;
setTimeout(function () {
window.focus();
scope.renderer.canvasElement.requestPointerLock();
}, 0);
}
loadRenderer(renderer) {
@@ -64,10 +87,10 @@ window.GameWindow = class {
this.onResize();
// Request focus
let minecraft = this.minecraft;
let scope = this;
canvas.onclick = function () {
if (minecraft.currentScreen === null) {
canvas.requestPointerLock();
if (scope.minecraft.currentScreen === null) {
scope.requestFocus();
}
}
}
@@ -90,14 +113,13 @@ window.GameWindow = class {
// Reinitialize current screen
if (!(this.minecraft.currentScreen === null)) {
this.minecraft.currentScreen.init(this.minecraft, this.width, this.height);
this.minecraft.currentScreen.setup(this.minecraft, this.width, this.height);
}
}
onFocusChanged() {
this.mouseLocked = document.pointerLockElement === this.renderer.canvasElement;
// Open in-game menu
if (!this.mouseLocked && this.minecraft.currentScreen === null) {
this.minecraft.displayScreen(new GuiIngameMenu());
}