Skip to content

Commit

Permalink
added gui open command, added crosshair auto scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Jul 26, 2024
1 parent 11b0a9d commit 63eabec
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 57 deletions.
15 changes: 15 additions & 0 deletions src/main/kotlin/org/polyfrost/crosshair/ModCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.polyfrost.crosshair

import cc.polyfrost.oneconfig.utils.commands.annotations.Command
import cc.polyfrost.oneconfig.utils.commands.annotations.Main
import org.polyfrost.crosshair.config.ModConfig

@Command(value = PolyCrosshair.MODID)
class ModCommand {

@Main
fun openGui() {
ModConfig.openGui()
}

}
2 changes: 2 additions & 0 deletions src/main/kotlin/org/polyfrost/crosshair/PolyCrosshair.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cc.polyfrost.oneconfig.config.core.ConfigUtils
import cc.polyfrost.oneconfig.events.EventManager
import cc.polyfrost.oneconfig.events.event.ShutdownEvent
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe
import cc.polyfrost.oneconfig.utils.commands.CommandManager
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.FMLInitializationEvent
Expand Down Expand Up @@ -34,6 +35,7 @@ object PolyCrosshair {
ModConfig
MinecraftForge.EVENT_BUS.register(CrosshairRenderer)
EventManager.INSTANCE.register(this)
CommandManager.INSTANCE.registerCommand(ModCommand())
}

@Mod.EventHandler
Expand Down
64 changes: 10 additions & 54 deletions src/main/kotlin/org/polyfrost/crosshair/config/Drawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {

var removeQueue = ArrayList<CrosshairEntry>()

var moveQueue = ArrayList<MoveType>()

private var scroll = 0f

private var scrollTarget = 0f
Expand Down Expand Up @@ -67,7 +65,7 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
}
resetButton.setClickAction {
runAsync {
reset()
clear()
}
}
saveButton.setClickAction {
Expand Down Expand Up @@ -110,16 +108,6 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
}

override fun draw(vg: Long, x: Int, y: Int, inputHandler: InputHandler) {
if (moveQueue.isNotEmpty()) {
var x = 0
var y = 0
for (i in moveQueue) {
x += i.x
y += i.y
}
move(x, y)
moveQueue.clear()
}

for (posY in 0..<ModConfig.canvaSize) {
for (posX in 0..<ModConfig.canvaSize) {
Expand Down Expand Up @@ -202,6 +190,12 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
return bufferedImage
}

fun clear() {
for (i in pixels) {
i.isToggled = false
}
}

fun loadImage(image: BufferedImage?, save: Boolean, entry: CrosshairEntry = CrosshairEntry()): OneImage? {
val loadedImage = OneImage(image)
val dimensionsSame = loadedImage.width == loadedImage.height
Expand Down Expand Up @@ -242,42 +236,11 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
return image
}

fun reset() {
val newEntry = CrosshairEntry()
toBufferedImage(newEntry.img)?.let {
loadImage(it, false, newEntry)
}
}

fun move(x: Int, y: Int) {
val newPositions = HashMap<Pos, Int>()
for (i in ModConfig.drawer) {
val pos = indexToPos(i.key)
val posX = pos.x + x
val posY = pos.y + y
pixels[i.key].isToggled = false
if (posX !in 0..<ModConfig.canvaSize || posY !in 0..<ModConfig.canvaSize) continue
newPositions[Pos(posX, posY)] = i.value
}
for (i in newPositions) {
val index = i.key.y * 32 + i.key.x
pixels[index].isToggled = true
pixels[index].color = i.value
}
}

fun getElement(entry: CrosshairEntry): PresetElement {
elements[entry] ?: elements.put(entry, PresetElement(entry))
return elements[entry]!!
}

enum class MoveType(val x: Int, val y: Int) {
UP(0, -1),
DOWN(0, 1),
LEFT(-1, 0),
RIGHT(1, 0)
}

override fun finishUpAndClose() {
val image = saveFromDrawer(true) ?: return
ModConfig.newCurrentCrosshair.img = toBase64(image.image)
Expand All @@ -288,18 +251,11 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {

override fun keyTyped(key: Char, keyCode: Int) {
if (mc.currentScreen !is OneConfigGui) return
if (keyCode == UKeyboard.KEY_W) moveQueue.add(MoveType.UP)
if (keyCode == UKeyboard.KEY_S) {
if (UKeyboard.isCtrlKeyDown()) {
runAsync {
save(saveFromDrawer(false))
}
} else {
moveQueue.add(MoveType.DOWN)
if (UKeyboard.isCtrlKeyDown() && keyCode == UKeyboard.KEY_S) {
runAsync {
save(saveFromDrawer(false))
}
}
if (keyCode == UKeyboard.KEY_A) moveQueue.add(MoveType.LEFT)
if (keyCode == UKeyboard.KEY_D) moveQueue.add(MoveType.RIGHT)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class PresetElement(val crosshair: CrosshairEntry) : BasicElement(149, 149, Colo
override fun onClick() {
if (copyButton.isHovered) return
if (removeButton.isHovered) return
Drawer.clear()
Drawer.loadImage(bufferedImage, false, crosshair)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ object CrosshairRenderer {
GL.translate((UResolution.windowWidth / 2).toFloat(), (UResolution.windowHeight / 2).toFloat(), 0f)
GL.rotate(crosshair.rotation.toFloat(), 0f, 0f, 1f)
val scale = crosshair.scale / 100f
val textureSize = if (ModConfig.mode) drawingImage.width else 16
val size = ceil(textureSize * mcScale * scale).toInt()
val translation = if (ModConfig.mode) if (crosshair.centered) (-size / 2).toFloat() else (-(size - mcScale) / 2).toInt().toFloat() else ceil(-7 * mcScale * scale)
val textureSize = 16
val autoScaledSize = if (ModConfig.canvaSize % 2 == 0) 16 else 15
val size = ceil((if (ModConfig.mode) autoScaledSize else textureSize) * mcScale * scale).toInt()
val translation = ceil((if (ModConfig.mode && crosshair.centered) -autoScaledSize / 2f else -7f) * mcScale * scale)
GL.translate(translation, translation, 0f)
Gui.drawScaledCustomSizeModalRect(0, 0, 0f, 0f, textureSize, textureSize, size, size, textureSize.toFloat(), textureSize.toFloat())
val c = getColor()
Expand Down

0 comments on commit 63eabec

Please sign in to comment.