From cf956efe2835eaf084874c24554d5f0dd589f7de Mon Sep 17 00:00:00 2001 From: Bibi Reden Date: Fri, 26 Jul 2024 05:10:18 -0500 Subject: [PATCH] Finally implement proper padding/adjustment of UI elements --- .../bibireden/playerex/ui/PlayerEXScreen.kt | 6 +- .../ui/components/AttributeComponent.kt | 59 ++++++++----------- .../buttons/AttributeButtonComponent.kt | 2 +- .../labels/AttributeLabelComponent.kt | 6 +- .../playerex/ui/menus/AttributesMenu.kt | 35 ++++++++--- .../assets/playerex/owo_ui/main_ui_model.xml | 1 - .../com/bibireden/playerex/keys/LevelKey.kt | 4 +- .../resources/assets/playerex/lang/en_us.json | 5 +- 8 files changed, 64 insertions(+), 54 deletions(-) diff --git a/src/client/kotlin/com/bibireden/playerex/ui/PlayerEXScreen.kt b/src/client/kotlin/com/bibireden/playerex/ui/PlayerEXScreen.kt index 0a7e97d9..98676854 100644 --- a/src/client/kotlin/com/bibireden/playerex/ui/PlayerEXScreen.kt +++ b/src/client/kotlin/com/bibireden/playerex/ui/PlayerEXScreen.kt @@ -1,7 +1,6 @@ package com.bibireden.playerex.ui import com.bibireden.playerex.PlayerEXClient -import com.bibireden.playerex.api.attribute.PlayerEXAttributes import com.bibireden.playerex.components.PlayerEXComponents import com.bibireden.playerex.components.player.IPlayerDataComponent import com.bibireden.playerex.ext.level @@ -15,9 +14,7 @@ import com.bibireden.playerex.ui.components.buttons.AttributeButtonComponent import com.bibireden.playerex.ui.util.Colors import com.bibireden.playerex.util.PlayerEXUtil import io.wispforest.owo.ui.base.BaseUIModelScreen -import io.wispforest.owo.ui.component.ButtonComponent -import io.wispforest.owo.ui.component.LabelComponent -import io.wispforest.owo.ui.component.TextBoxComponent +import io.wispforest.owo.ui.component.* import io.wispforest.owo.ui.container.FlowLayout import io.wispforest.owo.ui.core.Component import io.wispforest.owo.ui.core.ParentComponent @@ -25,7 +22,6 @@ import io.wispforest.owo.util.EventSource import net.minecraft.entity.attribute.EntityAttribute import net.minecraft.entity.player.PlayerEntity import net.minecraft.text.Text -import net.minecraft.util.Formatting import kotlin.reflect.KClass // Transformers diff --git a/src/client/kotlin/com/bibireden/playerex/ui/components/AttributeComponent.kt b/src/client/kotlin/com/bibireden/playerex/ui/components/AttributeComponent.kt index 24887303..4e206494 100644 --- a/src/client/kotlin/com/bibireden/playerex/ui/components/AttributeComponent.kt +++ b/src/client/kotlin/com/bibireden/playerex/ui/components/AttributeComponent.kt @@ -18,10 +18,7 @@ import io.wispforest.owo.ui.component.Components import io.wispforest.owo.ui.component.LabelComponent import io.wispforest.owo.ui.container.Containers import io.wispforest.owo.ui.container.FlowLayout -import io.wispforest.owo.ui.core.HorizontalAlignment -import io.wispforest.owo.ui.core.Positioning -import io.wispforest.owo.ui.core.Sizing -import io.wispforest.owo.ui.core.VerticalAlignment +import io.wispforest.owo.ui.core.* import net.minecraft.entity.attribute.EntityAttribute import net.minecraft.entity.player.PlayerEntity import net.minecraft.text.Text @@ -30,12 +27,15 @@ import net.minecraft.util.Formatting private val StackingBehavior.symbol: String get() = if (this == StackingBehavior.Add) "+" else "×" -class AttributeComponent(private val attribute: EntityAttribute, private val player: PlayerEntity, component: IPlayerDataComponent) : FlowLayout(Sizing.fill(35), Sizing.fixed(18), Algorithm.HORIZONTAL) { +class AttributeComponent(private val attribute: EntityAttribute, private val player: PlayerEntity, component: IPlayerDataComponent) : FlowLayout(Sizing.fill(100), Sizing.fixed(18), Algorithm.HORIZONTAL) { + val label: AttributeLabelComponent + fun refresh() { // todo: allow data_attributes to have API funcs for obtaining this data. + // from manager directly, e.g. (getFunctions() ...) val entries = DataAttributesClient.MANAGER.data.functions[attribute.id] if (!entries.isNullOrEmpty()) { - this.childById(LabelComponent::class, "${this.attribute.id}:label")?.tooltip( + label.tooltip( Text.translatable("playerex.ui.main.modified_attributes").also { text -> text.append("\n") text.append(Text.literal(attribute.id.toString()).formatted(Formatting.DARK_GRAY)) @@ -46,19 +46,12 @@ class AttributeComponent(private val attribute: EntityAttribute, private val pla text.apply { append(Text.translatable(childAttribute.translationKey).styled { it.withColor(Colors.SATURATED_BLUE) }) - append(" [") - append(Text.literal(formula.name.uppercase()).styled { it.withColor(if (formula == StackingFormula.Flat) Colors.SANDY else Colors.IMPISH_RED) }) - append("] ") + append(" (") append(Text.literal(function.behavior.symbol).styled { it.withColor(Colors.DARK_GREEN) }) - append(Text.literal("${function.value}")) - append( - Text.literal( - String.format( - " (%.2f)\n", - DataAttributesAPI.getValue(childAttribute, player).orElse(0.0) - ) - ).formatted(Formatting.GRAY) - ) + append(Text.literal("${function.value}:")) + append(Text.literal(formula.name.lowercase()).styled { it.withColor(if (formula == StackingFormula.Flat) Colors.SANDY else Colors.IMPISH_RED) }) + append(")") + append(Text.literal(" (%.2f)\n".format(DataAttributesAPI.getValue(childAttribute, player).orElse(0.0))).formatted(Formatting.GRAY)) formatted(Formatting.ITALIC) } } @@ -68,26 +61,26 @@ class AttributeComponent(private val attribute: EntityAttribute, private val pla } init { - this.child( + child( Components.label(Text.translatable(attribute.translationKey)) .verticalTextAlignment(VerticalAlignment.CENTER) .sizing(Sizing.content(), Sizing.fill(100)) - .also { this.refresh() } + .positioning(Positioning.relative(0, 50)) .id("${attribute.id}:label") ) - this.child(AttributeLabelComponent(attribute, player)) - this.child( - Containers.horizontalFlow(Sizing.content(), Sizing.fill(100)).also { - it.child(AttributeButtonComponent(attribute, player, component, AttributeButtonComponentType.Remove)) - it.child(AttributeButtonComponent(attribute, player, component, AttributeButtonComponentType.Add)) - it.child(Components.textBox(Sizing.fixed(27)).text("1").verticalSizing(Sizing.fixed(12)).id("entry:${attribute.id}")) - it.gap(4) - } - .horizontalAlignment(HorizontalAlignment.RIGHT) - .verticalAlignment(VerticalAlignment.CENTER) - .positioning(Positioning.relative(100, 0)) + + child(AttributeButtonComponent(attribute, player, component, AttributeButtonComponentType.Remove)) + child( + AttributeLabelComponent(attribute, player).also { label = it } + .horizontalSizing(Sizing.fill(34)) ) - this.gap(3) - this.verticalAlignment(VerticalAlignment.CENTER) + child(AttributeButtonComponent(attribute, player, component, AttributeButtonComponentType.Add)) + + horizontalAlignment(HorizontalAlignment.RIGHT) + verticalAlignment(VerticalAlignment.CENTER) + + verticalAlignment(VerticalAlignment.CENTER) + + refresh() } } \ No newline at end of file diff --git a/src/client/kotlin/com/bibireden/playerex/ui/components/buttons/AttributeButtonComponent.kt b/src/client/kotlin/com/bibireden/playerex/ui/components/buttons/AttributeButtonComponent.kt index 129d79f4..e5d3128c 100644 --- a/src/client/kotlin/com/bibireden/playerex/ui/components/buttons/AttributeButtonComponent.kt +++ b/src/client/kotlin/com/bibireden/playerex/ui/components/buttons/AttributeButtonComponent.kt @@ -19,7 +19,7 @@ class AttributeButtonComponent(private val attribute: EntityAttribute, private v Text.literal(type.symbol), { // reference text-box to get needed value to send to server - it.parent()?.childById(TextBoxComponent::class, "entry:${attribute.id}")?.let { box -> + it.parent()?.parent()?.childById(TextBoxComponent::class, "input")?.let { box -> val amount = box.text.toDoubleOrNull() ?: return@let val points = type.getPointsFromComponent(component) diff --git a/src/client/kotlin/com/bibireden/playerex/ui/components/labels/AttributeLabelComponent.kt b/src/client/kotlin/com/bibireden/playerex/ui/components/labels/AttributeLabelComponent.kt index 10588168..0610fd11 100644 --- a/src/client/kotlin/com/bibireden/playerex/ui/components/labels/AttributeLabelComponent.kt +++ b/src/client/kotlin/com/bibireden/playerex/ui/components/labels/AttributeLabelComponent.kt @@ -5,6 +5,7 @@ import com.bibireden.data_attributes.api.attribute.IEntityAttribute import com.bibireden.playerex.ext.id import com.bibireden.playerex.ui.util.Colors import io.wispforest.owo.ui.component.LabelComponent +import io.wispforest.owo.ui.core.HorizontalAlignment import io.wispforest.owo.ui.core.Sizing import io.wispforest.owo.ui.core.VerticalAlignment import net.minecraft.entity.attribute.EntityAttribute @@ -20,9 +21,10 @@ private fun createTextFromAttribute(attribute: EntityAttribute, player: PlayerEn class AttributeLabelComponent(private val attribute: EntityAttribute, private val player: PlayerEntity) : LabelComponent(createTextFromAttribute(attribute, player)) { init { - id("${attribute.id}:current_level") - verticalSizing(Sizing.fill(100)) + horizontalTextAlignment(HorizontalAlignment.CENTER) verticalTextAlignment(VerticalAlignment.CENTER) + + id("${attribute.id}:current_level") } fun refresh(): LabelComponent = text(createTextFromAttribute(attribute, player)) diff --git a/src/client/kotlin/com/bibireden/playerex/ui/menus/AttributesMenu.kt b/src/client/kotlin/com/bibireden/playerex/ui/menus/AttributesMenu.kt index ad7bb939..9b1bf52f 100644 --- a/src/client/kotlin/com/bibireden/playerex/ui/menus/AttributesMenu.kt +++ b/src/client/kotlin/com/bibireden/playerex/ui/menus/AttributesMenu.kt @@ -7,7 +7,9 @@ import com.bibireden.playerex.ui.components.MenuComponent import com.bibireden.playerex.ui.components.AttributeComponent import com.bibireden.playerex.ui.components.labels.AttributeLabelComponent import io.wispforest.owo.ui.component.Components +import io.wispforest.owo.ui.container.Containers import io.wispforest.owo.ui.container.FlowLayout +import io.wispforest.owo.ui.core.Insets import io.wispforest.owo.ui.core.OwoUIAdapter import io.wispforest.owo.ui.core.Positioning import io.wispforest.owo.ui.core.Sizing @@ -21,19 +23,34 @@ class AttributesMenu : MenuComponent(Sizing.fill(100), Sizing.fill(100), Algorit /** Whenever ANY attribute gets updated. */ private fun onAttributeUpdate() { // refresh all attribute labels - this.children().forEach { component -> - if (component !is AttributeComponent) return@forEach - component.refresh() - component.children().filterIsInstance().forEach(AttributeLabelComponent::refresh) + this.forEachDescendant { component -> + if (component is AttributeComponent) { + component.refresh() + return@forEachDescendant + } + if (component is AttributeLabelComponent) { + component.refresh() + return@forEachDescendant + } } } override fun build(player: ClientPlayerEntity, adapter: OwoUIAdapter, component: IPlayerDataComponent) { - child(Components.label(Text.translatable("playerex.ui.category.primary_attributes"))) - child(Components.box(Sizing.fill(35), Sizing.fixed(2))) - gap(5) - children(PlayerEXAttributes.PRIMARY_ATTRIBUTE_IDS.mapNotNull(Registries.ATTRIBUTE::get).map { AttributeComponent(it, player, component) }) - positioning(Positioning.relative(10, 25)) + child(Containers.verticalFlow(Sizing.fill(35), Sizing.fill(100)).apply { + child(Components.label(Text.translatable("playerex.ui.category.primary_attributes"))) + child( + Components.textBox(Sizing.fixed(27)) + .text("1") + .verticalSizing(Sizing.fixed(10)) + .positioning(Positioning.relative(100, 0)) + .id("input") + ) + child(Components.box(Sizing.fill(100), Sizing.fixed(2))) + gap(5) + children(PlayerEXAttributes.PRIMARY_ATTRIBUTE_IDS.mapNotNull(Registries.ATTRIBUTE::get).map { AttributeComponent(it, player, component) }) + }) + + padding(Insets.both(4, 4)) diff --git a/src/client/resources/assets/playerex/owo_ui/main_ui_model.xml b/src/client/resources/assets/playerex/owo_ui/main_ui_model.xml index 3ccecc8d..43d8018b 100644 --- a/src/client/resources/assets/playerex/owo_ui/main_ui_model.xml +++ b/src/client/resources/assets/playerex/owo_ui/main_ui_model.xml @@ -15,7 +15,6 @@ center 30 - 25 0,50 diff --git a/src/main/kotlin/com/bibireden/playerex/keys/LevelKey.kt b/src/main/kotlin/com/bibireden/playerex/keys/LevelKey.kt index 946e3cb0..c2d48feb 100644 --- a/src/main/kotlin/com/bibireden/playerex/keys/LevelKey.kt +++ b/src/main/kotlin/com/bibireden/playerex/keys/LevelKey.kt @@ -9,8 +9,8 @@ import net.minecraft.server.network.ServerPlayerEntity import kotlin.jvm.optionals.getOrNull class LevelKey : CachedPlayerKey(PlayerEX.id("level")) { - override fun get(player: ServerPlayerEntity): Int? { - return DataAttributesAPI.getValue(PlayerEXAttributes.LEVEL, player).map(Double::toInt).getOrNull() + override fun get(player: ServerPlayerEntity): Int { + return DataAttributesAPI.getValue(PlayerEXAttributes.LEVEL, player).map(Double::toInt).orElse(0) } override fun readFromNbt(tag: NbtCompound): Int { diff --git a/src/main/resources/assets/playerex/lang/en_us.json b/src/main/resources/assets/playerex/lang/en_us.json index 5df3fcc1..cd6e9fa0 100644 --- a/src/main/resources/assets/playerex/lang/en_us.json +++ b/src/main/resources/assets/playerex/lang/en_us.json @@ -2,7 +2,10 @@ "key.categories.playerex": "PlayerEX Controls", "playerex.key.main_screen": "Open Main Screen", - "playerex.ui.main.title": "⚑ PlayerEX • Leveling Screen", + "playerex.ui.main.title":[ + {"text": "\uD83D\uDDB3 ", "color": "#F0C25E"}, + {"text": "PlayerEX • Leveling Screen", "color": "white"} + ], "playerex.ui.main.exit": "Exit", "playerex.ui.main.exit.tooltip": "Exits from the window.",