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

Backend: Ghost Mode Graph Editor #2461

Merged
merged 1 commit into from
Sep 4, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class GraphConfig {
@ConfigEditorKeybind(defaultKey = Keyboard.KEY_F)
public int placeKey = Keyboard.KEY_F;

@Expose
@ConfigOption(name = "Toggle Ghost Position", desc = "Creates or removes the Ghost Position. This helps editing nodes tht are in the air.")
@ConfigEditorKeybind(defaultKey = Keyboard.KEY_F)
public int toggleGhostPosition = Keyboard.KEY_NONE;

@Expose
@ConfigOption(name = "Select Key", desc = "Select the nearest node to be active. Double press to unselect.")
@ConfigEditorKeybind(defaultKey = -98) // Middle Mouse
Expand All @@ -37,7 +42,7 @@ public class GraphConfig {
public int exitKey = Keyboard.KEY_HOME;

@Expose
@ConfigOption(name = "Edit Key", desc = "While holding the Key, edit the position of the active node with the minecraft movement controls.")
@ConfigOption(name = "Edit Key", desc = "While holding the Key, edit the position of the active node or the selection block with the minecraft movement controls.")
@ConfigEditorKeybind(defaultKey = Keyboard.KEY_TAB)
public int editKey = Keyboard.KEY_TAB;

Expand Down
57 changes: 48 additions & 9 deletions src/main/java/at/hannibal2/skyhanni/test/GraphEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import at.hannibal2.skyhanni.utils.KeyboardManager
import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyClicked
import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyHeld
import at.hannibal2.skyhanni.utils.LocationUtils
import at.hannibal2.skyhanni.utils.LocationUtils.distanceSqToPlayer
import at.hannibal2.skyhanni.utils.LocationUtils.playerLocation
import at.hannibal2.skyhanni.utils.LorenzColor
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzVec
Expand All @@ -36,6 +36,7 @@ import kotlinx.coroutines.runBlocking
import net.minecraft.client.settings.KeyBinding
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
import java.awt.Color
import kotlin.math.sqrt
import kotlin.time.Duration.Companion.milliseconds

Expand All @@ -53,6 +54,7 @@ object GraphEditor {

private var activeNode: GraphingNode? = null
private var closedNode: GraphingNode? = null
private var ghostPosition: LorenzVec? = null

private var seeThroughBlocks = true

Expand Down Expand Up @@ -92,6 +94,15 @@ object GraphEditor {
if (!isEnabled()) return
nodes.forEach { event.drawNode(it) }
edges.forEach { event.drawEdge(it) }
ghostPosition?.let {
event.drawWaypointFilled(
it,
if (activeNode == null) Color.RED else Color.GRAY,
seeThroughBlocks = seeThroughBlocks,
minimumAlpha = 0.2f,
inverseAlphaScale = true,
)
}
}

@SubscribeEvent
Expand All @@ -112,12 +123,19 @@ object GraphEditor {
add("§eLoad: §6${KeyboardManager.getKeyName(config.loadKey)}")
add("§eClear: §6${KeyboardManager.getKeyName(config.clearKey)}")
add("§eTutorial: §6${KeyboardManager.getKeyName(config.tutorialKey)}")
add("§eToggle Ghost Position: §6${KeyboardManager.getKeyName(config.toggleGhostPosition)}")
add(" ")
if (activeNode != null) add("§eText: §6${KeyboardManager.getKeyName(config.textKey)}")
}
if (!inTextMode && activeNode != null) {
add("§eEdit: §6${KeyboardManager.getKeyName(config.editKey)}")

if (!inTextMode) {
if (activeNode != null) {
add("§eEdit active node: §6${KeyboardManager.getKeyName(config.editKey)}")
} else if (ghostPosition != null) {
add("Edit Ghost Position: §6${KeyboardManager.getKeyName(config.editKey)}")
}
}

if (inEditMode) {
add("§ex+ §6${KeyboardManager.getKeyName(KeyboardManager.WasdInputMatrix.w.keyCode)}")
add("§ex- §6${KeyboardManager.getKeyName(KeyboardManager.WasdInputMatrix.s.keyCode)}")
Expand Down Expand Up @@ -274,7 +292,7 @@ object GraphEditor {
editModeClicks()
inEditMode = false
}
if (activeNode != null && config.editKey.isKeyHeld()) {
if ((activeNode != null || ghostPosition != null) && config.editKey.isKeyHeld()) {
inEditMode = true
return
}
Expand Down Expand Up @@ -312,9 +330,12 @@ object GraphEditor {
if (config.placeKey.isKeyClicked()) {
addNode()
}
if (config.toggleGhostPosition.isKeyClicked()) {
toggleGhostPosition()
}
if (config.selectKey.isKeyClicked()) {
activeNode = if (activeNode == closedNode) {
feedBackInTutorial("De selected active node.")
feedBackInTutorial("De-selected active node.")
null
} else {
feedBackInTutorial("Selected new active node.")
Expand Down Expand Up @@ -379,7 +400,13 @@ object GraphEditor {

private fun KeyBinding.handleEditClicks(vector: LorenzVec) {
if (this.keyCode.isKeyClicked()) {
activeNode?.position = activeNode?.position?.plus(vector) ?: return
activeNode?.let {
it.position = it.position + vector
} ?: run {
ghostPosition?.let {
ghostPosition = it + vector
}
}
}
}

Expand All @@ -402,7 +429,8 @@ object GraphEditor {
return
}
}
val position = LocationUtils.playerEyeLocation().roundLocationToBlock()

val position = ghostPosition ?: LocationUtils.playerEyeLocation().roundLocationToBlock()
if (nodes.any { it.position == position }) {
feedBackInTutorial("Can't create node, here is already another one.")
return
Expand All @@ -414,6 +442,16 @@ object GraphEditor {
addEdge(activeNode, node)
}

fun toggleGhostPosition() {
if (ghostPosition != null) {
ghostPosition = null
feedBackInTutorial("Disabled Ghost Position.")
} else {
ghostPosition = LocationUtils.playerEyeLocation().roundLocationToBlock()
feedBackInTutorial("Enabled Ghost Position.")
}
}

private fun getEdgeIndex(node1: GraphingNode?, node2: GraphingNode?) =
if (node1 != null && node2 != null && node1 != node2) GraphingEdge(
node1,
Expand Down Expand Up @@ -482,6 +520,7 @@ object GraphEditor {
edges.clear()
activeNode = null
closedNode = null
ghostPosition = null
}

private fun prune() { //TODO fix
Expand All @@ -492,6 +531,8 @@ object GraphEditor {
}
nodes.removeIf { hasNeighbours[it] == false }
}

fun LorenzVec.distanceSqToPlayer(): Double = ghostPosition?.let { distanceSq(it) } ?: distanceSq(playerLocation())
}

private class GraphingNode(val id: Int, var position: LorenzVec, var name: String? = null) {
Expand Down Expand Up @@ -540,6 +581,4 @@ private class GraphingEdge(val node1: GraphingNode, val node2: GraphingNode) {
}
return result
}

}

Loading