wip: player inventory

This commit is contained in:
Bixilon 2022-02-20 17:13:01 +01:00
parent aaf4385669
commit c3e3bdc9ac
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
8 changed files with 342 additions and 11 deletions

View File

@ -19,8 +19,10 @@ import de.bixilon.minosoft.data.registries.other.containers.Container
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.atlas.Vec2iBinding import de.bixilon.minosoft.gui.rendering.gui.atlas.Vec2iBinding
import de.bixilon.minosoft.gui.rendering.gui.elements.Element import de.bixilon.minosoft.gui.rendering.gui.elements.Element
import de.bixilon.minosoft.gui.rendering.gui.elements.Pollable
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions
import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY
import glm_.vec2.Vec2i import glm_.vec2.Vec2i
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
@ -28,12 +30,13 @@ class ContainerItemsElement(
guiRenderer: GUIRenderer, guiRenderer: GUIRenderer,
val container: Container, val container: Container,
val slots: Int2ObjectOpenHashMap<Vec2iBinding>, // ToDo: Use an array? val slots: Int2ObjectOpenHashMap<Vec2iBinding>, // ToDo: Use an array?
) : Element(guiRenderer) { ) : Element(guiRenderer), Pollable {
private val itemElements: MutableMap<Int, ItemElementData> = synchronizedMapOf() private val itemElements: MutableMap<Int, ItemElementData> = synchronizedMapOf()
private var revision = -1L private var revision = -1L
init { init {
silentApply() silentApply()
this._size = calculateSize()
} }
override fun forceRender(offset: Vec2i, consumer: GUIVertexConsumer, options: GUIVertexOptions?) { override fun forceRender(offset: Vec2i, consumer: GUIVertexConsumer, options: GUIVertexOptions?) {
@ -42,13 +45,28 @@ class ContainerItemsElement(
} }
} }
override fun silentApply(): Boolean { private fun calculateSize(): Vec2i {
val size = Vec2i.EMPTY
for (slot in slots.values) {
size.x = maxOf(slot.end.x, size.x)
size.y = maxOf(slot.end.y, size.y)
}
return size
}
override fun poll(): Boolean {
val revision = container.revision val revision = container.revision
if (this.revision == revision) { if (this.revision == revision) {
return false return false
} }
this.revision = revision this.revision = revision
return true
}
override fun forceSilentApply() {
var changes = false var changes = false
for ((slot, binding) in slots) { for ((slot, binding) in slots) {
val item = container[slot] val item = container[slot]
@ -80,15 +98,10 @@ class ContainerItemsElement(
} }
if (!changes) { if (!changes) {
return false return
} }
cacheUpToDate = false cacheUpToDate = false
return true
}
override fun forceSilentApply() {
silentApply()
} }

View File

@ -62,6 +62,7 @@ class ItemElement(
override fun forceRender(offset: Vec2i, consumer: GUIVertexConsumer, options: GUIVertexOptions?) { override fun forceRender(offset: Vec2i, consumer: GUIVertexConsumer, options: GUIVertexOptions?) {
val item = item ?: return val item = item ?: return
val size = size val size = size
val textureSize = size - 1
val model = item.item.model val model = item.item.model
if (model == null) { if (model == null) {
@ -72,13 +73,13 @@ class ItemElement(
val defaultState = item.item.block.defaultState val defaultState = item.item.block.defaultState
defaultState.material.color?.let { color = it } defaultState.material.color?.let { color = it }
defaultState.blockModel?.getParticleTexture(KUtil.RANDOM, Vec3i.EMPTY)?.let { defaultState.blockModel?.getParticleTexture(KUtil.RANDOM, Vec3i.EMPTY)?.let {
element = ImageElement(guiRenderer, it, size = size) element = ImageElement(guiRenderer, it, size = textureSize)
} }
} }
(element ?: ColorElement(guiRenderer, size, color)).render(offset, consumer, options) (element ?: ColorElement(guiRenderer, textureSize, color)).render(offset, consumer, options)
} else { } else {
model.render2d(offset, consumer, options, size, item) model.render2d(offset, consumer, options, textureSize, item)
} }
val countSize = countText.size val countSize = countText.size

View File

@ -19,4 +19,6 @@ import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
interface GUIBuilder<T : GUIElement> { interface GUIBuilder<T : GUIElement> {
fun build(guiRenderer: GUIRenderer): T fun build(guiRenderer: GUIRenderer): T
fun register(guiRenderer: GUIRenderer) {}
} }

View File

@ -23,6 +23,7 @@ import de.bixilon.minosoft.gui.rendering.gui.GUIElementDrawer
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.elements.LayoutedElement import de.bixilon.minosoft.gui.rendering.gui.elements.LayoutedElement
import de.bixilon.minosoft.gui.rendering.gui.elements.Pollable import de.bixilon.minosoft.gui.rendering.gui.elements.Pollable
import de.bixilon.minosoft.gui.rendering.gui.gui.screen.container.inventory.LocalInventoryScreen
import de.bixilon.minosoft.gui.rendering.gui.gui.screen.menu.pause.PauseMenu import de.bixilon.minosoft.gui.rendering.gui.gui.screen.menu.pause.PauseMenu
import de.bixilon.minosoft.gui.rendering.gui.hud.Initializable import de.bixilon.minosoft.gui.rendering.gui.hud.Initializable
import de.bixilon.minosoft.gui.rendering.gui.hud.elements.HUDBuilder import de.bixilon.minosoft.gui.rendering.gui.hud.elements.HUDBuilder
@ -48,6 +49,11 @@ class GUIManager(
for (element in elementCache.values) { for (element in elementCache.values) {
element.init() element.init()
} }
registerDefaultElements()
}
private fun registerDefaultElements() {
LocalInventoryScreen.register(guiRenderer)
} }
override fun postInit() { override fun postInit() {

View File

@ -0,0 +1,47 @@
/*
* Minosoft
* Copyright (C) 2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.gui.gui.screen.container
import de.bixilon.minosoft.data.registries.other.containers.Container
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.atlas.AtlasElement
import de.bixilon.minosoft.gui.rendering.gui.atlas.Vec2iBinding
import de.bixilon.minosoft.gui.rendering.gui.elements.items.ContainerItemsElement
import de.bixilon.minosoft.gui.rendering.gui.elements.primitive.AtlasImageElement
import de.bixilon.minosoft.gui.rendering.gui.gui.screen.Screen
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions
import glm_.vec2.Vec2i
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
abstract class ContainerScreen(
guiRenderer: GUIRenderer,
container: Container,
background: AtlasElement,
items: Int2ObjectOpenHashMap<Vec2iBinding> = background.slots,
) : Screen(guiRenderer) {
private val containerBackground = AtlasImageElement(guiRenderer, background)
protected val containerElement = ContainerItemsElement(guiRenderer, container, items)
override fun forceRender(offset: Vec2i, consumer: GUIVertexConsumer, options: GUIVertexOptions?) {
super.forceRender(offset, consumer, options)
val centerOffset = offset + (size - containerBackground.size) / 2
containerBackground.render(centerOffset, consumer, options)
containerElement.render(centerOffset, consumer, options)
}
}

View File

@ -0,0 +1,28 @@
/*
* Minosoft
* Copyright (C) 2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.gui.gui.screen.container.inventory
import de.bixilon.minosoft.data.registries.other.containers.PlayerInventory
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.gui.screen.container.ContainerScreen
import de.bixilon.minosoft.util.KUtil.toResourceLocation
open class InventoryScreen(
guiRenderer: GUIRenderer,
val inventory: PlayerInventory,
) : ContainerScreen(
guiRenderer,
inventory,
guiRenderer.atlasManager["minecraft:inventory_base".toResourceLocation()]!!,
)

View File

@ -0,0 +1,41 @@
/*
* Minosoft
* Copyright (C) 2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.gui.gui.screen.container.inventory
import de.bixilon.minosoft.config.key.KeyAction
import de.bixilon.minosoft.config.key.KeyBinding
import de.bixilon.minosoft.config.key.KeyCodes
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.gui.GUIBuilder
import de.bixilon.minosoft.gui.rendering.gui.hud.elements.LayoutedGUIElement
import de.bixilon.minosoft.util.KUtil.toResourceLocation
class LocalInventoryScreen(guiRenderer: GUIRenderer) : InventoryScreen(guiRenderer, guiRenderer.connection.player.inventory) {
companion object : GUIBuilder<LayoutedGUIElement<LocalInventoryScreen>> {
override fun build(guiRenderer: GUIRenderer): LayoutedGUIElement<LocalInventoryScreen> {
return LayoutedGUIElement(LocalInventoryScreen(guiRenderer))
}
override fun register(guiRenderer: GUIRenderer) {
guiRenderer.renderWindow.inputHandler.registerKeyCallback("minosoft:local_inventory".toResourceLocation(), KeyBinding(
mapOf(
KeyAction.PRESS to setOf(KeyCodes.KEY_E),
),
)) { guiRenderer.gui.open(LocalInventoryScreen) }
}
}
}

View File

@ -706,5 +706,198 @@
"start": [0, 86], "start": [0, 86],
"end": [200, 106] "end": [200, 106]
} }
},
"minecraft:inventory_base": {
"0": {
"texture": "minecraft:textures/gui/container/inventory.png",
"start": [0, 0],
"end": [176, 166],
"slots": {
"0": {
"start": [154, 28],
"end": [171, 45]
},
"1": {
"start": [98, 18],
"end": [115, 35]
},
"2": {
"start": [116, 18],
"end": [133, 35]
},
"3": {
"start": [98, 36],
"end": [115, 53]
},
"4": {
"start": [116, 36],
"end": [133, 53]
},
"5": {
"start": [8, 8],
"end": [25, 25]
},
"6": {
"start": [8, 26],
"end": [25, 43]
},
"7": {
"start": [8, 44],
"end": [25, 61]
},
"8": {
"start": [8, 62],
"end": [25, 79]
},
"9": {
"start": [8, 84],
"end": [25, 101]
},
"10": {
"start": [26, 84],
"end": [43, 101]
},
"11": {
"start": [44, 84],
"end": [61, 101]
},
"12": {
"start": [62, 84],
"end": [79, 101]
},
"13": {
"start": [80, 84],
"end": [97, 101]
},
"14": {
"start": [98, 84],
"end": [115, 101]
},
"15": {
"start": [116, 84],
"end": [133, 101]
},
"16": {
"start": [134, 84],
"end": [151, 101]
},
"17": {
"start": [152, 84],
"end": [169, 101]
},
"18": {
"start": [8, 102],
"end": [25, 119]
},
"19": {
"start": [26, 102],
"end": [43, 119]
},
"20": {
"start": [44, 102],
"end": [61, 119]
},
"21": {
"start": [62, 102],
"end": [79, 119]
},
"22": {
"start": [80, 102],
"end": [97, 119]
},
"23": {
"start": [98, 102],
"end": [115, 119]
},
"24": {
"start": [116, 102],
"end": [133, 119]
},
"25": {
"start": [134, 102],
"end": [151, 119]
},
"26": {
"start": [152, 102],
"end": [169, 119]
},
"27": {
"start": [8, 120],
"end": [25, 137]
},
"28": {
"start": [26, 120],
"end": [43, 137]
},
"29": {
"start": [44, 120],
"end": [61, 137]
},
"30": {
"start": [62, 120],
"end": [79, 137]
},
"31": {
"start": [80, 120],
"end": [97, 137]
},
"32": {
"start": [98, 120],
"end": [115, 137]
},
"33": {
"start": [116, 120],
"end": [133, 137]
},
"34": {
"start": [134, 120],
"end": [151, 137]
},
"35": {
"start": [152, 120],
"end": [169, 137]
},
"36": {
"start": [8, 142],
"end": [25, 159]
},
"37": {
"start": [26, 142],
"end": [43, 159]
},
"38": {
"start": [44, 142],
"end": [61, 159]
},
"39": {
"start": [62, 142],
"end": [79, 159]
},
"40": {
"start": [80, 142],
"end": [97, 159]
},
"41": {
"start": [98, 142],
"end": [115, 159]
},
"42": {
"start": [116, 142],
"end": [133, 159]
},
"43": {
"start": [134, 142],
"end": [151, 159]
},
"44": {
"start": [152, 142],
"end": [169, 159]
},
"45": {
"start": [77, 62],
"end": [94, 79]
}
}
}
} }
} }