move chat to web component ()

This commit is contained in:
Romain Beaumont 2021-03-14 20:34:35 +01:00 committed by GitHub
parent 332d28e1c5
commit cc1a14ea4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 250 additions and 249 deletions

View file

@ -7,16 +7,7 @@
</head> </head>
<body> <body>
<img id="crosshair" src="extra-textures/icons.png" style="display: none;"> <img id="crosshair" src="extra-textures/icons.png" style="display: none;">
<div id="chat-wrapper" class="chat-wrapper chat-display-wrapper" style="display: none;"> <chat-box id="chatbox" style="display: none;"></chat-box>
<div class="chat" id="chat">
<p>Welcome to prismarine-web-client! Chat appears here.</p>
</div>
</div>
<div id="chat-wrapper2" class="chat-wrapper chat-input-wrapper" style="display: none;">
<div class="chat" id="chat-input">
<input type="text" class="chat" id="chatinput"></input>
</div>
</div>
<hot-bar id="hotbar" style="display: none;"></hot-bar> <hot-bar id="hotbar" style="display: none;"></hot-bar>
<loading-screen id="loading-background" style="display: none;"></loading-screen> <loading-screen id="loading-background" style="display: none;"></loading-screen>
<prismarine-menu id="prismarine-menu"></prismarine-menu> <prismarine-menu id="prismarine-menu"></prismarine-menu>

View file

