Skip to content

Commit 0fdcd76

Browse files
committed
added text alignment, simple boolean button
1 parent 393c5d6 commit 0fdcd76

File tree

10 files changed

+128
-57
lines changed

10 files changed

+128
-57
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ object Matrices {
1010

1111
var vertexTransformer: Matrix4d? = null
1212

13-
fun translate(x: Double, y: Double, z: Double) {
13+
fun translate(x: Double, y: Double, z: Double = 0.0) {
1414
translate(x.toFloat(), y.toFloat(), z.toFloat())
1515
}
1616

17-
fun translate(x: Float, y: Float, z: Float) {
17+
fun translate(x: Float, y: Float, z: Float = 0f) {
1818
stack.last().translate(x, y, z)
1919
}
2020

21-
fun scale(x: Double, y: Double, z: Double) {
21+
fun scale(x: Double, y: Double, z: Double = 1.0) {
2222
stack.last().scale(x.toFloat(), y.toFloat(), z.toFloat())
2323
}
2424

25-
fun scale(x: Float, y: Float, z: Float) {
25+
fun scale(x: Float, y: Float, z: Float = 1f) {
2626
stack.last().scale(x, y, z)
2727
}
2828

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import com.lambda.module.ModuleRegistry
55
import com.lambda.module.tag.ModuleTag
66
import com.lambda.newgui.ScreenLayout.Companion.gui
77
import com.lambda.newgui.component.core.FilledRect.Companion.rect
8+
import com.lambda.newgui.component.layout.Layout.Companion.layout
89
import com.lambda.newgui.impl.clickgui.ModuleLayout.Companion.moduleLayout
910
import com.lambda.newgui.impl.clickgui.ModuleWindow.Companion.moduleWindow
10-
import com.lambda.util.math.Rect
1111
import com.lambda.util.math.Vec2d
1212
import com.lambda.util.math.setAlpha
1313
import java.awt.Color
@@ -17,7 +17,8 @@ object NewCGui : Module(
1717
description = "ggs",
1818
defaultTags = setOf(ModuleTag.CLIENT)
1919
) {
20-
val titleBarHeight by setting("Title Bar Height", 18.0, 0.0..25.0, 0.1)
20+
val titleBarHeight by setting("Title Bar Height", 18.0, 10.0..25.0, 0.1)
21+
val settingsHeight by setting("Settings Height", 16.0, 10.0..25.0, 0.1)
2122
val padding by setting("Padding", 2.0, 1.0..6.0, 0.1)
2223
val listStep by setting("List Step", 2.0, 0.0..6.0, 0.1)
2324
val autoResize by setting("Auto Resize", false)
@@ -34,15 +35,14 @@ object NewCGui : Module(
3435
val outlineWidth by setting("Outline Width", 10.0, 1.0..10.0, 0.1) { outline }
3536
val outlineColor by setting("Outline Color", Color.WHITE.setAlpha(0.6)) { outline }
3637
val outlineShade by setting("Outline Shade", true) { outline }
38+
val fontScale by setting("Font Scale", 1.0, 0.5..2.0, 0.1)
39+
val fontOffset by setting("Font Offset", 2.0, 0.0..5.0, 0.1)
3740

3841
val moduleEnabledColor by setting("Module Enabled Color", Color.WHITE.setAlpha(0.25))
3942
val moduleDisabledColor by setting("Module Disabled Color", Color.WHITE.setAlpha(0.05))
4043

4144
private val SCREEN get() = gui("New Click Gui") {
42-
rect {
43-
rectangle = Rect(Vec2d.ZERO, this.size)
44-
setColor(backgroundTint)
45-
}
45+
4646

4747
val tags = ModuleTag.defaults
4848
val modules = ModuleRegistry.modules
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
package com.lambda.newgui
22

3+
import com.lambda.config.settings.comparable.BooleanSetting
34
import com.lambda.core.Loadable
4-
import com.lambda.module.Module
55
import com.lambda.newgui.component.core.UIBuilder
66
import com.lambda.newgui.component.layout.Layout
7-
import com.lambda.newgui.impl.clickgui.ModuleLayout.Companion.moduleLayout
7+
import com.lambda.newgui.impl.clickgui.settings.BooleanButton.Companion.booleanSetting
8+
import java.lang.reflect.Type
9+
import kotlin.reflect.KClass
810

911
object GuiManager : Loadable {
10-
val typeMap = mutableMapOf<Class<*>, (owner: Layout, converted: Any) -> Layout>()
12+
val typeMap = mutableMapOf<Type, (owner: Layout, converted: Any) -> Layout>()
1113

12-
private inline fun <reified T> typeAdapter(noinline block: (Layout, T) -> Layout) {
14+
private inline fun <reified T : Any> typeAdapter(noinline block: (Layout, T) -> Layout) {
1315
typeMap[T::class.java] = { owner, converted -> block(owner, converted as T) }
1416
}
1517

1618
override fun load(): String {
17-
// Example, not meant to be used
18-
typeAdapter<Module> { owner, ref ->
19-
owner.moduleLayout(ref)
19+
typeAdapter<BooleanSetting> { owner, ref ->
20+
owner.booleanSetting(ref)
2021
}
2122

22-
return super.load()
23+
return "Loaded ${typeMap.size} gui type adapters."
2324
}
2425

2526
/**
2627
* Attempts to convert the given [reference] to the [Layout]
27-
*
28-
* Or throws [IllegalStateException] if there's no registered ui adapter for the type of the [reference]
2928
*/
3029
@UIBuilder
31-
inline fun <reified T : Any> Layout.layoutOf(reference: T, block: Layout.() -> Unit = {}): Layout {
32-
val clazz = T::class.java
33-
val adapter = typeMap[clazz] ?: throw IllegalArgumentException("Unable to convert ${clazz.simpleName} to a layout")
30+
inline fun <reified T : Any> Layout.layoutOf(reference: T, block: Layout.() -> Unit = {}): Layout? {
31+
val adapter = typeMap[T::class.java] ?: return null
3432
return adapter(this, reference).apply(children::add).apply(block)
3533
}
3634
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.lambda.newgui.component.core
22

3+
import com.lambda.newgui.component.HAlign
4+
import com.lambda.newgui.component.VAlign
35
import com.lambda.newgui.component.layout.Layout
46
import com.lambda.util.math.Vec2d
7+
import com.lambda.util.math.lerp
58
import java.awt.Color
69

710
class TextField(
@@ -16,8 +19,12 @@ class TextField(
1619
val textWidth get() = fr.getWidth(text, scale)
1720
val textHeight get() = fr.getHeight(scale)
1821

19-
private val fr get() = if (bold) renderer.boldFont else renderer.font
22+
var textHAlignment = HAlign.LEFT
23+
var textVAlignment = VAlign.CENTER
24+
var offsetX = 0.0
25+
var offsetY = 0.0
2026

27+
private val fr get() = if (bold) renderer.boldFont else renderer.font
2128
private val updateActions = mutableListOf<TextField.() -> Unit>()
2229

2330
fun onUpdate(block: TextField.() -> Unit) {
@@ -26,13 +33,17 @@ class TextField(
2633

2734
init {
2835
properties.interactionPassthrough = true
36+
fillParent()
2937

3038
onRender {
3139
updateActions.forEach { action ->
3240
action(this@TextField)
3341
}
3442

35-
val renderPos = Vec2d(renderPositionX, renderPositionY + textHeight * 0.5)
43+
val rx = renderPositionX + lerp(textHAlignment.multiplier, offsetX, renderWidth - textWidth - offsetX)
44+
val ry = renderPositionY + lerp(textVAlignment.multiplier, offsetY, renderHeight - textHeight - offsetY)
45+
val renderPos = Vec2d(rx, ry + textHeight * 0.5)
46+
3647
fr.build(text, renderPos, color, scale, shadow)
3748
}
3849
}

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

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,21 @@ open class Layout(
278278
heightTransform = height
279279
}
280280

281+
/**
282+
* Makes this layout expand up to parents rect
283+
*/
284+
fun fillParent(
285+
overrideX: () -> Double = { owner?.renderPositionX ?: ownerX },
286+
overrideY: () -> Double = { owner?.renderPositionY ?: ownerY },
287+
overrideWidth: () -> Double = { owner?.renderWidth?: ownerWidth },
288+
overrideHeight: () -> Double = { owner?.renderHeight?: ownerHeight }
289+
) {
290+
overrideX(overrideX)
291+
overrideY(overrideY)
292+
overrideWidth(overrideWidth)
293+
overrideHeight(overrideHeight)
294+
}
295+
281296
fun onEvent(e: GuiEvent) {
282297
if (e is GuiEvent.Render) {
283298
screenSize = RenderMain.screenSize
@@ -334,32 +349,32 @@ open class Layout(
334349
mouseClickActions.forEach { it(e.button, action) }
335350
}
336351
is GuiEvent.Render -> {
337-
val drawAction = {
338-
val drawChildren = renderWidth > 0.1 && renderHeight > 0.1
352+
if (properties.scissor) {
353+
scissor(rect) { render(e) }
354+
} else render(e)
355+
}
356+
}
357+
}
339358

340-
val partition by lazy {
341-
children.partition { !it.owningRenderer }
342-
}
359+
protected open fun render(e: GuiEvent) {
360+
val drawChildren = renderWidth > 0.1 && renderHeight > 0.1
343361

344-
renderActions.forEach { it(renderer) }
362+
val partition by lazy {
363+
children.partition { !it.owningRenderer }
364+
}
345365

346-
if (drawChildren) {
347-
partition.first.forEach { it.onEvent(e) }
348-
}
366+
renderActions.forEach { it(renderer) }
349367

350-
if (owningRenderer) {
351-
renderer.render()
352-
}
368+
if (drawChildren) {
369+
partition.first.forEach { it.onEvent(e) }
370+
}
353371

354-
if (drawChildren) {
355-
partition.second.forEach { it.onEvent(e) }
356-
}
357-
}
372+
if (owningRenderer) {
373+
renderer.render()
374+
}
358375

359-
if (properties.scissor) {
360-
scissor(rect, drawAction)
361-
} else drawAction()
362-
}
376+
if (drawChildren) {
377+
partition.second.forEach { it.onEvent(e) }
363378
}
364379
}
365380

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ class TitleBar(
2121
text = title
2222
bold = true
2323

24-
val tb = this@TitleBar
24+
textHAlignment = HAlign.CENTER
2525

26-
overrideX { tb.renderPositionX + tb.renderWidth * 0.5 - textWidth * 0.5 }
27-
overrideY { tb.renderPositionY + tb.renderHeight * 0.5 - textHeight * 0.5 }
26+
onUpdate {
27+
scale = NewCGui.fontScale
28+
}
2829
}
2930

3031
private var dragOffset: Vec2d? = null

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ open class Window(
100100
position = initialPosition
101101
size = initialSize
102102

103-
overrideWidth(animation.exp(::width, 0.8)::value)
104-
105-
overrideHeight {
103+
overrideSize(animation.exp(::width, 0.8)::value) {
106104
titleBar.renderHeight + when (minimizing) {
107105
Minimizing.Disabled -> targetHeight
108106
Minimizing.Relative -> heightAnimation

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lambda.newgui.component.window
22

33
import com.lambda.graphics.animation.Animation.Companion.exp
4+
import com.lambda.gui.api.GuiEvent
45
import com.lambda.module.modules.client.NewCGui
56
import com.lambda.newgui.component.core.UIBuilder
67
import com.lambda.newgui.component.layout.Layout
@@ -15,7 +16,10 @@ class WindowContent(
1516
private var dwheel = 0.0
1617
private var scrollOffset = 0.0
1718
private var rubberbandDelta = 0.0
19+
1820
private var renderScrollOffset by animation.exp({ scrollOffset + rubberbandDelta }, 0.7)
21+
private val scaleAnimation by animation.exp(1.0, 0.9, 0.7, ::scrolling)
22+
private var scrolling = false
1923

2024
init {
2125
overrideX { owner.titleBar.renderPositionX }
@@ -37,10 +41,11 @@ class WindowContent(
3741
scrollOffset + dwheel
3842
} else 0.0
3943

44+
scrolling = dwheel != 0.0
4045
dwheel = 0.0
4146

4247
val prevOffset = scrollOffset
43-
val maxScroll = renderHeight - getContentHeight() - NewCGui.padding * 2
48+
val maxScroll = renderHeight - getContentHeight() - NewCGui.padding
4449
scrollOffset = scrollOffset.coerceAtLeast(maxScroll).coerceAtMost(0.0)
4550

4651
rubberbandDelta += prevOffset - scrollOffset

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.newgui.impl.clickgui
22

3+
import com.lambda.config.settings.comparable.BooleanSetting
34
import com.lambda.graphics.animation.Animation.Companion.exp
45
import com.lambda.module.Module
56
import com.lambda.module.modules.client.NewCGui
@@ -8,10 +9,10 @@ import com.lambda.newgui.component.core.FilledRect
89
import com.lambda.newgui.component.core.UIBuilder
910
import com.lambda.newgui.component.layout.Layout
1011
import com.lambda.newgui.component.window.Window
12+
import com.lambda.newgui.impl.clickgui.settings.BooleanButton.Companion.booleanSetting
1113
import com.lambda.util.Mouse
1214
import com.lambda.util.math.Vec2d
1315
import com.lambda.util.math.lerp
14-
import com.lambda.util.math.multAlpha
1516

1617
class ModuleLayout(
1718
owner: Layout,
@@ -20,8 +21,8 @@ class ModuleLayout(
2021
owner,
2122
module.name,
2223
Vec2d.ZERO, Vec2d.ZERO,
23-
false, false, Minimizing.Relative, false,
24-
AutoResize.Disabled, // ToDo: should be ForceEnabled, temporarily using this mode to set the height manually
24+
false, true, Minimizing.Relative, false,
25+
AutoResize.ForceEnabled,
2526
true
2627
) {
2728
private val animation = animationTicker()
@@ -42,9 +43,10 @@ class ModuleLayout(
4243
with(titleBar) {
4344
with(textField) {
4445
bold = false
46+
textHAlignment = HAlign.LEFT
4547

46-
overrideX {
47-
titleBar.renderPositionX + (titleBar.renderHeight - textHeight) * 0.5
48+
onUpdate {
49+
offsetX = NewCGui.fontOffset
4850
}
4951
}
5052

@@ -79,6 +81,16 @@ class ModuleLayout(
7981
val cursor = if (titleBar.isHovered) Mouse.Cursor.Pointer else Mouse.Cursor.Arrow
8082
cursorController.setCursor(cursor)
8183
}
84+
85+
content.apply {
86+
module.settings.forEach { setting ->
87+
//layoutOf(setting) doesn't work
88+
89+
when (setting) {
90+
is BooleanSetting -> booleanSetting(setting)
91+
}
92+
}
93+
}
8294
}
8395

8496
private fun FilledRect.correctRadius() {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.lambda.newgui.impl.clickgui.settings
2+
3+
import com.lambda.config.settings.comparable.BooleanSetting
4+
import com.lambda.module.modules.client.NewCGui
5+
import com.lambda.newgui.component.core.TextField.Companion.textField
6+
import com.lambda.newgui.component.core.UIBuilder
7+
import com.lambda.newgui.component.layout.Layout
8+
9+
class BooleanButton(
10+
owner: Layout,
11+
val setting: BooleanSetting
12+
) : Layout(owner, true, true) {
13+
init {
14+
overrideSize(owner::renderWidth, NewCGui::settingsHeight)
15+
16+
textField {
17+
text = setting.name
18+
scale = NewCGui.fontScale * 0.95
19+
offsetX = NewCGui.fontOffset
20+
}
21+
}
22+
23+
companion object {
24+
/**
25+
* Creates a [BooleanButton] - visual representation of the [BooleanSetting]
26+
*/
27+
@UIBuilder
28+
fun Layout.booleanSetting(setting: BooleanSetting) =
29+
BooleanButton(this, setting).apply(children::add)
30+
}
31+
}

0 commit comments

Comments
 (0)