Skip to content

Commit

Permalink
better saving
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Apr 8, 2024
1 parent 0482db4 commit 51303c3
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 47 deletions.
22 changes: 18 additions & 4 deletions src/main/kotlin/org/polyfrost/crosshair/PolyCrosshair.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.polyfrost.crosshair

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 net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.FMLInitializationEvent
Expand All @@ -26,13 +28,25 @@ object PolyCrosshair {

@Mod.EventHandler
fun onFMLInitialization(event: FMLInitializationEvent) {
if (!dir.exists()) {
dir.mkdirs()
}
clearCaches()
dir.mkdirs()
ModConfig
MinecraftForge.EVENT_BUS.register(CrosshairRenderer)
EventManager.INSTANCE.register(this)
CrosshairRenderer.updateTexture()
}

@Subscribe
fun onShutDown(e: ShutdownEvent) {
clearCaches()
}

fun clearCaches() {
if (dir.listFiles()?.isNotEmpty() == true) {
for (file in dir.listFiles()!!) {
file.delete()
}
}
dir.delete()
}

}
43 changes: 19 additions & 24 deletions src/main/kotlin/org/polyfrost/crosshair/config/Drawer.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
@file:Suppress("UnstableAPIUsage")

package org.polyfrost.crosshair.config

import cc.polyfrost.oneconfig.config.elements.BasicOption
import cc.polyfrost.oneconfig.events.EventManager
import cc.polyfrost.oneconfig.gui.elements.BasicButton
import cc.polyfrost.oneconfig.images.OneImage
import cc.polyfrost.oneconfig.libs.universal.*
Expand Down Expand Up @@ -44,14 +42,19 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
private val colorSelector = ColorSelector()

init {
Utils.toBufferedImage(ModConfig.currentCrosshair)?.let {
loadImage(it, false)?.let {
CrosshairRenderer.updateTexture(it)
}
}
clearButton.setClickAction {
for (pixel in pixels) {
pixel.isToggled = false
}
}
saveButton.setClickAction {
runAsync {
save(saveFromDrawer())
Utils.save(saveFromDrawer())
}
}
exportButton.setClickAction {
Expand All @@ -68,7 +71,6 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
}
}
}
EventManager.INSTANCE.register(this)
}

