Skip to content

Commit 779beca

Browse files
committed
Boolean button
1 parent bdd7453 commit 779beca

File tree

8 files changed

+133
-48
lines changed

8 files changed

+133
-48
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ object NewCGui : Module(
3535
defaultTags = setOf(ModuleTag.CLIENT)
3636
) {
3737
val titleBarHeight by setting("Title Bar Height", 18.0, 10.0..25.0, 0.1)
38-
val settingsHeight by setting("Settings Height", 16.0, 10.0..25.0, 0.1)
38+
val moduleHeight by setting("Module Height", 18.0, 10.0..25.0, 0.1)
39+
val settingsHeight by setting("Settings Height", 14.0, 10.0..25.0, 0.1)
3940
val padding by setting("Padding", 2.0, 1.0..6.0, 0.1)
4041
val listStep by setting("List Step", 2.0, 0.0..6.0, 0.1)
4142
val autoResize by setting("Auto Resize", false)
@@ -59,7 +60,12 @@ object NewCGui : Module(
5960
val moduleDisabledColor by setting("Module Disabled Color", Color.WHITE.setAlpha(0.05))
6061

6162
private val SCREEN get() = gui("New Click Gui") {
62-
63+
rect {
64+
onUpdate {
65+
rectangle = owner!!.rect
66+
setColor(backgroundTint)
67+
}
68+
}
6369

6470
val tags = ModuleTag.defaults
6571
val modules = ModuleRegistry.modules

common/src/main/kotlin/com/lambda/newgui/component/core/FilledRect.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ class FilledRect(
4545
}
4646

4747
init {
48+
properties.interactionPassthrough = true
49+
4850
onRender {
4951
updateActions.forEach { action ->
5052
action(this@FilledRect)
5153
}
5254

55+
// make it pressable
56+
position = rectangle.leftTop
57+
size = rectangle.size
58+
5359
filled.build(
5460
rectangle,
5561
leftTopRadius,
@@ -79,14 +85,26 @@ class FilledRect(
7985
leftBottomColor = color
8086
}
8187

88+
fun setColorH(colorL: Color, colorR: Color) {
89+
leftTopColor = colorL
90+
rightTopColor = colorR
91+
rightBottomColor = colorR
92+
leftBottomColor = colorL
93+
}
94+
95+
fun setColorV(colorT: Color, colorB: Color) {
96+
leftTopColor = colorT
97+
rightTopColor = colorT
98+
rightBottomColor = colorB
99+
leftBottomColor = colorB
100+
}
101+
82102
companion object {
83103
/**
84104
* Creates a [FilledRect] component - layout-based rect representation
85105
*/
86106
@UIBuilder
87107
fun Layout.rect(block: FilledRect.() -> Unit = {}) =
88-
FilledRect(this).apply(children::add).apply {
89-
updateActions += block
90-
}
108+
FilledRect(this).apply(children::add).apply(block)
91109
}
92110
}

common/src/main/kotlin/com/lambda/newgui/component/core/OutlineRect.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class OutlineRect(
4242
}
4343

4444
init {
45+
properties.interactionPassthrough = true
46+
4547
onRender {
4648
updateActions.forEach { action ->
4749
action(this@OutlineRect)
@@ -73,8 +75,6 @@ class OutlineRect(
7375
*/
7476
@UIBuilder
7577
fun Layout.outline(block: OutlineRect.() -> Unit = {}) =
76-
OutlineRect(this).apply(children::add).apply {
77-
updateActions += block
78-
}
78+
OutlineRect(this).apply(children::add).apply(block)
7979
}
8080
}

common/src/main/kotlin/com/lambda/newgui/component/layout/Layout.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ open class Layout(
7777
get() = Vec2d(width, height)
7878
set(value) { width = value.x; height = value.y }
7979

80-
val leftTop get() = position
80+
val leftTop get() = renderPosition
8181
val rightTop get() = Vec2d(renderPositionX + renderWidth, renderPositionY)
8282
val rightBottom get() = Vec2d(renderPositionX + renderWidth, renderPositionY + renderHeight)
8383
val leftBottom get() = Vec2d(renderPositionX, renderPositionY + renderHeight)
@@ -436,10 +436,10 @@ open class Layout(
436436
* Use it to set the mouse cursor type for various conditions: hovering, resizing, typing etc...
437437
*/
438438
@UIBuilder
439-
@Suppress("UNUSED_EXPRESSION")
440439
fun Layout.cursorController(): Mouse.CursorController {
441-
this // hack ide to let me make this ui-related only
442-
return Mouse.CursorController()
440+
val con = Mouse.CursorController()
441+
onHide { con.reset() }
442+
return con
443443
}
444444
}
445445
}

common/src/main/kotlin/com/lambda/newgui/component/window/Window.kt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,44 @@ open class Window(
5656
val content = windowContent(scrollable)
5757

5858
protected val titleBarRect = rect {
59-
rectangle = titleBar.rect
60-
setColor(NewCGui.titleBackgroundColor)
59+
onUpdate {
60+
rectangle = titleBar.rect
61+
setColor(NewCGui.titleBackgroundColor)
6162

62-
val radius = NewCGui.roundRadius
63-
leftTopRadius = radius
64-
rightTopRadius = radius
63+
val radius = NewCGui.roundRadius
64+
leftTopRadius = radius
65+
rightTopRadius = radius
6566

66-
val bottomRadius = lerp(content.renderHeight, radius, 0.0)
67-
leftBottomRadius = bottomRadius
68-
rightBottomRadius = bottomRadius
67+
val bottomRadius = lerp(content.renderHeight, radius, 0.0)
68+
leftBottomRadius = bottomRadius
69+
rightBottomRadius = bottomRadius
6970

70-
shade = NewCGui.backgroundShade
71+
shade = NewCGui.backgroundShade
72+
}
7173
}
7274

7375
protected val contentRect = rect {
74-
rectangle = Rect(titleBar.leftBottom, this@Window.rightBottom)
75-
setColor(NewCGui.backgroundColor)
76+
onUpdate {
77+
rectangle = Rect(titleBar.leftBottom, this@Window.rightBottom)
78+
setColor(NewCGui.backgroundColor)
7679

77-
leftBottomRadius = NewCGui.roundRadius
78-
rightBottomRadius = NewCGui.roundRadius
80+
leftBottomRadius = NewCGui.roundRadius
81+
rightBottomRadius = NewCGui.roundRadius
7982

80-
shade = NewCGui.backgroundShade
83+
shade = NewCGui.backgroundShade
84+
}
8185
}
8286

8387
protected val outlineRect = outline {
84-
rectangle = this@Window.rect
85-
setColor(NewCGui.outlineColor)
88+
onUpdate {
89+
rectangle = this@Window.rect
90+
setColor(NewCGui.outlineColor)
8691

87-
roundRadius = NewCGui.roundRadius
88-
glowRadius = NewCGui.outlineWidth * NewCGui.outline.toInt().toDouble()
92+
roundRadius = NewCGui.roundRadius
93+
glowRadius = NewCGui.outlineWidth * NewCGui.outline.toInt().toDouble()
8994

90-
shade = NewCGui.outlineShade
95+
shade = NewCGui.outlineShade
96+
}
9197
}
9298

9399
// Position
@@ -150,10 +156,6 @@ open class Window(
150156
}
151157
}
152158

153-
onHide {
154-
cursorController.reset()
155-
}
156-
157159
onTick {
158160
// Update cursor
159161
val rxh = resizeXHovered || resizeX != null

common/src/main/kotlin/com/lambda/newgui/impl/clickgui/ModuleLayout.kt

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@
1717

1818
package com.lambda.newgui.impl.clickgui
1919

20-
import com.lambda.config.settings.comparable.BooleanSetting
2120
import com.lambda.graphics.animation.Animation.Companion.exp
2221
import com.lambda.module.Module
2322
import com.lambda.module.modules.client.NewCGui
2423
import com.lambda.newgui.GuiManager.layoutOf
2524
import com.lambda.newgui.component.HAlign
2625
import com.lambda.newgui.component.core.FilledRect
26+
import com.lambda.newgui.component.core.FilledRect.Companion.rect
2727
import com.lambda.newgui.component.core.UIBuilder
2828
import com.lambda.newgui.component.layout.Layout
2929
import com.lambda.newgui.component.window.Window
30-
import com.lambda.newgui.impl.clickgui.settings.BooleanButton.Companion.booleanSetting
3130
import com.lambda.util.Mouse
32-
import com.lambda.util.math.Vec2d
33-
import com.lambda.util.math.lerp
31+
import com.lambda.util.math.*
32+
import java.awt.Color
3433

3534
class ModuleLayout(
3635
owner: Layout,
@@ -47,6 +46,7 @@ class ModuleLayout(
4746
private val cursorController = cursorController()
4847

4948
private var enableAnimation by animation.exp(0.0, 1.0, 0.6, module::isEnabled)
49+
private var openAnimation by animation.exp(1.0, 0.0, 0.6, ::minimized)
5050

5151
// Could be true only if owner is ModuleWindow
5252
var isLast = false
@@ -68,19 +68,34 @@ class ModuleLayout(
6868
}
6969
}
7070

71+
overrideHeight(NewCGui::moduleHeight)
72+
7173
onMouseClick { button, action ->
7274
if (button == Mouse.Button.Left && action == Mouse.Action.Click) {
7375
module.toggle()
7476
}
7577
}
7678
}
7779

78-
onShow {
79-
enableAnimation = 0.0
80+
rect { // Separator
81+
onUpdate {
82+
val vec = Vec2d(
83+
lerp(openAnimation, titleBar.renderWidth * 0.5, NewCGui.fontOffset * 0.5),
84+
-0.25
85+
)
86+
87+
rectangle = Rect(
88+
pos1 = titleBar.leftBottom + vec,
89+
pos2 = titleBar.rightBottom - vec
90+
)
91+
92+
setColor(lerp(enableAnimation, Color.WHITE, Color.BLACK).setAlpha(0.2 * openAnimation))
93+
shade = NewCGui.outlineShade
94+
}
8095
}
8196

82-
onHide {
83-
cursorController.reset()
97+
onShow {
98+
enableAnimation = 0.0
8499
}
85100

86101
titleBarRect.onUpdate {

common/src/main/kotlin/com/lambda/newgui/impl/clickgui/SettingLayout.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import com.lambda.util.math.Vec2d
2929
*/
3030
abstract class SettingLayout <V : Any, T: AbstractSetting<V>> (
3131
owner: Layout,
32-
setting: T,
32+
val setting: T,
3333
expandable: Boolean = false
3434
) : Window( // going to use window to easily implement expandable settings (such as color picker)
3535
owner,
@@ -41,8 +41,14 @@ abstract class SettingLayout <V : Any, T: AbstractSetting<V>> (
4141
AutoResize.ForceEnabled,
4242
true
4343
) {
44+
protected val animation = animationTicker()
45+
protected val cursorController = cursorController()
46+
47+
var settingValue by setting
48+
4449
init {
45-
overrideSize(owner::renderWidth, NewCGui::settingsHeight)
50+
overrideWidth(owner::renderWidth)
51+
titleBar.overrideHeight(NewCGui::settingsHeight)
4652
minimized = true
4753

4854
with(titleBar.textField) {

common/src/main/kotlin/com/lambda/newgui/impl/clickgui/settings/BooleanButton.kt

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,57 @@
1818
package com.lambda.newgui.impl.clickgui.settings
1919

2020
import com.lambda.config.settings.comparable.BooleanSetting
21+
import com.lambda.graphics.animation.Animation.Companion.exp
22+
import com.lambda.module.modules.client.NewCGui
23+
import com.lambda.newgui.component.core.FilledRect.Companion.rect
2124
import com.lambda.newgui.component.core.UIBuilder
2225
import com.lambda.newgui.component.layout.Layout
2326
import com.lambda.newgui.impl.clickgui.SettingLayout
2427
import com.lambda.util.Mouse
28+
import com.lambda.util.math.Rect
29+
import com.lambda.util.math.Vec2d
30+
import com.lambda.util.math.lerp
31+
import com.lambda.util.math.setAlpha
32+
import java.awt.Color
2533

2634
class BooleanButton(
2735
owner: Layout,
2836
setting: BooleanSetting
2937
) : SettingLayout<Boolean, BooleanSetting>(owner, setting) {
38+
private var activeAnimation by animation.exp(0.0, 1.0, 0.6, ::settingValue)
39+
3040
init {
31-
titleBar.onMouseClick { button, action ->
32-
if (button == Mouse.Button.Left && action == Mouse.Action.Click) {
33-
setting.value = !setting.value
41+
val checkBox = rect { // Checkbox
42+
val shrink = 2.0
43+
setRadius(100.0)
44+
45+
onUpdate {
46+
val rb = this@BooleanButton.rightBottom
47+
val h = this@BooleanButton.renderHeight
48+
49+
rectangle = Rect(rb - Vec2d(h * 1.65, h), rb)
50+
.shrink(shrink) + Vec2d.LEFT * (NewCGui.fontOffset - shrink)
51+
52+
setColor(Color.BLACK.setAlpha(0.25))
53+
shade = NewCGui.backgroundShade
54+
}
55+
56+
onMouseClick { button, action ->
57+
if (button == Mouse.Button.Left && action == Mouse.Action.Click) {
58+
setting.value = !setting.value
59+
}
60+
}
61+
}
62+
63+
rect { // Knob
64+
setRadius(100.0)
65+
66+
onUpdate {
67+
val knobStart = Rect.basedOn(checkBox.leftTop, Vec2d.ONE * checkBox.renderHeight)
68+
val knobEnd = Rect(checkBox.rightBottom - checkBox.renderHeight, checkBox.rightBottom)
69+
rectangle = lerp(activeAnimation, knobStart, knobEnd).shrink(1.0)
70+
shade = NewCGui.backgroundShade
71+
setColor(Color.WHITE.setAlpha(0.25))
3472
}
3573
}
3674
}

0 commit comments

Comments
 (0)