Skip to content

Commit

Permalink
e
Browse files Browse the repository at this point in the history
  • Loading branch information
RedthMC committed Dec 3, 2023
1 parent 8a92ffa commit 18bd3bd
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 83 deletions.
89 changes: 14 additions & 75 deletions src/main/kotlin/org/polyfrost/crosshair/PolyCrosshair.kt
Original file line number Diff line number Diff line change
@@ -1,89 +1,28 @@
package org.polyfrost.crosshair

import cc.polyfrost.oneconfig.utils.dsl.mc
import net.minecraft.client.gui.Gui
import net.minecraft.client.renderer.texture.DynamicTexture
import net.minecraftforge.client.event.RenderGameOverlayEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.FMLInitializationEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.opengl.GL11
import org.polyfrost.crosshair.config.ModConfig
import org.polyfrost.crosshair.mixin.GuiIngameAccessor
import net.minecraft.client.renderer.GlStateManager as GL
import org.polyfrost.crosshair.render.CrosshairRenderer

@Mod(modid = PolyCrosshair.MODID, name = PolyCrosshair.NAME, version = PolyCrosshair.VERSION)
class PolyCrosshair {
companion object {
const val MODID = "@ID@"
const val NAME = "@NAME@"
const val VERSION = "@VER@"
@Mod(
modid = PolyCrosshair.MODID,
name = PolyCrosshair.NAME,
version = PolyCrosshair.VERSION,
modLanguageAdapter = "cc.polyfrost.oneconfig.utils.KotlinLanguageAdapter"
)
object PolyCrosshair {
const val MODID = "@ID@"
const val NAME = "@NAME@"
const val VERSION = "@VER@"

val texture by lazy {
DynamicTexture(15, 15)
}
val resourceLocation by lazy {
mc.textureManager.getDynamicTextureLocation("polycrosshair", texture)
}


fun updateTexture() {
val profile = ModConfig.profiles.selectedProfile ?: return
for (i in 0 until 225) {
texture.textureData[i] = if (profile.image.get(i)) profile.mainColor.rgb else 0x00000000
}
texture.updateDynamicTexture()
} // test now? ok
}

@Mod.EventHandler
fun onFMLInitialization(event: FMLInitializationEvent) {
ModConfig
updateTexture()
MinecraftForge.EVENT_BUS.register(CrosshairRenderer)
CrosshairRenderer.updateTexture()
}

@SubscribeEvent
fun onRenderCrosshair(event: RenderGameOverlayEvent.Pre) {
if (event.type != RenderGameOverlayEvent.ElementType.CROSSHAIRS) return
if (!ModConfig.enabled) return
if ((mc.ingameGUI as? GuiIngameAccessor)?.shouldShowCrosshair() == false) return

val profile = ModConfig.profiles.selectedProfile ?: return
event.isCanceled = true


GL.enableBlend()
if (profile.invertColor) {
GL.tryBlendFuncSeparate(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ONE_MINUS_SRC_COLOR, 1, 0)
}
GL.enableAlpha()
with(profile.mainColor) {
GL.color(red / 255f, green / 255f, blue / 255f, alpha / 255f)
}

// GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1)
//
// GL11.glRasterPos2f(event.resolution.scaledWidth / 2f - 7.5f, event.resolution.scaledHeight / 2f - 7.5f)
// GL11.glDrawPixels(15, 15, GL11.GL_COLOR_INDEX, GL11.GL_BITMAP, ByteBuffer.wrap(profile.image.toByteArray()))
// test this first?\

// draw
mc.textureManager.bindTexture(resourceLocation)
Gui.drawModalRectWithCustomSizedTexture(
(event.resolution.scaledWidth - 15) / 2,
(event.resolution.scaledHeight - 15) / 2,
0f,
0f,
15,
15,
15f,
15f
)
// GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 4)

if (profile.invertColor) {
GL.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
}
GL.disableBlend()
}
}
4 changes: 3 additions & 1 deletion src/main/kotlin/org/polyfrost/crosshair/config/Drawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cc.polyfrost.oneconfig.config.elements.BasicOption
import cc.polyfrost.oneconfig.utils.InputHandler
import org.polyfrost.crosshair.PolyCrosshair
import org.polyfrost.crosshair.config.configlist.Profile
import org.polyfrost.crosshair.render.CrosshairRenderer
import java.util.*

@Suppress("UnstableAPIUsage")
Expand Down Expand Up @@ -33,7 +34,8 @@ object Drawer : BasicOption(null, null, "", "", "", "", 1) {
bitSet.set(pixel.index, pixel.state)
}
profile.image = bitSet
PolyCrosshair.updateTexture()

CrosshairRenderer.updateTexture()
}

