Skip to content

Commit d3e4e74

Browse files
committed
implement main menu, implement textfield widget, implement create world screen, implement splash screen
1 parent 7014425 commit d3e4e74

35 files changed

+704
-196
lines changed

index.html

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
<div id="content">
1313
<div id="canvas-container" class="fullscreen"></div>
14-
<span id="pre-status">Loading scripts...</span>
1514
</div>
1615

1716
</body>

src/js/Start.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import Minecraft from './net/minecraft/client/Minecraft.js';
22

33
class Start {
44

5-
constructor(preStatusElementId) {
6-
this.preStatusElement = document.getElementById(preStatusElementId);
7-
}
8-
95
loadTextures(textures) {
106
let resources = [];
117
let index = 0;
@@ -37,15 +33,20 @@ class Start {
3733
"terrain/terrain.png",
3834
"terrain/sun.png",
3935
"terrain/moon.png",
40-
"char.png"
36+
"char.png",
37+
"gui/title/minecraft.png",
38+
"gui/title/background/panorama_0.png",
39+
"gui/title/background/panorama_1.png",
40+
"gui/title/background/panorama_2.png",
41+
"gui/title/background/panorama_3.png",
42+
"gui/title/background/panorama_4.png",
43+
"gui/title/background/panorama_5.png"
4144
]).then((resources) => {
42-
this.preStatusElement.remove();
43-
4445
// Launch actual game on canvas
4546
window.app = new Minecraft(canvasWrapperId, resources);
4647
});
4748
}
4849
}
4950

5051
// Launch game
51-
new Start("pre-status").launch("canvas-container");
52+
new Start().launch("canvas-container");

src/js/net/minecraft/client/GameWindow.js

+60-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import GuiIngameMenu from "./gui/screens/GuiIngameMenu.js";
22
import Keyboard from "../util/Keyboard.js";
3+
import Minecraft from "./Minecraft.js";
34

