Skip to content

Commit b26725d

Browse files
committed
Merge branch 'feature/renderer' of https://github.com/Avanatiker/NeoLambda into feature/renderer
2 parents 69023d6 + f074faf commit b26725d

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

common/src/main/kotlin/com/lambda/module/HudModule.kt

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,47 @@ abstract class HudModule(
1919

2020
protected abstract val width: Double
2121
protected abstract val height: Double
22+
private val size get() = Vec2d(width, height)
2223

23-
private var px by setting("Position X", 0.0, -10000.0..10000.0, 0.1) { false }
24-
private var py by setting("Position Y", 0.0, -10000.0..10000.0, 0.1) { false }
24+
private var relativePosX by setting("Position X", 0.0, -10000.0..10000.0, 0.1) { false }
25+
private var relativePosY by setting("Position Y", 0.0, -10000.0..10000.0, 0.1) { false }
26+
private var relativePos get() = Vec2d(relativePosX, relativePosY); set(value) {
27+
val vec = absToRelative(relativeToAbs(value))
28+
relativePosX = vec.x; relativePosY = vec.y
29+
}
30+
31+
var position get() = relativeToAbs(relativePos).let {
32+
val x = it.x.coerceIn(0.0, screenSize.x - width)
33+
val y = it.y.coerceIn(0.0, screenSize.y - height)
34+
Vec2d(x, y)
35+
}; set(value) { relativePos = absToRelative(value) }
36+
37+
private fun relativeToAbs(posIn: Vec2d) = posIn + (screenSize - size) * dockingMultiplier
38+
private fun absToRelative(posIn: Vec2d) = posIn - (screenSize - size) * dockingMultiplier
39+
40+
private val dockingH by setting("Docking H", HAlign.LEFT).apply {
41+
onValueChange { from, to ->
42+
val delta = to.multiplier - from.multiplier
43+
relativePosX += delta * (size.x - screenSize.x)
44+
}
45+
}
46+
private val dockingV by setting("Docking V", VAlign.TOP).apply {
47+
onValueChange { from, to ->
48+
val delta = to.multiplier - from.multiplier
49+
relativePosY += delta * (size.y - screenSize.y)
50+
}
51+
}
52+
53+
private val dockingMultiplier get() = Vec2d(dockingH.multiplier, dockingV.multiplier)
2554

26-
private var screenSize = Vec2d.ZERO
27-
var position get() = Vec2d(px, py); set(value) { setPos(value.x, value.y) }
2855
val rect get() = Rect.basedOn(position, width, height)
2956

57+
private var screenSize = Vec2d.ZERO
3058
private val renderer = RenderLayer()
3159

3260
protected fun onRender(block: RenderLayer.() -> Unit) =
3361
renderCallables.add(block)
3462

35-
private fun setPos(x: Double, y: Double) {
36-
val xRange = 0.0..screenSize.x - width
37-
val yRange = 0.0..screenSize.y - height
38-
39-
px = x.coerceIn(xRange)
40-
py = y.coerceIn(yRange)
41-
}
42-
4363
init {
4464
listener<RenderEvent.GUI.HUD> { event ->
4565
screenSize = event.screenSize
@@ -51,4 +71,18 @@ abstract class HudModule(
5171
renderer.render()
5272
}
5373
}
74+
75+
@Suppress("UNUSED")
76+
enum class HAlign(val multiplier: Float) {
77+
LEFT(0.0f),
78+
CENTER(0.5f),
79+
RIGHT(1.0f)
80+
}
81+
82+
@Suppress("UNUSED")
83+
enum class VAlign(val multiplier: Float) {
84+
TOP(0.0f),
85+
CENTER(0.5f),
86+
BOTTOM(1.0f)
87+
}
5488
}

0 commit comments

Comments
 (0)