Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties: Superseded PropertyValues by Elementa's States (concept) #46

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions api/Vigilance.api
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ public final class gg/essential/vigilance/Vigilant$CategoryPropertyBuilder {
public static synthetic fun paragraph$default (Lgg/essential/vigilance/Vigilant$CategoryPropertyBuilder;Lkotlin/reflect/KMutableProperty0;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public final fun percentSlider (Lkotlin/reflect/KMutableProperty0;Ljava/lang/String;Ljava/lang/String;ZZLkotlin/jvm/functions/Function1;)V
public static synthetic fun percentSlider$default (Lgg/essential/vigilance/Vigilant$CategoryPropertyBuilder;Lkotlin/reflect/KMutableProperty0;Ljava/lang/String;Ljava/lang/String;ZZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public final fun property (Lgg/essential/elementa/state/State;Lgg/essential/vigilance/data/PropertyType;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;IIFFIILjava/util/List;ZLjava/lang/String;ZZLkotlin/jvm/functions/Function1;)V
public final synthetic fun property (Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/data/PropertyType;Ljava/lang/String;Ljava/lang/String;IIFFIILjava/util/List;ZLjava/lang/String;ZZLkotlin/jvm/functions/Function1;)V
public final fun property (Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/data/PropertyType;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;IIFFIILjava/util/List;ZLjava/lang/String;ZZLkotlin/jvm/functions/Function1;)V
public final fun property (Lkotlin/reflect/KMutableProperty0;Lgg/essential/vigilance/data/PropertyType;Ljava/lang/String;Ljava/lang/String;IIFFIILjava/util/List;ZLjava/lang/String;ZZLkotlin/jvm/functions/Function1;)V
public static synthetic fun property$default (Lgg/essential/vigilance/Vigilant$CategoryPropertyBuilder;Lgg/essential/elementa/state/State;Lgg/essential/vigilance/data/PropertyType;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;IIFFIILjava/util/List;ZLjava/lang/String;ZZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public static synthetic fun property$default (Lgg/essential/vigilance/Vigilant$CategoryPropertyBuilder;Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/data/PropertyType;Ljava/lang/String;Ljava/lang/String;IIFFIILjava/util/List;ZLjava/lang/String;ZZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public static synthetic fun property$default (Lgg/essential/vigilance/Vigilant$CategoryPropertyBuilder;Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/data/PropertyType;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;IIFFIILjava/util/List;ZLjava/lang/String;ZZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public static synthetic fun property$default (Lgg/essential/vigilance/Vigilant$CategoryPropertyBuilder;Lkotlin/reflect/KMutableProperty0;Lgg/essential/vigilance/data/PropertyType;Ljava/lang/String;Ljava/lang/String;IIFFIILjava/util/List;ZLjava/lang/String;ZZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
Expand Down Expand Up @@ -276,7 +278,9 @@ public abstract class gg/essential/vigilance/data/PropertyCollector {

public final class gg/essential/vigilance/data/PropertyData {
public static final field Companion Lgg/essential/vigilance/data/PropertyData$Companion;
public fun <init> (Lgg/essential/vigilance/data/PropertyAttributes;Lgg/essential/elementa/state/State;Lgg/essential/vigilance/Vigilant;)V
public fun <init> (Lgg/essential/vigilance/data/PropertyAttributes;Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/Vigilant;)V
public fun <init> (Lgg/essential/vigilance/data/PropertyAttributesExt;Lgg/essential/elementa/state/State;Lgg/essential/vigilance/Vigilant;)V
public fun <init> (Lgg/essential/vigilance/data/PropertyAttributesExt;Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/Vigilant;)V
public final fun component1 ()Lgg/essential/vigilance/data/PropertyAttributes;
public final fun component2 ()Lgg/essential/vigilance/data/PropertyValue;
Expand All @@ -294,7 +298,9 @@ public final class gg/essential/vigilance/data/PropertyData {
public final fun getDependsOn ()Lgg/essential/vigilance/data/PropertyData;
public final fun getHasDependants ()Z
public final fun getInstance ()Lgg/essential/vigilance/Vigilant;
public final fun getState ()Lgg/essential/elementa/state/State;
public final fun getValue ()Lgg/essential/vigilance/data/PropertyValue;
public final fun getWriteDataToFile ()Z
public fun hashCode ()I
public final fun isHidden ()Z
public final fun setAction (Lkotlin/jvm/functions/Function1;)V
Expand Down Expand Up @@ -353,13 +359,25 @@ public abstract class gg/essential/vigilance/data/PropertyValue {
public abstract fun setValue (Ljava/lang/Object;Lgg/essential/vigilance/Vigilant;)V
}

public final class gg/essential/vigilance/data/PropertyValueBackedState : gg/essential/elementa/state/State {
public fun <init> (Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/Vigilant;)V
public fun get ()Ljava/lang/Object;
public fun set (Ljava/lang/Object;)V
}

public class gg/essential/vigilance/data/SortingBehavior {
public fun <init> ()V
public fun getCategoryComparator ()Ljava/util/Comparator;
public fun getPropertyComparator ()Ljava/util/Comparator;
public fun getSubcategoryComparator ()Ljava/util/Comparator;
}

public final class gg/essential/vigilance/data/StateBackedPropertyValue : gg/essential/vigilance/data/PropertyValue {
public fun <init> (Lgg/essential/elementa/state/State;)V
public fun getValue (Lgg/essential/vigilance/Vigilant;)Ljava/lang/Object;
public fun setValue (Ljava/lang/Object;Lgg/essential/vigilance/Vigilant;)V
}

public final class gg/essential/vigilance/data/ValueBackedPropertyValue : gg/essential/vigilance/data/PropertyValue {
public fun <init> (Ljava/lang/Object;)V
public fun getValue (Lgg/essential/vigilance/Vigilant;)Ljava/lang/Object;
Expand Down
75 changes: 60 additions & 15 deletions src/main/kotlin/gg/essential/vigilance/Vigilant.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gg.essential.vigilance

import com.electronwill.nightconfig.core.file.FileConfig
import gg.essential.elementa.state.BasicState
import gg.essential.elementa.state.State
import gg.essential.universal.UChat
import gg.essential.vigilance.data.*
import gg.essential.vigilance.gui.SettingsGui
Expand Down Expand Up @@ -99,17 +101,14 @@ abstract class Vigilant @JvmOverloads constructor(

@Suppress("UNCHECKED_CAST")
fun <T> registerListener(field: Field, listener: Consumer<T>) {
propertyCollector
.getProperties()
.firstOrNull { it.value is FieldBackedPropertyValue && it.value.field == field }!!
.action = { obj -> listener.accept(obj as T) }
propertyCollector.getProperty(field)!!
.setCallbackConsumer { obj -> listener.accept(obj as T) }
}

@Suppress("UNCHECKED_CAST")
fun <T> registerListener(propertyName: String, listener: Consumer<T>) {
propertyCollector.getProperties()
.firstOrNull { it.value is FieldBackedPropertyValue && it.value.field.name == propertyName }!!
.action = { obj -> listener.accept(obj as T) }
propertyCollector.getProperty(propertyName)!!
.setCallbackConsumer { obj -> listener.accept(obj as T) }
}

@Deprecated(
Expand Down Expand Up @@ -220,7 +219,7 @@ abstract class Vigilant @JvmOverloads constructor(
private fun readData() {
fileConfig.load()

propertyCollector.getProperties().filter { it.value.writeDataToFile }.forEach {
propertyCollector.getProperties().filter { it.writeDataToFile }.forEach {
val fullPath = it.attributesExt.fullPropertyPath()

var oldValue: Any? = fileConfig.get(fullPath)
Expand Down Expand Up @@ -249,7 +248,7 @@ abstract class Vigilant @JvmOverloads constructor(
fun writeData() {
if (!dirty) return

propertyCollector.getProperties().filter { it.value.writeDataToFile }.forEach {
propertyCollector.getProperties().filter { it.writeDataToFile }.forEach {
val fullPath = it.attributesExt.fullPropertyPath()

var toSet = it.getAsAny()
Expand Down Expand Up @@ -325,7 +324,7 @@ abstract class Vigilant @JvmOverloads constructor(
}

fun <T> property(
value: PropertyValue,
state: State<Any?>,
type: PropertyType,
name: String,
description: String = "",
Expand Down Expand Up @@ -362,9 +361,15 @@ abstract class Vigilant @JvmOverloads constructor(
triggerActionOnInitialization = triggerActionOnInitialization,
hidden = hidden,
),
value,
state,
instance
).also { it.attributesExt.searchTags.toMutableList().addAll(searchTags) }
).also {
if (type == PropertyType.BUTTON) {
it.writeDataToFile = false
}

it.attributesExt.searchTags.toMutableList().addAll(searchTags)
}

if (action != null) {
data.action = { action(it as T) }
Expand All @@ -373,6 +378,46 @@ abstract class Vigilant @JvmOverloads constructor(
properties.add(data)
}

fun <T> property(
value: PropertyValue,
type: PropertyType,
name: String,
description: String = "",
searchTags: List<String> = listOf(),
min: Int = 0,
max: Int = 0,
minF: Float = 0f,
maxF: Float = 0f,
decimalPlaces: Int = 1,
increment: Int = 1,
options: List<String> = listOf(),
allowAlpha: Boolean = true,
placeholder: String = "",
triggerActionOnInitialization: Boolean = true,
hidden: Boolean = false,
action: ((T) -> Unit)? = null
) {
property(
state = PropertyValueBackedState<Any?>(value, instance),
type = type,
name = name,
description = description,
searchTags = searchTags,
min = min,
max = max,
minF = minF,
maxF = maxF,
decimalPlaces = decimalPlaces,
increment = increment,
options = options,
allowAlpha = allowAlpha,
placeholder = placeholder,
triggerActionOnInitialization = triggerActionOnInitialization,
hidden = hidden,
action = action
)
}

fun <T> property(
field: KMutableProperty0<T>,
type: PropertyType,
Expand Down Expand Up @@ -631,15 +676,15 @@ abstract class Vigilant @JvmOverloads constructor(
hidden: Boolean = false,
action: (() -> Unit)
) {
property<Nothing>(
KFunctionBackedPropertyValue(action),
property(
BasicState(Unit),
PropertyType.BUTTON,
name,
description,
placeholder = buttonText,
triggerActionOnInitialization = triggerActionOnInitialization,
hidden = hidden,
action = null
action = { _: Any? -> action() }
)
}

Expand Down
17 changes: 12 additions & 5 deletions src/main/kotlin/gg/essential/vigilance/data/PropertyCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ abstract class PropertyCollector {
}

internal fun getProperty(propertyName: String): PropertyData? = collectedProperties.firstOrNull {
it.value is FieldBackedPropertyValue && it.value.field.name == propertyName ||
it.value is KPropertyBackedPropertyValue<*> && it.value.property.name == propertyName ||
it.value is MethodBackedPropertyValue && it.value.method.name == propertyName
propertyName == it.value.let { propertyValue ->
when (propertyValue) {
is FieldBackedPropertyValue -> propertyValue.field.name
is KPropertyBackedPropertyValue<*> -> propertyValue.property.name
is MethodBackedPropertyValue -> propertyValue.method.name
else -> return@firstOrNull false
}
}
}

internal fun getProperty(field: Field): PropertyData? = collectedProperties.firstOrNull {
it.value is FieldBackedPropertyValue && it.value.field == field ||
it.value is KPropertyBackedPropertyValue<*> && it.value.property.javaField == field
it.value.let { propertyValue ->
propertyValue is FieldBackedPropertyValue && propertyValue.field == field ||
propertyValue is KPropertyBackedPropertyValue<*> && propertyValue.property.javaField == field
}
}
}

Expand Down
Loading