Skip to content

Commit

Permalink
Add updating of label tooltip(s), add event to screen
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Jul 25, 2024
1 parent 1a4414e commit 9b4d4f7
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 63 deletions.
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/client/kotlin/com/bibireden/playerex/PlayerEXClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object PlayerEXClient : ClientModInitializer {
}
else {
DataAttributesAPI.getValue(attribute, entity).ifPresent { value ->
screen.onAttributesUpdated(attribute, value)
screen.onAttributeUpdated(attribute, value)
}
}
}
Expand Down
35 changes: 18 additions & 17 deletions src/client/kotlin/com/bibireden/playerex/ui/PlayerEXScreen.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -42,6 +37,9 @@ class PlayerEXScreen : BaseUIModelScreen<FlowLayout>(FlowLayout::class.java, Dat

private val playerComponent by lazy { PlayerEXComponents.PLAYER_DATA.get(this.client?.player!!) }

val onLevelUpdatedEvents = OnLevelUpdated.stream
val onLevelUpdated: EventSource<OnLevelUpdated> = onLevelUpdatedEvents.source()

override fun shouldPause(): Boolean = false

/** Whenever the level attribute gets modified, and on initialization of the screen, this will be called. */
Expand All @@ -55,15 +53,15 @@ class PlayerEXScreen : BaseUIModelScreen<FlowLayout>(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)
}
}

/** 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)
}
Expand All @@ -72,9 +70,9 @@ class PlayerEXScreen : BaseUIModelScreen<FlowLayout>(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
}
))
Expand All @@ -91,13 +89,12 @@ class PlayerEXScreen : BaseUIModelScreen<FlowLayout>(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) {
Expand All @@ -106,8 +103,8 @@ class PlayerEXScreen : BaseUIModelScreen<FlowLayout>(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")!!
Expand Down Expand Up @@ -147,6 +144,10 @@ class PlayerEXScreen : BaseUIModelScreen<FlowLayout>(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() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
20 changes: 7 additions & 13 deletions src/client/kotlin/com/bibireden/playerex/ui/menus/AttributeMenu.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}
}

Expand All @@ -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() }
}
}
17 changes: 10 additions & 7 deletions src/client/resources/assets/playerex/owo_ui/main_ui_model.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@
<flow-layout direction="horizontal">
<children>
<label>
<text translate="false">⚑ PlayerEX • Leveling Screen</text>
<text translate="true">playerex.ui.main.title</text>
<vertical-text-alignment>center</vertical-text-alignment>
<sizing>
<horizontal method="fill">25</horizontal>
<vertical method="fill">25</vertical>
</sizing>
<positioning type="relative">0,50</positioning>
</label>

<!-- points available area-->
<flow-layout direction="ltr-text-flow" id="points_available_window">
<flow-layout direction="horizontal">
<children>
<label id="points_available"><text>0</text></label>
<label><text>POINT(S) AVAILABLE</text></label>
<label id="points_available"><text>playerex.ui.main.skill_points_available</text></label>
</children>

<gap>5</gap>

<sizing>
<vertical method="fill">100</vertical>
<horizontal method="fill">20</horizontal>
<horizontal method="content">25</horizontal>
</sizing>
<positioning type="relative">50,0</positioning>

Expand Down Expand Up @@ -59,8 +62,8 @@
</button>

<button id="exit">
<text translate="false">Exit</text>
<tooltip-text> Exits from the window. </tooltip-text>
<text translate="true">playerex.ui.main.exit</text>
<tooltip-text translate="true">playerex.ui.main.exit.tooltip</tooltip-text>

<sizing><vertical method="fill">60</vertical><horizontal method="fill">4</horizontal></sizing>
<renderer><flat color="black" hovered-color="dark-red" disabled-color="red"/></renderer>
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/assets/playerex/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{
"key.categories.playerex": "PlayerEX Controls",
"playerex.key.main_screen": "Open Main Screen",

"playerex.ui.main.title": "⚑ PlayerEX • Leveling Screen",
"playerex.ui.main.exit": "Exit",
"playerex.ui.main.exit.tooltip": "Exits from the window.",

"playerex.ui.main.skill_points_available": [
{"index": 0},
{"text": " SKILL POINT(S)", "color": "white"}
],

"playerex.ui.category.primary_attributes": "Attributes",

"playerex.ui.attribute_functions": [ {"text": "❤ Attribute Function(s)", "color": "#F6CB3B"} ],
Expand Down

0 comments on commit 9b4d4f7

Please sign in to comment.