Skip to content

Commit

Permalink
screen logic is weird af
Browse files Browse the repository at this point in the history
  • Loading branch information
btwonion committed Jul 7, 2024
1 parent a456565 commit 71dd876
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/dev/nyon/autodrop/AutoDrop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.nyon.autodrop

import com.mojang.blaze3d.platform.InputConstants
import dev.nyon.autodrop.config.*
import dev.nyon.autodrop.config.screen.createYaclScreen
import dev.nyon.autodrop.extensions.DataComponentPatchSerializer
import dev.nyon.autodrop.extensions.ItemSerializer
import dev.nyon.konfig.config.config
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/dev/nyon/autodrop/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import net.minecraft.world.item.Item
data class Config(
var enabled: Boolean = true,
val triggerConfig: TriggerConfig = TriggerConfig(),
val archives: MutableList<Archive> = mutableListOf(),
val archives: MutableList<Archive> = mutableListOf(Archive(true, "Archive 1", mutableListOf(), mutableListOf())),
var dropDelay: Long = 200
)

Expand Down Expand Up @@ -49,7 +49,7 @@ data class TriggerConfig(
*/
@Serializable
data class Archive(
val enabled: Boolean = true,
var enabled: Boolean = true,
val name: String,
var entries: MutableList<ItemIdentification>,
var ignoredSlots: MutableList<Int>
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/dev/nyon/autodrop/config/ModMenuImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.nyon.autodrop.config

import com.terraformersmc.modmenu.api.ConfigScreenFactory
import com.terraformersmc.modmenu.api.ModMenuApi
import dev.nyon.autodrop.config.screen.createYaclScreen

@Suppress("unused")
class ModMenuImpl : ModMenuApi {
Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/dev/nyon/autodrop/config/screen/ArchiveScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.nyon.autodrop.config.screen

import dev.nyon.autodrop.extensions.screenComponent
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.screens.Screen

const val INNER_PAD = 5
const val OUTER_PAD = 10

class ArchiveScreen(private val parent: Screen?) : Screen(screenComponent("title")) {
@Suppress("unused")
private val archivesWidget = ArchivesWidget.also {
it.refreshEntries()
}

override fun render(guiGraphics: GuiGraphics, i: Int, j: Int, f: Float) {
renderBackground(guiGraphics, i, j, f)
archivesWidget.render(guiGraphics, i, j, f)
}

override fun onClose() {
minecraft!!.setScreen(parent)
}
}
86 changes: 86 additions & 0 deletions src/main/kotlin/dev/nyon/autodrop/config/screen/ArchivesWidget.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package dev.nyon.autodrop.config.screen

import dev.nyon.autodrop.config.Archive
import dev.nyon.autodrop.config.config
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.components.ObjectSelectionList
import net.minecraft.client.gui.narration.NarrationElementOutput
import net.minecraft.network.chat.Component
import dev.nyon.autodrop.minecraft as internalMinecraft

object ArchivesWidget : ObjectSelectionList<ArchivesWidgetEntry>(
internalMinecraft,
0,
0,
OUTER_PAD,
internalMinecraft.font.lineHeight + 2 * INNER_PAD
) {

override fun getX(): Int {
return OUTER_PAD
}

override fun getRowLeft(): Int {
return x + INNER_PAD
}

override fun getWidth(): Int {
return (internalMinecraft.screen!!.width / 4) - 2 * OUTER_PAD
}

override fun getHeight(): Int {
return (internalMinecraft.screen!!.height / 4) * 3 - 2 * OUTER_PAD
}

override fun getRowWidth(): Int {
return getWidth() - 2 * INNER_PAD
}

fun refreshEntries() {
clearEntries()
config.archives.forEach { archive ->
addEntry(ArchivesWidgetEntry(archive))
}
}
}

class ArchivesWidgetEntry(private val archive: Archive) : ObjectSelectionList.Entry<ArchivesWidgetEntry>() {
override fun render(
guiGraphics: GuiGraphics,
index: Int,
y: Int,
x: Int,
width: Int,
height: Int,
mouseX: Int,
mouseY: Int,
isSelected: Boolean,
delta: Float
) {
// Draw archive name
val hundredPercentAlphaWhite = 0xFFFFFFFF.toInt()
val textPad = height - internalMinecraft.font.lineHeight / 2
guiGraphics.drawString(internalMinecraft.font, Component.literal(archive.name), x, y + textPad / 2, 0xFFFFFF)

// tick box - outer rectangle
val rightX = x + width
guiGraphics.hLine(rightX, rightX - height, y, hundredPercentAlphaWhite)
guiGraphics.hLine(rightX, rightX - height, y + height - 1, hundredPercentAlphaWhite)
guiGraphics.vLine(rightX, y, y + height - 1, hundredPercentAlphaWhite)
guiGraphics.vLine(rightX - height, y, y + height - 1, hundredPercentAlphaWhite)

// tick box - inner square
if (archive.enabled) guiGraphics.fill(
rightX - 1, y + 2, rightX - height + 2, y + height - 2, hundredPercentAlphaWhite
)
}

override fun mouseClicked(d: Double, e: Double, i: Int): Boolean {
archive.enabled = !archive.enabled
return super.mouseClicked(d, e, i)
}

override fun getNarration(): Component {
return Component.literal(archive.name)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.nyon.autodrop.config
package dev.nyon.autodrop.config.screen

import dev.isxander.yacl3.dsl.*
import dev.nyon.autodrop.config.config
import dev.nyon.autodrop.minecraft
import dev.nyon.konfig.config.saveConfig
import net.minecraft.client.gui.screens.Screen

Expand All @@ -14,6 +16,16 @@ fun createYaclScreen(parent: Screen? = null): Screen = YetAnotherConfigLib("auto
}
}

val screen by rootOptions.registeringButton {
action { parent, _ ->
minecraft.setScreen(ArchiveScreen(parent))
}

descriptionBuilder {
addDefaultText(1)
}
}

val delay by rootOptions.registering {
binding(200, { config.dropDelay }, { config.dropDelay = it })
controller = numberField(min = 0L)
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/dev/nyon/autodrop/extensions/Component.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.nyon.autodrop.extensions

import net.minecraft.network.chat.Component

fun screenComponent(key: String): Component {
return Component.translatable("menu.autodrop.screen.$key")
}
3 changes: 3 additions & 0 deletions src/main/resources/assets/autodrop/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
"menu.autodrop.overlay.enabled": "enabled",
"menu.autodrop.overlay.disabled": "disabled",
"menu.autodrop.name": "AutoDrop",
"menu.autodrop.screen.title": "AutoDrop",
"key.autodrop.toggle": "Toggle AutoDrop",
"key.autodrop.gui": "Open GUI",
"key.autodrop.category": "AutoDrop",
"yacl3.config.autodrop.title": "AutoDrop",
"yacl3.config.autodrop.category.general": "General",
"yacl3.config.autodrop.category.general.root.option.enabled": "Enabled",
"yacl3.config.autodrop.category.general.root.option.enabled.description": "Toggles the functionality of the mod.",
"yacl3.config.autodrop.category.general.root.option.screen": "Open archives screen",
"yacl3.config.autodrop.category.general.root.option.screen.description": "Opens the screen to configure your archives.",
"yacl3.config.autodrop.category.general.root.option.delay": "Drop Delay",
"yacl3.config.autodrop.category.general.root.option.delay.description": "Defines the delay that is applied after the trigger for a drop.",
"yacl3.config.autodrop.category.general.group.triggers": "Triggers",
Expand Down

0 comments on commit 71dd876

Please sign in to comment.