Skip to content

Commit ab4107a

Browse files
committed
Added boolean button
1 parent 63021f7 commit ab4107a

File tree

12 files changed

+139
-57
lines changed

12 files changed

+139
-57
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ 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
67
import com.lambda.module.modules.client.ClickGui
78

89
abstract class ListWindow <T : ListButton> (
910
owner: AbstractClickGui
1011
) : WindowComponent<T>(owner) {
1112
override fun onEvent(e: GuiEvent) {
12-
if (e is GuiEvent.Tick) {
13-
contentComponents.children.forEachIndexed { i, button ->
14-
button.heightOffset = i * (ClickGui.buttonHeight + ClickGui.buttonStep)
13+
if (e is GuiEvent.Show || e is GuiEvent.Tick) {
14+
var y = 0.0
15+
contentComponents.children.forEach { button ->
16+
if (button is SettingButton<*, *> && !button.visible) return@forEach
17+
button.heightOffset = y
18+
y += button.size.y + ClickGui.buttonStep
1519
}
1620
}
1721

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import com.lambda.util.math.MathUtils.lerp
1111
import com.lambda.util.math.Rect
1212
import com.lambda.util.math.Vec2d
1313
import java.awt.Color
14-
import kotlin.math.abs
1514

1615
abstract class ButtonComponent(
1716
final override val owner: WindowComponent<*>
@@ -20,23 +19,21 @@ abstract class ButtonComponent(
2019
abstract val size: Vec2d
2120

2221
abstract val text: String
23-
abstract val active: Boolean
22+
protected abstract var activeAnimation: Double
2423

2524
private val actualSize get() = Vec2d(if (size.x == FILL_PARENT) owner.contentRect.size.x else size.x, size.y)
2625
final override val rect get() = Rect.basedOn(position, actualSize) + owner.contentRect.leftTop
2726

2827
private val layer = owner.subLayer
29-
private val renderer = layer.entry()
28+
protected val renderer = layer.entry()
3029
protected val animation = owner.animation
3130

32-
private var activeAnimation by animation.exp(0.0, 1.0, 0.15, ::active)
33-
private var toggleFxDirection by animation.exp(0.0, 1.0, 0.6, ::active)
3431
private var hoverRectAnimation by animation.exp({ 0.0 }, { 1.0 }, { if (renderHovered) 0.6 else 0.07 }, ::renderHovered)
3532
private var hoverFontAnimation by animation.exp(0.0, 1.0, 0.5, ::renderHovered)
3633
private var pressAnimation by animation.exp(0.0, 1.0, 0.5, ::pressed)
37-
private val interactAnimation get() = lerp(hoverRectAnimation, 1.5, pressAnimation) * 0.4
34+
protected val interactAnimation get() = lerp(hoverRectAnimation, 1.5, pressAnimation) * 0.4
3835
private val showAnimationRaw by animation.exp(0.0, 1.0, 0.7, owner::isOpen)
39-
private val showAnimation get() = lerp(0.0, showAnimationRaw, owner.showAnimation)
36+
protected open val showAnimation get() = lerp(0.0, showAnimationRaw, owner.showAnimation)
4037

4138
private var lastHoveredTime = 0L
4239
private val renderHovered get() = hovered ||
@@ -58,26 +55,6 @@ abstract class ButtonComponent(
5855
color(GuiSettings.mainColor.multAlpha(alpha))
5956
}
6057

61-
// Toggle fx
62-
renderer.rect {
63-
val left = rect - Vec2d(rect.size.x, 0.0)
64-
val right = rect + Vec2d(rect.size.x, 0.0)
65-
66-
position = lerp(left, right, activeAnimation)
67-
.clamp(rect)
68-
.shrink(interactAnimation)
69-
70-
// 0.0 .. 1.0 .. 0.0 animation
71-
val alpha = 1.0 - (abs(activeAnimation - 0.5) * 2.0)
72-
val color = GuiSettings.mainColor.multAlpha(alpha * 0.6 * showAnimation)
73-
74-
// "Tail" effect
75-
val leftColor = color.multAlpha(1.0 - toggleFxDirection)
76-
val rightColor = color.multAlpha(toggleFxDirection)
77-
78-
colorH(leftColor, rightColor)
79-
}
80-
8158
// Text
8259
renderer.font {
8360
text = this@ButtonComponent.text

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class ListButton(owner: WindowComponent<*>) : ButtonComponent(owner) {
1212
override val size get() = Vec2d(FILL_PARENT, ClickGui.buttonHeight)
1313

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

1818
override fun onEvent(e: GuiEvent) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ package com.lambda.gui.api.component.core
33
import com.lambda.gui.api.GuiEvent
44

55
interface IComponent {
6-
// TODO: Use event system?
76
fun onEvent(e: GuiEvent)
87
}

common/src/main/kotlin/com/lambda/gui/api/layer/LayerEntry.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.lambda.gui.api.layer
33
import com.lambda.graphics.renderer.IRenderEntry
44
import com.lambda.graphics.renderer.IRenderer
55
import com.lambda.graphics.renderer.gui.font.IFontEntry
6-
import com.lambda.graphics.renderer.gui.rect.IRectEntry
6+
import com.lambda.graphics.renderer.gui.rect.filled.IRectEntry
77

88
// Used to group all render entries related to a component
99
class LayerEntry (

common/src/main/kotlin/com/lambda/gui/api/layer/RenderLayer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.lambda.gui.api.layer
22

33
import com.lambda.graphics.renderer.gui.font.FontRenderer
4-
import com.lambda.graphics.renderer.gui.rect.RectRenderer
4+
import com.lambda.graphics.renderer.gui.rect.filled.RectRenderer
55
import com.lambda.module.modules.client.GuiSettings
66
import com.lambda.util.math.Vec2d
77

common/src/main/kotlin/com/lambda/gui/impl/clickgui/AbstractClickGui.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.lambda.gui.api.GuiEvent
66
import com.lambda.gui.api.LambdaGui
77
import com.lambda.gui.api.component.WindowComponent
88
import com.lambda.gui.api.component.core.list.ChildLayer
9-
import com.lambda.gui.impl.clickgui.windows.SettingsWindow
9+
import com.lambda.gui.impl.clickgui.windows.SettingWindow
1010
import com.lambda.module.modules.client.ClickGui
1111

1212
abstract class AbstractClickGui(name: String = "ClickGui") : LambdaGui(name, ClickGui) {
@@ -34,7 +34,7 @@ abstract class AbstractClickGui(name: String = "ClickGui") : LambdaGui(name, Cli
3434
showAnimation = 0.0
3535

3636
windows.children
37-
.filterIsInstance<SettingsWindow>()
37+
.filterIsInstance<SettingWindow>()
3838
.forEach(WindowComponent<*>::destroy)
3939
}
4040

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

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,68 @@
11
package com.lambda.gui.impl.clickgui.buttons
22

3+
import com.lambda.graphics.animation.Animation.Companion.exp
34
import com.lambda.gui.api.GuiEvent
45
import com.lambda.gui.api.component.WindowComponent
56
import com.lambda.gui.api.component.button.ListButton
6-
import com.lambda.gui.impl.clickgui.windows.SettingsWindow
7+
import com.lambda.gui.impl.clickgui.windows.SettingWindow
78
import com.lambda.module.Module
9+
import com.lambda.module.modules.client.GuiSettings
810
import com.lambda.util.Mouse
11+
import com.lambda.util.math.ColorUtils.multAlpha
12+
import com.lambda.util.math.MathUtils.lerp
13+
import com.lambda.util.math.Vec2d
914
import com.mojang.blaze3d.systems.RenderSystem.recordRenderCall
15+
import kotlin.math.abs
1016

1117
class ModuleButton(val module: Module, owner: WindowComponent<*>) : ListButton(owner) {
1218
override val text get() = module.name
13-
override val active get() = module.isEnabled
19+
private val active get() = module.isEnabled
1420
private val gui = owner.owner
1521

22+
override var activeAnimation by animation.exp(0.0, 1.0, 0.15, ::active)
23+
private var toggleFxDirection by animation.exp(0.0, 1.0, 0.7, ::active)
24+
25+
init {
26+
// Toggle fx
27+
renderer.rect {
28+
val left = rect - Vec2d(rect.size.x, 0.0)
29+
val right = rect + Vec2d(rect.size.x, 0.0)
30+
31+
position = lerp(left, right, activeAnimation)
32+
.clamp(rect)
33+
.shrink(interactAnimation)
34+
35+
// 0.0 .. 1.0 .. 0.0 animation
36+
val alpha = 1.0 - (abs(activeAnimation - 0.5) * 2.0)
37+
val color = GuiSettings.mainColor.multAlpha(alpha * 0.6 * showAnimation)
38+
39+
// "Tail" effect
40+
val leftColor = color.multAlpha(1.0 - toggleFxDirection)
41+
val rightColor = color.multAlpha(toggleFxDirection)
42+
43+
colorH(leftColor, rightColor)
44+
}
45+
}
46+
1647
override fun performClickAction(e: GuiEvent.MouseClick) {
1748
when (e.button) {
1849
Mouse.Button.Left -> if (hovered) module.toggle()
19-
Mouse.Button.Right -> {
50+
Mouse.Button.Right -> { // Open new settings window
2051
gui.apply {
21-
// Open new settings window or move existing one to the cursor
22-
val settingsWindow = windows.children.filterIsInstance<SettingsWindow>()
23-
.firstOrNull { it.button == this@ModuleButton }?.apply {
24-
position = e.mouse
25-
} ?: SettingsWindow(this@ModuleButton, this).apply {
26-
forceSetPosition(e.mouse)
52+
val check = windows.children
53+
.filterIsInstance<SettingWindow>()
54+
.filter { it.button.module == module }
55+
.onEach { it.isOpen = false }
56+
.isNotEmpty()
2757

28-
scheduleAction {
29-
windows.addChild(this)
30-
}
31-
}
58+
if (check) return
3259

3360
// we have to wait this tag window to be focused after handling a click event
3461
// to place settings window over it
3562
recordRenderCall {
36-
settingsWindow.focus()
63+
SettingWindow(this@ModuleButton, this).apply {
64+
forceSetPosition(e.mouse)
65+
}.apply(windows::addChild)
3766
}
3867
}
3968
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.lambda.gui.impl.clickgui.buttons
2+
3+
import com.lambda.config.AbstractSetting
4+
import com.lambda.graphics.animation.Animation.Companion.exp
5+
import com.lambda.gui.api.GuiEvent
6+
import com.lambda.gui.api.component.WindowComponent
7+
import com.lambda.gui.api.component.button.ListButton
8+
import com.lambda.util.math.MathUtils.lerp
9+
import com.lambda.util.math.MathUtils.toInt
10+
11+
abstract class SettingButton <V : Any, T : AbstractSetting<V>> (
12+
val setting: T,
13+
owner: WindowComponent<*>
14+
): ListButton(owner) {
15+
protected var value by setting
16+
var visible = true
17+
18+
private var visibilityAnimation by animation.exp(0.0, 1.0, 0.6, ::visible)
19+
override val showAnimation get() = lerp(0.0, super.showAnimation, visibilityAnimation)
20+
override val targetHeightOffset: Double get() {
21+
var out = super.targetHeightOffset
22+
if (!visible) out -= size.y * 0.5
23+
return out
24+
}
25+
26+
override var accessible: Boolean = false; get() = field && visible
27+
28+
override fun onEvent(e: GuiEvent) {
29+
if (e is GuiEvent.Show || e is GuiEvent.Tick) visible = setting.visibility()
30+
if (e is GuiEvent.Show) visibilityAnimation = visible.toInt().toDouble()
31+
super.onEvent(e)
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.lambda.gui.impl.clickgui.buttons.setting
2+
3+
import com.lambda.config.settings.comparable.BooleanSetting
4+
import com.lambda.graphics.animation.Animation.Companion.exp
5+
import com.lambda.gui.api.GuiEvent
6+
import com.lambda.gui.api.component.WindowComponent
7+
import com.lambda.gui.impl.clickgui.buttons.SettingButton
8+
import com.lambda.util.Mouse
9+
10+
class BooleanButton(
11+
setting: BooleanSetting,
12+
owner: WindowComponent<*>
13+
) : SettingButton<Boolean, BooleanSetting>(setting, owner) {
14+
override val text = setting.name
15+
override var activeAnimation by animation.exp(0.0, 1.0, 0.5, ::value)
16+
17+
override fun performClickAction(e: GuiEvent.MouseClick) {
18+
if (e.button == Mouse.Button.Left && hovered) value = !value
19+
}
20+
}

0 commit comments

Comments
 (0)