Skip to content

Commit 9ade9f7

Browse files
committed
Child logic rework(again), dropdown buttons
1 parent 6170a30 commit 9ade9f7

File tree

22 files changed

+160
-170
lines changed

22 files changed

+160
-170
lines changed

common/src/main/kotlin/com/lambda/config/serializer/gui/CustomModuleWindowSerializer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ object CustomModuleWindowSerializer : JsonSerializer<CustomModuleWindow>, JsonDe
4747
width = it["width"].asDouble
4848
height = it["height"].asDouble
4949
isOpen = it["isOpen"].asBoolean
50-
forceSetPosition(Vec2d(
50+
position = Vec2d(
5151
it["position"].asJsonArray[0].asDouble,
5252
it["position"].asJsonArray[1].asDouble
53-
))
53+
)
5454
}
5555
} ?: throw JsonParseException("Invalid window data")
5656
}

common/src/main/kotlin/com/lambda/config/serializer/gui/TagWindowSerializer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ object TagWindowSerializer : JsonSerializer<TagWindow>, JsonDeserializer<TagWind
3434
width = it["width"].asDouble
3535
height = it["height"].asDouble
3636
isOpen = it["isOpen"].asBoolean
37-
forceSetPosition(Vec2d(
37+
position = Vec2d(
3838
it["position"].asJsonArray[0].asDouble,
3939
it["position"].asJsonArray[1].asDouble
40-
))
40+
)
4141
}
4242
} ?: throw JsonParseException("Invalid window data")
4343
}

common/src/main/kotlin/com/lambda/graphics/gl/Scissor.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.lambda.module.modules.client.GuiSettings
55
import com.lambda.util.math.MathUtils.ceilToInt
66
import com.lambda.util.math.MathUtils.floorToInt
77
import com.lambda.util.math.Rect
8+
import com.mojang.blaze3d.systems.RenderSystem
89
import org.lwjgl.opengl.GL30C.*
910
import kotlin.math.max
1011

@@ -18,8 +19,9 @@ object Scissor {
1819
}
1920

