Skip to content

Commit

Permalink
[feat] added autocomplete for edit fields
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Oct 11, 2024
1 parent 27c761f commit cb45ccf
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 53 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
*I am glad to have this opportunity to work on this project and be supported with the first ever public project I will personally release.*

## Additions 🍎
- No more needing to leave the UI to your config json~ you can do everything you need to do in the UI!
- Allowed easier control of the config menu, and added some new features.
- A `Reset` option to reset your targeted attribute, refreshing all its entries to start anew.
- A `Remove` option to remove the targeted entry of your choice.
- A `Edit` option to edit the identifier to target a different entry.
- A `Add` option to include new entries.
- Attributes in configuration now will re-render in certain scenarios, allowing for a better experience with working with multiple attributes.
- You can also add into default entries.
- Added autocomplete that will appear while editing fields. This will show you currently registered entities and attributes when editing the respective id you want.
- Press [ENTER] to quickly grab the first entry on the autocomplete.
- Attribute components in configuration now will re-render in certain scenarios, allowing for a better experience with working with multiple attributes.
- Improved logic with entering fields, commit changes by pressing [Done], and [Reload] to refresh to the latest config.
- You can now actually use the search bar to look up the specific entries you wish to find.
- Translations should be compatible in the language you choose as well as the attribute id.
- e.g., looking up `playerex:luck`, or `Luck` should work.
Expand All @@ -30,4 +35,4 @@
- Made some changes to certain logic internally and micro-optimizations.
- Fixed CTD issues with editing function values.
- Separated config entries from defaults using color coding & tooltips.
- Resolved issue with diminishing returns. Hopefully, this should work.
- Integrated diminishing returns as intended.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import io.wispforest.endec.Endec
import net.minecraft.entity.attribute.EntityAttributeInstance
import kotlin.math.abs

// CN: ((1.0 - v2) * (1.0 - m).pow((v - v2) / m)) - ((1.0 - k2) * (1.0 - m).pow((k - k2) / m))

