Skip to content

Commit

Permalink
fixed searchbar
Browse files Browse the repository at this point in the history
better slot drag behavior
added put all button
  • Loading branch information
deirn committed Jul 5, 2020
1 parent 1737c9d commit 8459b2f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ yarn_mappings = 1.16.1+build.10
loader_version = 0.8.8+build.202

# Mod properties
mod_version = 0.2.6
mod_version = 0.2.7
maven_group = com.github.badasintended
archives_base_name = slotlink

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import badasintended.slotlink.common.SortBy
import badasintended.slotlink.common.positionOf
import badasintended.slotlink.common.sizeOf
import badasintended.slotlink.common.slotAction
import badasintended.slotlink.network.NetworkRegistry
import badasintended.slotlink.screen.AbstractRequestScreenHandler
import io.netty.buffer.Unpooled
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry
import net.minecraft.client.MinecraftClient
import net.minecraft.client.util.math.MatrixStack
import net.minecraft.item.ItemStack
import net.minecraft.network.PacketByteBuf
import net.minecraft.text.TranslatableText
import net.minecraft.util.registry.Registry
import spinnery.common.utility.StackUtilities.equalItemAndTag
Expand Down Expand Up @@ -50,6 +54,7 @@ abstract class AbstractRequestScreen<H : AbstractRequestScreenHandler>(c: H) : M
private val titleLabel: WTranslatableLabel
private val craftingLabel: WTranslatableLabel
private val playerInvLabel: WTranslatableLabel
private val playerInvSlots = arrayListOf<WPlayerSlot>()
private val scrollArea: WMouseArea
private val scrollbar: WFakeScrollbar
private val viewedSlots = arrayListOf<WMultiSlot>()
Expand Down Expand Up @@ -85,7 +90,7 @@ abstract class AbstractRequestScreen<H : AbstractRequestScreenHandler>(c: H) : M

// Crafting clear button
main.createChild(
{ WCraftingClearButton({ drawClearTooltip() }, { sort(lastSort, lastFilter) }) },
{ WPutButton({ drawClearTooltip() }, { onClearButtonClick() }) },
positionOf(craftingLabel, -6, 10),
sizeOf(6)
)
Expand Down Expand Up @@ -119,6 +124,13 @@ abstract class AbstractRequestScreen<H : AbstractRequestScreenHandler>(c: H) : M
)
playerInvLabel.setHidden<W>(hideLabel)

// that `move all to inventories` button
main.createChild(
{ WPutButton({ drawPutAllTooltip() }, { onPutAllButtonClick() }) },
positionOf(playerInvLabel, 155, 4),
sizeOf(6)
)

for (i in 0 until 27) {
val slot = main.createChild(
{ WPlayerSlot { sort(lastSort, lastFilter) } },
Expand All @@ -127,6 +139,7 @@ abstract class AbstractRequestScreen<H : AbstractRequestScreenHandler>(c: H) : M
)
slot.setInventoryNumber<WSlot>(0)
slot.setSlotNumber<WSlot>(i + 9)
playerInvSlots.add(slot)
}

for (i in 0 until 9) {
Expand All @@ -137,6 +150,7 @@ abstract class AbstractRequestScreen<H : AbstractRequestScreenHandler>(c: H) : M
)
slot.setInventoryNumber<WSlot>(0)
slot.setSlotNumber<WSlot>(i)
playerInvSlots.add(slot)
}

scrollArea = main.createChild(
Expand Down Expand Up @@ -174,7 +188,7 @@ abstract class AbstractRequestScreen<H : AbstractRequestScreenHandler>(c: H) : M

searchBar = main.createChild(
{ WSearchBar({ sort(lastSort, it) }, { drawSearchTooltip() }) },
positionOf(scrollArea, 0, (slotSize + 3), 1),
positionOf(scrollArea, 0, (slotSize + 3)),
sizeOf(146, 18)
)

Expand All @@ -191,6 +205,20 @@ abstract class AbstractRequestScreen<H : AbstractRequestScreenHandler>(c: H) : M

}

