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.
[ref] Changed edit field and text box to be more agnostic
- Loading branch information
1 parent
bcc678c
commit 733326b
Showing
5 changed files
with
22 additions
and
39 deletions.
There are no files selected for viewing
34 changes: 13 additions & 21 deletions
34
src/main/kotlin/com/bibireden/data_attributes/ui/components/boxes/ParsedTextBoxComponent.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,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 | ||
} | ||
} |
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