prismarine-web-client-mirror/lib/hotbar.js

142 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-03-14 14:33:41 -04:00
const { LitElement, html, css } = require('lit-element')
2021-03-14 14:56:54 -04:00
const invsprite = require('./invsprite.json')
2021-03-14 14:33:41 -04:00
class HotBar extends LitElement {
updated (changedProperties) {
if (changedProperties.has('bot')) {
// inventory listener
this.bot.on('spawn', () => {
this.init()
})
}
}
init () {
this.reloadHotbar()
this.reloadHotbarSelected(0)
document.addEventListener('wheel', (e) => {
const newSlot = ((this.bot.quickBarSlot + Math.sign(e.deltaY)) % 9 + 9) % 9
this.reloadHotbarSelected(newSlot)
})
this.bot.inventory.on('updateSlot', (slot, oldItem, newItem) => {
if (slot >= this.bot.inventory.hotbarStart + 9) return
if (slot < this.bot.inventory.hotbarStart) return
2021-03-14 14:56:54 -04:00
const sprite = newItem ? invsprite[newItem.name] : invsprite.air
const slotImage = this.shadowRoot.getElementById('hotbar-' + (slot - this.bot.inventory.hotbarStart))
slotImage.style['background-position-x'] = `-${sprite.x * 2}px`
slotImage.style['background-position-y'] = `-${sprite.y * 2}px`
2021-03-15 16:17:33 -04:00
slotImage.innerHTML = newItem?.count > 1 ? newItem.count : ''
2021-03-14 14:33:41 -04:00
})
}
async reloadHotbar () {
for (let i = 0; i < 9; i++) {
2021-03-14 14:56:54 -04:00
const item = this.bot.inventory.slots[this.bot.inventory.hotbarStart + i]
const sprite = item ? invsprite[item.name] : invsprite.air
const slotImage = this.shadowRoot.getElementById('hotbar-' + i)
slotImage.style['background-position-x'] = `-${sprite.x * 2}px`
slotImage.style['background-position-y'] = `-${sprite.y * 2}px`
2021-03-15 16:17:33 -04:00
slotImage.innerHTML = item?.count > 1 ? item.count : ''
2021-03-14 14:33:41 -04:00
}
}
async reloadHotbarSelected (slot) {
const planned = (20 * 4 * slot) + 'px'
this.shadowRoot.getElementById('hotbar-highlight').style.marginLeft = planned
this.bot.setQuickBarSlot(slot)
}
static get properties () {
return {
bot: { type: Object },
viewerVersion: { type: String }
}
}
static get styles () {
return css`
2021-03-15 16:17:33 -04:00
#hotbar-wrapper {
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: -o-crisp-edges;
image-rendering: pixelated;
-ms-interpolation-mode: nearest-neighbor;
}
2021-03-14 14:33:41 -04:00
#hotbar-image {
2021-03-15 16:17:33 -04:00
position: absolute;
top: 100%;
left: 50%;
height: calc(256px * 4);
width: calc(256px * 4);
transform: translate(calc(-182px * 2), calc(-22px * 4));
clip-path: inset(0px calc(74px * 4) calc(234px * 4) 0px);
2021-03-14 14:33:41 -04:00
}
2021-03-15 16:17:33 -04:00
2021-03-14 14:33:41 -04:00
#hotbar-items-wrapper {
2021-03-15 16:17:33 -04:00
position: absolute;
top: 100%;
left: 50%;
height: calc(256px * 4);
width: calc(256px * 4);
transform: translate(calc(-182px * 2), calc(-22px * 4));
clip-path: inset(0px calc(74px * 4) calc(234px * 4) 0px);
2021-03-14 14:33:41 -04:00
}
#hotbar-highlight {
2021-03-15 16:17:33 -04:00
position: absolute;
top: 100%;
left: 50%;
height: calc(256px * 4);
width: calc(256px * 4);
margin-left: calc(20px * 4 * 4); /* EDIT THIS TO CHANGE WHICH SLOT IS SELECTED */
transform: translate(calc((-24px * 2) - (20px * 4 * 4) ), calc((-22px * 4) + (-24px * 4) + 4px)); /* first need to translate up to account for clipping, then account for size of image, then 1px to center vertically over the image*/
clip-path: inset(calc(22px * 4) calc(232px * 4) calc(210px * 4) 0px);
2021-03-14 14:33:41 -04:00
}
.hotbar-item {
2021-03-15 16:17:33 -04:00
display: inline-block;
height: 32px;
width: 64px;
margin-top: 12px;
margin-left: 12px;
margin-right: 4.25px;
background-image: url('invsprite.png');
background-size: 2048px auto;
text-align: right;
font-size: 32px;
vertical-align: top;
padding-top: 32px;
color: #ffffff;
text-shadow: 2px 2px 0px #3f3f3f;
font-family: mojangles, minecraft, monospace;
2021-03-14 14:33:41 -04:00
}
`
}
render () {
return html`
<div id="hotbar-wrapper">
<img id="hotbar-image" src="textures/1.16.4/gui/widgets.png">
<img id="hotbar-highlight" src="textures/1.16.4/gui/widgets.png">
<div id="hotbar-items-wrapper">
2021-03-15 16:17:33 -04:00
<div class="hotbar-item" id="hotbar-0"></div
><div class="hotbar-item" id="hotbar-1"></div
><div class="hotbar-item" id="hotbar-2"></div
><div class="hotbar-item" id="hotbar-3"></div
><div class="hotbar-item" id="hotbar-4"></div
><div class="hotbar-item" id="hotbar-5"></div
><div class="hotbar-item" id="hotbar-6"></div
><div class="hotbar-item" id="hotbar-7"></div
><div class="hotbar-item" id="hotbar-8"></div>
2021-03-14 14:33:41 -04:00
</div>
</div>
`
}
}
window.customElements.define('hot-bar', HotBar)