Skip to content

Commit

Permalink
[ref] Changed edit field and text box to be more agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Oct 4, 2024
1 parent bcc678c commit 733326b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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<T>(val parser: (String) -> T?, horizontalSizing: Sizing?) : TextBoxComponent(horizontalSizing) {
var defaultColor = ColorCodes.TAN
private val colorConditions: MutableList<Pair<Int, ColorPredicate<T>>> = mutableListOf()
class ParsedTextBoxComponent<T>(val parser: Parser<String, T>, 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<T>): ParsedTextBoxComponent<T> {
colorConditions.add(Pair(color, condition))
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.wispforest.owo.ui.core.VerticalAlignment
import net.minecraft.text.Text
import org.jetbrains.annotations.ApiStatus

typealias ColorPredicate<T> = (T) -> Boolean
typealias EditFieldDecision<T> = (T, EditFieldComponent<T>) -> Unit
typealias EditFieldCancellation<T> = (EditFieldComponent<T>) -> Unit

Expand All @@ -27,7 +26,10 @@ class EditFieldComponent<A>(parser: Parser<String, A>, 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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ class AttributeFunctionProvider(val option: Option<AttributeFunctionConfig>) : 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)
}
Expand Down Expand Up @@ -142,9 +140,7 @@ class AttributeFunctionProvider(val option: Option<AttributeFunctionConfig>) : 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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -107,9 +106,7 @@ class AttributeOverrideProvider(val option: Option<Map<Identifier, AttributeOver
}
)

field.textBox
.addColorCondition(ColorCodes.RED, backing::containsKey)
.addColorCondition(ColorCodes.GREEN) { Registries.ATTRIBUTE.containsId(it) }
field.textBox.predicate = { it !in backing && Registries.ATTRIBUTE.containsId(it) }

container.child(0, field)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ class EntityTypesProvider(val option: Option<Map<Identifier, EntityTypeData>>) :
}
)

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)
}
Expand Down Expand Up @@ -145,9 +143,7 @@ class EntityTypesProvider(val option: Option<Map<Identifier, EntityTypeData>>) :
}
)

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)
}
Expand Down

0 comments on commit 733326b

Please sign in to comment.