From 733326b9a6abe1e7242649a53dad326a7ceb539c Mon Sep 17 00:00:00 2001 From: Bibi Reden Date: Fri, 4 Oct 2024 14:30:16 -0500 Subject: [PATCH] [ref] Changed edit field and text box to be more agnostic --- .../boxes/ParsedTextBoxComponent.kt | 34 +++++++------------ .../components/fields/EditFieldComponent.kt | 6 ++-- .../providers/AttributeFunctionProvider.kt | 8 ++--- .../providers/AttributeOverrideProvider.kt | 5 +-- .../config/providers/EntityTypesProvider.kt | 8 ++--- 5 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/main/kotlin/com/bibireden/data_attributes/ui/components/boxes/ParsedTextBoxComponent.kt b/src/main/kotlin/com/bibireden/data_attributes/ui/components/boxes/ParsedTextBoxComponent.kt index ad29a7e..5102098 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/ui/components/boxes/ParsedTextBoxComponent.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/ui/components/boxes/ParsedTextBoxComponent.kt @@ -1,36 +1,28 @@ package com.bibireden.data_attributes.ui.components.boxes +import com.bibireden.data_attributes.api.parser.Parser import com.bibireden.data_attributes.ui.colors.ColorCodes -import com.bibireden.data_attributes.ui.components.fields.ColorPredicate import io.wispforest.owo.ui.component.TextBoxComponent import io.wispforest.owo.ui.core.Sizing import org.jetbrains.annotations.ApiStatus @ApiStatus.Internal -class ParsedTextBoxComponent(val parser: (String) -> T?, horizontalSizing: Sizing?) : TextBoxComponent(horizontalSizing) { - var defaultColor = ColorCodes.TAN - private val colorConditions: MutableList>> = mutableListOf() +class ParsedTextBoxComponent(val parser: Parser, horizontalSizing: Sizing?) : TextBoxComponent(horizontalSizing) { + private var defaultColor = ColorCodes.TAN + private var parsed: T? = null - fun parse(): T? = parser(text) + var predicate: ((T) -> Boolean)? = null + + fun validate(onSuccess: ((T) -> Unit)? = null): Boolean = parsed + .let { z -> z != null && predicate.let { it == null || it(z) } + .also { if (it && onSuccess != null) onSuccess(z) } } init { - textValue.observe { text -> - val parsed = parser(text) - if (parsed != null) { - for ((color, predicate) in colorConditions) { - if (predicate(parsed)) { - setEditableColor(color) - return@observe - } - } + textValue.observe { + parsed = parser(it) + if (!validate { setEditableColor(ColorCodes.GREEN) }) { + setEditableColor(defaultColor) } - setEditableColor(defaultColor) } } - - /** Adds to the predicate what color to change the field to. */ - fun addColorCondition(color: Int, condition: ColorPredicate): ParsedTextBoxComponent { - colorConditions.add(Pair(color, condition)) - return this - } } \ No newline at end of file diff --git a/src/main/kotlin/com/bibireden/data_attributes/ui/components/fields/EditFieldComponent.kt b/src/main/kotlin/com/bibireden/data_attributes/ui/components/fields/EditFieldComponent.kt index 16a41eb..40bd845 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/ui/components/fields/EditFieldComponent.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/ui/components/fields/EditFieldComponent.kt @@ -10,7 +10,6 @@ import io.wispforest.owo.ui.core.VerticalAlignment import net.minecraft.text.Text import org.jetbrains.annotations.ApiStatus -typealias ColorPredicate = (T) -> Boolean typealias EditFieldDecision = (T, EditFieldComponent) -> Unit typealias EditFieldCancellation = (EditFieldComponent) -> Unit @@ -27,7 +26,10 @@ class EditFieldComponent(parser: Parser, private val onConfirmatio .also(::child) child(Components.button(Text.translatable("text.config.data_attributes.data_entry.yes")) { - textBox.parse()?.let { onConfirmation(it, this); this.remove() } + textBox.validate { + onConfirmation(it, this) + this.remove() + } } .renderer(ButtonRenderers.STANDARD) ) diff --git a/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/AttributeFunctionProvider.kt b/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/AttributeFunctionProvider.kt index fd9528c..e0fbcd8 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/AttributeFunctionProvider.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/AttributeFunctionProvider.kt @@ -63,9 +63,7 @@ class AttributeFunctionProvider(val option: Option) : F } ) - field.textBox - .addColorCondition(ColorCodes.RED) { backing[id]?.get(it) != null } - .addColorCondition(ColorCodes.GREEN) { Registries.ATTRIBUTE.containsId(it) } + field.textBox.predicate = { backing[id]?.get(it) == null && Registries.ATTRIBUTE.containsId(it) } cf.child(0, field) } @@ -142,9 +140,7 @@ class AttributeFunctionProvider(val option: Option) : F } ) - field.textBox - .addColorCondition(ColorCodes.RED) { backing[parentId]?.get(it) != null } - .addColorCondition(ColorCodes.GREEN) { Registries.ATTRIBUTE.containsId(it) } + field.textBox.predicate = { backing[parentId]?.get(it) == null && Registries.ATTRIBUTE.containsId(it) } child(0, field) } diff --git a/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/AttributeOverrideProvider.kt b/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/AttributeOverrideProvider.kt index 32e5487..31d92be 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/AttributeOverrideProvider.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/ui/config/providers/AttributeOverrideProvider.kt @@ -9,7 +9,6 @@ import com.bibireden.data_attributes.config.Validators import com.bibireden.data_attributes.config.models.OverridesConfigModel.AttributeOverride import com.bibireden.data_attributes.ext.round import com.bibireden.data_attributes.mutable.MutableEntityAttribute -import com.bibireden.data_attributes.ui.colors.ColorCodes import com.bibireden.data_attributes.ui.components.fields.FieldComponents import com.bibireden.data_attributes.ui.renderers.ButtonRenderers import io.wispforest.owo.config.Option @@ -107,9 +106,7 @@ class AttributeOverrideProvider(val option: Option>) : } ) - field.textBox - .addColorCondition(ColorCodes.RED, backing::containsKey) - .addColorCondition(ColorCodes.GREEN) { Registries.ENTITY_TYPE.containsId(it) } + field.textBox.predicate = { it !in backing && Registries.ENTITY_TYPE.containsId(it) } ct.child(0, field) } @@ -145,9 +143,7 @@ class EntityTypesProvider(val option: Option>) : } ) - field.textBox - .addColorCondition(ColorCodes.RED) { backing[parentId]?.data?.get(it) != null } - .addColorCondition(ColorCodes.GREEN) { Registries.ATTRIBUTE.containsId(it) } + field.textBox.predicate = { backing[parentId]?.data?.get(it) == null && Registries.ATTRIBUTE.containsId(it) } ct.child(0, field) }