forked from CleverNucleus/data-attributes
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] added autocomplete for edit fields
- Loading branch information
1 parent
27c761f
commit cb45ccf
Showing
8 changed files
with
78 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 56 additions & 17 deletions
73
src/main/kotlin/com/bibireden/data_attributes/ui/components/fields/EditFieldComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters