Skip to content

Commit

Permalink
Finally implement proper padding/adjustment of UI elements
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Jul 26, 2024
1 parent 8e2b745 commit cf956ef
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,17 +14,14 @@ 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
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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)
}
}
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<AttributeLabelComponent>().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<FlowLayout>, 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))



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<vertical-text-alignment>center</vertical-text-alignment>
<sizing>
<horizontal method="fill">30</horizontal>
<vertical method="fill">25</vertical>
</sizing>
<positioning type="relative">0,50</positioning>
</label>
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/bibireden/playerex/keys/LevelKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import net.minecraft.server.network.ServerPlayerEntity
import kotlin.jvm.optionals.getOrNull

class LevelKey : CachedPlayerKey<Int>(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 {
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/assets/playerex/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down

0 comments on commit cf956ef

Please sign in to comment.