@ -2,6 +2,7 @@
require('./lib/menu') require('./lib/menu')
require('./lib/loading_screen') require('./lib/loading_screen')
require('./lib/hotbar') require('./lib/hotbar')
require('./lib/chat')
const net = require('net') const net = require('net')
@ -13,7 +14,6 @@ const { WorldView, Viewer } = require('prismarine-viewer/viewer')
const pathfinder = require('mineflayer-pathfinder') const pathfinder = require('mineflayer-pathfinder')
const { Vec3 } = require('vec3') const { Vec3 } = require('vec3')
global.THREE = require('three') global.THREE = require('three')
const Chat = require('./lib/chat')
const maxPitch = 0.5 * Math.PI const maxPitch = 0.5 * Math.PI
const minPitch = -0.5 * Math.PI const minPitch = -0.5 * Math.PI
@ -25,8 +25,7 @@ async function main () {
menu.style = 'display: none;' menu.style = 'display: none;'
document.getElementById('hotbar').style = 'display:block' document.getElementById('hotbar').style = 'display:block'
document.getElementById('crosshair').style = 'display:block' document.getElementById('crosshair').style = 'display:block'
document.getElementById('chat-wrapper').style = 'display:block' document.getElementById('chatbox').style = 'display:block'
document.getElementById('chat-wrapper2').style = 'display:block'
document.getElementById('loading-background').style = 'display:block' document.getElementById('loading-background').style = 'display:block'
connect(options) connect(options)
@ -36,6 +35,7 @@ async function main () {
async function connect (options) { async function connect (options) {
const loadingScreen = document.getElementById('loading-background') const loadingScreen = document.getElementById('loading-background')
const hotbar = document.getElementById('hotbar') const hotbar = document.getElementById('hotbar')
const chat = document.getElementById('chatbox')
const viewDistance = 6 const viewDistance = 6
const hostprompt = options.server const hostprompt = options.server
@ -119,7 +119,7 @@ async function connect (options) {
renderer.setSize(window.innerWidth, window.innerHeight) renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement) document.body.appendChild(renderer.domElement)
const chat = Chat.init(bot._client, renderer) chat.init(bot._client, renderer)
// Create viewer // Create viewer
const viewer = new Viewer(renderer) const viewer = new Viewer(renderer)

View file

@ -1,3 +1,5 @@
const { LitElement, html, css } = require('lit-element')
const styles = { const styles = {
black: 'color:#000000', black: 'color:#000000',
dark_blue: 'color:#0000AA', dark_blue: 'color:#0000AA',
@ -33,9 +35,71 @@ const dictionary = {
'chat.type.text': '<%s> %s' 'chat.type.text': '<%s> %s'
} }
export function init (client, renderer) { class ChatBox extends LitElement {
const chat = document.querySelector('#chat') static get styles () {
const chatInput = document.querySelector('#chatinput') return css`
.chat-wrapper {
position: fixed;
background-color: rgba(0, 0, 0, 0.5);
z-index:10;
}
.chat-display-wrapper {
bottom: calc(8px * 16);
padding: 4px;
max-height: calc(90px * 8);
width: calc(320px * 4);
}
.chat-input-wrapper {
bottom: calc(2px * 16);
width: 100%;
overflow: hidden;
background-color: rgba(0, 0, 0, 0);
}
.chat {
overflow: hidden;
color: white;
font-size: 16px;
margin: 0px;
line-height: 100%;
text-shadow: 2px 2px 0px #3f3f3f;
font-family: mojangles, minecraft, monospace;
width: 100%;
max-height: calc(90px * 8)
}
input[type=text], #chatinput {
background-color: rgba(0, 0, 0, 0.5);
display: none;
}
li {
display: block;
}
`
}
render () {
return html`
<div id="chat-wrapper" class="chat-wrapper chat-display-wrapper">
<div class="chat" id="chat">
<p>Welcome to prismarine-web-client! Chat appears here.</p>
</div>
</div>
<div id="chat-wrapper2" class="chat-wrapper chat-input-wrapper">
<div class="chat" id="chat-input">
<input type="text" class="chat" id="chatinput"></input>
</div>
</div>
`
}
init (client, renderer) {
this.inChat = false
const chat = this.shadowRoot.querySelector('#chat')
const chatInput = this.shadowRoot.querySelector('#chatinput')
const chatHistory = [] const chatHistory = []
let chatHistoryPos = 0 let chatHistoryPos = 0
@ -47,13 +111,11 @@ export function init (client, renderer) {
// Show chat // Show chat
chat.style.display = 'block' chat.style.display = 'block'
const chatState = { const self = this
inChat: false
}
// Esc event - Doesnt work with onkeypress?! // Esc event - Doesnt work with onkeypress?!
document.onkeydown = function (e) { document.onkeydown = function (e) {
if (!chatState.inChat) return if (!self.inChat) return
e = e || window.event e = e || window.event
if (e.keyCode === 27 || e.key === 'Escape' || e.key === 'Esc') { if (e.keyCode === 27 || e.key === 'Escape' || e.key === 'Esc') {
disableChat() disableChat()
@ -69,7 +131,7 @@ export function init (client, renderer) {
// Chat events // Chat events
document.onkeypress = function (e) { document.onkeypress = function (e) {
e = e || window.event e = e || window.event
if (chatState.inChat === false) { if (self.inChat === false) {
if (e.code === 'KeyT') { if (e.code === 'KeyT') {
enableChat(false) enableChat(false)
} }
@ -80,7 +142,7 @@ export function init (client, renderer) {
return false return false
} }
if (!chatState.inChat) return if (!self.inChat) return
e.stopPropagation() e.stopPropagation()
if (e.code === 'Enter') { if (e.code === 'Enter') {
chatHistory.push(chatInput.value) chatHistory.push(chatInput.value)
@ -102,7 +164,7 @@ export function init (client, renderer) {
}); */ }); */
function enableChat (isCommand) { function enableChat (isCommand) {
// Set inChat value // Set inChat value
chatState.inChat = true self.inChat = true
// Exit the pointer lock // Exit the pointer lock
document.exitPointerLock() document.exitPointerLock()
// Show chat input // Show chat input
@ -120,7 +182,7 @@ export function init (client, renderer) {
function disableChat () { function disableChat () {
// Set inChat value // Set inChat value
chatState.inChat = false self.inChat = false
// Hide chat // Hide chat
hideChat() hideChat()
@ -220,6 +282,7 @@ export function init (client, renderer) {
}) })
hideChat() hideChat()
return chatState
} }
}
window.customElements.define('chat-box', ChatBox)

View file

@ -34,16 +34,6 @@ class LoadingScreen extends LitElement {
static get styles () { static get styles () {
return css` return css`
@font-face {
font-family: minecraft;
src: url(minecraftia.woff);
}
@font-face {
font-family: mojangles;
src: url(mojangles.ttf);
}
h1 { h1 {
font-family: mojangles, minecraft, monospace; font-family: mojangles, minecraft, monospace;
} }

View file

@ -1,3 +1,8 @@
html {
height: 100%;
overflow: hidden;
}
@font-face { @font-face {
font-family: minecraft; font-family: minecraft;
src: url(minecraftia.woff); src: url(minecraftia.woff);
@ -7,16 +12,17 @@
font-family: mojangles; font-family: mojangles;
src: url(mojangles.ttf); src: url(mojangles.ttf);
} }
html {
height: 100%;
overflow: hidden;
}
body { body {
margin:0; margin:0;
padding:0; padding:0;
font-family: sans-serif; font-family: sans-serif;
background: linear-gradient(#141e30, #243b55); background: linear-gradient(#141e30, #243b55);
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
} }
canvas { canvas {
@ -27,46 +33,6 @@ canvas {
padding: 0; padding: 0;
} }
.chat-wrapper {
position: fixed;
background-color: rgba(0, 0, 0, 0.5);
}
.chat-display-wrapper {
bottom: calc(8px * 16);
padding: 4px;
max-height: calc(90px * 8);
width: calc(320px * 4);
}
.chat-input-wrapper {
bottom: calc(2px * 16);
width: 100%;
overflow: hidden;
background-color: rgba(0, 0, 0, 0);
}
.chat {
overflow: hidden;
color: white;
font-size: 16px;
margin: 0px;
line-height: 100%;
text-shadow: 2px 2px 0px #3f3f3f;
font-family: mojangles, minecraft, monospace;
width: 100%;
max-height: calc(90px * 8)
}
input[type=text], #chatinput {
background-color: rgba(0, 0, 0, 0.5);
display: none;
}
li {
display: block;
}
#crosshair { #crosshair {
image-rendering: optimizeSpeed; image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges; image-rendering: -moz-crisp-edges;
@ -82,12 +48,3 @@ li {
transform: translate(calc(-50% + 120px * 4), calc(-50% + 120px * 4)); transform: translate(calc(-50% + 120px * 4), calc(-50% + 120px * 4));
clip-path: inset(0px calc(240px * 4) calc(240px * 4) 0px); clip-path: inset(0px calc(240px * 4) calc(240px * 4) 0px);
} }
body {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}