Skip to content

Commit

Permalink
Amend overlay position editing
Browse files Browse the repository at this point in the history
  • Loading branch information
VixidDev committed Jun 17, 2024
1 parent 70109a8 commit c0e14bb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/main/java/dev/vixid/vsm/config/VSMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.gson.annotations.Expose;
import com.mojang.brigadier.CommandDispatcher;
import dev.vixid.vsm.VSM;
import dev.vixid.vsm.utils.ChatUtils;
import io.github.notenoughupdates.moulconfig.Config;
import io.github.notenoughupdates.moulconfig.Social;
import io.github.notenoughupdates.moulconfig.annotations.Category;
Expand Down Expand Up @@ -49,7 +48,7 @@ public String getTitle() {

@Override
public void saveNow() {
ChatUtils.INSTANCE.chat("Saved config");
VSM.INSTANCE.getLogger().debug("Saved config");
super.saveNow();
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/dev/vixid/vsm/config/core/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public void add(int x, int y) {
this.y += y;
}

public void set(int x, int y) {
this.x = x;
this.y = y;
}

public UUID getUuid() {
if (this.uuid == null) {
this.uuid = UUID.randomUUID();
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/dev/vixid/vsm/VSM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import org.slf4j.Logger
import org.slf4j.LoggerFactory

object VSM {
private val logger = LoggerFactory.getLogger("vsm")
val logger: Logger = LoggerFactory.getLogger("vsm")

val config = ManagedConfig.create(File("config/vsm/config.json"), VSMConfig::class.java) {
// Overwrite the GsonMapper to our own, so we can exclude fields without Expose annotation
Expand Down
19 changes: 14 additions & 5 deletions src/main/kotlin/dev/vixid/vsm/overlays/PositionEditor.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.vixid.vsm.overlays

import dev.vixid.vsm.VSM
import dev.vixid.vsm.config.core.Position
import dev.vixid.vsm.utils.RenderUtils
import java.util.UUID
Expand All @@ -24,20 +25,28 @@ class PositionEditor(private val editorOverlays: Map<UUID, Pair<String, Position
}

override fun mouseDragged(mouseX: Double, mouseY: Double, button: Int, deltaX: Double, deltaY: Double): Boolean {
val position = mouseIsOverAnOverlay(mouseX, mouseY)
val label = getHoveredOverlayData(mouseX, mouseY)
val width = textRenderer.getWidth(label.first) / 2
val position = label.second

if (position.x != -1 && position.y != -1 && button == 0) {
position.add(deltaX.toInt(), deltaY.toInt())
position.set(mouseX.toInt() - width, mouseY.toInt() - textRenderer.fontHeight / 2)
}
return true
}

private fun mouseIsOverAnOverlay(mouseX: Double, mouseY: Double) : Position {
private fun getHoveredOverlayData(mouseX: Double, mouseY: Double) : Pair<String, Position> {
for (overlay in editorOverlays) {
val string = overlay.value.first
val position = overlay.value.second
val bounds = RenderUtils.getRectangleBoundsForString(string, position)
if (bounds.containsPosition(Position(mouseX, mouseY))) return overlay.value.second
if (bounds.containsPosition(Position(mouseX, mouseY))) return overlay.value
}
return Position(-1, -1)
return Pair("", Position(-1, -1))
}

override fun close() {
super.close()
VSM.config.instance.saveNow()
}
}

0 comments on commit c0e14bb

Please sign in to comment.