override fun getHeight() = 254
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/org/polyfrost/crosshair/config/ModConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ object ModConfig : Config(Mod(PolyCrosshair.NAME, ModType.HUD), "${PolyCrosshair
@CustomOption
var profiles = ProfileList()

var selectedIndex = 0

init {
initialize()
}
Expand All @@ -26,15 +28,16 @@ object ModConfig : Config(Mod(PolyCrosshair.NAME, ModType.HUD), "${PolyCrosshair
annotation: CustomOption,
page: OptionPage,
mod: Mod,
migrate: Boolean
migrate: Boolean,
): BasicOption? {
ConfigUtils.getSubCategory(page, "General", "").options.add(Drawer)
profiles.addOptionTo(this, page, category = "General")
profiles.addOptionTo(this, page, category = "General").select(selectedIndex)
return null
}

class ProfileList : ConfigList<Profile>() {
override fun onSelected(profile: Profile) {
selectedIndex = indexOf(profile)
Drawer.load(profile)
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/org/polyfrost/crosshair/config/Pixel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ class Pixel(val index: Int) : BasicElement(16, 16, ColorPalette.PRIMARY, true, 0

override fun update(x: Float, y: Float, inputHandler: InputHandler) {
super.update(x, y, inputHandler)

currentColor = colorAnimation.getColor(hovered, pressed)
if (!hovered) return

state = when {
inputHandler.isMouseDown -> true
inputHandler.isMouseDown(1) -> false
else -> return
}
setColorPalette(if (state) ColorPalette.PRIMARY else ColorPalette.SECONDARY)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class ConfigList<T> : ArrayList<T>() {
abstract fun onSelected(profile: T)
open fun postInitOptions(entry: ConfigEntry<T>, options: List<BasicOption>) {}

fun addOptionTo(config: Config, page: OptionPage, description: String = "", category: String = "General", subcategory: String = ""): BasicOption {
fun addOptionTo(config: Config, page: OptionPage, description: String = "", category: String = "General", subcategory: String = ""): ConfigListOption<T> {
val option = ConfigListOption(this, config, description, category, subcategory)
ConfigUtils.getSubCategory(page, category, subcategory).options.add(option)
return option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cc.polyfrost.oneconfig.renderer.asset.SVG
import cc.polyfrost.oneconfig.utils.InputHandler
import cc.polyfrost.oneconfig.utils.color.ColorPalette
import cc.polyfrost.oneconfig.utils.dsl.drawHollowRoundedRect
import org.polyfrost.crosshair.config.ModConfig
import java.util.*


Expand Down Expand Up @@ -69,6 +70,7 @@ class ConfigListOption<T>(
planToRemove = configEntry
}

fun select(index: Int) = select(configEntryList[index])
fun select(configEntry: ConfigEntry<T>) {
configList.selectedProfile = configEntry.config
configList.onSelected(configEntry.config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import java.util.*

class Profile {
private var imgBase64: String =
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" // 225 zeros | todo: get some preset crosshairs
"AAAOjo/v7///HxXvrUfF/i/+B/4D/gA+AA4AAg==" // 225 zeros | todo: get some preset crosshairs

@Exclude
var image: BitSet = try {
BitSet.valueOf(Base64.getDecoder().decode(imgBase64))

} catch (e: Exception) {
BitSet(225)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.polyfrost.crosshair.render

import cc.polyfrost.oneconfig.utils.dsl.mc
import net.minecraft.client.gui.Gui
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.client.renderer.texture.DynamicTexture
import net.minecraft.util.ResourceLocation
import net.minecraftforge.client.event.RenderGameOverlayEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.opengl.GL11
import org.polyfrost.crosshair.config.ModConfig
import org.polyfrost.crosshair.mixin.GuiIngameAccessor

object CrosshairRenderer {
private val texture = DynamicTexture(15, 15)
private val textureLocation: ResourceLocation =
mc.textureManager.getDynamicTextureLocation("polycrosshair", texture)

fun updateTexture() {
val profile = ModConfig.profiles.selectedProfile ?: return
for (i in 0 until 225) {
texture.textureData[i] = if (profile.image.get(i)) profile.mainColor.rgb else 0x00000000
}
texture.updateDynamicTexture()
}

@SubscribeEvent
fun onRenderCrosshair(event: RenderGameOverlayEvent.Pre) {
if (event.type != RenderGameOverlayEvent.ElementType.CROSSHAIRS) return
if (!ModConfig.enabled) return
if ((mc.ingameGUI as? GuiIngameAccessor)?.shouldShowCrosshair() == false) return

val profile = ModConfig.profiles.selectedProfile ?: return
event.isCanceled = true


GlStateManager.enableBlend()
if (profile.invertColor) {
GlStateManager.tryBlendFuncSeparate(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ONE_MINUS_SRC_COLOR, 1, 0)
}
GlStateManager.enableAlpha()
mc.textureManager.bindTexture(textureLocation)
Gui.drawModalRectWithCustomSizedTexture(
(event.resolution.scaledWidth - 15) / 2,
(event.resolution.scaledHeight - 15) / 2,
0f, 0f, 15, 15, 15f, 15f
)
if (profile.invertColor) {
GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
}
GlStateManager.disableBlend()
}
}
1 change: 1 addition & 0 deletions src/main/resources/mixins.polycrosshair.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"maxShiftBy": 5
},
"client": [
"GuiIngameAccessor"
],
"verbose": true
}

0 comments on commit 18bd3bd

Please sign in to comment.