Skip to content

Commit a49648e

Browse files
committed
Properties: Superseded PropertyValues by Elementa's States (concept)
1 parent c43db5a commit a49648e

File tree

6 files changed

+235
-104
lines changed

6 files changed

+235
-104
lines changed

api/Vigilance.api

+18
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ public final class gg/essential/vigilance/Vigilant$CategoryPropertyBuilder {
7373
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
7474
public final fun percentSlider (Lkotlin/reflect/KMutableProperty0;Ljava/lang/String;Ljava/lang/String;ZZLkotlin/jvm/functions/Function1;)V
7575
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
76+
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
7677
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
7778
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
7879
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
80+
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
7981
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
8082
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
8183
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
@@ -276,7 +278,9 @@ public abstract class gg/essential/vigilance/data/PropertyCollector {
276278

277279
public final class gg/essential/vigilance/data/PropertyData {
278280
public static final field Companion Lgg/essential/vigilance/data/PropertyData$Companion;
281+
public fun <init> (Lgg/essential/vigilance/data/PropertyAttributes;Lgg/essential/elementa/state/State;Lgg/essential/vigilance/Vigilant;)V
279282
public fun <init> (Lgg/essential/vigilance/data/PropertyAttributes;Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/Vigilant;)V
283+
public fun <init> (Lgg/essential/vigilance/data/PropertyAttributesExt;Lgg/essential/elementa/state/State;Lgg/essential/vigilance/Vigilant;)V
280284
public fun <init> (Lgg/essential/vigilance/data/PropertyAttributesExt;Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/Vigilant;)V
281285
public final fun component1 ()Lgg/essential/vigilance/data/PropertyAttributes;
282286
public final fun component2 ()Lgg/essential/vigilance/data/PropertyValue;
@@ -294,7 +298,9 @@ public final class gg/essential/vigilance/data/PropertyData {
294298
public final fun getDependsOn ()Lgg/essential/vigilance/data/PropertyData;
295299
public final fun getHasDependants ()Z
296300
public final fun getInstance ()Lgg/essential/vigilance/Vigilant;
301+
public final fun getState ()Lgg/essential/elementa/state/State;
297302
public final fun getValue ()Lgg/essential/vigilance/data/PropertyValue;
303+
public final fun getWriteDataToFile ()Z
298304
public fun hashCode ()I
299305
public final fun isHidden ()Z
300306
public final fun setAction (Lkotlin/jvm/functions/Function1;)V
@@ -353,13 +359,25 @@ public abstract class gg/essential/vigilance/data/PropertyValue {
353359
public abstract fun setValue (Ljava/lang/Object;Lgg/essential/vigilance/Vigilant;)V
354360
}
355361

362+
public final class gg/essential/vigilance/data/PropertyValueBackedState : gg/essential/elementa/state/State {
363+
public fun <init> (Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/Vigilant;)V
364+
public fun get ()Ljava/lang/Object;
365+
public fun set (Ljava/lang/Object;)V
366+
}
367+
356368
public class gg/essential/vigilance/data/SortingBehavior {
357369
public fun <init> ()V
358370
public fun getCategoryComparator ()Ljava/util/Comparator;
359371
public fun getPropertyComparator ()Ljava/util/Comparator;
360372
public fun getSubcategoryComparator ()Ljava/util/Comparator;
361373
}
362374

375+
public final class gg/essential/vigilance/data/StateBackedPropertyValue : gg/essential/vigilance/data/PropertyValue {
376+
public fun <init> (Lgg/essential/elementa/state/State;)V
377+
public fun getValue (Lgg/essential/vigilance/Vigilant;)Ljava/lang/Object;
378+
public fun setValue (Ljava/lang/Object;Lgg/essential/vigilance/Vigilant;)V
379+
}
380+
363381
public final class gg/essential/vigilance/data/ValueBackedPropertyValue : gg/essential/vigilance/data/PropertyValue {
364382
public fun <init> (Ljava/lang/Object;)V
365383
public fun getValue (Lgg/essential/vigilance/Vigilant;)Ljava/lang/Object;

src/main/kotlin/gg/essential/vigilance/Vigilant.kt

+60-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package gg.essential.vigilance
22

33
import com.electronwill.nightconfig.core.file.FileConfig
4+
import gg.essential.elementa.state.BasicState
5+
import gg.essential.elementa.state.State
46
import gg.essential.universal.UChat
57
import gg.essential.vigilance.data.*
68
import gg.essential.vigilance.gui.SettingsGui
@@ -99,17 +101,14 @@ abstract class Vigilant @JvmOverloads constructor(
99101

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

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

115114
@Deprecated(
@@ -220,7 +219,7 @@ abstract class Vigilant @JvmOverloads constructor(
220219
private fun readData() {
221220
fileConfig.load()
222221

223-
propertyCollector.getProperties().filter { it.value.writeDataToFile }.forEach {
222+
propertyCollector.getProperties().filter { it.writeDataToFile }.forEach {
224223
val fullPath = it.attributesExt.fullPropertyPath()
225224

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

252-
propertyCollector.getProperties().filter { it.value.writeDataToFile }.forEach {
251+
propertyCollector.getProperties().filter { it.writeDataToFile }.forEach {
253252
val fullPath = it.attributesExt.fullPropertyPath()
254253

255254
var toSet = it.getAsAny()
@@ -325,7 +324,7 @@ abstract class Vigilant @JvmOverloads constructor(
325324
}
326325

327326
fun <T> property(
328-
value: PropertyValue,
327+
state: State<Any?>,
329328
type: PropertyType,
330329
name: String,
331330
description: String = "",
@@ -362,9 +361,15 @@ abstract class Vigilant @JvmOverloads constructor(
362361
triggerActionOnInitialization = triggerActionOnInitialization,
363362
hidden = hidden,
364363
),
365-
value,
364+
state,
366365
instance
367-
).also { it.attributesExt.searchTags.toMutableList().addAll(searchTags) }
366+
).also {
367+
if (type == PropertyType.BUTTON) {
368+
it.writeDataToFile = false
369+
}
370+
371+
it.attributesExt.searchTags.toMutableList().addAll(searchTags)
372+
}
368373

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

381+
fun <T> property(
382+
value: PropertyValue,
383+
type: PropertyType,
384+
name: String,
385+
description: String = "",
386+
searchTags: List<String> = listOf(),
387+
min: Int = 0,
388+
max: Int = 0,
389+
minF: Float = 0f,
390+
maxF: Float = 0f,
391+
decimalPlaces: Int = 1,
392+
increment: Int = 1,
393+
options: List<String> = listOf(),
394+
allowAlpha: Boolean = true,
395+
placeholder: String = "",
396+
triggerActionOnInitialization: Boolean = true,
397+
hidden: Boolean = false,
398+
action: ((T) -> Unit)? = null
399+
) {
400+
property(
401+
state = PropertyValueBackedState<Any?>(value, instance),
402+
type = type,
403+
name = name,
404+
description = description,
405+
searchTags = searchTags,
406+
min = min,
407+
max = max,
408+
minF = minF,
409+
maxF = maxF,
410+
decimalPlaces = decimalPlaces,
411+
increment = increment,
412+
options = options,
413+
allowAlpha = allowAlpha,
414+
placeholder = placeholder,
415+
triggerActionOnInitialization = triggerActionOnInitialization,
416+
hidden = hidden,
417+
action = action
418+
)
419+
}
420+
376421
fun <T> property(
377422
field: KMutableProperty0<T>,
378423
type: PropertyType,
@@ -631,15 +676,15 @@ abstract class Vigilant @JvmOverloads constructor(
631676
hidden: Boolean = false,
632677
action: (() -> Unit)
633678
) {
634-
property<Nothing>(
635-
KFunctionBackedPropertyValue(action),
679+
property(
680+
BasicState(Unit),
636681
PropertyType.BUTTON,
637682
name,
638683
description,
639684
placeholder = buttonText,
640685
triggerActionOnInitialization = triggerActionOnInitialization,
641686
hidden = hidden,
642-
action = null
687+
action = { _: Any? -> action() }
643688
)
644689
}
645690

src/main/kotlin/gg/essential/vigilance/data/PropertyCollector.kt

+12-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@ abstract class PropertyCollector {
2020
}
2121

2222
internal fun getProperty(propertyName: String): PropertyData? = collectedProperties.firstOrNull {
23-
it.value is FieldBackedPropertyValue && it.value.field.name == propertyName ||
24-
it.value is KPropertyBackedPropertyValue<*> && it.value.property.name == propertyName ||
25-
it.value is MethodBackedPropertyValue && it.value.method.name == propertyName
23+
propertyName == it.value.let { propertyValue ->
24+
when (propertyValue) {
25+
is FieldBackedPropertyValue -> propertyValue.field.name
26+
is KPropertyBackedPropertyValue<*> -> propertyValue.property.name
27+
is MethodBackedPropertyValue -> propertyValue.method.name
28+
else -> return@firstOrNull false
29+
}
30+
}
2631
}
2732

2833
internal fun getProperty(field: Field): PropertyData? = collectedProperties.firstOrNull {
29-
it.value is FieldBackedPropertyValue && it.value.field == field ||
30-
it.value is KPropertyBackedPropertyValue<*> && it.value.property.javaField == field
34+
it.value.let { propertyValue ->
35+
propertyValue is FieldBackedPropertyValue && propertyValue.field == field ||
36+
propertyValue is KPropertyBackedPropertyValue<*> && propertyValue.property.javaField == field
37+
}
3138
}
3239
}
3340

0 commit comments

Comments
 (0)