Skip to content

Commit a63d0a1

Browse files
committed
Sliders & much stuff
1 parent 7591b93 commit a63d0a1

File tree

22 files changed

+595
-303
lines changed

22 files changed

+595
-303
lines changed

common/src/main/kotlin/com/lambda/config/settings/FunctionSetting.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import com.google.gson.JsonNull
2222
import com.google.gson.reflect.TypeToken
2323
import com.lambda.config.AbstractSetting
2424

25-
open class FunctionSetting<T : Any>(
25+
open class FunctionSetting<T>(
2626
override val name: String,
2727
private val defaultValue: () -> T,
2828
description: String,

common/src/main/kotlin/com/lambda/graphics/buffer/VertexPipeline.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,6 @@ class VertexPipeline(
185185
uploadedIndices = 0
186186
}
187187

188-
fun finalize() {
189-
190-
}
191-
192188
init {
193189
// All the buffers have been generated, all we have to
194190
// do now it bind them correctly and populate them

common/src/main/kotlin/com/lambda/graphics/renderer/gui/rect/FilledRectRenderer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ object FilledRectRenderer : AbstractGUIRenderer(
2929
) {
3030
private const val MIN_SIZE = 0.5
3131
private const val MIN_ALPHA = 1
32+
private const val EXPAND = 0.3
3233

3334
fun filledRect(
3435
rect: Rect,
@@ -100,8 +101,8 @@ object FilledRectRenderer : AbstractGUIRenderer(
100101
val rbr = rightBottomRadius.coerceAtMost(maxRadius).coerceAtLeast(0.0)
101102
val rtr = rightTopRadius.coerceAtMost(maxRadius).coerceAtLeast(0.0)
102103

103-
val p1 = pos1 - 0.25
104-
val p2 = pos2 + 0.25
104+
val p1 = pos1 - EXPAND
105+
val p2 = pos2 + EXPAND
105106

106107
shader["u_Size"] = size
107108
shader["u_RoundLeftTop"] = ltr

common/src/main/kotlin/com/lambda/graphics/renderer/gui/rect/OutlineRectRenderer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ object OutlineRectRenderer : AbstractGUIRenderer(
5858
leftBottom: Color = Color.WHITE,
5959
shade: Boolean = false,
6060
) = render(shade) {
61-
if (glowRadius < 1) return@render
61+
if (glowRadius < 0.1) return@render
6262

6363
grow(VERTICES_COUNT * 3)
6464

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717

1818
package com.lambda.gui
1919

20+
import com.lambda.config.settings.NumericSetting
2021
import com.lambda.config.settings.comparable.BooleanSetting
2122
import com.lambda.config.settings.comparable.EnumSetting
2223
import com.lambda.config.settings.FunctionSetting
2324
import com.lambda.core.Loadable
2425
import com.lambda.gui.component.core.UIBuilder
2526
import com.lambda.gui.component.layout.Layout
26-
import com.lambda.gui.impl.clickgui.settings.BooleanButton.Companion.booleanSetting
27-
import com.lambda.gui.impl.clickgui.settings.EnumSelector.Companion.enumSetting
27+
import com.lambda.gui.impl.clickgui.module.settings.BooleanButton.Companion.booleanSetting
28+
import com.lambda.gui.impl.clickgui.module.settings.EnumSlider.Companion.enumSetting
29+
import com.lambda.gui.impl.clickgui.module.settings.NumberSlider.Companion.numericSetting
2830
import com.lambda.gui.impl.clickgui.settings.UnitButton.Companion.unitSetting
2931
import kotlin.reflect.KClass
3032

@@ -44,10 +46,14 @@ object GuiManager : Loadable {
4446
owner.enumSetting(ref)
4547
}
4648

47-
typeAdapter<FunctionSetting<Unit>> { owner, ref ->
49+
typeAdapter<FunctionSetting<*>> { owner, ref ->
4850
owner.unitSetting(ref)
4951
}
5052

53+
typeAdapter<NumericSetting<*>> { owner, ref ->
54+
owner.numericSetting(ref)
55+
}
56+
5157
return "Loaded ${typeMap.size} gui type adapters."
5258
}
5359

@@ -59,5 +65,7 @@ object GuiManager : Loadable {
5965
reference: Any,
6066
block: Layout.() -> Unit = {}
6167
): Layout? =
62-
typeMap[reference::class]?.invoke(this, reference)?.apply(block)
68+
(typeMap[reference::class] ?: typeMap.entries.firstOrNull {
69+
reference::class.java.superclass == it.key.java
70+
}?.value)?.invoke(this, reference)?.apply(block)
6371
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class OutlineRect(
2525
owner: Layout
2626
) : Layout(owner) {
2727
@UIRenderPr0p3rty var roundRadius = 0.0
28-
@UIRenderPr0p3rty var glowRadius = 0.0
28+
@UIRenderPr0p3rty var glowRadius = 1.0
2929

3030
@UIRenderPr0p3rty var leftTopColor: Color = Color.WHITE
3131
@UIRenderPr0p3rty var rightTopColor: Color = Color.WHITE
@@ -58,6 +58,20 @@ class OutlineRect(
5858
leftBottomColor = color
5959
}
6060

61+
fun setColorH(colorL: Color, colorR: Color) {
62+
leftTopColor = colorL
63+
rightTopColor = colorR
64+
rightBottomColor = colorR
65+
leftBottomColor = colorL
66+
}
67+
68+
fun setColorV(colorT: Color, colorB: Color) {
69+
leftTopColor = colorT
70+
rightTopColor = colorT
71+
rightBottomColor = colorB
72+
leftBottomColor = colorB
73+
}
74+
6175
companion object {
6276
/**
6377
* Creates an [OutlineRect] component - layout-based rect representation

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

Lines changed: 74 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,19 @@ open class Layout(
4141

4242
// ToDo: impl alignmentLayout: Layout, instead of being able to align to the owner only
4343
// Position of the component
44+
@UIRenderPr0p3rty
4445
var position: Vec2d
4546
get() = Vec2d(positionX, positionY)
4647
set(value) { positionX = value.x; positionY = value.y }
4748

49+
@UIRenderPr0p3rty
4850
var positionX: Double
4951
get() = ownerX + (relativePosX + dockingOffsetX).let {
5052
if (!properties.clampPosition) return@let it
5153
it.coerceAtMost(ownerWidth - width).coerceAtLeast(0.0)
5254
}; set(value) { relativePosX = value - ownerX - dockingOffsetX }
5355

56+
@UIRenderPr0p3rty
5457
var positionY: Double
5558
get() = ownerY + (relativePosY + dockingOffsetY).let {
5659
if (!properties.clampPosition) return@let it
@@ -66,12 +69,13 @@ open class Layout(
6669
private var relativePosY = 0.0
6770

6871
// Size of the component
72+
@UIRenderPr0p3rty
6973
var size: Vec2d
7074
get() = Vec2d(width, height)
7175
set(value) { width = value.x; height = value.y }
7276

73-
var width = 0.0
74-
var height = 0.0
77+
@UIRenderPr0p3rty var width = 0.0
78+
@UIRenderPr0p3rty var height = 0.0
7579

7680
// Horizontal alignment
7781
var horizontalAlignment = HAlign.LEFT; set(to) {
@@ -118,9 +122,22 @@ open class Layout(
118122
protected open val scissorRect get() = rect
119123

120124
// Inputs
121-
protected var mousePosition = Vec2d.ZERO
125+
protected var mousePosition = Vec2d.ZERO; set(value) {
126+
if (field == value) return
127+
field = value
128+
129+
selectedChild = if (isHovered) children.lastOrNull {
130+
if (it.properties.interactionPassthrough) return@lastOrNull false
131+
val xh = (value.x - it.positionX) in 0.0..it.width
132+
val yh = (value.y - it.positionY) in 0.0..it.height
133+
xh && yh
134+
} else null
135+
}
122136
open val isHovered get() = owner?.let { it.selectedChild == this } ?: true
123137

138+
var pressedButton: Mouse.Button? = null
139+
protected val isPressed get() = pressedButton != null
140+
124141
// Actions
125142
private val showActions = mutableListOf<Layout.() -> Unit>()
126143
private val hideActions = mutableListOf<Layout.() -> Unit>()
@@ -130,6 +147,7 @@ open class Layout(
130147
private val keyPressActions = mutableListOf<Layout.(key: KeyCode) -> Unit>()
131148
private val charTypedActions = mutableListOf<Layout.(char: Char) -> Unit>()
132149
private val mouseClickActions = mutableListOf<Layout.(button: Mouse.Button, action: Mouse.Action) -> Unit>()
150+
private val mouseActions = mutableListOf<Layout.(button: Mouse.Button) -> Unit>()
133151
private val mouseMoveActions = mutableListOf<Layout.(mouse: Vec2d) -> Unit>()
134152
private val mouseScrollActions = mutableListOf<Layout.(delta: Double) -> Unit>()
135153

@@ -180,6 +198,7 @@ open class Layout(
180198
*/
181199
@LayoutBuilder
182200
fun <T : Layout> T.onUpdate(action: T.() -> Unit) {
201+
action(this)
183202
updateActions += { action() }
184203
}
185204

@@ -219,19 +238,21 @@ open class Layout(
219238
* @param action The action to be performed.
220239
*/
221240
@LayoutBuilder
222-
fun <T : Layout> T.onMouseClick(action: T.(button: Mouse.Button, action: Mouse.Action) -> Unit) {
223-
mouseClickActions += { button, mouseAction -> action(button, mouseAction) }
241+
fun <T : Layout> T.onMouse(button: Mouse.Button? = null, action: Mouse.Action? = null, block: T.(Mouse.Button) -> Unit) {
242+
mouseClickActions += { butt, act ->
243+
if ((butt == button || button == null) && (act == action || action == null)) block(butt)
244+
}
224245
}
225246

226247
/**
227-
* Sets the action to be performed when mouse button gets clicked.
248+
* Sets the action to be performed when mouse button gets released and this layout was clicked.
228249
*
229250
* @param action The action to be performed.
230251
*/
231252
@LayoutBuilder
232-
fun <T : Layout> T.onMouseClick(button: Mouse.Button, action: Mouse.Action, block: T.() -> Unit) {
233-
onMouseClick { butt, act ->
234-
if (butt == button && act == action) block()
253+
fun <T : Layout> T.onMouseAction(button: Mouse.Button? = null, acceptNotHovered: Boolean = false, action: T.(Mouse.Button) -> Unit) {
254+
mouseActions += {
255+
if (it == button || button == null && (isHovered || !acceptNotHovered)) action(it)
235256
}
236257
}
237258

@@ -255,72 +276,6 @@ open class Layout(
255276
mouseScrollActions += { delta -> action(delta) }
256277
}
257278

258-
/**
259-
* Force overrides drawn x position of the layout
260-
*/
261-
@LayoutBuilder
262-
fun overrideX(transform: () -> Double) {
263-
positionX = transform()
264-
265-
onUpdate {
266-
positionX = transform()
267-
}
268-
}
269-
270-
/**
271-
* Force overrides drawn y position of the layout
272-
*/
273-
@LayoutBuilder
274-
fun overrideY(transform: () -> Double) {
275-
positionY = transform()
276-
277-
onUpdate {
278-
positionY = transform()
279-
}
280-
}
281-
282-
/**
283-
* Force overrides drawn position of the layout
284-
*/
285-
@LayoutBuilder
286-
fun overridePosition(x: () -> Double, y: () -> Double) {
287-
overrideX(x)
288-
overrideY(y)
289-
}
290-
291-
/**
292-
* Force overrides drawn width of the layout
293-
*/
294-
@LayoutBuilder
295-
fun overrideWidth(transform: () -> Double) {
296-
width = transform()
297-
298-
onUpdate {
299-
width = transform()
300-
}
301-
}
302-
303-
/**
304-
* Force overrides drawn height of the layout
305-
*/
306-
@LayoutBuilder
307-
fun overrideHeight(transform: () -> Double) {
308-
height = transform()
309-
310-
onUpdate {
311-
height = transform()
312-
}
313-
}
314-
315-
/**
316-
* Force overrides drawn size of the layout
317-
*/
318-
@LayoutBuilder
319-
fun overrideSize(width: () -> Double, height: () -> Double) {
320-
overrideWidth(width)
321-
overrideHeight(height)
322-
}
323-
324279
/**
325280
* Removes this layout from its parent
326281
*/
@@ -343,21 +298,15 @@ open class Layout(
343298
ownerY = owner?.positionY ?: ownerY
344299
ownerWidth = owner?.width ?: screenSize.x
345300
ownerHeight = owner?.height ?: screenSize.y
346-
347-
// Select an element that's on foreground
348-
selectedChild = if (isHovered) children.lastOrNull {
349-
if (it.properties.interactionPassthrough) return@lastOrNull false
350-
val xh = (mousePosition.x - it.positionX) in 0.0..it.width
351-
val yh = (mousePosition.y - it.positionY) in 0.0..it.height
352-
xh && yh
353-
} else null
354301
}
355302
}
356303

357304
fun onEvent(e: GuiEvent) {
358305
// Update self
359306
when (e) {
360307
is GuiEvent.Show -> {
308+
pressedButton = null
309+
selectedChild = null
361310
mousePosition = Vec2d.ONE * -1000.0
362311
showActions.forEach { it(this) }
363312
}
@@ -381,16 +330,29 @@ open class Layout(
381330
}
382331
is GuiEvent.MouseMove -> {
383332
mousePosition = e.mouse
333+
384334
mouseMoveActions.forEach { it(this, e.mouse) }
385335
}
386336
is GuiEvent.MouseScroll -> {
387-
if (!isHovered) return
388337
mousePosition = e.mouse
338+
339+
if (!isHovered) return
389340
mouseScrollActions.forEach { it(this, e.delta) }
390341
}
391342
is GuiEvent.MouseClick -> {
392343
mousePosition = e.mouse
344+
393345
val action = if (isHovered) e.action else Mouse.Action.Release
346+
347+
val prevPressed = pressedButton
348+
pressedButton = e.button.takeIf { action == Mouse.Action.Click }
349+
350+
if (pressedButton == null) prevPressed?.let { button ->
351+
mouseActions.forEach {
352+
it.invoke(this, button)
353+
}
354+
}
355+
394356
mouseClickActions.forEach { it(this, e.button, action) }
395357
}
396358
}
@@ -434,6 +396,34 @@ open class Layout(
434396
) = Layout(this)
435397
.apply(children::add).apply(block)
436398

399+
/**
400+
* Creates an empty [Layout] behind given [layout]
401+
*
402+
* @param block Actions to perform within this component.
403+
*
404+
* Check [Layout] description for more info about batching.
405+
*/
406+
@UIBuilder
407+
fun Layout.layoutBehind(
408+
layout: Layout,
409+
block: Layout.() -> Unit = {},
410+
) = Layout(this)
411+
.insertLayout(this, layout, false).apply(block)
412+
413+
/**
414+
* Creates an empty [Layout] over given [layout].
415+
*
416+
* @param block Actions to perform within this component.
417+
*
418+
* Check [Layout] description for more info about batching.
419+
*/
420+
@UIBuilder
421+
fun Layout.layoutOver(
422+
layout: Layout,
423+
block: Layout.() -> Unit = {},
424+
) = Layout(this)
425+
.insertLayout(this, layout, true).apply(block)
426+
437427
/**
438428
* Creates new [AnimationTicker].
439429
*

0 commit comments

Comments
 (0)