gui: container: take item from container

This commit is contained in:
Bixilon 2022-02-22 17:40:31 +01:00
parent c545dca618
commit b1e5524f53
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 34 additions and 12 deletions

View File

@ -12,7 +12,7 @@
*/ */
package de.bixilon.minosoft.data.inventory package de.bixilon.minosoft.data.inventory
enum class InventoryActions( enum class ContainerClickActions(
val mode: Int, val mode: Int,
val button: Int, val button: Int,
val slot: Boolean, val slot: Boolean,

View File

@ -16,6 +16,8 @@ package de.bixilon.minosoft.gui.rendering.gui.elements.items
import de.bixilon.kutil.collections.CollectionUtil.synchronizedMapOf import de.bixilon.kutil.collections.CollectionUtil.synchronizedMapOf
import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedMap import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedMap
import de.bixilon.kutil.watcher.DataWatcher.Companion.observe import de.bixilon.kutil.watcher.DataWatcher.Companion.observe
import de.bixilon.minosoft.data.inventory.ContainerClickActions
import de.bixilon.minosoft.data.inventory.stack.ItemStack
import de.bixilon.minosoft.data.registries.other.containers.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.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.atlas.Vec2iBinding import de.bixilon.minosoft.gui.rendering.gui.atlas.Vec2iBinding
@ -30,6 +32,7 @@ import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions
import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY
import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.isGreater import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.isGreater
import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.isSmaller import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.isSmaller
import de.bixilon.minosoft.protocol.packets.c2s.play.container.ContainerClickC2SP
import glm_.vec2.Vec2i import glm_.vec2.Vec2i
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
@ -129,17 +132,29 @@ class ContainerItemsElement(
override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions): Boolean { override fun onMouseAction(position: Vec2i, button: MouseButtons, action: MouseActions): Boolean {
val consumed = super<AbstractLayout>.onMouseAction(position, button, action) val consumed = super<AbstractLayout>.onMouseAction(position, button, action)
if (action != MouseActions.PRESS) { if (action != MouseActions.PRESS || (button != MouseButtons.LEFT && button != MouseButtons.RIGHT)) {
return consumed return consumed
} }
val activeElement = activeElement val activeElement = activeElement
val stack = activeElement?.stack val stack = activeElement?.stack
val containerId = renderWindow.connection.player.containers.getKey(container) ?: return consumed
var floatingItem = this.floatingItem var floatingItem = this.floatingItem
if ((floatingItem != null && !floatingItem.visible) || stack?._valid != true) { if ((floatingItem != null && floatingItem.visible) || stack?._valid != true) {
return consumed return consumed
} }
floatingItem = FloatingItem(guiRenderer, activeElement.slotId, stack) val clickAction: ContainerClickActions
val stackToFloat: ItemStack
if (button == MouseButtons.LEFT) {
stackToFloat = stack
clickAction = ContainerClickActions.LEFT_MOUSE_CLICK
} else {
stackToFloat = stack.copy(count = maxOf(stack.item.count / 2, 1))
clickAction = ContainerClickActions.RIGHT_MOUSE_CLICK
stack.item.count = stack.item.count - stackToFloat.item.count
}
renderWindow.connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, activeElement.slotId, clickAction, container.createAction(), mapOf(activeElement.slotId to stack), stack))
floatingItem = FloatingItem(guiRenderer, activeElement.slotId, stackToFloat)
this.floatingItem = floatingItem this.floatingItem = floatingItem
floatingItem.show() floatingItem.show()

View File

@ -14,7 +14,7 @@
package de.bixilon.minosoft.gui.rendering.gui.elements.items package de.bixilon.minosoft.gui.rendering.gui.elements.items
import de.bixilon.minosoft.config.key.KeyCodes import de.bixilon.minosoft.config.key.KeyCodes
import de.bixilon.minosoft.data.inventory.InventoryActions import de.bixilon.minosoft.data.inventory.ContainerClickActions
import de.bixilon.minosoft.data.inventory.stack.ItemStack import de.bixilon.minosoft.data.inventory.stack.ItemStack
import de.bixilon.minosoft.data.registries.items.block.BlockItem import de.bixilon.minosoft.data.registries.items.block.BlockItem
import de.bixilon.minosoft.data.registries.other.containers.Container import de.bixilon.minosoft.data.registries.other.containers.Container
@ -164,13 +164,13 @@ class ItemElement(
// ToDo // ToDo
when (key) { when (key) {
KeyCodes.KEY_Q -> { KeyCodes.KEY_Q -> {
val action: InventoryActions val action: ContainerClickActions
if (controlDown) { if (controlDown) {
stack?.item?.count = 0 stack?.item?.count = 0
action = InventoryActions.DROP_STACK action = ContainerClickActions.DROP_STACK
} else { } else {
stack?.item?.decreaseCount() stack?.item?.decreaseCount()
action = InventoryActions.DROP_ITEM action = ContainerClickActions.DROP_ITEM
} }
renderWindow.connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slotId, action, container.createAction(), mapOf(slotId to stack), stack)) renderWindow.connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slotId, action, container.createAction(), mapOf(slotId to stack), stack))
} }

View File

@ -233,6 +233,10 @@ class GUIManager(
} }
fun pop() { fun pop() {
if (guiRenderer.dragged.element != null) {
guiRenderer.dragged.element = null
return
}
val previous = elementOrder.removeFirstOrNull() ?: return val previous = elementOrder.removeFirstOrNull() ?: return
previous.onClose() previous.onClose()
if (elementOrder.isEmpty()) { if (elementOrder.isEmpty()) {

View File

@ -33,9 +33,12 @@ class DraggedManager(
return return
} }
val position = guiRenderer.currentCursorPosition val position = guiRenderer.currentCursorPosition
field?.element?.onDragEnd(position, null) // ToDo val previous = field
previous?.element?.onDragEnd(position, guiRenderer.gui.onDragLeave(previous.element))
field = value field = value
element?.element?.onDragStart(position, null) // ToDo guiRenderer.gui.onMouseMove(Vec2i(-1, -1)) // move mouse ot
value?.element?.onDragStart(position, guiRenderer.gui.onDragMove(position, value.element))
applyCursor() applyCursor()
} }
override var lastTickTime: Long = -1L override var lastTickTime: Long = -1L

View File

@ -12,7 +12,7 @@
*/ */
package de.bixilon.minosoft.protocol.packets.c2s.play.container package de.bixilon.minosoft.protocol.packets.c2s.play.container
import de.bixilon.minosoft.data.inventory.InventoryActions import de.bixilon.minosoft.data.inventory.ContainerClickActions
import de.bixilon.minosoft.data.inventory.stack.ItemStack import de.bixilon.minosoft.data.inventory.stack.ItemStack
import de.bixilon.minosoft.protocol.packets.c2s.PlayC2SPacket import de.bixilon.minosoft.protocol.packets.c2s.PlayC2SPacket
import de.bixilon.minosoft.protocol.packets.factory.LoadPacket import de.bixilon.minosoft.protocol.packets.factory.LoadPacket
@ -27,7 +27,7 @@ class ContainerClickC2SP(
val containerId: Int, val containerId: Int,
val revision: Int, val revision: Int,
val slot: Int, val slot: Int,
val action: InventoryActions, val action: ContainerClickActions,
val actionId: Int, val actionId: Int,
val next: Map<Int, ItemStack?>, val next: Map<Int, ItemStack?>,
val clickedItem: ItemStack?, val clickedItem: ItemStack?,