blob: ad6232646d4b4e0a4306cf92fd7955a9e99717a3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package at.hannibal2.skyhanni.mixins.hooks
import at.hannibal2.skyhanni.features.chroma.ChromaFontRenderer
import at.hannibal2.skyhanni.features.chroma.ChromaManager
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils
import net.minecraft.client.renderer.GlStateManager
/**
* Object to handle chroma font states from handler methods from MixinFontRenderer
*
* Modified from SkyblockAddons
*
* Credit: [FontRendererHook.java](https://github.com/BiscuitDevelopment/SkyblockAddons/blob/main/src/main/java/codes/biscuit/skyblockaddons/asm/hooks/FontRendererHook.java)
*/
object FontRendererHook {
private val config get() = ChromaManager.config
private const val CHROMA_FORMAT_INDEX = 22
private const val WHITE_FORMAT_INDEX = 15
private var CHROMA_COLOR: Int = -0x1
private val DRAW_CHROMA = ChromaFontRenderer(CHROMA_COLOR)
private var CHROMA_COLOR_SHADOW: Int = -0xAAAAAB
private val DRAW_CHROMA_SHADOW = ChromaFontRenderer(CHROMA_COLOR_SHADOW)
private var currentDrawState: ChromaFontRenderer? = null
private var previewChroma = false
var cameFromChat = false
/**
* Setups the [ChromaFontRenderer][at.hannibal2.skyhanni.features.chroma.ChromaFontRenderer] for rendering text
* in chroma. This should only be used when you don't have control over the color code a string uses, or it
* doesn't make sense to add §Z color code to a string.
*
* If you do have control over the color code, you can prepend the string with §Z instead.
*
* **Usage:**
*
* Surround string render call with this method and [endChromaFont].
* ```
* FontRendererHook.setupChromaFont()
* // render string call(s) here...
* FontRendererHook.endChromaFont()
* ```
*
* Note: This only works if the string render call ends up using
* [FontRenderer#drawString()][net.minecraft.client.gui.FontRenderer.drawString] rather than a custom font renderer
*
*/
private fun setupChromaFont() {
DRAW_CHROMA.startChroma()
DRAW_CHROMA_SHADOW.startChroma()
}
/**
* See [setupChromaFont]
*/
private fun endChromaFont() {
DRAW_CHROMA.endChroma()
DRAW_CHROMA_SHADOW.endChroma()
}
@JvmStatic
fun beginChromaRendering(text: String, shadow: Boolean) {
if (!isEnabled()) return
if (config.allChroma && config.ignoreChat && cameFromChat) {
endChromaFont()
return
}
if (text == "§fPlease star the mod on GitHub!") {
previewChroma = true
setupChromaFont()
}
currentDrawState = if (shadow) DRAW_CHROMA_SHADOW else DRAW_CHROMA
// Best feature ngl
if (config.allChroma) {
// Handles setting the base color of text when they don't use color codes i.e. MoulConfig
if (shadow) {
GlStateManager.color(0.33f, 0.33f, 0.33f, RenderUtils.getAlpha())
} else {
GlStateManager.color(1f, 1f, 1f, RenderUtils.getAlpha())
}
setupChromaFont()
}
currentDrawState?.loadChromaEnv()
}
@JvmStatic
fun toggleChromaOn() {
if (!LorenzUtils.inSkyBlock) return
currentDrawState?.newChromaEnv()?.bindActualColor(RenderUtils.getAlpha())
}
@JvmStatic
fun forceWhiteColorCode(formatIndex: Int): Int {
if (!isEnabled()) return formatIndex
val drawState = currentDrawState ?: return formatIndex
if (drawState.getChromaState() && formatIndex <= WHITE_FORMAT_INDEX) { // If it's a color code
return WHITE_FORMAT_INDEX
}
return formatIndex
}
@JvmStatic
fun restoreChromaState() {
if (!isEnabled()) return
currentDrawState?.restoreChromaEnv()
}
@JvmStatic
fun endChromaRendering() {
if (!isEnabled()) return
if (previewChroma) {
previewChroma = false
endChromaFont()
}
if (config.allChroma) endChromaFont()
currentDrawState?.endChromaEnv()
}
@JvmStatic
fun insertZColorCode(constant: String): String {
return if (LorenzUtils.inSkyBlock && !isChromaEnabled()) constant else "0123456789abcdefklmnorz"
}
@JvmStatic
fun toggleChromaAndResetStyle(formatIndex: Int): Boolean {
if (!isEnabled()) return false
if (formatIndex == CHROMA_FORMAT_INDEX) {
toggleChromaOn()
return true
}
return false
}
private fun isChromaEnabled() = config.enabled.get()
private fun isEnabled() = LorenzUtils.inSkyBlock && isChromaEnabled()
}
|