Skip to content

Commit

Permalink
[ref] Refractored all field components, added parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Oct 3, 2024
1 parent 22b3430 commit 0e05f17
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.bibireden.data_attributes.api.parser

typealias Parser<I, O> = (I) -> O?
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bibireden.data_attributes.ui.components.fields
package com.bibireden.data_attributes.ui.components.boxes

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
Expand All @@ -10,6 +11,8 @@ class ParsedTextBoxComponent<T>(val parser: (String) -> T?, horizontalSizing: Si
var defaultColor = ColorCodes.TAN
private val colorConditions: MutableList<Pair<Int, ColorPredicate<T>>> = mutableListOf()

fun parse(): T? = parser(text)

init {
textValue.observe { text ->
val parsed = parser(text)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
package com.bibireden.data_attributes.ui.components.fields

import com.bibireden.data_attributes.api.parser.Parser
import com.bibireden.data_attributes.ui.components.boxes.ParsedTextBoxComponent
import com.bibireden.data_attributes.ui.renderers.ButtonRenderers
import io.wispforest.owo.ui.component.Components
import io.wispforest.owo.ui.container.FlowLayout
import io.wispforest.owo.ui.core.Sizing
import io.wispforest.owo.ui.core.VerticalAlignment
import net.minecraft.text.Text
import net.minecraft.util.Identifier
import org.jetbrains.annotations.ApiStatus
import java.util.function.Consumer

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

// todo: add parser on EditFieldComponent
@ApiStatus.Internal
class EditFieldComponent(private val onConfirmation: Consumer<EditFieldComponent>, private val onDenial: Consumer<EditFieldComponent>) : FlowLayout(Sizing.fill(70), Sizing.fill(5), Algorithm.HORIZONTAL) {
val textBox: ParsedTextBoxComponent<Identifier>
class EditFieldComponent<A>(parser: Parser<String, A>, private val onConfirmation: EditFieldDecision<A>, private val onCancel: EditFieldCancellation<A>?) : FlowLayout(Sizing.fill(70), Sizing.fill(5), Algorithm.HORIZONTAL) {
val textBox: ParsedTextBoxComponent<A>

init {
verticalAlignment(VerticalAlignment.CENTER)

this.textBox = ParsedTextBoxComponent(Identifier::tryParse, Sizing.fill(60))
this.textBox = ParsedTextBoxComponent(parser, Sizing.fill(60))
.apply { verticalSizing(Sizing.fixed(12)) }
.also(::child)

child(Components.button(Text.translatable("text.config.data_attributes.data_entry.yes")) { onConfirmation.accept(this) }
child(Components.button(Text.translatable("text.config.data_attributes.data_entry.yes")) {
textBox.parse()?.let { onConfirmation(it, this); this.remove() }
}
.renderer(ButtonRenderers.STANDARD)
)
child(Components.button(Text.translatable("text.config.data_attributes.data_entry.no")) { onDenial.accept(this) }
child(Components.button(Text.translatable("text.config.data_attributes.data_entry.no")) {
onCancel?.let { it(this) }
this.remove()
}
.renderer(ButtonRenderers.STANDARD)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.bibireden.data_attributes.ui.components.fields

class FieldComponents {
// fun edit(): EditFieldComponent {}
import net.minecraft.util.Identifier

object FieldComponents {
fun identifier(onConfirmation: EditFieldDecision<Identifier>, onCancel: EditFieldCancellation<Identifier>? = null): EditFieldComponent<Identifier> {
return EditFieldComponent(Identifier::tryParse, onConfirmation, onCancel)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.bibireden.data_attributes.ui.colors.ColorCodes
import com.bibireden.data_attributes.ui.components.CollapsibleFoldableContainer
import com.bibireden.data_attributes.ui.components.fields.EditFieldComponent
import com.bibireden.data_attributes.ui.components.RemoveButtonComponent
import com.bibireden.data_attributes.ui.components.fields.FieldComponents
import com.bibireden.data_attributes.ui.renderers.ButtonRenderers
import io.wispforest.owo.config.Option
import io.wispforest.owo.config.ui.component.OptionValueProvider
Expand Down Expand Up @@ -59,15 +60,13 @@ class AttributeFunctionProvider(val option: Option<AttributeFunctionConfig>) : F

fl.child(Components.button(Text.translatable("text.config.data_attributes.data_entry.edit")) {
if (cf.childById(FlowLayout::class.java, "edit-field") == null) {
val field = EditFieldComponent(
{
val newId = Identifier.tryParse(it.textBox.text.toString()) ?: return@EditFieldComponent
if (backing.containsKey(newId) || !Registries.ATTRIBUTE.containsId(newId)) return@EditFieldComponent
val field = FieldComponents.identifier(
{ newId, _ ->
if (backing.containsKey(newId) || !Registries.ATTRIBUTE.containsId(newId)) return@identifier
// ensured that this exists and is possible to swap
backing.remove(id)?.let { backing[newId] = it }
refreshAndDisplayAttributes()
},
EditFieldComponent::remove
}
)

field.textBox
Expand Down Expand Up @@ -124,19 +123,17 @@ class AttributeFunctionProvider(val option: Option<AttributeFunctionConfig>) : F
.renderer(ButtonRenderers.STANDARD))
fl.child(Components.button(Text.translatable("text.config.data_attributes.data_entry.edit")) {
if (fl.childById(FlowLayout::class.java, "edit-field") == null) {
val field = EditFieldComponent(
{
val newId = Identifier.tryParse(it.textBox.text.toString()) ?: return@EditFieldComponent
val field = FieldComponents.identifier(
{ newId, _ ->
val newFunction = function.copy(id = newId)
val currentList = backing[parentId]
if (!Registries.ATTRIBUTE.containsId(newId) || currentList?.find { it.id == newId } != null) return@EditFieldComponent
if (!Registries.ATTRIBUTE.containsId(newId) || currentList?.find { it.id == newId } != null) return@identifier
// ensured that this exists and is possible to swap
val list = currentList?.filter { it.id != function.id }?.toMutableList() ?: return@EditFieldComponent
val list = currentList?.filter { it.id != function.id }?.toMutableList() ?: return@identifier
list.add(0, newFunction)
backing[parentId] = list
refreshAndDisplayAttributes()
},
EditFieldComponent::remove
}
)

field.textBox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.bibireden.data_attributes.config.models.OverridesConfigModel.Attribut
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.EditFieldComponent
import com.bibireden.data_attributes.ui.components.fields.FieldComponents
import com.bibireden.data_attributes.ui.renderers.ButtonRenderers
import io.wispforest.owo.config.Option
import io.wispforest.owo.config.ui.component.ConfigToggleButton
Expand Down Expand Up @@ -90,11 +90,10 @@ class AttributeOverrideProvider(val option: Option<Map<Identifier, AttributeOver

content.child(Components.button(Text.translatable("text.config.data_attributes.data_entry.edit")) {
if (container.childById(FlowLayout::class.java, "edit-field") == null) {
val field = EditFieldComponent(
{
val newId = Identifier.tryParse(it.textBox.text.toString()) ?: return@EditFieldComponent
val newAttribute = (Registries.ATTRIBUTE[newId] ?: return@EditFieldComponent) as MutableEntityAttribute
if (backing.containsKey(newId)) return@EditFieldComponent
val field = FieldComponents.identifier(
{ newId, _ ->
val newAttribute = Registries.ATTRIBUTE[newId] as? MutableEntityAttribute ?: return@identifier
if (backing.containsKey(newId)) return@identifier
// ensured that this exists and is possible to swap
this.backing.remove(id)
this.backing[newId] = override.copy(
Expand All @@ -104,10 +103,8 @@ class AttributeOverrideProvider(val option: Option<Map<Identifier, AttributeOver
formula = StackingFormula.Flat,
format = AttributeFormat.Whole
)
it.remove()
refreshAndDisplayAttributes()
},
EditFieldComponent::remove
}
)

field.textBox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.bibireden.data_attributes.data.EntityTypeData
import com.bibireden.data_attributes.ui.colors.ColorCodes
import com.bibireden.data_attributes.ui.components.CollapsibleFoldableContainer
import com.bibireden.data_attributes.ui.components.RemoveButtonComponent
import com.bibireden.data_attributes.ui.components.fields.EditFieldComponent
import com.bibireden.data_attributes.ui.components.fields.FieldComponents
import com.bibireden.data_attributes.ui.renderers.ButtonRenderers
import io.wispforest.owo.config.Option
import io.wispforest.owo.config.ui.component.OptionValueProvider
Expand Down Expand Up @@ -52,15 +52,13 @@ class EntityTypesProvider(val option: Option<Map<Identifier, EntityTypeData>>) :

fl.child(Components.button(Text.translatable("text.config.data_attributes.data_entry.edit")) {
if (ct.childById(FlowLayout::class.java, "edit-field") == null) {
val field = EditFieldComponent(
{
val newId = Identifier.tryParse(it.textBox.text.toString()) ?: return@EditFieldComponent
if (backing.containsKey(newId) || !Registries.ENTITY_TYPE.containsId(newId)) return@EditFieldComponent
val field = FieldComponents.identifier(
{ newId, field ->
if (backing.containsKey(newId) || !Registries.ENTITY_TYPE.containsId(newId)) return@identifier
// ensured that this exists and is possible to swap
backing.remove(id)?.let { backing[newId] = it }
refreshAndDisplayEntries(true)
},
EditFieldComponent::remove
}
)

field.textBox
Expand Down Expand Up @@ -126,17 +124,15 @@ class EntityTypesProvider(val option: Option<Map<Identifier, EntityTypeData>>) :

fl.child(Components.button(Text.translatable("text.config.data_attributes.data_entry.edit")) {
if (ct.childById(FlowLayout::class.java, "edit-field") == null) {
val field = EditFieldComponent(
{
val entry = backing[parentId]?.data?.toMutableMap() ?: return@EditFieldComponent
val newId = Identifier.tryParse(it.textBox.text.toString()) ?: return@EditFieldComponent
if (entry.containsKey(newId) || !Registries.ATTRIBUTE.containsId(newId)) return@EditFieldComponent
val field = FieldComponents.identifier(
{ newId, _ ->
val entry = backing[parentId]?.data?.toMutableMap() ?: return@identifier
if (entry.containsKey(newId) || !Registries.ATTRIBUTE.containsId(newId)) return@identifier
// ensured that this exists and is possible to swap
entry[newId] = entry.remove(id) ?: return@EditFieldComponent
entry[newId] = entry.remove(id) ?: return@identifier
backing[parentId] = EntityTypeData(entry)
refreshAndDisplayEntries(true)
},
EditFieldComponent::remove
}
)

field.textBox
Expand Down

0 comments on commit 0e05f17

Please sign in to comment.