From 9b4d4f7f65fba20fdd92099f09af54d0fe5e8ec8 Mon Sep 17 00:00:00 2001 From: Bibi Reden Date: Thu, 25 Jul 2024 12:29:02 -0500 Subject: [PATCH] Add updating of label tooltip(s), add event to screen --- gradle.properties | 5 +- .../com/bibireden/playerex/PlayerEXClient.kt | 2 +- .../bibireden/playerex/ui/PlayerEXScreen.kt | 35 ++++++------ .../components/labels/AttributeComponent.kt | 55 +++++++++++-------- .../playerex/ui/menus/AttributeMenu.kt | 20 +++---- .../assets/playerex/owo_ui/main_ui_model.xml | 17 +++--- .../resources/assets/playerex/lang/en_us.json | 10 ++++ 7 files changed, 81 insertions(+), 63 deletions(-) diff --git a/gradle.properties b/gradle.properties index 10d8027c..9487cc74 100644 --- a/gradle.properties +++ b/gradle.properties @@ -26,10 +26,11 @@ additional_entity_attributes_version=1.7.0+1.20.0 projectile_damage_attribute_version=3.2.2+1.20.1-fabric all_the_trims_version=3.4.2 placeholder_api_version=2.1.1+1.20 -ranged_weapon_api_version=1.1.0+1.20.1 +ranged_weapon_api_version=1.1.2+1.20.1 +# in-house opc_version=1.0.0+1.20.1-fabric -data_attributes_version=2.0.0-beta.3+1.20.1-fabric +data_attributes_version=2.0.0-beta.4+1.20.1-fabric # owo owo_version=0.11.2+1.20 diff --git a/src/client/kotlin/com/bibireden/playerex/PlayerEXClient.kt b/src/client/kotlin/com/bibireden/playerex/PlayerEXClient.kt index 731bafa3..d0732bcc 100644 --- a/src/client/kotlin/com/bibireden/playerex/PlayerEXClient.kt +++ b/src/client/kotlin/com/bibireden/playerex/PlayerEXClient.kt @@ -45,7 +45,7 @@ object PlayerEXClient : ClientModInitializer { } else { DataAttributesAPI.getValue(attribute, entity).ifPresent { value -> - screen.onAttributesUpdated(attribute, value) + screen.onAttributeUpdated(attribute, value) } } } diff --git a/src/client/kotlin/com/bibireden/playerex/ui/PlayerEXScreen.kt b/src/client/kotlin/com/bibireden/playerex/ui/PlayerEXScreen.kt index fffdedf5..486c5a4e 100644 --- a/src/client/kotlin/com/bibireden/playerex/ui/PlayerEXScreen.kt +++ b/src/client/kotlin/com/bibireden/playerex/ui/PlayerEXScreen.kt @@ -1,22 +1,16 @@ package com.bibireden.playerex.ui -import com.bibireden.data_attributes.DataAttributes -import com.bibireden.data_attributes.api.DataAttributesAPI -import com.bibireden.data_attributes.api.attribute.EntityAttributeSupplier -import com.bibireden.data_attributes.api.attribute.IEntityAttribute 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.id import com.bibireden.playerex.ext.level import com.bibireden.playerex.networking.NetworkingChannels import com.bibireden.playerex.networking.NetworkingPackets import com.bibireden.playerex.networking.types.UpdatePacketType import com.bibireden.playerex.registry.AttributesMenuRegistry import com.bibireden.playerex.ui.components.MenuComponent -import com.bibireden.playerex.ui.components.labels.AttributeComponent -import com.bibireden.playerex.ui.components.labels.AttributeLabelComponent +import com.bibireden.playerex.ui.components.MenuComponent.OnLevelUpdated import com.bibireden.playerex.util.PlayerEXUtil import io.wispforest.owo.ui.base.BaseUIModelScreen import io.wispforest.owo.ui.component.ButtonComponent @@ -25,6 +19,7 @@ import io.wispforest.owo.ui.component.TextBoxComponent import io.wispforest.owo.ui.container.FlowLayout import io.wispforest.owo.ui.core.Component import io.wispforest.owo.ui.core.ParentComponent +import io.wispforest.owo.util.EventSource import net.minecraft.entity.attribute.EntityAttribute import net.minecraft.entity.player.PlayerEntity import net.minecraft.text.Text @@ -42,6 +37,9 @@ class PlayerEXScreen : BaseUIModelScreen(FlowLayout::class.java, Dat private val playerComponent by lazy { PlayerEXComponents.PLAYER_DATA.get(this.client?.player!!) } + val onLevelUpdatedEvents = OnLevelUpdated.stream + val onLevelUpdated: EventSource = onLevelUpdatedEvents.source() + override fun shouldPause(): Boolean = false /** Whenever the level attribute gets modified, and on initialization of the screen, this will be called. */ @@ -55,7 +53,7 @@ class PlayerEXScreen : BaseUIModelScreen(FlowLayout::class.java, Dat } updatePointsAvailable() - updateLevelUpButton(player, root.childById(TextBoxComponent::class, "level:amount")!!.text, root.childById(ButtonComponent::class, "level:button")!!) + updateLevelUpButton(player, root.childById(TextBoxComponent::class, "level:amount")!!.text) this.uiAdapter.rootComponent.forEachDescendant { descendant -> if (descendant is MenuComponent) descendant.onLevelUpdatedEvents.sink().onLevelUpdated(level) @@ -63,7 +61,7 @@ class PlayerEXScreen : BaseUIModelScreen(FlowLayout::class.java, Dat } /** Whenever any attribute is updated, this will be called. */ - fun onAttributesUpdated(attribute: EntityAttribute, value: Double) { + fun onAttributeUpdated(attribute: EntityAttribute, value: Double) { this.uiAdapter.rootComponent.forEachDescendant { descendant -> if (descendant is MenuComponent) descendant.onAttributeUpdatedEvents.sink().onAttributeUpdated(attribute, value) } @@ -72,9 +70,9 @@ class PlayerEXScreen : BaseUIModelScreen(FlowLayout::class.java, Dat private fun updatePointsAvailable() { this.uiAdapter.rootComponent.childById(LabelComponent::class, "points_available")?.apply { - text(Text.literal(playerComponent.skillPoints.toString()) + text(Text.translatable("playerex.ui.main.skill_points_available", playerComponent.skillPoints) .formatted(when (playerComponent.skillPoints) { - 0 -> Formatting.WHITE + 0 -> Formatting.GRAY else -> Formatting.YELLOW } )) @@ -91,13 +89,12 @@ class PlayerEXScreen : BaseUIModelScreen(FlowLayout::class.java, Dat content.child(pages[currentPage]) } - private fun updateLevelUpButton(player: PlayerEntity, text: String, levelUpButton: ButtonComponent) { + private fun updateLevelUpButton(player: PlayerEntity, text: String) { val amount = text.toIntOrNull() ?: return val result = player.level + amount - if (result > PlayerEXAttributes.LEVEL.maxValue) return - - levelUpButton.tooltip(Text.translatable("playerex.ui.level_button", PlayerEXUtil.getRequiredXpForLevel(player, result), amount, player.experienceLevel)) + this.uiAdapter.rootComponent.childById(ButtonComponent::class, "level:button") + ?.tooltip(Text.translatable("playerex.ui.level_button", PlayerEXUtil.getRequiredXpForLevel(player, result), amount, player.experienceLevel)) } override fun build(rootComponent: FlowLayout) { @@ -106,8 +103,8 @@ class PlayerEXScreen : BaseUIModelScreen(FlowLayout::class.java, Dat val levelAmount = rootComponent.childById(TextBoxComponent::class, "level:amount")!! val levelUpButton = rootComponent.childById(ButtonComponent::class, "level:button")!! - updateLevelUpButton(player, levelAmount.text, levelUpButton) - levelAmount.onChanged().subscribe { updateLevelUpButton(player, it, levelUpButton) } + updateLevelUpButton(player, levelAmount.text) + levelAmount.onChanged().subscribe { updateLevelUpButton(player, it) } val previousPage = rootComponent.childById(ButtonComponent::class, "previous")!! val pageCounter = rootComponent.childById(LabelComponent::class, "counter")!! @@ -147,6 +144,10 @@ class PlayerEXScreen : BaseUIModelScreen(FlowLayout::class.java, Dat levelAmount.text.toIntOrNull()?.let { NetworkingChannels.MODIFY.clientHandle().send(NetworkingPackets.Level(it)) } } + onLevelUpdated.subscribe { + this.updateLevelUpButton(player, rootComponent.childById(TextBoxComponent::class, "level:amount")!!.text) + } + exit.onPress { this.close() } } diff --git a/src/client/kotlin/com/bibireden/playerex/ui/components/labels/AttributeComponent.kt b/src/client/kotlin/com/bibireden/playerex/ui/components/labels/AttributeComponent.kt index 3c050a8a..9db8d09e 100644 --- a/src/client/kotlin/com/bibireden/playerex/ui/components/labels/AttributeComponent.kt +++ b/src/client/kotlin/com/bibireden/playerex/ui/components/labels/AttributeComponent.kt @@ -7,7 +7,9 @@ import com.bibireden.data_attributes.api.attribute.StackingBehavior import com.bibireden.playerex.components.player.IPlayerDataComponent import com.bibireden.playerex.ext.id import com.bibireden.playerex.ui.PlayerEXScreen.AttributeButtonComponentType +import com.bibireden.playerex.ui.childById 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.Positioning @@ -21,32 +23,39 @@ import net.minecraft.util.Formatting private val StackingBehavior.symbol: String get() = if (this == StackingBehavior.Add) "+" else "×" -class AttributeComponent(attribute: EntityAttribute, player: PlayerEntity, component: IPlayerDataComponent) : FlowLayout(Sizing.fill(100), Sizing.fixed(18), Algorithm.HORIZONTAL) { +class AttributeComponent(val attribute: EntityAttribute, val player: PlayerEntity, component: IPlayerDataComponent) : FlowLayout(Sizing.fill(100), Sizing.fixed(18), Algorithm.HORIZONTAL) { + fun updateTooltip() { + // todo: allow data_attributes to have API funcs for obtaining this data. + val entries = DataAttributes.FUNCTIONS_CONFIG.functions.data[attribute.id] + if (!entries.isNullOrEmpty()) { + this.childById(LabelComponent::class, "${this.attribute.id}:label")?.tooltip( + Text.translatable("playerex.ui.attribute_functions").also { text -> + text.append("\n") + text.append(Text.literal(attribute.id.toString()).formatted(Formatting.DARK_GRAY)) + text.append("\n\n") + entries.forEach { function -> + val childAttribute = EntityAttributeSupplier(function.id).get() ?: return@forEach + text.append(Text.translatable(childAttribute.translationKey).formatted(Formatting.AQUA)) + text.append(Text.literal(" ${function.behavior.symbol}").formatted(Formatting.GREEN)) + text.append(Text.literal("${function.value}")) + text.append( + Text.literal( + String.format( + " (%.2f)\n", + DataAttributesAPI.getValue(EntityAttributeSupplier(function.id), player).orElse(0.0) + ) + ).formatted(Formatting.GRAY) + ) + } + text.formatted(Formatting.ITALIC) + }) + } + } + init { this.child( Components.label(Text.translatable(attribute.translationKey)).sizing(Sizing.content(), Sizing.fill(100)) - .also { label -> - // todo: allow data_attributes to have API funcs for obtaining this data. - val entries = DataAttributes.FUNCTIONS_CONFIG.functions.data[attribute.id] - if (!entries.isNullOrEmpty()) { - label.tooltip(Text.translatable("playerex.ui.attribute_functions").also { text -> - text.append("\n") - text.append(Text.literal(attribute.id.toString()).formatted(Formatting.DARK_GRAY)) - text.append("\n\n") - entries.forEach { function -> - val childAttribute = EntityAttributeSupplier(function.id).get() ?: return@forEach - text.append(Text.translatable(childAttribute.translationKey).formatted(Formatting.AQUA)) - text.append(Text.literal(" ${function.behavior.symbol}").formatted(Formatting.GREEN)) - text.append(Text.literal("${function.value}")) - text.append(Text.literal(String.format( - " (%.2f)\n", - DataAttributesAPI.getValue(EntityAttributeSupplier(function.id), player).orElse(0.0) - )).formatted(Formatting.GRAY)) - } - text.formatted(Formatting.ITALIC) - }) - } - }.id("${attribute.id}:label") + .also { this.updateTooltip() }.id("${attribute.id}:label") ) this.child(AttributeLabelComponent(attribute, player)) this.child( diff --git a/src/client/kotlin/com/bibireden/playerex/ui/menus/AttributeMenu.kt b/src/client/kotlin/com/bibireden/playerex/ui/menus/AttributeMenu.kt index 341dc476..9a9e21cc 100644 --- a/src/client/kotlin/com/bibireden/playerex/ui/menus/AttributeMenu.kt +++ b/src/client/kotlin/com/bibireden/playerex/ui/menus/AttributeMenu.kt @@ -1,10 +1,8 @@ package com.bibireden.playerex.ui.menus -import com.bibireden.data_attributes.api.attribute.StackingBehavior import com.bibireden.playerex.api.attribute.PlayerEXAttributes import com.bibireden.playerex.components.player.IPlayerDataComponent import com.bibireden.playerex.ext.level -import com.bibireden.playerex.ui.childById import com.bibireden.playerex.ui.components.MenuComponent import com.bibireden.playerex.ui.components.labels.AttributeComponent import com.bibireden.playerex.ui.components.labels.AttributeLabelComponent @@ -14,21 +12,17 @@ import io.wispforest.owo.ui.core.OwoUIAdapter import io.wispforest.owo.ui.core.Positioning import io.wispforest.owo.ui.core.Sizing import net.minecraft.client.network.ClientPlayerEntity -import net.minecraft.entity.attribute.EntityAttribute import net.minecraft.registry.Registries import net.minecraft.text.Text -private val StackingBehavior.symbol: String - get() = if (this == StackingBehavior.Add) "+" else "×" - class AttributeMenu : MenuComponent(Sizing.fill(75), Sizing.content(), Algorithm.VERTICAL) { - private fun onLevelUpdate(level: Int) { - - } + private fun onLevelUpdate(level: Int) {} - private fun onAttributeUpdate(attribute: EntityAttribute? = null, value: Double? = null) { - for (id in PlayerEXAttributes.PRIMARY_ATTRIBUTE_IDS) { - this.childById(AttributeLabelComponent::class, "${id}:current_level")!!.update() + /** Whenever ANY attribute gets updated. */ + private fun onAttributeUpdate() { + this.forEachDescendant { descendant -> + if (descendant is AttributeComponent) descendant.updateTooltip() + if (descendant is AttributeLabelComponent) descendant.update() } } @@ -43,6 +37,6 @@ class AttributeMenu : MenuComponent(Sizing.fill(75), Sizing.content(), Algorithm this.onAttributeUpdate() this.onLevelUpdated.subscribe(::onLevelUpdate) - this.onAttributeUpdated.subscribe(::onAttributeUpdate) + this.onAttributeUpdated.subscribe { _, _ -> onAttributeUpdate() } } } \ No newline at end of file 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 9594cb20..9e52e045 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 @@ -11,23 +11,26 @@ - + - - + 5 100 - 20 + 25 50,0 @@ -59,8 +62,8 @@