Skip to content

Commit

Permalink
custom size and more
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Apr 10, 2024
1 parent c980d1b commit 478093c
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.polyfrost.crosshair.mixin;

import net.minecraft.client.renderer.EntityRenderer;
import org.polyfrost.crosshair.render.CrosshairRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(EntityRenderer.class)
public abstract class EntityRendererMixin {

@Inject(method = "updateCameraAndRender", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiIngame;renderGameOverlay(F)V"))
private void draw(float partialTicks, long nanoTime, CallbackInfo ci) {
CrosshairRenderer.INSTANCE.drawCrosshair((EntityRenderer) (Object) this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public class GuiIngameMixin {
private void check(CallbackInfoReturnable<Boolean> cir) {
ModConfig cfg = ModConfig.INSTANCE;
Minecraft mc = Minecraft.getMinecraft();
if (!cfg.getShowInThirdPerson() && mc.gameSettings.thirdPersonView != 0) cir.setReturnValue(false);
if (cfg.getShowInSpectator() && mc.playerController.isSpectator()) cir.setReturnValue(true);
if (cfg.getShowInDebug() && mc.gameSettings.showDebugInfo) cir.setReturnValue(true);
if ((!cfg.getShowInGuis() && mc.currentScreen != null) || (!cfg.getShowInThirdPerson() && mc.gameSettings.thirdPersonView != 0)) {
cir.setReturnValue(false);
}
if ((cfg.getShowInSpectator() && mc.playerController.isSpectator()) || (cfg.getShowInDebug() && mc.gameSettings.showDebugInfo)) {
cir.setReturnValue(true);
}
}
}
126 changes: 90 additions & 36 deletions src/main/kotlin/org/polyfrost/crosshair/config/Drawer.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
@file:Suppress("UnstableAPIUsage")
package org.polyfrost.crosshair.config

import cc.polyfrost.oneconfig.config.core.OneColor
import cc.polyfrost.oneconfig.config.elements.BasicOption
import cc.polyfrost.oneconfig.gui.animations.*
import cc.polyfrost.oneconfig.gui.elements.BasicButton
import cc.polyfrost.oneconfig.images.OneImage
import cc.polyfrost.oneconfig.libs.universal.*
import cc.polyfrost.oneconfig.libs.universal.UKeyboard
import cc.polyfrost.oneconfig.renderer.scissor.ScissorHelper
import cc.polyfrost.oneconfig.utils.*
import cc.polyfrost.oneconfig.utils.color.ColorPalette
import cc.polyfrost.oneconfig.utils.dsl.runAsync
import cc.polyfrost.oneconfig.utils.dsl.*
import org.polyfrost.crosshair.PolyCrosshair
import org.polyfrost.crosshair.elements.*
import org.polyfrost.crosshair.render.CrosshairRenderer
import org.polyfrost.crosshair.utils.*
import java.awt.Image
import java.awt.image.BufferedImage
import java.util.*
import kotlin.collections.HashMap
import kotlin.math.ceil


private fun notify(message: String) = Notifications.INSTANCE.send(PolyCrosshair.NAME, message)

object Drawer : BasicOption(null, null, "", "", "", "", 2) {

val pixels: Array<Pixel> = Array(225) { Pixel(it) }
val pixels: Array<Pixel> = Array(1024) { Pixel(it) }

var elements = HashMap<String, PresetElement>()

var removeQueue = ArrayList<String>()

var moveQueue = ArrayList<MoveType>()

private var scroll = 0f

private var scrollTarget = 0f

private var scrollAnimation: Animation = DummyAnimation(0f)

// private val modeSwitch = DualOption("Drawing", "Custom Image")

private val clearButton = BasicButton(64, 32, "Clear", 2, ColorPalette.PRIMARY_DESTRUCTIVE)

private val saveButton = BasicButton(64, 32, "Save", 2, ColorPalette.PRIMARY)
Expand All @@ -48,18 +57,16 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
}
}
clearButton.setClickAction {
for (pixel in pixels) {
pixel.isToggled = false
}
reset()
}
saveButton.setClickAction {
runAsync {
Utils.save(saveFromDrawer())
Utils.save(saveFromDrawer(false))
}
}
exportButton.setClickAction {
runAsync {
saveFromDrawer()?.let { Utils.copy(it.image) }
saveFromDrawer(false)?.let { Utils.copy(it.image) }
}

}
Expand All @@ -74,6 +81,10 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
}

override fun draw(vg: Long, x: Int, y: Int, inputHandler: InputHandler) {
// var y = y
// modeSwitch.draw(vg, x.toFloat(), y.toFloat(), inputHandler)
// if (ModConfig.mode) return
// y += 48
if (moveQueue.isNotEmpty()) {
var x = 0
var y = 0
Expand All @@ -85,9 +96,17 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
moveQueue.clear()
}

for (pixel in pixels) {
pixel.draw(vg, x.toFloat(), y.toFloat(), inputHandler)
for (posY in 0 ..< ModConfig.canvaSize) {
for (posX in 0 ..< ModConfig.canvaSize) {
pixels[Utils.posToIndex(posX, posY)].draw(vg, x.toFloat(), y.toFloat(), inputHandler)
}
}

if (ModConfig.canvaSize % 2 == 0) {
nanoVGHelper.drawLine(vg, (x + 128).toFloat(), (y + 108).toFloat(), (x + 128).toFloat(), (y + 148).toFloat(), 1f, OneColor("703A3AFF").rgb)
nanoVGHelper.drawLine(vg, (x + 108).toFloat(), (y + 128).toFloat(), (x + 148).toFloat(), (y + 128).toFloat(), 1f, OneColor("703A3AFF").rgb)
}

importButton.draw(vg, (x + 270).toFloat(), (y + 48).toFloat(), inputHandler)
clearButton.draw(vg, (x + 270).toFloat(), (y + 174).toFloat(), inputHandler)
saveButton.draw(vg, (x + 270).toFloat(), (y + 222).toFloat(), inputHandler)
Expand All @@ -102,12 +121,38 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {

removeQueue.clear()

val scissor = ScissorHelper.INSTANCE.scissor(vg, (x + 349).toFloat(), y.toFloat(), 644f, 254f)
val height = (149 + 16) * ceil(ModConfig.crosshairs.size / 4f) - 16

if (height <= 256) scrollAnimation = DummyAnimation(0f)

scroll = scrollAnimation.get()

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

if (scissor.isInScissor(inputHandler.mouseX(), inputHandler.mouseY())) {
inputHandler.unblockDWheel()

val dWheel = inputHandler.dWheel.toFloat()

inputHandler.blockDWheel()

if (dWheel != 0f) {
scrollTarget += dWheel

if (scrollTarget > 0f) scrollTarget = 0f
else if (scrollTarget < 256 - height) scrollTarget = (256 - height)

scrollAnimation = EaseOutQuad(150, scroll, scrollTarget, false)
}
} else {
inputHandler.unblockDWheel()
inputHandler.stopBlockingInput()
}

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

ScissorHelper.INSTANCE.resetScissor(vg, scissor)
Expand All @@ -128,47 +173,59 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {

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.")
if (loadedImage.width != loadedImage.height || loadedImage.width !in 15..32) {
notify("Image size isn't supported.")
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
ModConfig.canvaSize = loadedImage.height
for (posY in 0 ..< ModConfig.canvaSize) {
for (posX in 0 ..< ModConfig.canvaSize) {
val c = loadedImage.image.getRGB(posX, posY)
pixels[Utils.posToIndex(posX, posY)].isToggled = c shr 24 != 0
pixels[Utils.posToIndex(posX, posY)].color = c
}
}
if (save) Utils.save(loadedImage)
return loadedImage
}

fun saveFromDrawer(): OneImage? {
val image = OneImage(15, 15)
if (ModConfig.drawer.isEmpty()) {
fun saveFromDrawer(close: Boolean): OneImage? {
val image = OneImage(ModConfig.canvaSize, ModConfig.canvaSize)
if (ModConfig.drawer.isEmpty() && !close) {
notify("Crosshair cant be empty.")
return null
}
for (i in ModConfig.drawer) {
val pos = indexToPos(i.key)
val c = i.value.color
val pos = Utils.indexToPos(i.key)
if (pos.x >= ModConfig.canvaSize || pos.y >= ModConfig.canvaSize) {
pixels[i.key].isToggled = false
continue
}
val c = i.value
image.setColorAtPos(pos.x, pos.y, c)
}
return image
}

fun reset() {
for (pixel in pixels) {
pixel.isToggled = false
}
ModConfig.drawer.clear()
}

fun move(x: Int, y: Int) {
val newPositions = HashMap<Pos, Int>()
for (i in ModConfig.drawer) {
val pos = indexToPos(i.key)
val pos = Utils.indexToPos(i.key)
val posX = pos.x + x
val posY = pos.y + y
pixels[i.key].isToggled = false
if (posX !in 0..14 || posY !in 0..14) continue
newPositions[Pos(posX, posY)] = i.value.color
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 * 15 + i.key.x
if (index !in 0..224) continue
val index = i.key.y * 32 + i.key.x
pixels[index].isToggled = true
pixels[index].color = i.value
}
Expand All @@ -179,9 +236,6 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
return elements[base64]!!
}

fun indexToPos(index: Int): Pos =
Pos(index % 15, index / 15)

enum class MoveType(val x: Int, val y: Int) {
UP(0, -1),
DOWN(0, 1),
Expand All @@ -190,12 +244,12 @@ object Drawer : BasicOption(null, null, "", "", "", "", 2) {
}

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

override fun getHeight() = 254
override fun getHeight() = 256

override fun keyTyped(key: Char, keyCode: Int) {
if (keyCode == UKeyboard.KEY_W) moveQueue.add(MoveType.UP)
Expand Down
44 changes: 37 additions & 7 deletions src/main/kotlin/org/polyfrost/crosshair/config/ModConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,46 @@ import cc.polyfrost.oneconfig.config.core.*
import cc.polyfrost.oneconfig.config.data.*
import cc.polyfrost.oneconfig.config.elements.*
import org.polyfrost.crosshair.PolyCrosshair
import org.polyfrost.crosshair.utils.Utils
import java.lang.reflect.Field

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

@Exclude
var drawer = HashMap<Int, PixelInfo>()
var mode = false

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

var currentCrosshair = ""

@CustomOption
var crosshairs = ArrayList<String>()

@Exclude
var penColor = OneColor(255, 255, 255, 255)
var penColor = OneColor(-1)

@Dropdown(
name = "Mirror",
options = ["Off", "Horizontal", "Vertical", "Quadrant"]
)
var mirror = 0

@Switch(name = "Dynamic Color")
@Slider(
name = "Canva Size",
min = 15f, max = 32f
)
var canvaSize = 15
get() = field.coerceIn(15, 32)

@Switch(name = "Dynamic Color (Overlay)")
var dynamicColor = false

@Switch(name = "Invert Color")
var invertColor = false

@Slider(name = "Overlay Opacity", min = 0f, max = 100f)
var dynamicOpacity = 100

@Switch(name = "Hostile")
var hostile = false

Expand All @@ -49,11 +68,14 @@ object ModConfig : Config(Mod(PolyCrosshair.NAME, ModType.HUD), "${PolyCrosshair
@Switch(name = "Show in F3 (Debug)")
var showInDebug = false

@Switch(name = "Show in GUIs")
var showInGuis = true

@Switch(name = "Show in Third Person")
var showInThirdPerson = false
var showInThirdPerson = true

@Switch(name = "Show in Spectator Mode")
var showInSpectator = true
var showInSpectator = false

@Slider(name = "Rotation", min = -180f, max = 180f)
var rotation = 0
Expand All @@ -69,13 +91,21 @@ object ModConfig : Config(Mod(PolyCrosshair.NAME, ModType.HUD), "${PolyCrosshair

init {
initialize()
val options = listOf("hostile", "passive", "player", "hostileColor", "passiveColor", "playerColor")
val options = listOf("hostile", "passive", "player", "hostileColor", "passiveColor", "playerColor", "dynamicOpacity")
for (i in options) {
hideIf(i) { !dynamicColor }
}
addDependency(options[3], options[0])
addDependency(options[4], options[1])
addDependency(options[5], options[2])
addListener("canvaSize") {
for (i in drawer) {
val pos = Utils.indexToPos(i.key)
if (pos.x >= canvaSize || pos.y >= canvaSize) {
Drawer.pixels[i.key].isToggled = false
}
}
}
}

override fun getCustomOption(
Expand Down
Loading

0 comments on commit 478093c

Please sign in to comment.