diff options
author | Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> | 2024-06-10 03:11:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-10 03:11:42 +0200 |
commit | 1fe45f026afeff9d87107380bf8c4cf58051b29f (patch) | |
tree | 362a2d3cf9f0e1c4dbfabb710cd51ab0c7c11ee3 /src/main/java/at/hannibal2/skyhanni/data | |
parent | b1ff611e4f764c5fba338571540f0ad2389994ac (diff) | |
download | skyhanni-1fe45f026afeff9d87107380bf8c4cf58051b29f.tar.gz skyhanni-1fe45f026afeff9d87107380bf8c4cf58051b29f.tar.bz2 skyhanni-1fe45f026afeff9d87107380bf8c4cf58051b29f.zip |
Backend: Graph Editor (#1454)
Co-authored-by: Cal <cwolfson58@gmail.com>
Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com>
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/model/Graph.kt | 30 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/model/TextInput.kt | 133 |
2 files changed, 151 insertions, 12 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/model/Graph.kt b/src/main/java/at/hannibal2/skyhanni/data/model/Graph.kt index c25a567fb..76c97e662 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/model/Graph.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/model/Graph.kt @@ -1,5 +1,6 @@ package at.hannibal2.skyhanni.data.model +import at.hannibal2.skyhanni.utils.LorenzUtils.round import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.json.SkyHanniTypeAdapters.registerTypeAdapter import at.hannibal2.skyhanni.utils.json.fromJson @@ -36,7 +37,8 @@ value class Graph( override fun lastIndexOf(element: GraphNode) = graph.lastIndexOf(element) companion object { - val gson = GsonBuilder().setPrettyPrinting().registerTypeAdapter<Graph>({ out, value -> + val gson = GsonBuilder().setPrettyPrinting().registerTypeAdapter<Graph>( + { out, value -> out.beginObject() value.forEach { out.name(it.id.toString()).beginObject() @@ -48,13 +50,14 @@ value class Graph( out.beginObject() it.neighbours.forEach { (node, weight) -> val id = node.id.toString() - out.name(id).value(weight) + out.name(id).value(weight.round(2)) } out.endObject() out.endObject() } out.endObject() - }, { reader -> + }, + { reader -> reader.beginObject() val list = mutableListOf<GraphNode>() val neigbourMap = mutableMapOf<GraphNode, List<Pair<Int, Double>>>() @@ -100,7 +103,8 @@ value class Graph( } reader.endObject() Graph(list) - }).create() + }, + ).create() fun fromJson(json: String): Graph = gson.fromJson<Graph>(json) fun fromJson(json: JsonElement): Graph = gson.fromJson<Graph>(json) @@ -158,14 +162,16 @@ fun Graph.findShortestPathAsGraphWithDistance(start: GraphNode, end: GraphNode): } } - return Graph(buildList { - var current = end - while (current != start) { - add(current) - current = previous[current] ?: return Graph(emptyList()) to 0.0 - } - add(start) - }.reversed()) to distances[end]!! + return Graph( + buildList { + var current = end + while (current != start) { + add(current) + current = previous[current] ?: return Graph(emptyList()) to 0.0 + } + add(start) + }.reversed(), + ) to distances[end]!! } fun Graph.findShortestPath(start: GraphNode, end: GraphNode): List<LorenzVec> = diff --git a/src/main/java/at/hannibal2/skyhanni/data/model/TextInput.kt b/src/main/java/at/hannibal2/skyhanni/data/model/TextInput.kt new file mode 100644 index 000000000..22ac4982b --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/model/TextInput.kt @@ -0,0 +1,133 @@ +package at.hannibal2.skyhanni.data.model + +import at.hannibal2.skyhanni.utils.KeyboardManager +import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyClicked +import at.hannibal2.skyhanni.utils.OSUtils +import at.hannibal2.skyhanni.utils.StringUtils.insert +import kotlinx.coroutines.runBlocking +import net.minecraft.client.settings.KeyBinding +import org.lwjgl.input.Keyboard +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable + +class TextInput { + + var textBox: String = "" + private var carriage: Int? = null + + fun editText() = textBox.let { + with(carriage) { + if (this == null) it + else it.insert(this, '|') + } + }.replace("§", "&&") + + fun finalText() = textBox.replace("&&", "§") + + fun makeActive() = Companion.activate(this) + fun disable() = Companion.disable() + fun handle() = Companion.handleTextInput() + fun clear() { + textBox = "" + carriage = null + } + + companion object { + private var activeInstance: TextInput? = null + + fun activate(instance: TextInput) { + activeInstance = instance + timeSinceKeyEvent = Keyboard.getEventNanoseconds() + } + + fun disable() { + activeInstance = null + } + + fun onMinecraftInput(keyBinding: KeyBinding, cir: CallbackInfoReturnable<Boolean>) { + if (activeInstance != null) { + cir.returnValue = false + return + } + } + + private var timeSinceKeyEvent = 0L + + private var carriage + get() = activeInstance?.carriage + set(value) { + activeInstance?.carriage = value + } + + private var textBox + get() = activeInstance?.textBox ?: "" + set(value) { + activeInstance?.textBox = value + } + + private fun handleTextInput() { + if (KeyboardManager.isCopyingKeysDown()) { + OSUtils.copyToClipboard(textBox) + return + } + if (KeyboardManager.isPastingKeysDown()) { + runBlocking { + textBox = OSUtils.readFromClipboard() ?: return@runBlocking + } + return + } + val tcarriage = carriage + + if (Keyboard.KEY_LEFT.isKeyClicked()) { + carriage = tcarriage?.moveCarriageLeft() ?: (textBox.length - 1) + return + } + if (Keyboard.KEY_RIGHT.isKeyClicked()) { + carriage = when { + tcarriage == null -> null + (tcarriage >= textBox.length - 1) -> null + else -> moveCarriageRight(tcarriage) + } + return + } + if (Keyboard.KEY_DELETE.isKeyClicked()) { // Does not work for some reason + if (tcarriage != null) { + textBox.removeRange(tcarriage, tcarriage + 1) + } else { + textBox.dropLast(1) + } + return + } + + if (timeSinceKeyEvent == Keyboard.getEventNanoseconds()) return + timeSinceKeyEvent = Keyboard.getEventNanoseconds() + val char = Keyboard.getEventCharacter() + textBox = when (char) { + Char(0) -> return + '\b' -> if (tcarriage != null) { + if (tcarriage == 0) { + textBox.substring(1) + } else { + carriage = tcarriage.minus(1) + textBox.removeRange(tcarriage - 1, tcarriage) + } + } else { + textBox.dropLast(1) + } + + else -> if (tcarriage != null) { + carriage = tcarriage + 1 + textBox.insert(tcarriage, char) + } else { + textBox + char + } + } + } + + private fun moveCarriageRight(tcarriage: Int) = tcarriage + 1 + + private fun Int.moveCarriageLeft(): Int = when { + this > 0 -> this - 1 + else -> 0 + } + } +} |