Skip to content

Commit

Permalink
Updated and optimized GUI 1.1.9
Browse files Browse the repository at this point in the history
Updated and optimized GUI 1.1.9 + new blocks
  • Loading branch information
Kleeat committed Oct 22, 2023
2 parents 5b601af + 578af07 commit d8ddcc7
Show file tree
Hide file tree
Showing 33 changed files with 283 additions and 105 deletions.
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

</body>

<!-- Font -->
<script src="src/js/net/minecraft/util/FontLoader.js"></script>

<!-- Libraries -->
<script type="module" src="libraries/pako.es5.min.js"></script>
<script type="module" src="libraries/aes.js"></script>
Expand Down
1 change: 1 addition & 0 deletions src/js/net/minecraft/client/GameSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default class GameSettings {
this.keyCrouching = 'ShiftLeft';
this.keySprinting = 'ControlLeft';
this.keyTogglePerspective = 'F5';
this.keyToggleDebug = 'F3'
this.keyOpenChat = 'KeyT';
this.keyOpenInventory = 'KeyE';
this.keyPlayerList = 'Tab';
Expand Down
5 changes: 5 additions & 0 deletions src/js/net/minecraft/client/GameWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ export default class GameWindow {
if (event.key !== 'F11') {
event.preventDefault();
}
else {
this.updateWindowSize();
}

// Ignore key input if mouse is not inside window
if (!this.mouseInsideWindow) {
Expand Down Expand Up @@ -379,6 +382,8 @@ export default class GameWindow {
if (this.canvasDebug.width !== this.canvas.width || this.canvasDebug.height !== this.canvas.height) {
this.canvasDebug.width = this.canvas.width;
this.canvasDebug.height = this.canvas.height;
let ctx = this.canvasDebug.getContext('2d');
ctx.scale(this.scaleFactor, this.scaleFactor);
}

if (this.canvasPlayerList.width !== this.canvas.width || this.canvasPlayerList.height !== this.canvas.height) {
Expand Down
15 changes: 12 additions & 3 deletions src/js/net/minecraft/client/Minecraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import UUID from "../util/UUID.js";
import FocusStateType from "../util/FocusStateType.js";
import Session from "../util/Session.js";
import PlayerControllerMultiplayer from "./network/controller/PlayerControllerMultiplayer.js";
import Splash from "../../../../resources/splashes.json" assert { type: 'json' };

export default class Minecraft {

Expand Down Expand Up @@ -56,6 +57,9 @@ export default class Minecraft {
this.settings = new GameSettings();
this.settings.load();

// Persistent splash
this.splashText = this.getSpashText();

// Load session from settings
if (this.settings.session === null) {
let username = "Player" + Math.floor(Math.random() * 100);
Expand Down Expand Up @@ -354,7 +358,7 @@ export default class Minecraft {
}

// Toggle debug overlay
if (button === "F3") {
if (button === this.settings.keyToggleDebug) {
this.settings.debugOverlay = !this.settings.debugOverlay;
this.settings.save();
}
Expand Down Expand Up @@ -385,7 +389,7 @@ export default class Minecraft {
hitResult.x + 0.5,
hitResult.y + 0.5,
hitResult.z + 0.5,
1.0,
2.0,
1.0
);

Expand Down Expand Up @@ -455,7 +459,7 @@ export default class Minecraft {
hitResult.x + 0.5,
hitResult.y + 0.5,
hitResult.z + 0.5,
1.0,
2.0,
sound.getPitch() * 0.8
);
}
Expand Down Expand Up @@ -527,4 +531,9 @@ export default class Minecraft {
context.drawImage(image, 0, 0, image.width, image.height);
return new THREE.CanvasTexture(canvas);
}

getSpashText() {
let i = Math.floor(Math.random() * Splash.splashes.length);
return Splash.splashes[i];
}
}
2 changes: 1 addition & 1 deletion src/js/net/minecraft/client/entity/PlayerEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export default class PlayerEntity extends EntityLiving {

// Play sound
if (!block.isLiquid()) {
this.minecraft.soundManager.playSound(sound.getStepSound(), this.x, this.y, this.z, 0.15, sound.getPitch());
this.minecraft.soundManager.playSound(sound.getStepSound(), this.x, this.y, this.z, 0.25, sound.getPitch());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/net/minecraft/client/gui/Gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class Gui {
}

getStringWidth(stack, string) {
return this.minecraft.fontRenderer.getStringWidth(string);
return this.minecraft.fontRenderer.getStringWidth(stack, string);
}

drawRect(stack, left, top, right, bottom, color, alpha = 1) {
Expand Down
6 changes: 5 additions & 1 deletion src/js/net/minecraft/client/gui/overlay/IngameOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export default class IngameOverlay extends Gui {

// Render debug canvas on stack
if (this.minecraft.settings.debugOverlay) {
stack.drawImage(this.window.canvasDebug, 0, 0);
let canvasDebug = this.window.canvasDebug;
let canvas = this.window.canvas;
let debugHeight = canvasDebug.height * this.window.scaleFactor;
let debugWidth = canvasDebug.width * this.window.scaleFactor;
stack.drawImage(canvasDebug, 0, 0, debugWidth, debugHeight, 0, 0, canvas.width, canvas.height);
}

// Render player list
Expand Down
1 change: 0 additions & 1 deletion src/js/net/minecraft/client/gui/screens/GuiContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default class GuiContainer extends GuiScreen {
drawScreen(stack, mouseX, mouseY, partialTicks) {
this.drawDefaultBackground(stack);
this.drawInventoryBackground(stack);
this.drawString(stack, "Creative Inventory", this.x + 8, this.y + 6, 0x404040);

// Rebuild items
if (this.container.dirty) {
Expand Down
16 changes: 10 additions & 6 deletions src/js/net/minecraft/client/gui/screens/GuiControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class GuiControls extends GuiScreen {

let settings = this.minecraft.settings;

let y = this.height / 2 - 50;
let y = this.height / 2 - 74;
this.buttonList.push(new GuiSliderButton("Mouse Sensitivity", settings.sensitivity, 50, 150, this.width / 2 - 100, y, 200, 20, value => {
settings.sensitivity = value;
}).setDisplayNameBuilder(function (name, value) {
Expand All @@ -31,19 +31,23 @@ export default class GuiControls extends GuiScreen {
settings.keySprinting = key;
}));

this.buttonList.push(new GuiKeyButton("Toggle Perspective", settings.keyTogglePerspective, this.width / 2 - 100, y + 24 * 2, 200, 20, key => {
this.buttonList.push(new GuiKeyButton("Toggle Debug", settings.keyToggleDebug, this.width / 2 - 100, y + 24 * 2, 200, 20, key => {
settings.keyToggleDebug = key;
}));

this.buttonList.push(new GuiKeyButton("Toggle Perspective", settings.keyTogglePerspective, this.width / 2 - 100, y + 24 * 3, 200, 20, key => {
settings.keyTogglePerspective = key;
}));

this.buttonList.push(new GuiKeyButton("Open Chat", settings.keyOpenChat, this.width / 2 - 100, y + 24 * 3, 200, 20, key => {
this.buttonList.push(new GuiKeyButton("Open Chat", settings.keyOpenChat, this.width / 2 - 100, y + 24 * 4, 200, 20, key => {
settings.keyOpenChat = key;
}));

this.buttonList.push(new GuiKeyButton("Open Inventory", settings.keyOpenInventory, this.width / 2 - 100, y + 24 * 4, 200, 20, key => {
this.buttonList.push(new GuiKeyButton("Open Inventory", settings.keyOpenInventory, this.width / 2 - 100, y + 24 * 5, 200, 20, key => {
settings.keyOpenInventory = key;
}));

this.buttonList.push(new GuiButton("Done", this.width / 2 - 100, y + 130, 200, 20, () => {
this.buttonList.push(new GuiButton("Done", this.width / 2 - 100, y + 154, 200, 20, () => {
this.minecraft.displayScreen(this.previousScreen);
}));
}
Expand All @@ -53,7 +57,7 @@ export default class GuiControls extends GuiScreen {
this.drawDefaultBackground(stack);

// Title
this.drawCenteredString(stack, "Controls", this.width / 2, 50);
this.drawCenteredString(stack, "Controls", this.width / 2, 40);

super.drawScreen(stack, mouseX, mouseY, partialTicks);
}
Expand Down
8 changes: 4 additions & 4 deletions src/js/net/minecraft/client/gui/screens/GuiMainMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ export default class GuiMainMenu extends GuiScreen {
super();

this.panoramaTimer = 0;
this.splashText = "Minecraft written in JavaScript!";
this.splashText = '';
}

init() {
super.init();
this.textureLogo = this.getTexture("gui/title/minecraft.png");
this.splashText = this.minecraft.splashText;

let y = this.height / 4 + 48;

Expand Down Expand Up @@ -84,16 +85,15 @@ export default class GuiMainMenu extends GuiScreen {
}

drawLogo(stack, x, y) {
this.drawSprite(stack, this.textureLogo, 0, 0, 155, 44, x, y, 155, 44);
this.drawSprite(stack, this.textureLogo, 0, 45, 155, 44, x + 155, y, 155, 44);
this.drawSprite(stack, this.textureLogo, 0, 0, 274, 51, x, y, 274, 51);
}

drawSplash(stack) {
let f = 1.8 - Math.abs(Math.sin((new Date().getTime() % 1000) / 1000.0 * Math.PI * 2.0) * 0.1);
f = f * 100.0 / (this.getStringWidth(stack, this.splashText) + 32);

stack.save();
stack.translate((this.width / 2 + 90), 70.0, 0.0);
stack.translate((this.width / 2 + 120), 80.0, 0.0);
stack.rotate(MathHelper.toRadians(-20));
stack.scale(f, f, f);

Expand Down
2 changes: 1 addition & 1 deletion src/js/net/minecraft/client/gui/screens/GuiOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class GuiOptions extends GuiScreen {
this.drawDefaultBackground(stack);

// Title
this.drawCenteredString(stack, "Settings", this.width / 2, 50);
this.drawCenteredString(stack, "Settings", this.width / 2, 40);

super.drawScreen(stack, mouseX, mouseY, partialTicks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class GuiContainerCreative extends GuiContainer {
}

drawTitle(stack) {
this.drawString(stack, "Creative Inventory", this.x + 8, this.y + 6, 0xff404040, false);
this.drawString(stack, "Creative Inventory", this.x + 8, this.y + 6, 0x404040, false);
}

drawInventoryBackground(stack) {
Expand Down
96 changes: 15 additions & 81 deletions src/js/net/minecraft/client/render/gui/FontRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import MathHelper from "../../../util/MathHelper.js";

export default class FontRenderer {

static FONT_HEIGHT = 9;
static FONT_HEIGHT = 8;

static BITMAP_SIZE = 16;
static FIELD_SIZE = 8;
Expand Down Expand Up @@ -52,59 +52,21 @@ export default class FontRenderer {
}

drawString(stack, string, x, y, color = -1, shadow = true) {
if (!this.isSafari && shadow) { // TODO Fix filter on Safari
if (shadow) {
this.drawStringRaw(stack, string, x + 1, y + 1, color, true);
}
this.drawStringRaw(stack, string, x, y, color);
this.drawStringRaw(stack, string, x, y, color, false);
}

drawStringRaw(stack, string, x, y, color = -1, isShadow = false) {
stack.save();

// Set color
if (color !== -1 || isShadow) {
this.setColor(stack, color, isShadow);
}

let alpha = ((color & 0xFF000000) >>> 24) / 255;

// For each character
for (let i = 0; i < string.length; i++) {
let character = string[i];
let index = FontRenderer.CHAR_INDEX_LOOKUP.indexOf(character);
let code = character.charCodeAt(0);

// Handle color codes if character is &
if (character === FontRenderer.COLOR_PREFIX && i !== string.length - 1) {
// Get the next character
let nextCharacter = string[i + 1];

// Change color of string
this.setColor(stack, this.getColorOfCharacter(nextCharacter), isShadow);

// Skip the color code for rendering
i += 1;
continue;
}
this.setColor(stack, color, isShadow);

// Get character offset in bitmap
let textureOffsetX = index % FontRenderer.BITMAP_SIZE * FontRenderer.FIELD_SIZE;
let textureOffsetY = Math.floor(index / FontRenderer.BITMAP_SIZE) * FontRenderer.FIELD_SIZE;

// Draw character
Gui.drawSprite(
stack,
this.texture,
textureOffsetX, textureOffsetY,
FontRenderer.FIELD_SIZE, FontRenderer.FIELD_SIZE,
Math.floor(x), Math.floor(y),
FontRenderer.FIELD_SIZE, FontRenderer.FIELD_SIZE,
alpha
);

// Increase drawing cursor
x += this.charWidths[code];
}
// Draw string
stack.font = "8px Minecraft";
stack.fillText(string, x, y+6);

stack.restore();
}
Expand All @@ -121,23 +83,9 @@ export default class FontRenderer {
return r << 16 | g << 8 | b;
}

getStringWidth(string) {
let length = 0;

// For each character
for (let i = 0; i < string.length; i++) {

// Check for color code
if (string[i] === FontRenderer.COLOR_PREFIX) {
// Skip the next character
i++;
} else {
// Add the width of the character
let code = string[i].charCodeAt(0);
length += this.charWidths[code];
}
}
return length;
getStringWidth(stack, string) {
stack.font = "8px Minecraft";
return stack.measureText(string).width;
}


Expand All @@ -150,34 +98,20 @@ export default class FontRenderer {
}

setColor(stack, color, isShadow = false) {
let a = 255;
if (isShadow) {
color = (color & 0xFCFCFC) >> 2;
a = 127;
}

let r = (color & 0xFF0000) >> 16;
let g = (color & 0x00FF00) >> 8;
let b = (color & 0x0000FF);
let hsv = MathHelper.rgb2hsv(r, g, b);
let hue = hsv[0] + 270;
let saturation = hsv[1];
let brightness = hsv[2] / 255 * 100;

// TODO fix colors
let saturate1 = saturation * 1000;
let saturate2 = saturation * 5000;
let saturate3 = saturation * 100;

if (!this.isSafari) { // TODO Fix filter on Safari
stack.filter = "sepia()"
+ " saturate(" + saturate1 + "%)"
+ " hue-rotate(" + hue + "deg)"
+ " saturate(" + saturate2 + "%)"
+ " brightness(" + brightness + "%)"
+ " saturate(" + saturate3 + "%)";
}

stack.fillStyle = `rgba(${r},${g},${b},${a})`;
}

listFormattedStringToWidth(text, wrapWidth) {
return text.split("\n"); // TODO Implement wrap logic
return text.split("\n"); // TODO: Implement wrap logic
}
}
2 changes: 1 addition & 1 deletion src/js/net/minecraft/client/render/gui/ScreenRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class ScreenRenderer {
}

getLimitedScaleFactor() {
return Math.min(this.window.scaleFactor, 4);
return Math.min(this.window.scaleFactor, 5);
}

}
3 changes: 2 additions & 1 deletion src/js/net/minecraft/client/sound/SoundManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export default class SoundManager {

// Create sound
let sound = new THREE.PositionalAudio(this.audioListener);
sound.setDistanceModel('inverse');
sound.setRefDistance(0.1);
sound.setRolloffFactor(6);
sound.setRolloffFactor(0.1);
sound.setFilter(sound.context.createBiquadFilter());
sound.setVolume(0);

Expand Down

0 comments on commit d8ddcc7

Please sign in to comment.