45
export default class GameWindow {
56

@@ -29,17 +30,16 @@ export default class GameWindow {
2930
this.canvasItems = document.createElement('canvas');
3031
this.wrapper.appendChild(this.canvasItems);
3132

32-
// On resize
33-
let scope = this;
33+
let mouseDownInterval = null;
3434

3535
// Request focus
36-
document.onclick = function () {
37-
if (scope.minecraft.currentScreen === null) {
38-
scope.requestFocus();
36+
document.onclick = () => {
37+
if (this.minecraft.currentScreen === null) {
38+
this.requestFocus();
3939
}
4040
}
4141

42-
window.addEventListener('resize', _ => scope.updateWindowSize(), false);
42+
window.addEventListener('resize', _ => this.updateWindowSize(), false);
4343

4444
// Focus listener
4545
document.addEventListener('pointerlockchange', _ => this.onFocusChanged(), false);
@@ -50,51 +50,59 @@ export default class GameWindow {
5050

5151
// Handle mouse move on screen
5252
if (!(minecraft.currentScreen === null)) {
53-
minecraft.currentScreen.mouseDragged(event.x / scope.scaleFactor, event.y / scope.scaleFactor, event.code);
53+
minecraft.currentScreen.mouseDragged(event.x / this.scaleFactor, event.y / this.scaleFactor, event.code);
5454
}
5555
}, false);
5656

5757
// Mouse release
5858
document.addEventListener('mouseup', event => {
5959
// Handle mouse release on screen
6060
if (!(minecraft.currentScreen === null)) {
61-
minecraft.currentScreen.mouseReleased(event.x / scope.scaleFactor, event.y / scope.scaleFactor, event.code);
61+
minecraft.currentScreen.mouseReleased(event.x / this.scaleFactor, event.y / this.scaleFactor, event.code);
6262
}
63+
64+
clearInterval(mouseDownInterval);
6365
}, false);
6466

6567
// Losing focus event
66-
this.canvas.addEventListener("mouseout", function () {
68+
this.canvas.addEventListener("mouseout", () => {
6769
if (minecraft.currentScreen === null) {
6870
minecraft.displayScreen(new GuiIngameMenu());
6971
}
72+
73+
clearInterval(mouseDownInterval);
7074
});
7175

7276
// Mouse buttons
73-
document.addEventListener('mousedown', function (event) {
77+
document.addEventListener('mousedown', event => {
7478
// Create sound engine (It has to be created after user interaction)
7579
if (!minecraft.soundManager.isCreated()) {
7680
minecraft.soundManager.create(minecraft.worldRenderer);
7781
}
7882

7983
// Handle in-game mouse click
80-
if (!scope.isMobile) {
84+
if (!this.isMobile) {
8185
minecraft.onMouseClicked(event.button);
86+
87+
// Start interval to repeat the mouse event
88+
clearInterval(mouseDownInterval);
89+
mouseDownInterval = setInterval(() => minecraft.onMouseClicked(event.button), 250);
8290
}
8391

8492
// Handle mouse click on screen
8593
if (!(minecraft.currentScreen === null)) {
86-
minecraft.currentScreen.mouseClicked(event.x / scope.scaleFactor, event.y / scope.scaleFactor, event.code);
94+
minecraft.currentScreen.mouseClicked(event.x / this.scaleFactor, event.y / this.scaleFactor, event.code);
8795
}
8896
}, false);
8997

9098
// Mouse scroll
91-
document.addEventListener('wheel', function (event) {
99+
document.addEventListener('wheel', (event) => {
92100
let delta = Math.sign(event.deltaY);
93101
minecraft.onMouseScroll(delta);
94102
}, false);
95103

96104
// Keyboard interaction with screen
97-
window.addEventListener('keydown', function (event) {
105+
window.addEventListener('keydown', (event) => {
98106
if (event.code === "F11") {
99107
return; // Toggle fullscreen
100108
}
@@ -104,32 +112,43 @@ export default class GameWindow {
104112

105113
if (!(minecraft.currentScreen === null)) {
106114
// Handle key type on screen
107-
minecraft.currentScreen.keyTyped(event.code);
115+
minecraft.currentScreen.keyTyped(event.code, event.key);
108116
} else if (event.code === 'Escape') {
109117
minecraft.displayScreen(new GuiIngameMenu());
110118
} else {
111119
minecraft.onKeyPressed(event.code);
112120
}
113121
});
114122

123+
// Keyboard interaction with screen
124+
window.addEventListener('keyup', (event) => {
125+
// Prevent key
126+
event.preventDefault();
127+
128+
if (!(minecraft.currentScreen === null)) {
129+
// Handle key release on screen
130+
minecraft.currentScreen.keyReleased(event.code);
131+
}
132+
});
133+
115134
// Touch interaction
116135
let touchStart;
117-
window.addEventListener('touchstart', function (event) {
136+
window.addEventListener('touchstart', (event) => {
118137
for (let i = 0; i < event.touches.length; i++) {
119138
let touch = event.touches[i];
120139

121140
let x = touch.pageX;
122141
let y = touch.pageY;
123142

124-
let isRightHand = x > scope.wrapper.offsetWidth / 2;
143+
let isRightHand = x > this.wrapper.offsetWidth / 2;
125144

126145
if (isRightHand) {
127146
touchStart = Date.now();
128147
} else {
129-
let tileSize = scope.wrapper.offsetWidth / 8;
148+
let tileSize = this.wrapper.offsetWidth / 8;
130149

131150
let tileX = 0;
132-
let tileY = scope.wrapper.offsetHeight - tileSize * 3;
151+
let tileY = this.wrapper.offsetHeight - tileSize * 3;
133152

134153
let relX = x - tileX;
135154
let relY = y - tileY;
@@ -169,28 +188,28 @@ export default class GameWindow {
169188

170189
// Touch movement
171190
let prevTouch;
172-
window.addEventListener('touchmove', function (event) {
191+
window.addEventListener('touchmove', (event) => {
173192
for (let i = 0; i < event.touches.length; i++) {
174193
let touch = event.touches[i];
175194

176195
let x = touch.pageX;
177196
let y = touch.pageY;
178197

179198
// Right hand
180-
let isRightHand = x > scope.wrapper.offsetWidth / 2;
199+
let isRightHand = x > this.wrapper.offsetWidth / 2;
181200

182201
if (isRightHand) {
183202
// Player movement
184203
if (prevTouch) {
185-
scope.mouseMotionX = (x - prevTouch.pageX) * 10;
186-
scope.mouseMotionY = -(y - prevTouch.pageY) * 10;
204+
this.mouseMotionX = (x - prevTouch.pageX) * 10;
205+
this.mouseMotionY = -(y - prevTouch.pageY) * 10;
187206
}
188207

189208
prevTouch = touch;
190209
}
191210
}
192211
});
193-
window.addEventListener('touchend', function (event) {
212+
window.addEventListener('touchend', (event) => {
194213
// Break block
195214
if (!prevTouch && touchStart && (Date.now() - touchStart) < 1000) {
196215
minecraft.onMouseClicked(2);
@@ -204,7 +223,7 @@ export default class GameWindow {
204223
let touch = event.changedTouches[i];
205224

206225
// Left hand
207-
let isLeftHand = touch.pageX < scope.wrapper.offsetWidth / 2;
226+
let isLeftHand = touch.pageX < this.wrapper.offsetWidth / 2;
208227

209228
// Release all keys
210229
if (isLeftHand) {
@@ -326,4 +345,20 @@ export default class GameWindow {
326345
return false;
327346
}
328347

348+
close() {
349+
this.openUrl(Minecraft.URL_GITHUB);
350+
}
351+
352+
openUrl(url, newTab) {
353+
if (newTab) {
354+
window.open(url, '_blank').focus();
355+
} else {
356+
window.location = url;
357+
}
358+
}
359+
360+
async getClipboardText() {
361+
return navigator.clipboard.readText();
362+
}
363+
329364
}

0 commit comments

Comments
 (0)