-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
218 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/main/kotlin/dev/nyon/autodrop/config/screen/ArchiveItemsWidget.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package dev.nyon.autodrop.config.screen | ||
|
||
import dev.nyon.autodrop.config.Archive | ||
import dev.nyon.autodrop.config.ItemIdentificator | ||
import dev.nyon.autodrop.extensions.screenComponent | ||
import net.minecraft.client.gui.GuiGraphics | ||
import net.minecraft.client.gui.components.Button | ||
import net.minecraft.client.gui.components.ObjectSelectionList | ||
import net.minecraft.core.registries.BuiltInRegistries | ||
import net.minecraft.network.chat.Component | ||
import net.minecraft.world.item.Item | ||
import net.minecraft.world.item.ItemStack | ||
import net.minecraft.world.item.Items | ||
import kotlin.math.max | ||
import dev.nyon.autodrop.minecraft as internalMinecraft | ||
|
||
class ArchiveItemsWidget(var archive: Archive) : ObjectSelectionList<ArchiveItemEntry>( | ||
internalMinecraft, 0, 0, OUTER_PAD, 20 + 2 * INNER_PAD | ||
) { | ||
override fun getX(): Int { | ||
return internalMinecraft.screen!!.width / 4 + OUTER_PAD | ||
} | ||
|
||
override fun getRowLeft(): Int { | ||
return x + INNER_PAD | ||
} | ||
|
||
override fun getRowWidth(): Int { | ||
return getWidth() - 2 * INNER_PAD | ||
} | ||
|
||
override fun getScrollbarPosition(): Int { | ||
return right - 7 | ||
} | ||
|
||
override fun getMaxScroll(): Int { | ||
return max(0, maxPosition - getHeight() + INNER_PAD) | ||
} | ||
|
||
override fun renderWidget(guiGraphics: GuiGraphics, i: Int, j: Int, f: Float) { | ||
width = (internalMinecraft.screen!!.width / 4) * 3 - 2 * OUTER_PAD | ||
height = internalMinecraft.screen!!.height - 2 * OUTER_PAD | ||
super.renderWidget(guiGraphics, i, j, f) | ||
} | ||
|
||
fun refreshEntries() { | ||
scrollAmount = 0.0 | ||
clearEntries() | ||
archive.entries.map { | ||
ArchiveItemEntry(it) { | ||
archive.entries.remove(it) | ||
refreshEntries() | ||
} | ||
}.forEach(::addEntry) | ||
} | ||
} | ||
|
||
class ArchiveItemEntry(private val itemIdentificatior: ItemIdentificator, private val onRemove: () -> Unit) : | ||
ObjectSelectionList.Entry<ArchiveItemEntry>() { | ||
private val item: Item = itemIdentificatior.type ?: Items.AIR | ||
private val itemLocationString = BuiltInRegistries.ITEM.getKey(item).run { | ||
val string = toString() | ||
if (string.length > 20) return@run "${string.take(17)}..." | ||
else return@run string | ||
} | ||
|
||
private val removeButton = Button.builder(screenComponent("widget.items.remove")) { | ||
onRemove() | ||
}.width(75).build() | ||
|
||
private val modifyButton = Button.builder(screenComponent("widget.items.modify")) { | ||
// TODO: open modify screen | ||
}.width(75).build() | ||
|
||
override fun render( | ||
guiGraphics: GuiGraphics, | ||
index: Int, | ||
y: Int, | ||
x: Int, | ||
width: Int, | ||
height: Int, | ||
mouseX: Int, | ||
mouseY: Int, | ||
isSelected: Boolean, | ||
delta: Float | ||
) { | ||
val textPad = 7 | ||
guiGraphics.renderItem(ItemStack(item), x + INNER_PAD, y + 4) | ||
guiGraphics.drawString( | ||
internalMinecraft.font, itemLocationString, x + INNER_PAD * 2 + height, y + textPad, 0xFFFFFF | ||
) | ||
|
||
val twentyCharacterWidth = internalMinecraft.font.width(Component.literal("minecraft:chestplate")) * 2 | ||
guiGraphics.drawCenteredString( | ||
internalMinecraft.font, | ||
screenComponent("widget.items.component.${!itemIdentificatior.components.isEmpty}"), | ||
x + INNER_PAD * 3 + twentyCharacterWidth, | ||
y + textPad, | ||
0xFFFFFF | ||
) | ||
guiGraphics.drawString( | ||
internalMinecraft.font, | ||
screenComponent("widget.items.amount", itemIdentificatior.amount.toString()), | ||
x + INNER_PAD * 4 + (twentyCharacterWidth * 1.5).toInt(), | ||
y + textPad, | ||
0xFFFFFF | ||
) | ||
|
||
removeButton.setPosition(x + width - removeButton.width, y) | ||
removeButton.render(guiGraphics, mouseX, mouseY, delta) | ||
modifyButton.setPosition(x + width - removeButton.width - (INNER_PAD * 0.5).toInt() - modifyButton.width, y) | ||
modifyButton.render(guiGraphics, mouseX, mouseY, delta) | ||
} | ||
|
||
override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { | ||
if (removeButton.isMouseOver(d, e)) return removeButton.mouseClicked(d, e, i) | ||
if (modifyButton.isMouseOver(d, e)) return modifyButton.mouseClicked(d, e, i) | ||
return super.mouseClicked(d, e, i) | ||
} | ||
|
||
override fun getNarration(): Component { | ||
return Component.literal(item.description.toString()) | ||
} | ||
} |
67 changes: 62 additions & 5 deletions
67
src/main/kotlin/dev/nyon/autodrop/config/screen/ArchiveScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,89 @@ | ||
package dev.nyon.autodrop.config.screen | ||
|
||
import dev.nyon.autodrop.config.Archive | ||
import dev.nyon.autodrop.config.config | ||
import dev.nyon.autodrop.extensions.screenComponent | ||
import dev.nyon.konfig.config.saveConfig | ||
import net.minecraft.ChatFormatting | ||
import net.minecraft.client.gui.GuiGraphics | ||
import net.minecraft.client.gui.components.Button | ||
import net.minecraft.client.gui.screens.Screen | ||
import dev.nyon.autodrop.minecraft as internalMinecraft | ||
|
||
const val INNER_PAD = 5 | ||
const val OUTER_PAD = 10 | ||
|
||
class ArchiveScreen(private val parent: Screen?) : Screen(screenComponent("title")) { | ||
var selected: Archive? = null | ||
var selected: Archive = config.archives.first() | ||
|
||
@Suppress("unused") | ||
private val archivesWidget = ArchivesWidget(this).also { | ||
addWidget(it) | ||
it.refreshEntries() | ||
} | ||
|
||
private val archiveItemsWidget = ArchiveItemsWidget(selected).also { | ||
addWidget(it) | ||
it.refreshEntries() | ||
} | ||
|
||
private val doneButton = Button.builder(screenComponent("done")) { | ||
onClose() | ||
}.width(internalMinecraft.screen!!.width / 4 - 2 * OUTER_PAD).build().also { addWidget(it) } | ||
|
||
private val setIgnoredSlotsButton = Button.builder(screenComponent("ignored")) { | ||
// TODO: open ignored slots screen | ||
}.width(internalMinecraft.screen!!.width / 4 - 2 * OUTER_PAD).build().also { addWidget(it) } | ||
|
||
private val createArchiveButton = Button.builder(screenComponent("create")) { | ||
// TODO: open create archive screen | ||
}.width(internalMinecraft.screen!!.width / 4 - 2 * OUTER_PAD).build().also { addWidget(it) } | ||
|
||
private val deleteArchiveButton = Button.builder(screenComponent("delete").withStyle(ChatFormatting.DARK_RED)) { | ||
config.archives.remove(selected) | ||
selected = config.archives.first() | ||
archivesWidget.refreshEntries() | ||
}.width(internalMinecraft.screen!!.width / 4 - 2 * OUTER_PAD).build().also { addWidget(it) } | ||
|
||
override fun onClose() { | ||
minecraft!!.setScreen(parent) | ||
saveConfig(config) | ||
} | ||
|
||
override fun render(guiGraphics: GuiGraphics, i: Int, j: Int, f: Float) { | ||
renderBackground(guiGraphics, i, j, f) | ||
archivesWidget.render(guiGraphics, i, j, f) | ||
override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, tickDelta: Float) { | ||
renderBackground(guiGraphics, mouseX, mouseY, tickDelta) | ||
archivesWidget.render(guiGraphics, mouseX, mouseY, tickDelta) | ||
|
||
// render archive item list, if empty, render info | ||
if (selected.entries.isEmpty()) guiGraphics.drawCenteredString( | ||
internalMinecraft.font, | ||
screenComponent("noitems"), | ||
internalMinecraft.screen!!.width / 8 * 5, | ||
internalMinecraft.screen!!.height / 3, | ||
0xFFFFFF | ||
) | ||
else archiveItemsWidget.render(guiGraphics, mouseX, mouseY, tickDelta) | ||
|
||
// render control buttons | ||
doneButton.setPosition(OUTER_PAD, internalMinecraft.screen!!.height - OUTER_PAD - 20) | ||
doneButton.width = internalMinecraft.screen!!.width / 4 - 2 * OUTER_PAD | ||
doneButton.render(guiGraphics, mouseX, mouseY, tickDelta) | ||
|
||
setIgnoredSlotsButton.setPosition(OUTER_PAD, internalMinecraft.screen!!.height - OUTER_PAD - 2 * 20 - 3) | ||
setIgnoredSlotsButton.width = internalMinecraft.screen!!.width / 4 - 2 * OUTER_PAD | ||
setIgnoredSlotsButton.render(guiGraphics, mouseX, mouseY, tickDelta) | ||
|
||
createArchiveButton.setPosition(OUTER_PAD, internalMinecraft.screen!!.height - OUTER_PAD - 3 * 20 - 6) | ||
createArchiveButton.width = internalMinecraft.screen!!.width / 4 - 2 * OUTER_PAD | ||
createArchiveButton.render(guiGraphics, mouseX, mouseY, tickDelta) | ||
|
||
deleteArchiveButton.setPosition(OUTER_PAD, internalMinecraft.screen!!.height - OUTER_PAD - 4 * 20 - 9) | ||
deleteArchiveButton.width = internalMinecraft.screen!!.width / 4 - 2 * OUTER_PAD | ||
deleteArchiveButton.render(guiGraphics, mouseX, mouseY, tickDelta) | ||
} | ||
|
||
fun select(archive: Archive) { | ||
selected = archive | ||
archiveItemsWidget.archive = archive | ||
archiveItemsWidget.refreshEntries() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package dev.nyon.autodrop.extensions | ||
|
||
import net.minecraft.network.chat.Component | ||
import net.minecraft.network.chat.MutableComponent | ||
|
||
fun screenComponent(key: String): Component { | ||
return Component.translatable("menu.autodrop.screen.$key") | ||
fun screenComponent(key: String, vararg objects: Any): MutableComponent { | ||
return Component.translatable("menu.autodrop.screen.$key", *objects) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters