Skip to content

Commit d8f5619

Browse files
authored
Window grid snapping and GUI fixes (#413)
* Focus test * Hide module feature * Window grid snapping * Upgrade Kotlin 1.7.20 -> 1.7.21 * Fix frame margin * Resize corner ToDo: change render layer * Change mutex to sync block * Added freemove * Code refactor * Revert hide buttons bc it broke plugins * Oopsie * Fix hover object bug and margin split * Fix bottom margin and adjustable rubberband speed * Fix OpenGL 1281: Invalid value * Make SettingWindow unscrollable * Fix saving window size setting and option to disable scroll rubberband * Fix double click height calc * Fix left resize event * Make auto resize fully work
1 parent 8b2554b commit d8f5619

File tree

12 files changed

+150
-159
lines changed

12 files changed

+150
-159
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ forgeVersion=14.23.5.2860
1010
mappingsChannel=stable
1111
mappingsVersion=39-1.12
1212

13-
kotlinVersion=1.7.20
13+
kotlinVersion=1.7.21
1414
kotlinxCoroutinesVersion=1.6.4

src/main/kotlin/com/lambda/client/gui/AbstractLambdaGui.kt

+9-10
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ abstract class AbstractLambdaGui<S : SettingWindow<*>, E : Any> : GuiScreen() {
8888
safeListener<TickEvent.ClientTickEvent> { event ->
8989
if (event.phase != TickEvent.Phase.START) return@safeListener
9090

91-
blurShader.shader?.let {
91+
blurShader.shader?.let { shaderGroup ->
9292
val multiplier = ClickGUI.blur * fadeMultiplier
93-
for (shader in it.listShaders) {
93+
shaderGroup.listShaders.forEach { shader ->
9494
shader.shaderManager.getShaderUniform("multiplier")?.set(multiplier)
9595
}
9696
}
9797

9898
if (displayed.value || alwaysTicking) {
99-
for (window in windowList) window.onTick()
99+
windowList.forEach { it.onTick() }
100100
}
101101
}
102102

@@ -143,7 +143,7 @@ abstract class AbstractLambdaGui<S : SettingWindow<*>, E : Any> : GuiScreen() {
143143

144144
displayed.value = true
145145

146-
for (window in windowList) window.onDisplayed()
146+
windowList.forEach { it.onDisplayed() }
147147
}
148148

149149
override fun initGui() {
@@ -153,7 +153,7 @@ abstract class AbstractLambdaGui<S : SettingWindow<*>, E : Any> : GuiScreen() {
153153
width = scaledResolution.scaledWidth + 16
154154
height = scaledResolution.scaledHeight + 16
155155

156-
for (window in windowList) window.onGuiInit()
156+
windowList.forEach { it.onGuiInit() }
157157
}
158158

159159
override fun onGuiClosed() {
@@ -165,7 +165,7 @@ abstract class AbstractLambdaGui<S : SettingWindow<*>, E : Any> : GuiScreen() {
165165

166166
displayed.value = false
167167

168-
for (window in windowList) window.onClosed()
168+
windowList.forEach { it.onClosed() }
169169
updateSettingWindow()
170170
}
171171
// End of gui init
@@ -318,11 +318,10 @@ abstract class AbstractLambdaGui<S : SettingWindow<*>, E : Any> : GuiScreen() {
318318
}
319319

320320
private fun drawEachWindow(renderBlock: (WindowComponent) -> Unit) {
321-
for (window in windowList) {
322-
if (!window.visible) continue
321+
windowList.filter { it.visible }.forEach {
323322
glPushMatrix()
324-
glTranslatef(window.renderPosX, window.renderPosY, 0.0f)
325-
renderBlock(window)
323+
glTranslatef(it.renderPosX, it.renderPosY, 0.0f)
324+
renderBlock(it)
326325
glPopMatrix()
327326
}
328327
}

src/main/kotlin/com/lambda/client/gui/clickgui/LambdaClickGui.kt

+6-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.lambda.client.gui.clickgui.window.ModuleSettingWindow
99
import com.lambda.client.gui.rgui.Component
1010
import com.lambda.client.gui.rgui.windows.ListWindow
1111
import com.lambda.client.module.AbstractModule
12+
import com.lambda.client.module.Category
1213
import com.lambda.client.module.ModuleManager
1314
import com.lambda.client.module.modules.client.ClickGUI
1415
import com.lambda.client.plugin.PluginManager
@@ -37,37 +38,24 @@ object LambdaClickGui : AbstractLambdaGui<ModuleSettingWindow, AbstractModule>()
3738
private var moduleCount = ModuleManager.modules.size
3839

3940
init {
40-
val allButtons = ModuleManager.modules
41-
.groupBy { it.category.displayName }
42-
.mapValues { (_, modules) -> modules.map { ModuleButton(it) } }
43-
4441
var posX = 0.0f
45-
var posY = 0.0f
46-
val screenWidth = mc.displayWidth / ClickGUI.getScaleFactorFloat()
4742

48-
/* Modules */
49-
for ((category, buttons) in allButtons) {
50-
val window = ListWindow(category, posX, posY, 90.0f, 300.0f, Component.SettingGroup.CLICK_GUI)
51-
52-
window.addAll(buttons.customSort())
43+
Category.values().forEach { category ->
44+
val window = ListWindow(category.displayName, posX, 0.0f, 90.0f, 300.0f, Component.SettingGroup.CLICK_GUI)
5345
windows.add(window)
54-
posX += 90.0f
5546

56-
if (posX > screenWidth) {
57-
posX = 0.0f
58-
posY += 100.0f
59-
}
47+
posX += 90.0f
6048
}
6149

6250
/* Plugins */
63-
pluginWindow = PluginWindow("Plugins", posX, posY)
51+
pluginWindow = PluginWindow("Plugins", posX, 0.0f)
6452
pluginWindow.add(ImportPluginButton)
6553
pluginWindow.add(DownloadPluginButton)
6654
windows.add(pluginWindow)
6755

6856
posX += 120.0f
6957

70-
remotePluginWindow = PluginWindow("Remote plugins", posX, posY)
58+
remotePluginWindow = PluginWindow("Remote plugins", posX, 0.0f)
7159
remotePluginWindow.visible = false
7260
windows.add(remotePluginWindow)
7361

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

+17-26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.lambda.client.gui.hudgui
33
import com.lambda.client.event.events.RenderOverlayEvent
44
import com.lambda.client.event.listener.listener
55
import com.lambda.client.gui.AbstractLambdaGui
6-
import com.lambda.client.gui.clickgui.LambdaClickGui
76
import com.lambda.client.gui.hudgui.component.HudButton
87
import com.lambda.client.gui.hudgui.window.HudSettingWindow
98
import com.lambda.client.gui.rgui.Component
@@ -14,6 +13,7 @@ import com.lambda.client.module.modules.client.HudEditor
1413
import com.lambda.client.util.graphics.GlStateUtils
1514
import com.lambda.client.util.graphics.VertexHelper
1615
import com.lambda.client.util.math.Vec2f
16+
import com.lambda.client.util.threads.safeListener
1717
import net.minecraftforge.fml.common.gameevent.InputEvent
1818
import org.lwjgl.input.Keyboard
1919
import org.lwjgl.opengl.GL11.*
@@ -26,43 +26,35 @@ object LambdaHudGui : AbstractLambdaGui<HudSettingWindow, AbstractHudElement>()
2626

2727
init {
2828
var posX = 0.0f
29-
var posY = 0.0f
30-
val screenWidth = LambdaClickGui.mc.displayWidth / ClickGUI.getScaleFactorFloat()
3129

32-
for (category in AbstractHudElement.Category.values()) {
30+
AbstractHudElement.Category.values().forEach { category ->
3331
val window = ListWindow(category.displayName, posX, 0.0f, 90.0f, 300.0f, Component.SettingGroup.HUD_GUI)
3432
windowList.add(window)
3533
hudWindows[category] = window
3634

3735
posX += 90.0f
38-
39-
if (posX > screenWidth) {
40-
posX = 0.0f
41-
posY += 100.0f
42-
}
4336
}
4437

4538
listener<InputEvent.KeyInputEvent> {
4639
val eventKey = Keyboard.getEventKey()
4740

4841
if (eventKey == Keyboard.KEY_NONE || Keyboard.isKeyDown(Keyboard.KEY_F3)) return@listener
4942

50-
for (child in windowList) {
51-
if (child !is AbstractHudElement) continue
52-
if (!child.bind.isDown(eventKey)) continue
53-
child.visible = !child.visible
54-
}
43+
windowList
44+
.filterIsInstance<AbstractHudElement>()
45+
.filter { it.bind.isDown(eventKey) }
46+
.forEach { it.visible = !it.visible }
5547
}
5648
}
5749

5850
internal fun register(hudElement: AbstractHudElement) {
5951
val button = HudButton(hudElement)
60-
hudWindows[hudElement.category]!!.add(button)
52+
hudWindows[hudElement.category]?.add(button)
6153
windowList.add(hudElement)
6254
}
6355

6456
internal fun unregister(hudElement: AbstractHudElement) {
65-
hudWindows[hudElement.category]!!.children.removeIf { it is HudButton && it.hudElement == hudElement }
57+
hudWindows[hudElement.category]?.children?.removeIf { it is HudButton && it.hudElement == hudElement }
6658
windowList.remove(hudElement)
6759
}
6860

@@ -98,27 +90,26 @@ object LambdaHudGui : AbstractLambdaGui<HudSettingWindow, AbstractHudElement>()
9890
}
9991

10092
private fun setHudButtonVisibility(function: (HudButton) -> Boolean) {
101-
windowList.filterIsInstance<ListWindow>().forEach {
102-
for (child in it.children) {
103-
if (child !is HudButton) continue
104-
child.visible = function(child)
93+
windowList.filterIsInstance<ListWindow>().forEach { window ->
94+
window.children.filterIsInstance<HudButton>().forEach { button ->
95+
button.visible = function(button)
10596
}
10697
}
10798
}
10899

109100
init {
110-
listener<RenderOverlayEvent>(0) {
111-
if (mc?.world == null || mc?.player == null || mc?.currentScreen == this || mc?.gameSettings?.showDebugInfo != false) return@listener
101+
safeListener<RenderOverlayEvent>(0) {
102+
if (Hud.isDisabled) return@safeListener
112103

113104
val vertexHelper = VertexHelper(GlStateUtils.useVbo())
114105
GlStateUtils.rescaleLambda()
115106

116-
if (Hud.isEnabled) {
117-
for (window in windowList) {
118-
if (window !is AbstractHudElement || !window.visible) continue
107+
windowList
108+
.filterIsInstance<AbstractHudElement>()
109+
.filter { it.visible }
110+
.forEach { window ->
119111
renderHudElement(vertexHelper, window)
120112
}
121-
}
122113

123114
GlStateUtils.rescaleMc()
124115
GlStateUtils.depth(true)

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ open class Component(
3131
protected val dockingHSetting = setting("Docking H", HAlign.LEFT)
3232
protected val dockingVSetting = setting("Docking V", VAlign.TOP)
3333

34-
private var widthSetting = setting("Width", widthIn, 0.0f..69420.911f, 0.1f, { false }, { _, it -> it.coerceIn(minWidth, max(scaledDisplayWidth, minWidth)) })
35-
private var heightSetting = setting("Height", heightIn, 0.0f..69420.911f, 0.1f, { false }, { _, it -> it.coerceIn(minHeight, max(scaledDisplayHeight, minHeight)) })
34+
private var widthSetting = setting("Width", widthIn, 0.0f..69420.914f, 0.1f, { false })
35+
private var heightSetting = setting("Height", heightIn, 0.0f..69420.914f, 0.1f, { false })
3636

37-
private var relativePosXSetting = setting("Pos X", posXIn, -69420.911f..69420.911f, 0.1f, { false },
38-
{ _, it -> if (this is WindowComponent && LambdaMod.ready) absToRelativeX(relativeToAbsX(it).coerceIn(1.0f, max(scaledDisplayWidth - width - 1.0f, 1.0f))) else it })
39-
private var relativePosYSetting = setting("Pos Y", posYIn, -69420.911f..69420.911f, 0.1f, { false },
40-
{ _, it -> if (this is WindowComponent && LambdaMod.ready) absToRelativeY(relativeToAbsY(it).coerceIn(1.0f, max(scaledDisplayHeight - height - 1.0f, 1.0f))) else it })
37+
private var relativePosXSetting = setting("Pos X", posXIn, -69420.914f..69420.914f, 0.1f, { false },
38+
{ _, it -> if (this is WindowComponent && LambdaMod.ready) absToRelativeX(relativeToAbsX(it).coerceIn(.0f, max(scaledDisplayWidth - width, .0f))) else it })
39+
private var relativePosYSetting = setting("Pos Y", posYIn, -69420.914f..69420.914f, 0.1f, { false },
40+
{ _, it -> if (this is WindowComponent && LambdaMod.ready) absToRelativeY(relativeToAbsY(it).coerceIn(.0f, max(scaledDisplayHeight - height, .0f))) else it })
4141

4242
var width by widthSetting
4343
open var height by heightSetting

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

+36-24
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.lambda.client.gui.rgui
22

33
import com.lambda.client.commons.interfaces.Nameable
4+
import com.lambda.client.module.modules.client.ClickGUI.gridSize
45
import com.lambda.client.setting.GuiConfig.setting
56
import com.lambda.client.setting.configs.AbstractConfig
67
import com.lambda.client.util.graphics.AnimationUtils
78
import com.lambda.client.util.graphics.font.HAlign
89
import com.lambda.client.util.graphics.font.VAlign
910
import com.lambda.client.util.math.Vec2f
11+
import org.lwjgl.input.Keyboard
1012
import kotlin.math.max
1113
import kotlin.math.min
14+
import kotlin.math.roundToInt
1215

1316
open class WindowComponent(
1417
name: String,
@@ -106,38 +109,41 @@ open class WindowComponent(
106109
else -> null
107110
}
108111

109-
val centerSplitterVCenter = if (draggableHeight != height && horizontalSide == HAlign.CENTER) 2.5 else min(15.0, preDragSize.x / 3.0)
112+
val centerSplitterVCenter = if (draggableHeight != height && horizontalSide == HAlign.CENTER) {
113+
2.5
114+
} else {
115+
min(15.0, preDragSize.x / 3.0)
116+
}
117+
110118
val verticalSide = when (relativeClickPos.y) {
111119
in -2.0..centerSplitterVCenter -> VAlign.TOP
112120
in centerSplitterVCenter..preDragSize.y - centerSplitterV -> VAlign.CENTER
113121
in preDragSize.y - centerSplitterV..preDragSize.y + 2.0 -> VAlign.BOTTOM
114122
else -> null
115123
}
116124

125+
if (horizontalSide == null || verticalSide == null) return
126+
117127
val draggedDist = mousePos.minus(clickPos)
118128

119-
if (horizontalSide != null && verticalSide != null) {
120-
if (resizable && !minimized && (horizontalSide != HAlign.CENTER || verticalSide != VAlign.CENTER)) {
121-
handleResizeX(horizontalSide, draggedDist)
122-
handleResizeY(verticalSide, draggedDist)
129+
if (resizable && !minimized && (horizontalSide != HAlign.CENTER || verticalSide != VAlign.CENTER)) {
130+
handleResizeX(horizontalSide, draggedDist)
131+
handleResizeY(verticalSide, draggedDist)
123132

124-
onResize()
125-
} else if (draggableHeight == height || relativeClickPos.y <= draggableHeight) {
126-
posX = (preDragPos.x + draggedDist.x).coerceIn(1.0f, mc.displayWidth - width - 1.0f)
127-
posY = (preDragPos.y + draggedDist.y).coerceIn(1.0f, mc.displayHeight - height - 1.0f)
133+
onResize()
134+
} else if (draggableHeight == height || relativeClickPos.y <= draggableHeight) {
135+
posX = roundOnGrid(preDragPos.x + draggedDist.x).coerceIn(.0f, mc.displayWidth - width)
136+
posY = roundOnGrid(preDragPos.y + draggedDist.y).coerceIn(.0f, mc.displayHeight - height)
128137

129-
onReposition()
130-
} else {
131-
// TODO
132-
}
138+
onReposition()
133139
}
134140
}
135141

136142
private fun handleResizeX(horizontalSide: HAlign, draggedDist: Vec2f) {
137143
when (horizontalSide) {
138144
HAlign.LEFT -> {
139-
val draggedX = max(draggedDist.x, 1.0f - preDragPos.x)
140-
var newWidth = max(preDragSize.x - draggedX, minWidth)
145+
val draggedX = max(roundOnGrid(draggedDist.x), 1.0f - preDragPos.x)
146+
var newWidth = max(roundOnGrid(preDragSize.x - draggedX), minWidth)
141147

142148
if (maxWidth != -1.0f) newWidth = min(newWidth, maxWidth)
143149
newWidth = min(newWidth, scaledDisplayWidth - 2.0f)
@@ -147,25 +153,25 @@ open class WindowComponent(
147153
posX += prevWidth - newWidth
148154
}
149155
HAlign.RIGHT -> {
150-
val draggedX = min(draggedDist.x, preDragPos.x + preDragSize.x - 1.0f)
151-
var newWidth = max(preDragSize.x + draggedX, minWidth)
156+
val draggedX = min(roundOnGrid(draggedDist.x), preDragPos.x + preDragSize.x - 1.0f)
157+
var newWidth = max(roundOnGrid(preDragSize.x + draggedX), minWidth)
152158

153159
if (maxWidth != -1.0f) newWidth = min(newWidth, maxWidth)
154160
newWidth = min(newWidth, scaledDisplayWidth - posX - 2.0f)
155161

156162
width = newWidth
157163
}
158164
else -> {
159-
// Nothing lol
165+
// Ignored
160166
}
161167
}
162168
}
163169

164170
private fun handleResizeY(verticalSide: VAlign, draggedDist: Vec2f) {
165171
when (verticalSide) {
166172
VAlign.TOP -> {
167-
val draggedY = max(draggedDist.y, 1.0f - preDragPos.y)
168-
var newHeight = max(preDragSize.y - draggedY, minHeight)
173+
val draggedY = max(roundOnGrid(draggedDist.y), 1.0f - preDragPos.y)
174+
var newHeight = max(roundOnGrid(preDragSize.y - draggedY), minHeight)
169175

170176
if (maxHeight != -1.0f) newHeight = min(newHeight, maxHeight)
171177
newHeight = min(newHeight, scaledDisplayHeight - 2.0f)
@@ -175,20 +181,26 @@ open class WindowComponent(
175181
posY += prevHeight - newHeight
176182
}
177183
VAlign.BOTTOM -> {
178-
val draggedY = min(draggedDist.y, preDragPos.y + preDragSize.y - 1.0f)
179-
var newHeight = max(preDragSize.y + draggedY, minHeight)
184+
val draggedY = min(roundOnGrid(draggedDist.y), preDragPos.y + preDragSize.y - 1.0f)
185+
var newHeight = max(roundOnGrid(preDragSize.y + draggedY), minHeight)
180186

181187
if (maxHeight != -1.0f) newHeight = min(newHeight, maxHeight)
182188
newHeight = min(newHeight, scaledDisplayHeight - posY - 2.0f)
183189

184190
height = newHeight
185191
}
186-
else -> {
187-
// Nothing lol
192+
VAlign.CENTER -> {
193+
// Ignored
188194
}
189195
}
190196
}
191197

198+
private fun roundOnGrid(delta: Float) =
199+
if (gridSize == .0f
200+
|| Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)
201+
|| Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)
202+
) delta else (delta / gridSize).roundToInt() * gridSize
203+
192204
fun isInWindow(mousePos: Vec2f): Boolean {
193205
return visible && mousePos.x in preDragPos.x - 2.0f..preDragPos.x + preDragSize.x + 2.0f
194206
&& mousePos.y in preDragPos.y - 2.0f..preDragPos.y + max(preDragSize.y * renderMinimizeProgress, draggableHeight) + 2.0f

src/main/kotlin/com/lambda/client/gui/rgui/component/Slider.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ open class Slider(
168168
RenderUtils2D.drawRectFilled(
169169
vertexHelper,
170170
posEnd = Vec2d(textWidth, textHeight).plus(4.0),
171-
color = GuiColors.backGround.apply { a = (a * alpha).toInt() })
171+
color = GuiColors.backGround.apply { a = (a * alpha).toInt() }
172+
)
173+
172174
if (ClickGUI.windowOutline) {
173175
RenderUtils2D.drawRectOutline(
174176
vertexHelper,

src/main/kotlin/com/lambda/client/gui/rgui/windows/BasicWindow.kt

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ open class BasicWindow(
3232
ClickGUI.radius,
3333
color = GuiColors.backGround
3434
)
35+
3536
if (ClickGUI.windowOutline) {
3637
RenderUtils2D.drawRoundedRectOutline(
3738
vertexHelper,

0 commit comments

Comments
 (0)