aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/features/debug
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-07-11 21:01:58 +0200
committernea <nea@nea.moe>2023-07-11 21:01:58 +0200
commit4d93f475aadc42c4bf83c3a0749af41659235c71 (patch)
treec8e01710defe66e06c50fa962c72fdac66a35a1f /src/main/kotlin/moe/nea/firmament/features/debug
parent4444fcca44d9a53c8162d69e0e9f19fd214c2f54 (diff)
downloadfirmament-4d93f475aadc42c4bf83c3a0749af41659235c71.tar.gz
firmament-4d93f475aadc42c4bf83c3a0749af41659235c71.tar.bz2
firmament-4d93f475aadc42c4bf83c3a0749af41659235c71.zip
Bulk commit
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/features/debug')
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/debug/DebugView.kt53
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/debug/DeveloperFeatures.kt2
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/debug/ObjectRenderer.kt52
3 files changed, 105 insertions, 2 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/features/debug/DebugView.kt b/src/main/kotlin/moe/nea/firmament/features/debug/DebugView.kt
new file mode 100644
index 0000000..e8c85d8
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/features/debug/DebugView.kt
@@ -0,0 +1,53 @@
+package moe.nea.firmament.features.debug
+
+import io.github.cottonmc.cotton.gui.client.CottonHud
+import io.github.cottonmc.cotton.gui.widget.WBox
+import io.github.cottonmc.cotton.gui.widget.data.Axis
+import java.util.Optional
+import kotlin.time.Duration.Companion.seconds
+import net.minecraft.scoreboard.Scoreboard
+import net.minecraft.scoreboard.Team
+import net.minecraft.text.StringVisitable
+import net.minecraft.text.Style
+import net.minecraft.text.Text
+import net.minecraft.util.Formatting
+import moe.nea.firmament.Firmament
+import moe.nea.firmament.events.TickEvent
+import moe.nea.firmament.features.FirmamentFeature
+import moe.nea.firmament.util.MC
+import moe.nea.firmament.util.TimeMark
+
+object DebugView : FirmamentFeature {
+ private data class StoredVariable<T>(
+ val obj: T,
+ val timer: TimeMark,
+ )
+
+ private val storedVariables: MutableMap<String, StoredVariable<*>> = mutableMapOf()
+ override val identifier: String
+ get() = "debug-view"
+ override val defaultEnabled: Boolean
+ get() = Firmament.DEBUG
+
+ fun <T : Any?> showVariable(label: String, obj: T) {
+ synchronized(this) {
+ storedVariables[label] = StoredVariable(obj, TimeMark.now())
+ }
+ }
+
+ val debugWidget = WBox(Axis.VERTICAL)
+
+
+ override fun onLoad() {
+ TickEvent.subscribe {
+ synchronized(this) {
+ storedVariables.entries.removeIf { it.value.timer.passedTime() > 1.seconds }
+ if (storedVariables.isEmpty()) {
+ CottonHud.add(debugWidget, 20, 20)
+ } else {
+ CottonHud.remove(debugWidget)
+ }
+ }
+ }
+ }
+}
diff --git a/src/main/kotlin/moe/nea/firmament/features/debug/DeveloperFeatures.kt b/src/main/kotlin/moe/nea/firmament/features/debug/DeveloperFeatures.kt
index e89ee6f..253bc0d 100644
--- a/src/main/kotlin/moe/nea/firmament/features/debug/DeveloperFeatures.kt
+++ b/src/main/kotlin/moe/nea/firmament/features/debug/DeveloperFeatures.kt
@@ -14,8 +14,6 @@ import moe.nea.firmament.util.TimeMark
import moe.nea.firmament.util.iterate
object DeveloperFeatures : FirmamentFeature {
- override val name: String
- get() = "developer"
override val identifier: String
get() = "developer"
override val config: TConfig
diff --git a/src/main/kotlin/moe/nea/firmament/features/debug/ObjectRenderer.kt b/src/main/kotlin/moe/nea/firmament/features/debug/ObjectRenderer.kt
new file mode 100644
index 0000000..f5185cd
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/features/debug/ObjectRenderer.kt
@@ -0,0 +1,52 @@
+package moe.nea.firmament.features.debug
+
+import io.github.cottonmc.cotton.gui.widget.WBox
+import io.github.cottonmc.cotton.gui.widget.WLabel
+import io.github.cottonmc.cotton.gui.widget.WWidget
+import io.github.cottonmc.cotton.gui.widget.data.Axis
+import kotlinx.serialization.encodeToString
+import kotlinx.serialization.json.Json
+import kotlin.reflect.KProperty1
+import net.minecraft.text.Text
+import moe.nea.firmament.gui.WSpacer
+
+class ObjectRenderer(val box: WBox) {
+ var indent = 0
+
+ fun beginObject() {
+ indent++
+ }
+
+ fun endObject() {
+ indent--
+ }
+
+ fun emit(label: String, widget: WWidget) {
+ WSpacer(WBox(Axis.VERTICAL).also {
+ it.add(WWidget())
+ it.add(widget)
+ }, indent * 18)
+ }
+
+ fun <T : Any?> getDebuggingView(label: String, obj: T) {
+ if (obj == null) {
+ emit(label, WLabel(Text.literal("§cnull")))
+ return
+ }
+ if (obj is String) {
+ emit(label, WLabel(Text.literal(Json.encodeToString(obj))))
+ }
+ getObject(label, obj)
+ }
+
+ fun <T : Any> getObject(label: String, obj: T) {
+ emit(label, WLabel(Text.literal(obj::class.simpleName ?: "<unknown>")))
+ beginObject()
+ for (prop in obj::class.members.filterIsInstance<KProperty1<T, *>>()) {
+ val child = prop.get(obj)
+ getDebuggingView(prop.name, child)
+ }
+ endObject()
+ }
+
+}