override fun draw(vg: Long, x: Int, y: Int, inputHandler: InputHandler) {
Expand All @@ -93,7 +95,7 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
exportButton.draw(vg, (x + 270).toFloat(), y.toFloat(), inputHandler)

for (i in removeQueue) {
ModConfig.presets.remove(i)
ModConfig.crosshairs.remove(i)
getElement(i).onRemove()
elements.remove(i)
}
Expand All @@ -102,10 +104,10 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {

val scissor = ScissorHelper.INSTANCE.scissor(vg, (x + 349).toFloat(), y.toFloat(), 644f, 254f)

for (i in 0..<ModConfig.presets.size) {
for (i in 0..<ModConfig.crosshairs.size) {
val posX = i % 4
val posY = i / 4
getElement(ModConfig.presets[i]).draw(vg, x + 349 + posX * 165f, y + posY * 165f, inputHandler)
getElement(ModConfig.crosshairs[i]).draw(vg, x + 349 + posX * 165f, y + posY * 165f, inputHandler)
}

ScissorHelper.INSTANCE.resetScissor(vg, scissor)
Expand All @@ -124,48 +126,39 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
return bufferedImage
}

fun loadImage(image: BufferedImage?, save: Boolean) {
fun loadImage(image: BufferedImage?, save: Boolean): OneImage? {
val loadedImage = OneImage(image)
if (loadedImage.width != 15 || loadedImage.height != 15) {
notify("Image must be 15 x 15.")
return
return null
}
for (i in 0..224) {
val pos = indexToPos(i)
val c = loadedImage.image.getRGB(pos.x, pos.y)
pixels[i].isToggled = c shr 24 != 0
pixels[i].color = c
}
if (save) save(loadedImage)
if (save) Utils.save(loadedImage)
return loadedImage
}

fun saveFromDrawer(): OneImage? {
val image = OneImage(15, 15)
if (ModConfig.crosshair.isEmpty()) {
if (ModConfig.drawer.isEmpty()) {
notify("Crosshair cant be empty.")
return null
}
for (i in ModConfig.crosshair) {
for (i in ModConfig.drawer) {
val pos = indexToPos(i.key)
val c = i.value.color
image.setColorAtPos(pos.x, pos.y, c)
}
return image
}

fun save(image: OneImage?) {
image ?: return
val base64 = Utils.toBase64(image.image)
if (ModConfig.presets.contains(base64)) {
notify("Duplicated crosshair.")
return
}
ModConfig.presets.add(base64)
}

fun move(x: Int, y: Int) {
val newPositions = HashMap<Pos, Int>()
for (i in ModConfig.crosshair) {
for (i in ModConfig.drawer) {
val pos = indexToPos(i.key)
val posX = pos.x + x
val posY = pos.y + y
Expand Down Expand Up @@ -197,7 +190,9 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
}

override fun finishUpAndClose() {
CrosshairRenderer.updateTexture()
val image = saveFromDrawer() ?: return
ModConfig.currentCrosshair = Utils.toBase64(image.image)
CrosshairRenderer.updateTexture(image)
}

override fun getHeight() = 254
Expand Down
15 changes: 6 additions & 9 deletions src/main/kotlin/org/polyfrost/crosshair/config/ModConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import java.lang.reflect.Field

object ModConfig : Config(Mod(PolyCrosshair.NAME, ModType.HUD), "${PolyCrosshair.MODID}/config.json") {

@CustomOption
var crosshair = HashMap<Int, PixelInfo>()
@Exclude
var drawer = HashMap<Int, PixelInfo>()

var presets = ArrayList<String>() //base64
var currentCrosshair = ""

@CustomOption
var crosshairs = ArrayList<String>()

@Exclude
var penColor = OneColor(255, 255, 255, 255)
Expand Down Expand Up @@ -66,12 +69,6 @@ object ModConfig : Config(Mod(PolyCrosshair.NAME, ModType.HUD), "${PolyCrosshair

init {
initialize()
for (i in 0..224) {
Drawer.pixels[i].isToggled = false
val crosshair = crosshair[i] ?: continue
Drawer.pixels[i].isToggled = true
Drawer.pixels[i].color = crosshair.color
}
val options = listOf("hostile", "passive", "player", "hostileColor", "passiveColor", "playerColor")
for (i in options) {
hideIf(i) { !dynamicColor }
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/org/polyfrost/crosshair/config/Pixel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Pixel(val index: Int) : BasicElement(16, 16, ColorPalette.PRIMARY, true, 0
var color = -1
set(value) {
if (value shr 24 == 0) isToggled = false
ModConfig.crosshair[index]?.color = value
ModConfig.drawer[index]?.color = value
field = value
}

Expand All @@ -32,9 +32,9 @@ class Pixel(val index: Int) : BasicElement(16, 16, ColorPalette.PRIMARY, true, 0
if (lastToggled != isToggled) {
lastToggled = isToggled
if (isToggled) {
ModConfig.crosshair[index] = PixelInfo(color)
ModConfig.drawer[index] = PixelInfo(color)
} else {
ModConfig.crosshair.remove(index)
ModConfig.drawer.remove(index)
}
}
currentColor = if (isToggled) color else if (index % 2 == 0) ColorPalette.SECONDARY.normalColor else ColorPalette.SECONDARY.hoveredColor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@file:Suppress("UnstableAPIUsage")
package org.polyfrost.crosshair.render

import cc.polyfrost.oneconfig.config.core.OneColor
import cc.polyfrost.oneconfig.images.OneImage
import cc.polyfrost.oneconfig.libs.universal.UResolution
import cc.polyfrost.oneconfig.utils.dsl.mc
import net.minecraft.client.gui.Gui
Expand All @@ -25,13 +27,14 @@ object CrosshairRenderer {
private val whiteTextureLocation: ResourceLocation =
mc.textureManager.getDynamicTextureLocation("polycrosshair", whiteTexture)

fun updateTexture() {
fun updateTexture(image: OneImage) {
for (i in 0 until 225) {
whiteTexture.textureData[i] = 0x00000000
texture.textureData[i] = 0x00000000
ModConfig.crosshair[i] ?: continue
val color = image.image.getRGB(i % 15, i / 15)
if (color == 0x00000000) continue
whiteTexture.textureData[i] = Color(-1).rgb
texture.textureData[i] = ModConfig.crosshair[i]!!.color
texture.textureData[i] = color
}
texture.updateDynamicTexture()
whiteTexture.updateDynamicTexture()
Expand Down
21 changes: 17 additions & 4 deletions src/main/kotlin/org/polyfrost/crosshair/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cc.polyfrost.oneconfig.images.OneImage
import cc.polyfrost.oneconfig.utils.IOUtils
import cc.polyfrost.oneconfig.utils.Notifications
import org.polyfrost.crosshair.PolyCrosshair
import org.polyfrost.crosshair.config.ModConfig
import java.awt.Image
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
Expand All @@ -17,15 +18,26 @@ private fun notify(message: String) = Notifications.INSTANCE.send(PolyCrosshair.

object Utils {

fun export(image: BufferedImage, name: String): String {
fun export(image: BufferedImage?, name: String): String {
image ?: return ""
val path = PolyCrosshair.path + name + ".png"
OneImage(image).save(path)
return path
}

fun toBufferedImage(string: String): BufferedImage {
fun save(image: OneImage?) {
image ?: return
val base64 = toBase64(image.image)
if (ModConfig.crosshairs.contains(base64)) {
notify("Duplicated crosshair.")
return
}
ModConfig.crosshairs.add(base64)
}

fun toBufferedImage(string: String): BufferedImage? {
val bytes = Base64.getDecoder().decode(string)
return ImageIO.read(ByteArrayInputStream(bytes));
return ImageIO.read(ByteArrayInputStream(bytes))
}

fun toBase64(image: BufferedImage): String {
Expand All @@ -36,7 +48,8 @@ object Utils {
return encoded
}

fun copy(image: Image) {
fun copy(image: Image?) {
image ?: return
IOUtils.copyImageToClipboard(image)
notify("Crosshair has been copied to clipboard.")
}
Expand Down

0 comments on commit 51303c3

Please sign in to comment.