private fun onClearButtonClick() {
c.clearCraft()
ClientSidePacketRegistry.INSTANCE.sendToServer(NetworkRegistry.CRAFT_CLEAR, PacketByteBuf(Unpooled.buffer()))
sort(lastSort, lastFilter)
}

private fun onPutAllButtonClick() {
val filledSlots = playerInvSlots.filterNot { it.stack.isEmpty }
filledSlots.forEach { slot ->
slotAction(c, slot.slotNumber, slot.inventoryNumber, 0, QUICK_MOVE, c.player)
}
sort(lastSort, lastFilter)
}

private fun onSlotAreaClick() {
if (!slotActionPerformed) {
slotAction(c, 0, -2, 0, PICKUP, c.player)
Expand Down Expand Up @@ -320,6 +348,8 @@ abstract class AbstractRequestScreen<H : AbstractRequestScreenHandler>(c: H) : M

private fun drawClearTooltip() = drawTooltip("block.slotlink.request.craft.clearTooltip")

private fun drawPutAllTooltip() = drawTooltip("block.slotlink.request.putAllTooltip")

private fun drawTooltip(vararg translationKeys: String) {
val client = MinecraftClient.getInstance()
val mouse = client.mouse
Expand Down Expand Up @@ -352,7 +382,7 @@ abstract class AbstractRequestScreen<H : AbstractRequestScreenHandler>(c: H) : M
scrollArea.setSize<W>(sizeOf(162, slotSize))
scrollbar.setSize<W>(sizeOf(14, slotSize))
slotArea.setSize<W>(sizeOf(144, slotSize))
searchBar.setPosition<W>(positionOf(scrollArea, 0, slotSize + 3, 1))
searchBar.setPosition<W>(positionOf(scrollArea, 0, slotSize + 3))

sort(lastSort, lastFilter)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ package badasintended.slotlink.client.gui.widget

import badasintended.slotlink.Mod
import badasintended.slotlink.common.spinneryId
import badasintended.slotlink.network.NetworkRegistry.CRAFT_CLEAR
import badasintended.slotlink.screen.AbstractRequestScreenHandler
import io.netty.buffer.Unpooled
import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry.INSTANCE
import net.minecraft.client.render.VertexConsumerProvider
import net.minecraft.client.util.math.MatrixStack
import net.minecraft.network.PacketByteBuf
import spinnery.client.render.BaseRenderer
import spinnery.common.registry.ThemeRegistry
import spinnery.widget.WAbstractButton
Expand All @@ -19,9 +14,9 @@ import spinnery.widget.api.Style
import kotlin.math.floor

@Environment(EnvType.CLIENT)
class WCraftingClearButton(
class WPutButton(
private val tooltip: () -> Unit,
private val sort: () -> Unit
private val click: () -> Unit
) : WButton() {

override fun draw(matrices: MatrixStack, provider: VertexConsumerProvider.Immediate) {
Expand All @@ -34,18 +29,13 @@ class WCraftingClearButton(
val slotStyle = Style.of(ThemeRegistry.getStyle(theme, spinneryId("slot")))
val tint = slotStyle.asColor("background.unfocused")

BaseRenderer.drawTexturedQuad(matrices, provider, x, y, z, w, h, tint, Mod.id("textures/gui/clear.png"))
BaseRenderer.drawTexturedQuad(matrices, provider, x, y, z, w, h, tint, Mod.id("textures/gui/put.png"))

if (isFocused) tooltip.invoke()
}

override fun <W : WAbstractButton> setLowered(toggleState: Boolean): W {
if (toggleState) {
(`interface`.container as AbstractRequestScreenHandler).clearCraft()
INSTANCE.sendToServer(CRAFT_CLEAR, PacketByteBuf(Unpooled.buffer()))
sort.invoke()
}

if (toggleState) click.invoke()
return super.setLowered(toggleState)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.util.math.BlockPos
import spinnery.common.registry.NetworkRegistry.SLOT_UPDATE_PACKET
import spinnery.common.registry.NetworkRegistry.createSlotUpdatePacket
import spinnery.common.utility.MutablePair
import spinnery.common.utility.StackUtilities
import spinnery.widget.WSlot
import spinnery.widget.api.Action
import spinnery.widget.api.Action.PICKUP_ALL
import spinnery.widget.api.Action.QUICK_MOVE
import spinnery.widget.api.Action.Subtype.FROM_SLOT_TO_CURSOR_CUSTOM_FULL_STACK
import spinnery.widget.api.Action.Subtype.FROM_SLOT_TO_SLOT_CUSTOM_FULL_STACK
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set


abstract class AbstractRequestScreenHandler(syncId: Int, player: PlayerEntity, buf: PacketByteBuf) :
ModScreenHandler(syncId, player) {
Expand Down Expand Up @@ -51,6 +58,9 @@ abstract class AbstractRequestScreenHandler(syncId: Int, player: PlayerEntity, b

private val context: ScreenHandlerContext = ScreenHandlerContext.create(player.world, blockPos)

private val fixedSplitSlots = linkedSetOf<WSlot>()
private val fixedSingleSlots = linkedSetOf<WSlot>()

init {
for (i in 0 until totalInventory) inventoryPos.add(buf.readBlockPos())

Expand Down Expand Up @@ -234,6 +244,54 @@ abstract class AbstractRequestScreenHandler(syncId: Int, player: PlayerEntity, b
}
}

/**
* Opened a PR to spinnery but i'm not patient
*
* TODO: delete if merged
*/
override fun onSlotDrag(slotNumber: IntArray, inventoryNumber: IntArray, action: Action) {
val slots: MutableSet<WSlot> = LinkedHashSet()

for (i in slotNumber.indices) {
val slot = getInterface().getSlot<WSlot>(inventoryNumber[i], slotNumber[i])
if (slot != null) slots.add(slot)
}

if (slots.isEmpty()) return

val split = if (action.isSplit) (playerInventory.cursorStack.count / slots.size).coerceAtLeast(1) else 1
var stackA = if (action.isPreview) playerInventory.cursorStack.copy() else playerInventory.cursorStack

if (stackA.isEmpty) return

for (slotA in slots) {
if (slotA.refuses(stackA)) continue
val stackB: ItemStack = if (action.isPreview) slotA.stack.copy() else slotA.stack

val stacks: MutablePair<ItemStack, ItemStack> =
StackUtilities.merge(stackA, stackB, split, stackA.maxCount.coerceAtMost(split))
if (action.isPreview) {
previewCursorStack = stacks.first.copy()
slotA.setPreviewStack<WSlot>(stacks.second.copy())
} else {
stackA = stacks.first
previewCursorStack = ItemStack.EMPTY
slotA.setStack(stacks.second)
}
}
}

/**
* TODO: delete if merged
*/
override fun getDragSlots(mouseButton: Int): MutableSet<WSlot>? {
return when (mouseButton) {
0 -> fixedSplitSlots
1 -> fixedSingleSlots
else -> null
}
}

override fun onContentChanged(inventory: Inventory) {
if ((inventory == craftingInv)) {
craftItem()
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/slotlink/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"block.slotlink.request.search.tooltip1" : "@: Search via identifier",
"block.slotlink.request.search.tooltip2" : "#: Search via datapack tag",
"block.slotlink.request.craft.clearTooltip" : "Clear",
"block.slotlink.request.putAllTooltip" : "Put all to network",
"block.slotlink.request.tooltip" : "Storage network's interface for taking & adding items.",
"block.slotlink.cable" : "Cable",
"block.slotlink.cable.tooltip" : "Previously called as The Pipe.",
Expand Down

0 comments on commit 8459b2f

Please sign in to comment.