2021
private fun registerScissor(rect: Rect, block: () -> Unit) {
21-
scissor(rect)
22+
stack.add(rect)
2223

24+
scissor(rect)
2325
block()
2426

2527
stack.removeLast()
@@ -28,22 +30,19 @@ object Scissor {
2830

2931
private fun scissor(entry: Rect?) {
3032
if (entry == null) {
31-
glDisable(GL_SCISSOR_TEST)
33+
RenderSystem.disableScissor()
3234
return
3335
}
3436

35-
stack.add(entry)
36-
3737
val pos1 = entry.leftTop * GuiSettings.scale
3838
val pos2 = entry.rightBottom * GuiSettings.scale
3939

40-
val width = max(pos2.x - pos1.x, 0.0)
41-
val height = max(pos2.y - pos1.y, 0.0)
40+
val width = max(pos2.x - pos1.x, 1.0)
41+
val height = max(pos2.y - pos1.y, 1.0)
4242

4343
val y = mc.window.framebufferHeight - pos1.y - height
4444

45-
glEnable(GL_SCISSOR_TEST)
46-
glScissor(
45+
RenderSystem.enableScissor(
4746
pos1.x.floorToInt(),
4847
y.floorToInt(),
4948
width.ceilToInt(),

common/src/main/kotlin/com/lambda/gui/GuiConfigurable.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object GuiConfigurable : Configurable(GuiConfig), Loadable {
2929
ModuleTag.defaults.mapIndexed { index, tag ->
3030
TagWindow(tag, ownerGui).apply {
3131
val step = 5.0
32-
forceSetPosition(Vec2d((width + step) * index, 0.0) + step)
32+
position = Vec2d((width + step) * index, 0.0) + step
3333
}
3434
}
3535
}

common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.lambda.module.Module
1111
import com.lambda.util.KeyCode
1212
import com.lambda.util.Mouse
1313
import com.lambda.util.Nameable
14+
import com.lambda.util.math.Rect
1415
import com.lambda.util.math.Vec2d
1516
import com.mojang.blaze3d.systems.RenderSystem.recordRenderCall
1617
import net.minecraft.client.gui.DrawContext
@@ -23,6 +24,8 @@ abstract class LambdaGui(
2324
private val owner: Module? = null
2425
) : Screen(Text.of(name)), IComponent, Nameable {
2526
private var screenSize = Vec2d.ZERO
27+
override val rect get() = Rect(Vec2d.ZERO, screenSize)
28+
2629
val animation = AnimationTicker()
2730

2831
private val renderListener = UnsafeListener(0, this, false) { event ->

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package com.lambda.gui.api.component
22

33
import com.lambda.gui.api.GuiEvent
44
import com.lambda.gui.api.component.core.IComponent
5-
import com.lambda.gui.api.component.core.IRectComponent
65
import com.lambda.util.Mouse
6+
import com.lambda.util.math.Rect
77

8-
abstract class InteractiveComponent : IComponent, IRectComponent {
8+
abstract class InteractiveComponent : IComponent {
99
protected var hovered = false
1010
protected var pressed = false
1111

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.lambda.gui.api.component
33
import com.lambda.gui.api.GuiEvent
44
import com.lambda.gui.api.component.button.ListButton
55
import com.lambda.gui.impl.clickgui.AbstractClickGui
6-
import com.lambda.gui.impl.clickgui.buttons.SettingButton
76
import com.lambda.module.modules.client.ClickGui
87

98
abstract class ListWindow <T : ListButton> (
@@ -13,7 +12,6 @@ abstract class ListWindow <T : ListButton> (
1312
if (e is GuiEvent.Show || e is GuiEvent.Tick) {
1413
var y = 0.0
1514
contentComponents.children.forEach { button ->
16-
if (button is SettingButton<*, *> && !button.visible) return@forEach
1715
button.heightOffset = y
1816
y += button.size.y + ClickGui.buttonStep
1917
}

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

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.lambda.gui.api.component
22

3-
import com.lambda.Lambda.mc
43
import com.lambda.graphics.animation.Animation.Companion.exp
54
import com.lambda.graphics.gl.Scissor.scissor
65
import com.lambda.graphics.renderer.gui.font.IFontEntry
@@ -18,47 +17,48 @@ import com.lambda.util.math.MathUtils.lerp
1817
import com.lambda.util.math.MathUtils.toInt
1918
import com.lambda.util.math.Rect
2019
import com.lambda.util.math.Vec2d
21-
import com.lambda.util.primitives.extension.partialTicks
2220
import java.awt.Color
2321
import kotlin.math.abs
2422

2523
abstract class WindowComponent <T : ChildComponent> (
2624
final override val owner: AbstractClickGui
27-
) : ChildComponent() {
25+
) : ChildComponent(owner) {
2826
abstract val title: String
2927

3028
abstract var width: Double
3129
abstract var height: Double
3230

3331
var position = Vec2d.ZERO
34-
private var prevPosition = position
3532

3633
var isOpen = true
34+
override val isActive get() = isOpen
35+
3736
private var dragOffset: Vec2d? = null
3837
private val padding get() = ClickGui.windowPadding
3938

40-
final override val rect get() = Rect.basedOn(renderPosition, width, renderHeight + titleBarHeight)
41-
val contentRect get() = rect.shrink(padding).moveFirst(Vec2d(0.0, titleBarHeight - padding))
39+
final override val rect get() = Rect.basedOn(position, width, renderHeight + titleBarHeight)
40+
private val contentRect get() = rect.shrink(padding).moveFirst(Vec2d(0.0, titleBarHeight - padding))
4241

4342
private val titleBar get() = Rect.basedOn(rect.leftTop, rect.size.x, titleBarHeight)
4443
private val titleBarHeight get() = titleFont.height + 2 + padding * 2
4544
private val titleFont: IFontEntry
4645

4746
private val layer = RenderLayer()
4847
private val renderer = layer.entry()
49-
val subLayer = RenderLayer()
48+
private val contentLayer = RenderLayer()
49+
50+
private val animation = owner.animation
51+
private val gui = owner
5052

51-
val animation = owner.animation
52-
open val showAnimation get() = owner.showAnimation
53+
// Show animation for children
54+
private val showAnimation0 by animation.exp(0.0, 1.0, 0.5, ::isOpen)
55+
override val showAnimation get() = lerp(0.0, showAnimation0, gui.showAnimation)
5356

5457
private val actualHeight get() = height + padding * 2 * isOpen.toInt()
5558
private var renderHeightAnimation by animation.exp({ 0.0 }, ::actualHeight, 0.6, ::isOpen)
5659
private val renderHeight get() = lerp(0.0, renderHeightAnimation, showAnimation)
57-
private val renderPosition get() = lerp(prevPosition, position, mc.partialTicks)
5860

59-
val contentComponents = ChildLayer<T> { child ->
60-
child.rect in contentRect && accessible && isOpen
61-
}
61+
val contentComponents = ChildLayer.Drawable<T>(gui, this, contentLayer, ::contentRect)
6262

6363
/*val titleBarComponents = ChildLayer<ButtonComponent> { child ->
6464
child.rect in titleBar && accessible
@@ -71,7 +71,7 @@ abstract class WindowComponent <T : ChildComponent> (
7171
roundRadius = ClickGui.windowRadius
7272
shade = GuiSettings.shadeBackground
7373

74-
val alpha = (showAnimation * 2.0).coerceIn(0.0, 1.0)
74+
val alpha = (gui.showAnimation * 2.0).coerceIn(0.0, 1.0)
7575
color(GuiSettings.backgroundColor.multAlpha(alpha))
7676
}
7777

@@ -81,15 +81,15 @@ abstract class WindowComponent <T : ChildComponent> (
8181
outerGlow = ClickGui.windowRadius
8282
shade = GuiSettings.shade
8383

84-
val alpha = (showAnimation * 2.0).coerceIn(0.0, 1.0)
84+
val alpha = (gui.showAnimation * 2.0).coerceIn(0.0, 1.0)
8585
color(GuiSettings.mainColor.multAlpha(alpha))
8686
}
8787

8888
// Title
8989
titleFont = renderer.font {
9090
text = title
9191
position = titleBar.center - widthVec * 0.5
92-
color = Color.WHITE.setAlpha(showAnimation)
92+
color = Color.WHITE.setAlpha(gui.showAnimation)
9393
}
9494
}
9595

@@ -101,22 +101,21 @@ abstract class WindowComponent <T : ChildComponent> (
101101
dragOffset = null
102102
}
103103

104-
is GuiEvent.Tick -> {
105-
prevPosition = position
106-
}
107-
108104
is GuiEvent.Render -> {
109-
layer.assignOffset(renderPosition)
110-
subLayer.assignOffset(renderPosition)
105+
layer.assignOffset(position)
106+
contentLayer.assignOffset(position)
111107

112108
// TODO: fix blur
113109
// BlurPostProcessor.render(rect, ClickGui.windowBlur, guiAnimation)
114110

115111
layer.render()
116112

117113
scissor(contentRect) {
118-
subLayer.render()
114+
contentLayer.render()
115+
contentComponents.onEvent(e)
119116
}
117+
118+
return
120119
}
121120

122121
is GuiEvent.MouseMove -> {
@@ -149,11 +148,6 @@ abstract class WindowComponent <T : ChildComponent> (
149148
//titleBarComponents.onEvent(e)
150149
}
151150

152-
fun forceSetPosition(pos: Vec2d) {
153-
position = pos
154-
prevPosition = position
155-
}
156-
157151
fun focus() {
158152
// move window into foreground
159153
owner.apply {
@@ -177,6 +171,6 @@ abstract class WindowComponent <T : ChildComponent> (
177171

178172
override fun onRemove() {
179173
layer.destroy()
180-
subLayer.destroy()
174+
contentLayer.destroy()
181175
}
182176
}

common/src/main/kotlin/com/lambda/gui/api/component/button/ButtonComponent.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.lambda.gui.api.component.button
22

33
import com.lambda.graphics.animation.Animation.Companion.exp
44
import com.lambda.gui.api.GuiEvent
5-
import com.lambda.gui.api.component.WindowComponent
65
import com.lambda.gui.api.component.core.list.ChildComponent
6+
import com.lambda.gui.api.component.core.list.ChildLayer
77
import com.lambda.module.modules.client.ClickGui
88
import com.lambda.module.modules.client.GuiSettings
99
import com.lambda.util.math.ColorUtils.multAlpha
@@ -13,31 +13,29 @@ import com.lambda.util.math.Vec2d
1313
import java.awt.Color
1414

1515
abstract class ButtonComponent(
16-
final override val owner: WindowComponent<*>
17-
) : ChildComponent() {
16+
final override val owner: ChildLayer.Drawable<*>
17+
) : ChildComponent(owner) {
1818
abstract val position: Vec2d
1919
abstract val size: Vec2d
2020

2121
abstract val text: String
22+
protected open val textY: Double get() = rect.size.y
2223
protected abstract var activeAnimation: Double
2324

24-
private val actualSize get() = Vec2d(if (size.x == FILL_PARENT) owner.contentRect.size.x else size.x, size.y)
25-
final override val rect get() = Rect.basedOn(position, actualSize) + owner.contentRect.leftTop
25+
private val actualSize get() = Vec2d(if (size.x == FILL_PARENT) owner.rect.size.x else size.x, size.y)
26+
final override val rect get() = Rect.basedOn(position, actualSize) + owner.rect.leftTop
2627

27-
private val layer = owner.subLayer
28-
protected val renderer = layer.entry()
29-
protected val animation = owner.animation
28+
protected val renderer = owner.renderer.entry()
29+
protected val animation = owner.gui.animation
3030

3131
private var hoverRectAnimation by animation.exp({ 0.0 }, { 1.0 }, { if (renderHovered) 0.6 else 0.07 }, ::renderHovered)
3232
private var hoverFontAnimation by animation.exp(0.0, 1.0, 0.5, ::renderHovered)
3333
private var pressAnimation by animation.exp(0.0, 1.0, 0.5, ::pressed)
3434
protected val interactAnimation get() = lerp(hoverRectAnimation, 1.5, pressAnimation) * 0.4
35-
private val showAnimationRaw by animation.exp(0.0, 1.0, 0.7, owner::isOpen)
36-
protected open val showAnimation get() = lerp(0.0, showAnimationRaw, owner.showAnimation)
35+
override val showAnimation: Double get() = owner.showAnimation
3736

3837
private var lastHoveredTime = 0L
39-
private val renderHovered get() = hovered ||
40-
System.currentTimeMillis() - lastHoveredTime < 110 // a bit more than 2 ticks
38+
private val renderHovered get() = hovered || System.currentTimeMillis() - lastHoveredTime < 110
4139

4240
init {
4341
// Active color
@@ -64,8 +62,8 @@ abstract class ButtonComponent(
6462

6563
color = lerp(Color.WHITE, GuiSettings.mainColor, activeAnimation).multAlpha(showAnimation)
6664

67-
val x = rect.left + ClickGui.windowPadding + interactAnimation + hoverFontAnimation
68-
position = Vec2d(x, rect.center.y)
65+
val x = ClickGui.windowPadding + interactAnimation + hoverFontAnimation
66+
position = rect.leftTop + Vec2d(x, textY)
6967
}
7068
}
7169

@@ -85,7 +83,7 @@ abstract class ButtonComponent(
8583
}
8684

8785
override fun onRelease(e: GuiEvent.MouseClick) {
88-
performClickAction(e)
86+
if (hovered) performClickAction(e)
8987
}
9088

9189
override fun onRemove() {

common/src/main/kotlin/com/lambda/gui/api/component/button/ListButton.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package com.lambda.gui.api.component.button
22

33
import com.lambda.graphics.animation.Animation.Companion.exp
44
import com.lambda.gui.api.GuiEvent
5-
import com.lambda.gui.api.component.WindowComponent
5+
import com.lambda.gui.api.component.core.list.ChildLayer
66
import com.lambda.module.modules.client.ClickGui
77
import com.lambda.util.math.MathUtils.toInt
88
import com.lambda.util.math.Vec2d
99

10-
abstract class ListButton(owner: WindowComponent<*>) : ButtonComponent(owner) {
10+
abstract class ListButton(owner: ChildLayer.Drawable<*>) : ButtonComponent(owner) {
1111
override val position get() = Vec2d(0.0, renderHeightOffset)
1212
override val size get() = Vec2d(FILL_PARENT, ClickGui.buttonHeight)
1313

1414
var heightOffset = 0.0
15-
protected open val targetHeightOffset get() = heightOffset * owner.showAnimation * owner.isOpen.toInt()
16-
private var renderHeightOffset by animation.exp(::targetHeightOffset, 0.5)
15+
protected open val targetHeightOffset get() = heightOffset * owner.showAnimation * owner.isActive.toInt()
16+
private var renderHeightOffset by animation.exp(::targetHeightOffset, 0.7)
1717

1818
override fun onEvent(e: GuiEvent) {
1919
super.onEvent(e)

0 commit comments

Comments
 (0)