Skip to content

Commit 0d0fb47

Browse files
committed
Hud: Chat and Collision snapping
1 parent b4796fb commit 0d0fb47

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

src/main/kotlin/com/lambda/client/gui/hudgui/AbstractHudElement.kt

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.lambda.client.commons.interfaces.Alias
44
import com.lambda.client.commons.interfaces.DisplayEnum
55
import com.lambda.client.commons.interfaces.Nameable
66
import com.lambda.client.event.LambdaEventBus
7+
import com.lambda.client.gui.GuiManager
78
import com.lambda.client.gui.rgui.windows.BasicWindow
89
import com.lambda.client.module.modules.client.GuiColors
910
import com.lambda.client.module.modules.client.Hud
@@ -18,6 +19,7 @@ import com.lambda.client.util.math.Vec2d
1819
import com.lambda.client.util.math.Vec2f
1920
import com.lambda.client.util.text.MessageSendHelper
2021
import com.lambda.client.util.threads.safeListener
22+
import net.minecraft.client.gui.GuiChat
2123
import net.minecraftforge.fml.common.gameevent.TickEvent
2224
import org.lwjgl.opengl.GL11.glScalef
2325

@@ -34,7 +36,13 @@ abstract class AbstractHudElement(
3436
val bind by setting("Bind", Bind())
3537
val scale by setting("Scale", 1.0f, 0.1f..4.0f, 0.05f)
3638
val default = setting("Default", false)
39+
private val overridePrimaryColor by setting("Override Primary Color", false)
40+
private val overridePrimaryColorValue by setting("Override Primary Color Value", Hud.primaryColor, visibility = { overridePrimaryColor })
41+
private val overrideSecondaryColor by setting("Override Secondary Color", false)
42+
private val overrideSecondaryColorValue by setting("Override Secondary Color Value", Hud.secondaryColor, visibility = { overrideSecondaryColor })
3743

44+
val primaryColor get() = if (overridePrimaryColor) overridePrimaryColorValue else Hud.primaryColor
45+
val secondaryColor get() = if (overrideSecondaryColor) overrideSecondaryColorValue else Hud.secondaryColor
3846
override val resizable = false
3947

4048
final override val minWidth: Float get() = FontRenderAdapter.getFontHeight() * scale * 2.0f
@@ -47,12 +55,64 @@ abstract class AbstractHudElement(
4755
open val hudHeight: Float get() = 10f
4856

4957
val settingList get() = GuiConfig.getSettings(this)
58+
private var chatSnapping = false
59+
private val snappedElements = mutableListOf<AbstractHudElement>()
60+
private val chatSnapY = 15f
5061

5162
init {
5263
safeListener<TickEvent.ClientTickEvent> {
5364
if (it.phase != TickEvent.Phase.END || !visible) return@safeListener
5465
width = maxWidth
5566
height = maxHeight
67+
if (Hud.chatSnap) {
68+
if (mc.currentScreen is GuiChat && !chatSnapping) {
69+
val screenH = (mc.currentScreen as GuiChat).height
70+
if (posY >= screenH - height - 3 && posX <= 3 && yShift == 0.0f) {
71+
val prevPosYSnap = posY
72+
yShift = -chatSnapY
73+
snappedElements.clear()
74+
GuiManager.getHudElementOrNull(componentName)?.let { snappedElements.add(it) }
75+
chatSnapCheck(componentName, prevPosYSnap)
76+
chatSnapping = true
77+
}
78+
} else if (mc.currentScreen !is GuiChat && chatSnapping) {
79+
yShift = 0.0f
80+
for (element in snappedElements) {
81+
element.yShift = 0.0f
82+
}
83+
snappedElements.clear()
84+
chatSnapping = false
85+
}
86+
}
87+
}
88+
}
89+
90+
private fun chatSnapCheck(thisElement: String, prevSnapY: Float) {
91+
for (element in GuiManager.hudElements) {
92+
if (!snappedElements.contains(element)
93+
&& element.componentName != thisElement
94+
&& element.visible
95+
&& element.posY + element.height >= prevSnapY - 3
96+
&& element.posX <= 3) {
97+
snappedElements.add(element)
98+
chatSnapCheck(element.componentName, element.posY)
99+
element.yShift = -chatSnapY
100+
}
101+
}
102+
}
103+
104+
override fun onReposition() {
105+
super.onReposition()
106+
if (Hud.collisionSnapping) {
107+
for (element in GuiManager.hudElements) {
108+
if (element.componentName != componentName && element.visible && element.posY + element.height >= posY && element.posY <= posY + height && element.posX + element.width >= posX && element.posX <= posX + width) {
109+
if (posY + height / 2 <= element.posY + element.height / 2) {
110+
posY = element.posY - height
111+
} else {
112+
posY = element.posY + element.height
113+
}
114+
}
115+
}
56116
}
57117
}
58118

@@ -112,9 +172,4 @@ abstract class AbstractHudElement(
112172
MISC("Misc")
113173
}
114174

115-
protected companion object {
116-
val primaryColor get() = Hud.primaryColor
117-
val secondaryColor get() = Hud.secondaryColor
118-
}
119-
120175
}

src/main/kotlin/com/lambda/client/gui/rgui/Component.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ open class Component(
4545
var relativePosY by relativePosYSetting
4646
var dockingH by dockingHSetting
4747
var dockingV by dockingVSetting
48+
var yShift = 0.0f
4849

4950
var posX: Float
5051
get() {
@@ -57,7 +58,7 @@ open class Component(
5758

5859
var posY: Float
5960
get() {
60-
return relativeToAbsY(relativePosY)
61+
return relativeToAbsY(relativePosY) + yShift
6162
}
6263
set(value) {
6364
if (!LambdaMod.ready) return

src/main/kotlin/com/lambda/client/module/modules/client/Hud.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ object Hud : Module(
1414
val hudFrame by setting("Hud Frame", false)
1515
val primaryColor by setting("Primary Color", ColorHolder(255, 240, 246), false)
1616
val secondaryColor by setting("Secondary Color", ColorHolder(108, 0, 43), false)
17+
val chatSnap by setting("Chat Snap", true)
18+
val collisionSnapping by setting("Collision Snapping", true)
1719
}

0 commit comments

Comments
 (0)