1
+ package com.lambda.client.gui.hudgui.elements.client
2
+
3
+ import com.lambda.client.commons.extension.sumByFloat
4
+ import com.lambda.client.commons.interfaces.DisplayEnum
5
+ import com.lambda.client.gui.hudgui.HudElement
6
+ import com.lambda.client.module.AbstractModule
7
+ import com.lambda.client.module.ModuleManager
8
+ import com.lambda.client.util.AsyncCachedValue
9
+ import com.lambda.client.util.TimeUnit
10
+ import com.lambda.client.util.color.ColorHolder
11
+ import com.lambda.client.util.graphics.VertexHelper
12
+ import com.lambda.client.util.graphics.font.FontRenderAdapter
13
+ import com.lambda.client.util.graphics.font.HAlign
14
+ import com.lambda.client.util.graphics.font.TextComponent
15
+ import com.lambda.client.util.graphics.font.VAlign
16
+ import com.lambda.client.util.threads.safeAsyncListener
17
+ import net.minecraft.client.renderer.GlStateManager
18
+ import net.minecraftforge.fml.common.gameevent.TickEvent
19
+ import kotlin.math.max
20
+
21
+ internal object Bindings : HudElement(
22
+ name = " Bindings" ,
23
+ category = Category .CLIENT ,
24
+ description = " Display current module keybindings" ,
25
+ enabledByDefault = false
26
+ ) {
27
+ private val sortingMode by setting(" Sorting Mode" , SortingMode .LENGTH )
28
+ private val disabledColor by setting(" Disabled Color" , ColorHolder (255 , 255 , 255 ), false )
29
+ private val enabledColor by setting(" Enabled Color" , ColorHolder (0 , 255 , 0 ), false )
30
+
31
+ private var cacheWidth = 20.0f
32
+ private var cacheHeight = 20.0f
33
+ override val hudWidth: Float get() = cacheWidth
34
+ override val hudHeight: Float get() = cacheHeight
35
+
36
+ @Suppress(" UNUSED" )
37
+ private enum class SortingMode (
38
+ override val displayName : String ,
39
+ val comparator : Comparator <AbstractModule >
40
+ ) : DisplayEnum {
41
+ LENGTH (" Length" , compareByDescending { it.textLine.getWidth() }),
42
+ ALPHABET (" Alphabet" , compareBy { it.name }),
43
+ CATEGORY (" Category" , compareBy { it.category.ordinal })
44
+ }
45
+
46
+ private var modulesWithBindings: List <AbstractModule > = emptyList()
47
+ private val lineHeight = FontRenderAdapter .getFontHeight() + 2.0f
48
+
49
+ init {
50
+ relativePosX = - 2.0f
51
+ relativePosY = 2.0f
52
+ dockingH = HAlign .RIGHT
53
+
54
+ safeAsyncListener<TickEvent .ClientTickEvent > {event ->
55
+ if (event.phase != TickEvent .Phase .END ) return @safeAsyncListener
56
+ // this isn't terribly efficient, consider creating events for editing bindings and module toggle state
57
+ modulesWithBindings = sortedModuleList
58
+ .filter { it.bind.value.isEmpty.not () }
59
+ cacheWidth = modulesWithBindings.maxOfOrNull {
60
+ it.textLine.getWidth() + 4.0f
61
+ } ? : 20.0f
62
+ cacheHeight = max(modulesWithBindings.sumByFloat { lineHeight }, 20.0f )
63
+ }
64
+ }
65
+
66
+ override fun renderHud (vertexHelper : VertexHelper ) {
67
+ super .renderHud(vertexHelper)
68
+ GlStateManager .pushMatrix()
69
+ GlStateManager .translate(width / scale * dockingH.multiplier, 0.0f , 0.0f )
70
+ if (dockingV == VAlign .BOTTOM ) {
71
+ GlStateManager .translate(0.0f , height / scale - (FontRenderAdapter .getFontHeight() + 2.0f ), 0.0f )
72
+ }
73
+
74
+ drawModuleList()
75
+
76
+ GlStateManager .popMatrix()
77
+ }
78
+
79
+ private fun drawModuleList () {
80
+ for (module in modulesWithBindings) {
81
+ GlStateManager .pushMatrix()
82
+ val textLine = module.textLine
83
+ val textWidth = textLine.getWidth()
84
+ val stringPosX = textWidth * dockingH.multiplier
85
+ val margin = 2.0f * dockingH.offset
86
+ GlStateManager .translate(- stringPosX - margin, 0.0f , 0.0f )
87
+ textLine.drawLine(1.0f , true , HAlign .LEFT , FontRenderAdapter .useCustomFont)
88
+ GlStateManager .popMatrix()
89
+ var yOffset = lineHeight
90
+ if (dockingV == VAlign .BOTTOM ) yOffset * = - 1.0f
91
+ GlStateManager .translate(0.0f , yOffset, 0.0f )
92
+ }
93
+ }
94
+
95
+ private val AbstractModule .textLine get() = this .newTextLine()
96
+
97
+ private val sortedModuleList: List <AbstractModule > by AsyncCachedValue (1L , TimeUnit .SECONDS ) {
98
+ ModuleManager .modules
99
+ .filter { it.category != com.lambda.client.module.Category .CLIENT }
100
+ .sortedWith(this .sortingMode.comparator)
101
+ }
102
+
103
+ private fun AbstractModule.newTextLine () =
104
+ TextComponent .TextLine (" " ).apply {
105
+ val lineColor: ColorHolder = if (isEnabled) enabledColor else disabledColor
106
+ add(TextComponent .TextElement (name, lineColor))
107
+ add(TextComponent .TextElement (" [" + bind.value.toString() + " ]" , lineColor))
108
+ if (dockingH == HAlign .RIGHT ) reverse()
109
+ }
110
+
111
+ }
0 commit comments