Skip to content

Commit 45f689d

Browse files
committed
Docking grid
1 parent 90c92a1 commit 45f689d

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

common/src/main/kotlin/com/lambda/gui/api/component/core/DockingRect.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.lambda.gui.api.component.core
22

3+
import com.lambda.module.modules.client.ClickGui
34
import com.lambda.util.math.MathUtils.coerceIn
5+
import com.lambda.util.math.MathUtils.roundToStep
46
import com.lambda.util.math.Rect
57
import com.lambda.util.math.Vec2d
68

@@ -35,9 +37,9 @@ abstract class DockingRect {
3537

3638
var screenSize: Vec2d = Vec2d.ZERO
3739

38-
var position
40+
var position: Vec2d
3941
get() = relativeToAbs(relativePos).coerceIn(0.0, screenSize.x - size.x, 0.0, screenSize.y - size.y)
40-
set(value) { relativePos = absToRelative(value); if (autoDocking) autoDocking() }
42+
set(value) { relativePos = absToRelative(value.roundToStep(ClickGui.dockingGridSize)); if (autoDocking) autoDocking() }
4143

4244
private val dockingOffset get() = (screenSize - size) * Vec2d(dockingH.multiplier, dockingV.multiplier)
4345

common/src/main/kotlin/com/lambda/gui/impl/clickgui/buttons/setting/NumberSlider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class NumberSlider<N>(
3838
}
3939
}
4040

41-
override fun getText() = "$setting"
41+
override fun getText() = "$setting".replace(',', '.') // "0,0".toDouble() is null
4242
override fun setStringValue(string: String) {
4343
string.toDoubleOrNull()?.let(::setValue)
4444
}

common/src/main/kotlin/com/lambda/module/modules/client/ClickGui.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ object ClickGui : Module(
3030
// Alignment
3131
val allowHAlign by setting("Allow H Docking", false)
3232
val allowVAlign by setting("Allow V Docking", true)
33+
val dockingGridSize by setting("Docking Grid Size", 1.0, 0.0..20.0, 0.5)
3334

3435
init {
3536
onEnable {

common/src/main/kotlin/com/lambda/util/math/MathUtils.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.lambda.util.math
22

33
import com.lambda.interaction.rotation.Rotation
4+
import com.lambda.module.modules.client.ClickGui
45
import com.lambda.util.math.ColorUtils.a
56
import com.lambda.util.math.ColorUtils.b
67
import com.lambda.util.math.ColorUtils.g
78
import com.lambda.util.math.ColorUtils.r
9+
import com.lambda.util.math.MathUtils.roundToStep
10+
import net.fabricmc.loader.impl.lib.sat4j.core.Vec
811
import net.minecraft.util.math.Vec3d
912
import java.awt.Color
1013
import java.math.BigDecimal
@@ -33,13 +36,18 @@ object MathUtils {
3336

3437
fun <T : Number> T.roundToStep(step: T): T {
3538
val stepD = step.toDouble()
39+
if (stepD == 0.0) return this
40+
3641
var value = round(toDouble() / stepD) * stepD
3742
value = value.roundToPlaces(decimalPlaces(stepD))
3843
if (abs(value) == 0.0) value = 0.0
3944

4045
return typeConvert(value)
4146
}
4247

48+
fun Vec2d.roundToStep(step: Double): Vec2d =
49+
Vec2d(x.roundToStep(step), y.roundToStep(step))
50+
4351
fun Double.roundToPlaces(places: Int) =
4452
BigDecimal(this).setScale(places, RoundingMode.HALF_EVEN).toDouble()
4553

0 commit comments

Comments
 (0)