enum class StackingFormula(val function: (k: Double, k2: Double, v: Double, v2: Double, instance: EntityAttributeInstance) -> Double) {
Flat({ k, _, v, _, _ -> k - v }),
Diminished({ k, k2, v, v2, instance ->
val base = instance.baseValue
val smoothness = (instance.attribute as IEntityAttribute).`data_attributes$smoothness`()
val result = base + (((k - base) - v) * smoothness)
result
base + (((k - base) - v) * smoothness)
});

companion object {
Expand All @@ -29,26 +26,4 @@ enum class StackingFormula(val function: (k: Double, k2: Double, v: Double, v2:
fun stack(current: Double, input: Double): Double = current + abs(input)

fun result(k: Double, k2: Double, v: Double, v2: Double, instance: EntityAttributeInstance): Double = this.function(k, k2, v, v2, instance)
}


// the solution?
//val min = attribute.`data_attributes$min`()
//val max = attribute.`data_attributes$max`()
//
//val k = k / 100
//val k2 = k2 / 100
//val v = v / 100
//val v2 = v2 / 100
//
//val s = MathHelper.clamp(attribute.`data_attributes$smoothness`(), 0.01, 1.0)
//
//val result1 = (1.0 - v2)
//val result2 = (1.0 - s).pow((v - v2) / s)
//val result3 = result1 * result2
//
//val result4 = (1.0 - k2)
//val result5 = (1.0 - s).pow((k - k2) / s)
//val result6 = (result4 * result5)
//
//((((result3-result6)) * (max-min)) - min).round(2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ParsedTextBoxComponent<T>(val parser: Parser<String, T>, horizontalSizing:
.also { if (it && onSuccess != null) onSuccess(z) } }

init {
setMaxLength(500)
textValue.observe {
parsed = parser(it)
if (!validate { setEditableColor(ColorCodes.GREEN) }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,82 @@
package com.bibireden.data_attributes.ui.components.fields

import com.bibireden.data_attributes.api.parser.Parser
import com.bibireden.data_attributes.ui.colors.ColorCodes
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.Containers
import io.wispforest.owo.ui.container.FlowLayout
import io.wispforest.owo.ui.core.Insets
import io.wispforest.owo.ui.core.Sizing
import io.wispforest.owo.ui.core.VerticalAlignment
import net.minecraft.text.Style
import net.minecraft.text.Text
import org.jetbrains.annotations.ApiStatus
import org.lwjgl.glfw.GLFW

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

// todo: add parser on EditFieldComponent
@ApiStatus.Internal
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) {
class EditFieldComponent<A>(parser: Parser<String, A>, private val onConfirmation: EditFieldDecision<A>, private val onCancel: EditFieldCancellation<A>? = null, private val autocomplete: Collection<A>? = null) : FlowLayout(Sizing.fill(70), Sizing.content(), Algorithm.VERTICAL) {
val textBox: ParsedTextBoxComponent<A>

private val choices = mutableListOf<A>()

init {
verticalAlignment(VerticalAlignment.CENTER)
padding(Insets.vertical(2))

child(Containers.horizontalFlow(Sizing.fill(100), Sizing.content(2)).also { hf ->
hf.verticalAlignment(VerticalAlignment.CENTER)
hf.id("edit-field")

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

child(Components.button(Text.translatable("text.config.data_attributes.data_entry.yes")) {
textBox.validate {
onConfirmation(it, this)
hf.child(Components.button(Text.translatable("text.config.data_attributes.data_entry.yes")) {
textBox.validate {
this.remove()
onConfirmation(it, this)
}
}
.renderer(ButtonRenderers.STANDARD)
)
hf.child(Components.button(Text.translatable("text.config.data_attributes.data_entry.no")) {
this.remove()
onCancel?.let { it(this) }
}
.renderer(ButtonRenderers.STANDARD)
)
})

if (autocomplete != null) {
child(Containers.verticalFlow(Sizing.fill(100), Sizing.content()).also { sl ->
sl.gap(2)
sl.clearChildren()

textBox.onChanged().subscribe { txt ->
sl.clearChildren()
choices.clear()

if (txt.isEmpty()) return@subscribe

for (k in autocomplete) {
if (k.toString().contains(txt)) {
choices.add(k)
sl.child(Components.label(Text.literal(k.toString()).setStyle(Style.EMPTY.withColor(ColorCodes.TAN))))
}
}
}

textBox.keyPress().subscribe { code, _, _ ->
if (choices.isNotEmpty() && code == GLFW.GLFW_KEY_ENTER) {
textBox.text = choices.first().toString()
}
true
}
})
}
.renderer(ButtonRenderers.STANDARD)
)
child(Components.button(Text.translatable("text.config.data_attributes.data_entry.no")) {
onCancel?.let { it(this) }
this.remove()
}
.renderer(ButtonRenderers.STANDARD)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.bibireden.data_attributes.ui.components.fields
import net.minecraft.util.Identifier

object FieldComponents {
fun identifier(onConfirmation: EditFieldDecision<Identifier>, onCancel: EditFieldCancellation<Identifier>? = null): EditFieldComponent<Identifier> {
return EditFieldComponent(Identifier::tryParse, onConfirmation, onCancel)
fun identifier(onConfirmation: EditFieldDecision<Identifier>, onCancel: EditFieldCancellation<Identifier>? = null, autocomplete: Collection<Identifier>? = null): EditFieldComponent<Identifier> {
return EditFieldComponent(Identifier::tryParse, onConfirmation, onCancel, autocomplete)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class AttributeFunctionProvider(val option: Option<AttributeFunctionConfig>) : F
backing.remove(id)?.let { backing[newId] = it }

refreshAndDisplayEntries()
}
},
autocomplete = Registries.ATTRIBUTE.ids
)

field.textBox.predicate = { backing[id]?.get(it) == null && Registries.ATTRIBUTE.containsId(it) }
Expand Down Expand Up @@ -139,7 +140,8 @@ class AttributeFunctionProvider(val option: Option<AttributeFunctionConfig>) : F
backing[parentId] = entry

refreshAndDisplayEntries()
}
},
autocomplete = Registries.ATTRIBUTE.ids
)

field.textBox.predicate = { backing[parentId]?.get(it) == null && Registries.ATTRIBUTE.containsId(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class AttributeOverrideProvider(val option: Option<Map<Identifier, AttributeOver
)

refreshAndDisplayAttributes()
}
},
autocomplete = Registries.ATTRIBUTE.ids
)

field.textBox.predicate = { it !in backing && Registries.ATTRIBUTE.containsId(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class EntityTypesProvider(val option: Option<Map<Identifier, EntityTypeData>>) :

backing.remove(id)?.let { backing[newId] = it }
refreshAndDisplayEntries(true)
}
},
autocomplete = Registries.ENTITY_TYPE.ids
)

field.textBox.predicate = { it !in backing && Registries.ENTITY_TYPE.containsId(it) }
Expand Down Expand Up @@ -140,7 +141,8 @@ class EntityTypesProvider(val option: Option<Map<Identifier, EntityTypeData>>) :
backing[parentId] = EntityTypeData(entry)

refreshAndDisplayEntries(true)
}
},
autocomplete = Registries.ATTRIBUTE.ids
)

field.textBox.predicate = { backing[parentId]?.data?.get(it) == null && Registries.ATTRIBUTE.containsId(it) }
Expand Down

0 comments on commit cb45ccf

Please sign in to comment.